kron — Compute the Kronecker (tensor) product of two scalars, vectors, matrices, or N-D arrays.
kron(A, B) forms the Kronecker (tensor) product of arrays A and B. Every element of A scales a copy of B, yielding a block-structured array whose size is size(A) .* size(B) when interpreted with MATLAB's column-major ordering.
How kron works in RunMat
- Works with scalars, vectors, matrices, and higher-rank tensors; trailing singleton dimensions are honoured.
- If either input is complex, the output is complex and uses MATLAB's complex arithmetic rules.
- Logical and char inputs are promoted to double precision before multiplication.
- Scalar arguments act as uniform scalars (
kron(a, B)behaves likea * B). - Empty dimensions propagate; if any replicated dimension is zero, the result is empty along that axis.
- Invalid inputs (non-numeric/non-logical, or values that would overflow the maximum size) raise descriptive MATLAB-style errors.
How kron runs on the GPU
When a GPU provider is active, RunMat first consults the provider's kron hook so the operation can run entirely on the device. Providers that have not implemented this hook fall back to a safe path: the runtime gathers both operands to host memory, performs the Kronecker product in Rust, and then attempts to upload the result back to the originating device so downstream GPU work can continue without additional copies.
GPU memory and residency
You usually do **not** need to call gpuArray explicitly. RunMat's planner keeps values resident on the GPU whenever it is profitable. Explicit gpuArray calls remain supported for MATLAB compatibility and for users who want direct control over residency.
Examples
Computing the Kronecker product of two matrices
A = [1 2; 3 4];
B = [0 5; 6 7];
C = kron(A, B)Expected output:
C =
0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28Kronecker product with row and column vectors
row = [1 2 3];
col = (1:3)';
K = kron(row, col);
size(K)Expected output:
ans =
3 9Scaling a matrix with a scalar using kron
A = [1 2; 3 4];
S = kron(2, A)Expected output:
S =
2 4
6 8Building block-diagonal systems with kron
I = eye(3);
M = [1 0; 0 -1];
blockDiag = kron(I, M)Kronecker product of complex matrices
A = [1+2i 0; 0 3-1i];
B = [0 1; 2 3i];
C = kron(A, B)Using kron with logical masks
mask = logical([1 0; 0 1]);
tile = kron(mask, ones(2))Running kron on GPU-resident tensors
G = gpuArray(rand(2));
H = gpuArray([1 -1; -1 1]);
K = kron(G, H);
isgpuarray(K)Expected output:
ans = logical 1FAQ
When should I use kron instead of standard matrix multiplication?
Use kron when you need block matrices built from every combination of two operands. Matrix multiplication collapses dimensions, while kron expands them.
Does kron preserve sparsity?
The current dense runtime converts inputs to dense double or complex tensors. Sparse fidelity is on the roadmap; today, sparse inputs are first densified.
Can I mix real and complex inputs?
Yes. If either operand is complex, the output is complex, with MATLAB-compatible real/imaginary components.
What happens with logical or boolean inputs?
Logical arrays are converted to doubles (0 and 1) before the Kronecker product, mirroring MATLAB's behaviour.
Are character arrays supported?
Yes. Character arrays are converted to their Unicode code points (double precision) before forming the product.
How big can the result be?
kron checks for overflow when multiplying dimension sizes. If the result would exceed the maximum addressable size, the builtin raises a descriptive error before allocating memory.
Does the GPU path always stay on-device?
When the provider supplies a kron hook, execution stays on-device. Otherwise RunMat gathers to the host, computes the product, and re-uploads the result when a provider is available.
Related functions to explore
These functions work well alongside kron. Each page has runnable examples you can try in the browser.
repmat, reshape, sum, gpuArray, gather, cat, circshift, diag, flip, fliplr, flipud, horzcat, ipermute, permute, rot90, squeeze, tril, triu, vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how kron works, line by line, in Rust.
- View kron.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.