cross — Compute vector cross products in MATLAB and RunMat.
cross(A, B) computes vector cross products for matching 3-element vectors. MATLAB documents single and double data operands; RunMat mode additionally admits logical and all eight real or complex integer classes through explicit compatibility gates. Output is single when either data operand is single and double otherwise.
Syntax
C = cross(A, B)
C = cross(A, B, dim)Inputs
A- Type
Any- Required
- Yes
- Description
- Left operand.
B- Type
Any- Required
- Yes
- Description
- Right operand.
dim- Type
NumericScalar- Required
- Yes
- Description
- Dimension with extent 3.
Returns
C- Type
NumericArray- Description
- Cross product result.
Errors
RunMat:cross:InvalidArgument- When
- Argument count or dimension argument is invalid.
- Message
cross: invalid argument
RunMat:cross:InvalidInput- When
- Inputs are unsupported or incompatible for cross product.
- Message
cross: A and B must be the same size.
RunMat:cross:Internal- When
- Runtime cannot materialize cross outputs.
- Message
cross: internal runtime failure
Behavior and supported arrays
cross(A, B) computes vector cross products for matching 3-element vectors. MATLAB documents single and double data operands; RunMat mode additionally admits logical and all eight real or complex integer classes through explicit compatibility gates. Output is single when either data operand is single and double otherwise.
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.
Typed-integer and logical data operands are RunMat-only extensions, independently gated for A and B, and cross a floating evaluation boundary. Output is single when either data operand is single and double otherwise; arithmetic has floating rounding/overflow behavior rather than integer saturation.
An ordinary positive integer-valued double dim is documented. Because the public cross reference does not list typed or logical classes for dim, exact all-eight-class typed-integer dimensions and logical dimensions are conservatively classified as separately gated RunMat extensions; resident typed dimensions gather through their owner for exact validation.
A native provider hook is used only for same-owner, same-device real floating handles whose result has the required class. Other supported resident values gather independently and eligible real or complex floating results return to the first owner.
GPU residency and provider behavior
You usually do not need to call gpuArray explicitly for cross. RunMat uses a class-preserving native hook for eligible same-owner real inputs and otherwise follows a gather -> host compute -> class-preserving re-upload path for supported real and complex floating values.
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.
Same-owner, same-device real floating inputs may use a provider cross hook when its result preserves the required f32/f64 class and belongs to the invoked provider/device. Other resident inputs gather through their owners; eligible real or complex floating results are restored when the owner preserves class and complexity.
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 1Computing the cross product of column vectors
u = [1; 0; 0];
v = [0; 1; 0];
w = cross(u, v)Expected output:
w =
0
0
1Applying 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 0Evaluating 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 1Using 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 for documented single/double complex inputs. RunMat mode also accepts typed complex integers under the corresponding operand gate, converts both components to double, and applies the standard bilinear formula without conjugation.
Does the result stay on the GPU?⌄
Same-owner, same-device real floating inputs remain resident when a provider's cross hook returns the required class. Other resident inputs gather independently, and eligible real or complex floating results return to the first owner. Typed complex integers work on the host, but the current provider ABI cannot encode typed complex-integer resident buffers.
Related Linalg functions
Ops
ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Source you can read
Every RunMat function's source is public. Read exactly how cross is executed, line by line, in Rust.
- View the source for cross in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.