Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.cross

Computes the cross product of two vectors or arrays of vectors in R^3. By default, it treats the last axis of each input as the vector-component axis and returns the resulting vector cross product(s).

How it works

np.cross computes the cross product of vector components in R^3. It treats the final axis as the vector axis by default, supports arrays containing multiple vectors, and lets you override the vector and output axes with axis-related arguments.

Syntax

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

Parameters

NameTypeDescription
aarray_likeComponents of the first vector or vectors.
barray_likeComponents of the second vector or vectors.
axisaint, optionalAxis of a defining its vectors; defaults to the last axis.
axisbint, optionalAxis of b defining its vectors; defaults to the last axis.
axiscint, optionalAxis of the result containing the cross-product vectors; defaults to the last axis.
axisint, optionalShared axis of a, b, and the result defining the vectors and cross products.

Returns

ndarray: vector cross product(s).

Key points

  • The function accepts either individual vectors or arrays containing vectors and computes their cross product.
  • For three-dimensional inputs, the result is perpendicular to both operands.
  • Unless configured otherwise, the final dimension of each input holds its vector components.
  • axisa, axisb, and axisc each default to the final dimension of their corresponding array.
  • The return value is an ndarray containing one or more cross-product vectors.

Common mistakes

  • For the stable documentation, the vector-defining axes of array inputs must have length three.
  • Use axis when the same vector axis applies to both inputs and the output; it overrides the individual axis settings.

Example: Cross product of two vectors

import numpy as np

a = np.array([1, 0, 0])
b = np.array([0, 1, 0])
print(np.cross(a, b))  # [0 0 1]

Computes the vector perpendicular to the two supplied three-dimensional vectors.

When to use it

Single-vector geometry

Compute a perpendicular direction from two 3D vectors in geometry, physics, or graphics.

Batch vector operations

Apply the same cross-product operation across batches of vectors stored in arrays.

Custom axis layouts

Work with arrays whose vector components are not on the final dimension.

Interview questions

QWhat does np.cross compute, and what is its default vector axis?

It returns the cross product of two vectors in R^3, producing a vector perpendicular to both inputs. For array inputs, vectors are identified along the last axis by default.

Follow-up: How would you handle vectors stored along another dimension? Set axisa and axisb for the input vector axes and axisc for the output vector axis, or use axis when one axis applies to all three.

QWhat are the required inputs and return value?

a and b are array-like inputs containing the first and second vector components. The function returns an ndarray containing the vector cross product or products.

Follow-up: What constraint applies to the vector axes in the current stable documentation? They must have three dimensions.

QHow do the axis controls work?

axisa, axisb, and axisc independently select the vector axes of a, b, and the result; each defaults to -1, meaning the last axis. The axis argument provides one shared axis selection for the inputs and output.

Follow-up: Why might explicit axis arguments be needed? A batch array may store vector components on a non-final dimension, so the defaults would identify the wrong axis.

  • numpy.linalg.cross
  • numpy.cross

Source: numpy.cross. Checked 2026-10-15.