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!!

## All 3 Replies

Cross Product and Normalization in python
import math

``````def crossProduct(v1, v2, vR):
vR =   ( (v1 * v2) - (v1 * v2) )
vR = - ( (v1 * v2) - (v1 * v2) )
vR =   ( (v1 * v2) - (v1 * v2) )

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

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

vect1 = 3
vect1 = -3
vect1 = 1

vect2 = 4
vect2 = 9
vect2 = 2

crossProduct(vect1, vect2, vectResult)

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

", y: " + ("%.4f" % vectResult) + \
", z: " + ("%.4f" % vectResult)

normalize(vectResult, vectNormal)

print "x: " + ("%.4f" % vectNormal) + \
", y: " + ("%.4f" % vectNormal) + \
", z: " + ("%.4f" % vectNormal)``````

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.

for idx in range(3):

``````b = A[:, idx]
print b ``````

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]``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.