# Contraction of square tensors

Let there be tensors A and B

A = Outer[Times, {1, 0}, {2, 0}]
B = Grad[{f[x, y], g[x, y]}, {x, y}]


with output

{{2, 0}, {0, 0}}
{{(f^(1,0))[x,y],(f^(0,1))[x,y]},{(g^(1,0))[x,y],(g^(0,1))[x,y]}}


Now, I am looking for tensor contraction of A and B ($$A:B$$) as follows

TensorContract[A, B]


Which produces output

TensorContract::contr: Invalid contraction {(f^(1,0))[x,y],(f^(0,1))[x,y]}.


How can get the correct result, in this case $$2\frac{\partial f}{\partial x}$$?

• Total[A B,2] or TensorContract[TensorProduct[A, B], {{1, 3}, {2, 4}}]. Commented Apr 2, 2019 at 17:21
• Thanks @HenrikSchumacher, If you can post it as an answer, I can accept it. In the second solution what does weird order of indices {{1,3},{2,4}} stands for? Commented Apr 2, 2019 at 17:50

Simplest method for matrices:

Total[A B, 2]


One can also use a Frobenius innerproduct

Tr[Transpose[A].B]


or simply

Flatten[A].Flatten[B]


In fact, the last one should be the most efficient for large numerical matrices (it takes advantage of vectorization and fused multiply-add operations).

If one insists on using TensorContract, one has to generate a tensor $$A \otimes B$$ first and then contract the slot pairs {1, 3} and {2, 4}

TensorContract[TensorProduct[A, B], {{1, 3}, {2, 4}}]


This is however not a good idea, because the intermediate tensor $$A \otimes B$$ contains $$n^4$$ elements when $$A$$ and $$B$$ are both $$n \times n$$ matrices.

By the way, Tr[Transpose[A].B] is also of complexity $$O(n^3)$$ due to the matrix-matrix product; so better also avoid that one.

• Using Activate@TensorContract[Inactive[TensorProduct][A, B], {{1, 3}, {2, 4}}] helps alleviate the intermediate swell, although it still won't be as fast as the other methods. Commented Apr 2, 2019 at 18:01
• @CarlWoll Ah, that's good to know! Thank you. Commented Apr 2, 2019 at 18:02