Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
communitycreator

What is the numpy.cross() function?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The numpy.cross() function in NumPy is used to compute the cross product of two given vector arrays.

Syntax


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

Parameter value

The numpy.cross() function takes the following parameter values:

  • a (required): This is an array_like component of the first vector.

  • b (required): This is an array_like component of the second vector.

  • axisa (optional): This is the axis of the first vector that defines it. Its default value is the last axis.

  • axisb (optional): This is the axis of the second vector that defines it. Its default value is the last axis.

  • axisc (optional): This is the axis of the third vector that contains the cross product vector.

  • axis (optional): If defined, it represents the axis of the first, second, and third vectors and cross products. In essence, it overrides the axisa, axisb and axisc.

Return value

The numpy.cross() function returns a vector cross product.


Note: It’s worth mentioning that a ValueError is raised if the dimensions of a and/or b are not equal to 22 or 33.


Code

import numpy as np
# creating vectors a and b with dimensions 3
a = [4, 3, 8]
b = [1, 9, 3]
# creating multiple vectors x and y with dimensions 3
x = np.array([[6, 3, 7], [3, 9, 1], [8, 8, 0]])
y = np.array([[8, 8, 0], [1, 9, 3], [6, 3, 7]])
# computing the cross product with axis' defined
myarray1 = np.cross(a, b, axisa = 0, axisb = 0)
myarray2 = np.cross(x, y, axisa = 0, axisb = 0)
# computing the cross product without defining axis
myarray3 = np.cross(x, y)
# printing vector cross products
print("Cross product of vectors 'a' and 'b':\n", myarray1)
print("\nCross product of vectors 'x' and 'y' with 'axisa' and 'axisb' defined:\n", myarray2)
print("\nCross product of vectors 'x' and 'y' without defining axis:\n", myarray3)
Implementing the numpy.cross() function

Explanation

  • Line 1: We import the numpy module.

  • Lines 4-5: We create the vector components a and b of dimensions 3.

  • Line 8-9: We create multiple vectors x and y with dimensions 3.

  • Line 12: We compute cross products of vectors a, b with axisa and axisb equal to 0.

  • Line 13: We compute cross products of vectors x, y with axisa and axisb equal to 0.

  • Line 16: We compute cross products of vectors x, y without any axis.

  • Line 19-21: We print the cross product of vectors stored in variable2 myarray1, myarray2, and myarray3.

RELATED TAGS

numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring