1
votes

I currently have two vectors of shape (300L,) each. However, when I am trying to compute cross product between them through numpy cross function, it's throwing up the following error: z = np.cross(x,y)

C:\Users\SMG059\AppData\Local\Continuum\Anaconda\lib\site-packages\numpy\core\numeric.pyc in cross(a, b, axisa, axisb, axisc, axis) 1525 "(dimension must be 2 or 3)") 1526 if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3): -> 1527 raise ValueError(msg) 1528 1529 # Create the output array

ValueError: incompatible dimensions for cross product (dimension must be 2 or 3)

Can someone please tell me how to compute cross product for large vectors?

1
The vector cross product is only defined in three dimensions. Do you mean you have two arrays, each of 300, 3-dimensional vectors?xnx
I think what you're looking for is the determinant of a matrix. The cross product of two 3-length vectors is calculated using a determinant. Is that correct? docs.scipy.org/doc/numpy/reference/generated/…Charlie Haley
Also worth noting: Cross product only exists for vectors of 3 and 7 dimensions. math.stackexchange.com/questions/424482/… math.stackexchange.com/questions/720813/…Charlie Haley
I actually want to determine the angle between the arrays. Is there a way to do that? That's why I needed cross product.Salamander
Are you sure it isn't the dot product that you're after, then? np.dot will work with an arbitrary number of dimensions.xnx

1 Answers

0
votes

Based on your comment about wanting to know the angle between the arrays, I think you do in fact want the dot product. For example, here is a function that will give you the angle between two vectors.

def angle(vector1, vector2):
    # cos(theta) = v1 dot v2 / ||v1|| * ||v2||
    import numpy
    numerator = numpy.dot(vector1, vector2)
    denominator = numpy.linalg.norm(vector1) * numpy.linalg.norm(vector2)
    x = numerator / denominator if denominator else 0
    return numpy.arccos(x)