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, 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.
Does RunMat run kron 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 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?⌄
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 Array functions
Shape
cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat
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.