RunMat
GitHub

cross — Compute vector cross products in MATLAB and RunMat.

cross(A, B) computes vector cross products for matching 3-element vectors. For matrices and higher-rank arrays, it uses the first dimension of length 3 unless an explicit dimension is provided, following MATLAB semantics.

Syntax

C = cross(A, B)
C = cross(A, B, dim)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesLeft operand.
BAnyYesRight operand.
dimNumericScalarYesDimension with extent 3.

Returns

NameTypeDescription
CNumericArrayCross product result.

Errors

IdentifierWhenMessage
RunMat:cross:InvalidArgumentArgument count or dimension argument is invalid.cross: invalid argument
RunMat:cross:InvalidInputInputs are unsupported or incompatible for cross product.cross: A and B must be the same size.
RunMat:cross:InternalRuntime cannot materialize cross outputs.cross: internal runtime failure

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.

Does RunMat run 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

Using cross with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how cross changes the result.

Run a small cross example, explain the result, then change one input and compare the output.

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.

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.