0

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the normalization is:

def vectors(b): 
    b = b/np.sqrt(np.sum(b**2.,axis=0))
    b = b/np.linalg.norm(b)     
    z = np.array([0.,0.,1.])
    n1 = np.cross(z,b,axis=0)
    n1 = n1/np.linalg.norm(n1) ## normalize n

    return [n1] 

n1 = vectors(A)

How can I make a loop that picks the first column and makes the calculation, then the second column and so on, but I do not know how to make it!. Could you please help me at this point. Thank in advance!!

3
Contributors
3
Replies
35
Views
10 Months
Discussion Span
Last Post by Xavier_5
0

Cross Product and Normalization in python
import math

def crossProduct(v1, v2, vR):
  vR[0] =   ( (v1[1] * v2[2]) - (v1[2] * v2[1]) )
  vR[1] = - ( (v1[0] * v2[2]) - (v1[2] * v2[0]) )
  vR[2] =   ( (v1[0] * v2[1]) - (v1[1] * v2[0]) )

def normalize(v1, vR):
  fMag = math.sqrt( (v1[0] ** 2) + (v1[1] ** 2) + (v1[2] ** 2) )
  vR[0] = v1[0] / fMag
  vR[1] = v1[1] / fMag
  vR[2] = v1[2] / fMag

vect1 = [ None, None, None ]
vect2 = [ None, None, None ]
vectResult = [ None, None, None ]
vectNormal = [ None, None, None ]

vect1[0] = 3
vect1[1] = -3
vect1[2] = 1 

vect2[0] = 4
vect2[1] = 9
vect2[2] = 2

crossProduct(vect1, vect2, vectResult)

print "x: " + ("%.4f" % vectResult[0]) + \

     ", y: " + ("%.4f" % vectResult[1]) + \
     ", z: " + ("%.4f" % vectResult[2]) 

normalize(vectResult, vectNormal)

print "x: " + ("%.4f" % vectNormal[0]) + \
            ", y: " + ("%.4f" % vectNormal[1]) + \
             ", z: " + ("%.4f" % vectNormal[2])

I hope this will help you. I have some good work experience with a OOH Media software and my words are clearly based on what I felt through such processes in the past.

Edited by Reverend Jim: Corrected code formatting

0

Not sure but maybe Ma(n x m) * Mb (m * k) --> Mc (n * k):

for i in range (0,n):
        for j in range (0,m):
            for k in range (0,m)
               Mc[i][j] = Mc[i][j] + Ma[i][k] * Mb[k][j]
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.