Python Forum

Full Version: Element wise computation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
(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.