# psychopy.tools.mathtools.cross¶

psychopy.tools.mathtools.cross(v0, v1, out=None, dtype=None)[source]

Cross product of 3D vectors.

The behavior of this function depends on the dimensions of the inputs:

• If v0 and v1 are 1D, the cross product is returned as 1D vector.

• If v0 and v1 are 2D, a 2D array of cross products between corresponding row vectors are returned.

• If either v0 and v1 are 1D and 2D, an array of cross products between each row of the 2D vector and the 1D vector are returned.

Parameters
• v0 (array_like) – Vector(s) in form [x, y, z] or [x, y, z, 1].

• v1 (array_like) – Vector(s) in form [x, y, z] or [x, y, z, 1].

• out (ndarray, optional) – Optional output array. Must be same shape and dtype as the expected output if out was not specified.

• dtype (dtype or str, optional) – Data type for computations can either be ‘float32’ or ‘float64’. If out is specified, the data type of out is used and this argument is ignored. If out is not provided, ‘float64’ is used by default.

Returns

Cross product of v0 and v1.

Return type

ndarray

Notes

• If input vectors are 4D, the last value of cross product vectors is always set to one.

• If input vectors v0 and v1 are Nx3 and out is Nx4, the cross product is computed and the last column of out is filled with ones.

Examples

Find the cross product of two vectors:

a = normalize([1, 2, 3])
b = normalize([3, 2, 1])
c = cross(a, b)


If input arguments are 2D, the function returns the cross products of corresponding rows:

# create two 6x3 arrays with random numbers
shape = (6, 3,)
a = normalize(np.random.uniform(-1.0, 1.0, shape))
b = normalize(np.random.uniform(-1.0, 1.0, shape))
cprod = np.zeros(shape)  # output has the same shape as inputs
cross(a, b, out=cprod)


If a 1D and 2D vector are specified, the cross product of each row of the 2D array and the 1D array is returned as a 2D array:

a = normalize([1, 2, 3])
b = normalize(np.random.uniform(-1.0, 1.0, (6, 3,)))
cprod = np.zeros(a.shape)
cross(a, b, out=cprod)


Back to top