oss — NumPy V1.23 Manual

Skip to main content Back to top Ctrl+K
  • User Guide
  • API reference
  • Building from source
  • Development
  • Release notes
  • Learn
  • NEPs
Choose version
  • GitHub

Section Navigation

  • NumPy’s module structure
  • Array objects
  • Universal functions (ufunc)
  • Routines and objects by topic
    • Constants
    • Array creation routines
    • Array manipulation routines
    • Bit-wise operations
    • String functionality
    • Datetime support functions
    • Data type routines
    • Mathematical functions with automatic domain
    • Floating point error handling
    • Exceptions and Warnings
    • Discrete Fourier Transform
    • Functional programming
    • Input and output
    • Indexing routines
    • Linear algebra
    • Logic functions
    • Masked array operations
    • Mathematical functions
      • numpy.sin
      • numpy.cos
      • numpy.tan
      • numpy.arcsin
      • numpy.asin
      • numpy.arccos
      • numpy.acos
      • numpy.arctan
      • numpy.atan
      • numpy.hypot
      • numpy.arctan2
      • numpy.atan2
      • numpy.degrees
      • numpy.radians
      • numpy.unwrap
      • numpy.deg2rad
      • numpy.rad2deg
      • numpy.sinh
      • numpy.cosh
      • numpy.tanh
      • numpy.arcsinh
      • numpy.asinh
      • numpy.arccosh
      • numpy.acosh
      • numpy.arctanh
      • numpy.atanh
      • numpy.round
      • numpy.around
      • numpy.rint
      • numpy.fix
      • numpy.floor
      • numpy.ceil
      • numpy.trunc
      • numpy.prod
      • numpy.sum
      • numpy.nanprod
      • numpy.nansum
      • numpy.cumulative_sum
      • numpy.cumulative_prod
      • numpy.cumprod
      • numpy.cumsum
      • numpy.nancumprod
      • numpy.nancumsum
      • numpy.diff
      • numpy.ediff1d
      • numpy.gradient
      • numpy.cross
      • numpy.trapezoid
      • numpy.exp
      • numpy.expm1
      • numpy.exp2
      • numpy.log
      • numpy.log10
      • numpy.log2
      • numpy.log1p
      • numpy.logaddexp
      • numpy.logaddexp2
      • numpy.i0
      • numpy.sinc
      • numpy.signbit
      • numpy.copysign
      • numpy.frexp
      • numpy.ldexp
      • numpy.nextafter
      • numpy.spacing
      • numpy.lcm
      • numpy.gcd
      • numpy.add
      • numpy.reciprocal
      • numpy.positive
      • numpy.negative
      • numpy.multiply
      • numpy.divide
      • numpy.power
      • numpy.pow
      • numpy.subtract
      • numpy.true_divide
      • numpy.floor_divide
      • numpy.float_power
      • numpy.fmod
      • numpy.mod
      • numpy.modf
      • numpy.remainder
      • numpy.divmod
      • numpy.angle
      • numpy.real
      • numpy.imag
      • numpy.conj
      • numpy.conjugate
      • numpy.maximum
      • numpy.max
      • numpy.amax
      • numpy.fmax
      • numpy.nanmax
      • numpy.minimum
      • numpy.min
      • numpy.amin
      • numpy.fmin
      • numpy.nanmin
      • numpy.convolve
      • numpy.clip
      • numpy.sqrt
      • numpy.cbrt
      • numpy.square
      • numpy.absolute
      • numpy.fabs
      • numpy.sign
      • numpy.heaviside
      • numpy.nan_to_num
      • numpy.real_if_close
      • numpy.interp
    • Miscellaneous routines
    • Polynomials
    • Random sampling
    • Set routines
    • Sorting, searching, and counting
    • Statistics
    • Test support
    • Window functions
  • Typing (numpy.typing)
  • Packaging
  • NumPy C-API
  • Array API standard compatibility
  • CPU/SIMD optimizations
  • Thread Safety
  • Global Configuration Options
  • NumPy security
  • Testing guidelines
  • Status of numpy.distutils and migration advice
  • numpy.distutils user guide
  • NumPy and SWIG
  • NumPy reference
  • Routines and objects by topic
  • Mathematical functions
  • numpy.cross
numpy.cross# numpy.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[source]#

Return the cross product of two (arrays of) vectors.

The cross product of a and b in \(R^3\) is a vector perpendicular to both a and b. If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. Where the dimension of either a or b is 2, the third component of the input vector is assumed to be zero and the cross product calculated accordingly. In cases where both input vectors have dimension 2, the z-component of the cross product is returned.

Parameters: aarray_like

Components of the first vector(s).

barray_like

Components of the second vector(s).

axisaint, optional

Axis of a that defines the vector(s). By default, the last axis.

axisbint, optional

Axis of b that defines the vector(s). By default, the last axis.

axiscint, optional

Axis of c containing the cross product vector(s). Ignored if both input vectors have dimension 2, as the return is scalar. By default, the last axis.

axisint, optional

If defined, the axis of a, b and c that defines the vector(s) and cross product(s). Overrides axisa, axisb and axisc.

Returns: cndarray

Vector cross product(s).

Raises: ValueError

When the dimension of the vector(s) in a and/or b does not equal 2 or 3.

See also

inner

Inner product

outer

Outer product.

linalg.cross

An Array API compatible variation of np.cross, which accepts (arrays of) 3-element vectors only.

ix_

Construct index arrays.

Notes

Supports full broadcasting of the inputs.

Dimension-2 input arrays were deprecated in 2.0.0. If you do need this functionality, you can use:

defcross2d(x, y): return x[..., 0] * y[..., 1] - x[..., 1] * y[..., 0]

Examples

Try it in your browser!

Vector cross-product.

>>> importnumpyasnp >>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> np.cross(x, y) array([-3, 6, -3])

One vector with dimension 2.

>>> x = [1, 2] >>> y = [4, 5, 6] >>> np.cross(x, y) array([12, -6, -3])

Equivalently:

>>> x = [1, 2, 0] >>> y = [4, 5, 6] >>> np.cross(x, y) array([12, -6, -3])

Both vectors with dimension 2.

>>> x = [1,2] >>> y = [4,5] >>> np.cross(x, y) array(-3)

Multiple vector cross-products. Note that the direction of the cross product vector is defined by the right-hand rule.

>>> x = np.array([[1,2,3], [4,5,6]]) >>> y = np.array([[4,5,6], [1,2,3]]) >>> np.cross(x, y) array([[-3, 6, -3], [ 3, -6, 3]])

The orientation of c can be changed using the axisc keyword.

>>> np.cross(x, y, axisc=0) array([[-3, 3], [ 6, -6], [-3, 3]])

Change the vector definition of x and y using axisa and axisb.

>>> x = np.array([[1,2,3], [4,5,6], [7, 8, 9]]) >>> y = np.array([[7, 8, 9], [4,5,6], [1,2,3]]) >>> np.cross(x, y) array([[ -6, 12, -6], [ 0, 0, 0], [ 6, -12, 6]]) >>> np.cross(x, y, axisa=0, axisb=0) array([[-24, 48, -24], [-30, 60, -30], [-36, 72, -36]]) Go BackOpen In Tab On this page
  • cross

Tag » Collinear Vectors Python