kron — Compute Kronecker products in MATLAB and RunMat.
kron(A, B) forms the Kronecker (tensor) product of A and B. Output sizing and column-major block ordering follow MATLAB semantics.
Syntax
C = kron(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left numeric/logical/complex input array. |
B | Any | Yes | — | Right numeric/logical/complex input array. |
Returns
| Name | Type | Description |
|---|---|---|
C | Any | Kronecker product of inputs A and B. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:kron:TooManyInputs | Extra arguments were supplied after A and B. | kron: too many input arguments |
RunMat:kron:UnsupportedInput | Input values are not numeric/logical/complex arrays. | kron: unsupported input type |
RunMat:kron:Internal | Internal conversion/allocation/provider path fails. | kron: internal operation failed |
How kron works
- Works with scalars, vectors, and matrices. Higher-rank tensor inputs are a RunMat extension and are rejected while strict compatibility mode disables extensions.
- 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.
- All eight integer classes are supported. Integer operands must have the same class, or the other operand must be a scalar double; multiplication preserves the integer class and saturates on overflow. Complex integer arithmetic is rejected.
- 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.
Does RunMat run kron on the GPU?
For real floating-point operands, RunMat uses the exact owning provider's kron hook when the operands share a compatible device and precision; mixed host/resident floating inputs may upload the host operand temporarily and use the same path. Provider outputs are validated for shape, ownership, device, storage, precision, and non-aliasing. Typed-integer and otherwise unsupported cases gather automatically, compute with class-preserving host semantics, and restore the result to the source provider when possible. An explicit gpuArray source must remain resident or the call errors, while automatically resident values may return to host only when their class cannot be represented safely.
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 1Using kron with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how kron changes the result.
Run a small kron example, explain the result, then change one input and compare the output.
FAQ
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?⌄
Supported real floating-point operands stay on-device when their exact owning provider supplies a valid kron result. Unsupported cases gather automatically, compute on the host, and restore the class-preserving result to the source provider. Explicit gpuArray inputs either produce a resident result or raise an error; the source handles are not consumed.
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how kron is executed, line by line, in Rust.
- View the source for kron in Rust on GitHub
- Learn how the RunMat 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 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.