RunMat
GitHub

cross — Compute the vector cross product of matching 3-element vectors along a chosen dimension.

cross(A, B) computes the vector cross product for matching 3-element vectors. When A and B are matrices or higher-rank tensors, cross operates along the first dimension whose length is 3, unless you supply an explicit dimension.

How cross works

  • Inputs A and B must be the same size.
  • With no dimension argument, cross uses the first dimension whose extent is exactly 3.
  • cross(A, B, dim) requires dim to be a valid array dimension and size(A, dim) == 3.
  • The output has the same shape as the inputs because each input 3-vector produces one output 3-vector.
  • Logical and integer inputs are promoted to double precision before evaluation.
  • Real gpuArray inputs remain GPU-resident when the active provider implements the cross hook; otherwise RunMat gathers, evaluates on the host, and re-uploads the real result.

How RunMat runs cross on the GPU

The WGPU provider computes real-valued cross products by gathering the three vector components into temporary device tensors, applying multiply/subtract kernels on-device, and scattering the results back into the output tensor. Providers that do not implement cross fall back to the host reference path.

GPU memory and residency

You usually do not need to call gpuArray explicitly for cross. RunMat keeps real-valued tensors on the GPU whenever the active provider supports the operation, and otherwise falls back to a gather -> host compute -> re-upload path automatically.

Examples

Computing the cross product of row vectors

a = [1 0 0];
b = [0 1 0];
c = cross(a, b)

Expected output:

c =
     0     0     1

Computing the cross product of column vectors

u = [1; 0; 0];
v = [0; 1; 0];
w = cross(u, v)

Expected output:

w =
     0
     0
     1

Applying cross row-wise across a matrix

A = [1 0 0; 0 1 0];
B = [0 1 0; 0 0 1];
C = cross(A, B, 2)

Expected output:

C =
     0     0     1
     1     0     0

Evaluating cross on GPU-resident tensors

G1 = gpuArray([1 0 0]);
G2 = gpuArray([0 1 0]);
G = cross(G1, G2);
result = gather(G)

Expected output:

result =
     0     0     1

FAQ

Does cross require vectors of length 3?

Yes. The operating dimension must have extent 3, whether the vectors are stored as rows, columns, or slices of a higher-rank tensor.

How is the default dimension chosen?

RunMat follows MATLAB semantics and picks the first dimension whose size is exactly 3.

What happens if I pass dim explicitly?

The dimension must exist and must have length 3; otherwise cross raises a descriptive error.

Can I use complex inputs?

Yes. Complex host inputs use the standard bilinear cross-product formula without conjugation.

Does the result stay on the GPU?

For real-valued gpuArray inputs, yes when the provider implements cross. Complex results currently remain on the host because GPU complex tensor support is not yet wired through this builtin.

These functions work well alongside cross. Each page has runnable examples you can try in the browser.

dot, kron, mtimes, norm, sum

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cross works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat for free

Open the sandbox and start running MATLAB code in seconds. No account required.