NumPy cross()

The numpy.cross() method computes the cross product of two vectors.

Example

import numpy as np

# create two input arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# compute the cross product of array1 and array2 result = np.cross(array1, array2)
print(result) # Output:[-3 6 -3]

cross() Syntax

The syntax of the numpy.cross() method is:

numpy.cross(a, b, axisa = -1, axisb = -1, axisc = -1, axis = None)

cross() Arguments

The numpy.cross() method takes following arguments:

  • a - the first input array
  • b - the second input array
  • axisa (optional) - the axis along which to take the cross product for a
  • axisb (optional) - the axis along which to take the cross product for b
  • axisc (optional) -the axis along which to take the cross product for c
  • axis (optional) - if specified, it overrides axisa, axisb, and axisc

Note: All the optionals arguments here take integer values.


cross() Return Value

The numpy.cross() method returns an array containing the cross product of a and b.


Example 1: Find the Cross Product of Two Arrays

import numpy as np

# create two input arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# compute the cross product of array1 and array2 result = np.cross(array1, array2)
print(result)

Output

[-3  6 -3]

Here, we have two input arrays:

array1 = [1, 2, 3]
array2 = [4, 5, 6]

Now to compute the cross product, we apply the following formula:

cross product = (array1[1] * array2[2] - array1[2] * array2[1], 
                 array1[2] * array2[0] - array1[0] * array2[2], 
                 array1[0] * array2[1] - array1[1] * array2[0])

Then, substituting the values from the input arrays:

cross product = (2 * 6 - 3 * 5, 
                 3 * 4 - 1 * 6, 
                 1 * 5 - 2 * 4)

Finally, after evaluating the expressions:

cross product = (-3, 6, -3)

Therefore, the output array of np.cross(array1, array2) is [ -3, 6, -3].


Example 2: Use of axisa, axisb, axisc Arguments in cross()

import numpy as np

# create two input arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[7, 8, 9], [10, 11, 12]])

# compute the cross product # along the first axis of each array resultAxis = np.cross(array1, array2, axisa = 1, axisb = 1, axisc = 1)
print("Result with axisa = 1, axisb = 1, axisc = 1:") print(resultAxis)

Output

Result with axisa = 1, axisb = 1, axisc = 1:
[[-6 12 -6]
 [-6 12 -6]]

Here, to compute the cross product along axis 1, we consider the vectors along axis 1 of both array1 and array2:

array1 = ([[1, 2, 3],
          [4, 5, 6]])

array2 = ([[7, 8, 9],
          [10, 11, 12]])

For the first row of array1 and array2, the cross product is calculated as:

[1, 2, 3] × [7, 8, 9] = [-6, 12, -6]

Similarly, for the second row of array1 and array2, the cross product is calculated as:

[4, 5, 6] × [10, 11, 12] = [-6, 12, -6]

Example 3: Use of axis Argument in cross()

import numpy as np

# create two input arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[7, 8, 9], [10, 11, 12]])

# compute the cross product along axis 0 axis0 = np.cross(array1, array2, axis = 0)
print("Result with axis = 0:") print(axis0)
# compute the cross product along axis 1 axis1 = np.cross(array1, array2, axis = 1)
print("\nResult with axis = 1:") print(axis1)

Output

Result with axis=0:
[-18 -18 -18]

Result with axis=1:
[[-6 12 -6]
 [-6 12 -6]]

Here,

  • axis = 0 - output [-18, -18, -18] represents the cross product along the column vectors of array1 and array2.
  • axis = 1 - output [[ -6, 12, -6], [ -6, 12, -6]] represents the cross product along the row vectors of array1 and array2.

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community