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}}

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}$?

  • $\begingroup$ Total[A B,2] or TensorContract[TensorProduct[A, B], {{1, 3}, {2, 4}}]. $\endgroup$ Commented Apr 2, 2019 at 17:21
  • $\begingroup$ 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? $\endgroup$
    – alekhine
    Commented Apr 2, 2019 at 17:50

1 Answer 1


Simplest method for matrices:

Total[A B, 2]

One can also use a Frobenius innerproduct


or simply


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.

  • 4
    $\begingroup$ 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. $\endgroup$
    – Carl Woll
    Commented Apr 2, 2019 at 18:01
  • $\begingroup$ @CarlWoll Ah, that's good to know! Thank you. $\endgroup$ Commented Apr 2, 2019 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.