Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Element wise computation
#1
Hello everyone

Suppose I have two arrays:
[Image: generator-matrix.png]
and
[Image: received-message.png]

And I wanted to do calculations using this equation:
[Image: operations.png]

The code that I have created so far:
import numpy as np
import math as mt

G = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0, 0], [1, 0, 1, 0, 1, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1]])

r = np.array([-56.53511658, -96.6740462, -85.23212419, -90.05029513, -86.19336384, -79.31883055, -67.7323369, -86.49784743])

def boxplus(a, b):
    return 2 * aatanh(mt.tanh(a / 2) * mt.tanh(b / 2))

def aatanh(x):
    if x == 1:
        return mt.atanh(x - 0.0000001)
    elif x == -1:
        return mt.atanh(x + 0.0000001)
    else:
        return mt.atanh(x)

L = np.zeros(8)
Right now I am stuck on how to do the boxplus operation and put it into array L. And please forgive me if my post is in wrong location.
Reply
#2
I would try something like this:

import numpy as np
import math as mt

G = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0, 0], [1, 0, 1, 0, 1, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1]])
 
r = np.array([-56.53511658, -96.6740462, -85.23212419, -90.05029513, -86.19336384, -79.31883055, -67.7323369, -86.49784743])

def main():
    s = r.reshape((1, len(r)))
    print(s)
    M = G * s.T
    print(M)
    tmp = np.tanh(M*0.5)
    print(tmp)
    p = np.prod(tmp, axis=0)
    print(p)
    print(2 * np.arctanh(p))

if __name__ == '__main__':
    main()
The numerical result is deceiving however, due to the large values in the array r
Output:
[[-56.53511658 -96.6740462 -85.23212419 -90.05029513 -86.19336384 -79.31883055 -67.7323369 -86.49784743]] paillasse/pf/numbox.py:17: RuntimeWarning: divide by zero encountered in arctanh [[-56.53511658 -0. -0. -0. -0. -0. -0. -0. ] [-96.6740462 -96.6740462 -0. -0. -0. -0. -0. -0. ] print(2 * np.arctanh(p)) [-85.23212419 -0. -85.23212419 -0. -0. -0. -0. -0. ] [-90.05029513 -90.05029513 -90.05029513 -90.05029513 -0. -0. -0. -0. ] [-86.19336384 -0. -0. -0. -86.19336384 -0. -0. -0. ] [-79.31883055 -79.31883055 -0. -0. -79.31883055 -79.31883055 -0. -0. ] [-67.7323369 -0. -67.7323369 -0. -67.7323369 -0. -67.7323369 -0. ] [-86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743]] [[-1. -0. -0. -0. -0. -0. -0. -0.] [-1. -1. -0. -0. -0. -0. -0. -0.] [-1. -0. -1. -0. -0. -0. -0. -0.] [-1. -1. -1. -1. -0. -0. -0. -0.] [-1. -0. -0. -0. -1. -0. -0. -0.] [-1. -1. -0. -0. -1. -1. -0. -0.] [-1. -0. -1. -0. -1. -0. -1. -0.] [-1. -1. -1. -1. -1. -1. -1. -1.]] [1. 0. 0. 0. 0. 0. 0. 0.] [inf 0. 0. 0. 0. 0. 0. 0.]
Note that the boxplus operator returns 0 as soon as one of the arguments is 0, and the matrix G contains many zeroes.
divon likes this post
Reply
#3
(Apr-29-2022, 08:09 AM)Gribouillis Wrote: I would try something like this:

import numpy as np
import math as mt

G = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0], [1, 1, 0, 0, 1, 1, 0, 0], [1, 0, 1, 0, 1, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1]])
 
r = np.array([-56.53511658, -96.6740462, -85.23212419, -90.05029513, -86.19336384, -79.31883055, -67.7323369, -86.49784743])

def main():
    s = r.reshape((1, len(r)))
    print(s)
    M = G * s.T
    print(M)
    tmp = np.tanh(M*0.5)
    print(tmp)
    p = np.prod(tmp, axis=0)
    print(p)
    print(2 * np.arctanh(p))

if __name__ == '__main__':
    main()
The numerical result is deceiving however, due to the large values in the array r
Output:
[[-56.53511658 -96.6740462 -85.23212419 -90.05029513 -86.19336384 -79.31883055 -67.7323369 -86.49784743]] paillasse/pf/numbox.py:17: RuntimeWarning: divide by zero encountered in arctanh [[-56.53511658 -0. -0. -0. -0. -0. -0. -0. ] [-96.6740462 -96.6740462 -0. -0. -0. -0. -0. -0. ] print(2 * np.arctanh(p)) [-85.23212419 -0. -85.23212419 -0. -0. -0. -0. -0. ] [-90.05029513 -90.05029513 -90.05029513 -90.05029513 -0. -0. -0. -0. ] [-86.19336384 -0. -0. -0. -86.19336384 -0. -0. -0. ] [-79.31883055 -79.31883055 -0. -0. -79.31883055 -79.31883055 -0. -0. ] [-67.7323369 -0. -67.7323369 -0. -67.7323369 -0. -67.7323369 -0. ] [-86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743 -86.49784743]] [[-1. -0. -0. -0. -0. -0. -0. -0.] [-1. -1. -0. -0. -0. -0. -0. -0.] [-1. -0. -1. -0. -0. -0. -0. -0.] [-1. -1. -1. -1. -0. -0. -0. -0.] [-1. -0. -0. -0. -1. -0. -0. -0.] [-1. -1. -0. -0. -1. -1. -0. -0.] [-1. -0. -1. -0. -1. -0. -1. -0.] [-1. -1. -1. -1. -1. -1. -1. -1.]] [1. 0. 0. 0. 0. 0. 0. 0.] [inf 0. 0. 0. 0. 0. 0. 0.]
Note that the boxplus operator returns 0 as soon as one of the arguments is 0, and the matrix G contains many zeroes.

Thank you very much for your help, @Gribouillis. It gives me a new perspective on the problem. And please forgive me for being so late in replying to your post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,903 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  How do I read in a Formula in Excel and convert it to do the computation in Python? JaneTan 2 2,665 Jul-07-2021, 02:06 PM
Last Post: Marbelous
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,815 Apr-23-2021, 08:09 AM
Last Post: sx999
  5 python columns, row-wise as input to a function dervast 1 2,033 Jul-17-2019, 08:53 PM
Last Post: Larz60+
  Inserting a python list into a dataframe column wise mahmoud899 0 4,292 Mar-04-2019, 11:44 PM
Last Post: mahmoud899
  Unable to locate element no such element gahhon 6 4,521 Feb-18-2019, 02:09 PM
Last Post: gahhon
  how to program robot to pass wise man puzzle steven12341234 0 1,952 Dec-02-2018, 08:31 AM
Last Post: steven12341234
  Change single element in 2D list changes every 1D element AceScottie 9 12,101 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020