For instance, is there some way I can say "let A and B be arbitrary real $m\times n$ and $k\times m$ matrices, Simplify[Transpose[Transpose[A].Transpose[B]]]" and Mathematica would simplify it to B.A?

I know I can set A and B to be matrices containing symbols (e.g. A = Table[Subscript[a,i,j],{i,m},{j,n}]), but results can get quite messy if the problem is more complex than Transpose[Transpose[A].Transpose[B]]

EDIT: To answer @Searke and @Artes questions in the comments: I'm currently watching this Stanford online machine learning course. If you look at the lecture notes, pages 8-11, you see a some matrix calculations. I can follow these calculations with pen and paper, but I haven't found a way to derive e.g. this result from page 11 using Mathematica:

enter image description here

  • 2
    $\begingroup$ Nope. The issue is that for a given symbol there is no way to say "Oh this symbol is a symmetric, real matrix." To the best of my knowledge, there is no package for this. $\endgroup$ – Searke Mar 20 '12 at 13:17
  • 1
    $\begingroup$ I would actually be very interested in hearing what people think such functionality should be able to do. Does some other software do this and how do they do it? $\endgroup$ – Searke Mar 20 '12 at 13:18
  • $\begingroup$ See related question: mathematica.stackexchange.com/questions/8/… $\endgroup$ – Eli Lansey Oct 4 '12 at 11:16
  • 1
    $\begingroup$ What about this answer? mathematica.stackexchange.com/a/16378/1089 $\endgroup$ – chris Apr 20 '14 at 20:31
  • 1
    $\begingroup$ There's a MatrixD package that lets you differentiate matrix expressions -- mathematica.stackexchange.com/questions/138708/… $\endgroup$ – Yaroslav Bulatov Apr 26 '17 at 19:49

Indeed this is a one liner in NCAlgebra:

<< NC`
<< NCAlgebra`
NCGrad[1/2 (x ** z - y)^T ** (x ** z - y), z]

which results in

-y^T ** x + z^T ** x^T ** x


I guess that V9 now adds this capability:

$Assumptions = {
  Element[A, Matrices[{m, n}]],
  Element[B, Matrices[{n, k}]]

(* Out: B.A *)
  • 4
    $\begingroup$ This should be the accepted answer. $\endgroup$ – masterxilo Oct 31 '16 at 20:13
  • 4
    $\begingroup$ If you replace one of the ns with n+1 , it doesn't give an error message, though I would want one. $\endgroup$ – Gus Mar 22 '17 at 22:32
  • $\begingroup$ @Gus - I agree. $\endgroup$ – Mark McClure Mar 22 '17 at 23:57
  • $\begingroup$ This example yields the following error using Mathematica 11.3: "TensorDimensions::dotdim: Dot contraction of Transpose[A] and Transpose[B] is invalid because dimensions m and k are incompatible." $\endgroup$ – Jack H Apr 19 '18 at 1:46
  • 3
    $\begingroup$ The assumptions in the example are inconsistent with the input to TensorReduce. Either the input is Transpose[Transpose[B].Transpose[A]] or the assumptions are {Element[A, Matrices[{n, m}]], Element[B, Matrices[{k, n}]]}. Otherwise the dimensions do not agree and there is an error message. $\endgroup$ – jose May 15 '18 at 19:32

Initially, Mathematica is not designed for such abstract calculations.

But, Mathematica is a powerful programming language, so that one can add such functionality easily.

See the following examples in related area of differential geometry:


I am not sure, but maybe this software for Mathematica http://www.math.ucsd.edu/~ncalg/ could somehow help. The software is for a package called NCAlgebra developed by UC San Diego. I am not familiar with the detailed usage, but it claims to implement capability to study noncommutative inequalities, linear controls, and semidefinite programming within Mathmeatica.

  • 3
    $\begingroup$ Hi Konstantin, welcome to Mathematica.SE. Can you add some information about the software here? When that link dies ("when", not "if") your answer becomes useless. $\endgroup$ – stevenvh Oct 3 '12 at 17:56

Your Answer

