About Tensor Contraction Calculator
This tensor contraction calculator multiplies two tensors and sums over the indices you choose, using an einsum expression, and reports the shape and values of what remains. It opens on two rank 3 tensors with ijk,jkl->il, which contracts two axes at once. It is free, needs no sign-up and runs in your browser, so nothing you enter is uploaded.
Contraction is summing over a repeated index. Multiplying matrices is the standard case: in C[i,k] = Σ_j A[i,j] B[j,k], the index j is contracted, so the rank drops by two from the 4 axes entered to the 2 that remain. A dot product contracts every index and leaves a rank 0 number. Contracting no index at all is the tensor product.
Write the contraction in the Einsum tab: repeated letters that you leave out of the output are the contracted ones. For a rank 3 and a rank 2 tensor, ijk,kl->ijl contracts k and leaves rank 3. Contracting an index within a single tensor, as in a trace (ii->), also works, and axis sizes of a contracted index must match.
How to use Tensor Contraction Calculator
- Pick a mode: Shape and reshape, Tensor product, Kronecker product, or Einsum.
- Enter tensor A as nested brackets like [[1, 2, 3], [4, 5, 6]], or as a flat list of numbers with a shape such as 2, 3 in the shape box. One shape entry may be -1.
- For the product and einsum modes, enter tensor B the same way.
- In Shape mode, type a target shape to reshape to (for example 3, -1) and optionally an axes order for transpose (for example 1, 0).
- In Einsum mode, write the expression, such as ij,jk->ik, or pick an example chip: matrix product, trace, diagonal, transpose or a three-matrix chain. One box appears for each operand, labelled with its axes.
- Read the rank, shape and element count above each result, and the values below as nested brackets.
Features
- Shape inference from nested brackets, or a flat list plus a shape.
- Reshape with one -1 dimension worked out automatically.
- Transpose with reversed axes by default, or a custom axes order for any rank.
- Tensor (outer) product whose shape is the shape of A followed by the shape of B.
- Kronecker product with NumPy-style padding, so a vector and a matrix can be combined.
- Einsum with one, two or three operands: the trace (ii->), the diagonal (ii->i), the transpose (ij->ji), the matrix product, a three-matrix chain (ij,jk,kl->il), batch products and more, with an implicit output when you leave off the arrow.
- Each tensor box is labelled with the axes the expression gives it, and the expression is shown as a KaTeX formula.
- Shapes and small results are typeset with KaTeX, for example the shape as R with a superscript of 2 by 3 and a matrix as a bracketed array.
- A size guard: an einsum over 10 million multiply-adds is refused with the exact count, and one over 100,000 runs in a Web Worker so the page stays responsive.
- Readable errors for ragged lists, non-numbers, mismatched sizes and bad axes.
- Rank, shape and element count shown for every result, and values printed as nested brackets rounded to nine decimals.
Contraction as einsum
The contracted indices are the letters that appear in the inputs but not after the arrow.
ij,jk->ik contract j (matrix product)
ijk,kl->ijl contract k
ij,ij-> contract i and j (all entries)Worked examples
ijk,jkl->il with A [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] and B [[[1, 0], [0, 1]], [[1, 1], [0, 1]]] gives Shape [2, 2]: [[4, 9], [12, 21]].
Both j and k are contracted, so two axes from each tensor disappear and rank 3 plus rank 3 leaves rank 2.
ij,jk->ik with A of shape [2, 3] and B of shape [3, 2] gives Shape [2, 2]. With A [[1, 2, 3], [4, 5, 6]] and B [[1, 2], [3, 4], [5, 6]] the result is [[22, 28], [49, 64]].
ij,ij-> with A = [[1, 2, 3], [4, 5, 6]] twice gives 91, a rank 0 result
Every index is contracted.
ij,jk->ik with A of shape [2, 3] and B of shape [2, 2] gives Error: Axis "j" has sizes 3 and 2.
The contracted index must have the same size in both tensors.
Good to know
Using this page
Open the Einsum tab, type your expression, and enter one tensor as nested brackets for each operand. The page takes one to three operands, so ii-> (a trace) and ij,jk,kl->il (a chain) both work. A calculation of more than 10 million multiply-adds is refused.
Privacy: how it works
- All arithmetic is done by TypeScript code in your browser tab. There is no network request when you calculate, no library is downloaded to do it, and nothing is stored.
Tips
- Use -1 in a reshape to let the calculator fill in one dimension. A 24-element tensor reshaped to 4, -1 becomes shape [4, 6].
- If a reshape fails, multiply your target dimensions and compare with the element count shown for tensor A.
- To check an einsum string, try tiny tensors with distinct values, then compare the rank and shape before the values.
- A custom transpose order lists the old axes in their new positions. For shape [2, 3, 4], the order 1, 0, 2 gives shape [3, 2, 4].
Frequently asked questions
Is it really free?
Yes. There is no price, no trial and no credit card. Ulkaa Tool does the work with your own device, so there is no server bill to pass on to you.
Do I need an account?
No. There is no sign-up and no login. Open the page and use it. An Ulkaa account is not needed for anything on this tool.
Does it run in my browser?
Yes. Your values are processed by code running in your browser tab, on your own device.
Is my data uploaded?
No. Your values are never sent to Ulkaa or to anyone else. You can check this yourself by watching the Network tab in your browser developer tools while you use the tool.
What is tensor contraction?
It is summing the product of two tensors over one or more indices that they share. Matrix multiplication and the dot product are special cases.
How does contraction change the rank?
Each contracted index removes one axis from each tensor. Contracting one index between a rank 2 and a rank 2 tensor leaves rank 2.
Can I contract two indices of the same tensor, as in a trace?
Yes. Write ii-> with one operand. For [[1, 2], [3, 4]] the trace is 5.
More Tensor Calculator pages
- Tensor CalculatorCheck shapes, reshape and transpose, and compute tensor products, Kronecker products and einsum contractions.
- Tensor Product CalculatorThe tensor product of two tensors, with the result shape worked out.
- Tensor Shape CalculatorShape, rank and element count of a tensor, plus reshape and transpose.
- Einsum CalculatorEvaluate einsum expressions on one to three tensors and check your own.
- Kronecker Product CalculatorThe Kronecker product of two matrices, with block layout.