So, I'd like to define a matrix M, that does not decompose into it's constituents when I do things like Tr[M], but I also want it's type to be retained.

(By type, I mean, what sort of a data structure it is, integer, or matrix, or vector, not literally the data-types available.)

For example:

In: Tr[a*M]

Out: a*Tr[M]

The solution from this answer doesn't seem to retain the type.

Is there any way to do this?

Edit: Basically, have an atomic symbol whose type is say a 2*3 matrix (or whatever is the equivalent of that in Mathematica), but whose individual elements don't matter for computation.

If I define a matrix as follows:

M = {{a,b},{c,d}}

Then, Tr[M] gives a+d. But I don't want the symbol to expand that way.

I'd like Tr[M] to give Tr[M] but Tr[a*M] to give a*Tr[M] and have other linear functions expand similarly.

  • $\begingroup$ I do not understand the question (not even what you mean by "type") ... a lot more explanation is needed, with concrete examples and a clear explanation of the motivation. Please edit and update the question. $\endgroup$
    – Szabolcs
    Jun 16, 2019 at 10:54
  • $\begingroup$ "have an atomic symbol" makes no sense because all symbols are atomic. {{a,b},{c,d}} is not atomic, and is not a symbol. Thus, do not give a value to M, keep it as a symbol, and look up symbolic tensor manipulations in the docs. Perhaps this is what you want. $\endgroup$
    – Szabolcs
    Jun 16, 2019 at 11:25

2 Answers 2


You could use my TensorSimplify package help with this. Install the paclet with:

    "Site" -> "http://raw.githubusercontent.com/carlwoll/TensorSimplify/master"

Once installed, you can load the package with:


This package defines the helper functions ToTensor and FromTensor. ToTensor converts expressions containing Tr and Dot into the equivalent version using TensorContract, and FromTensor does the opposite. So, your Tr expression can be simplified with:

$Assumptions = M \[Element] Matrices[{3, 3}] && a \[Element] Complexes;

FromTensor @ ToTensor @ Tr[a M]

a Tr[M]

An alternative is to just use the package's TensorSimplify function:

TensorSimplify @ Tr[a M]

a Tr[M]

TensorSimplify will work for more complicated arguments to Tr:

TensorSimplify @ Tr[a M + a^2 M.M]

a Tr[M] + a^2 Tr[M.M]

  • $\begingroup$ Thank you for the answer, and for writing this useful package! $\endgroup$ Jun 16, 2019 at 17:25

Perhaps what you want is symbolic tensors: http://reference.wolfram.com/language/tutorial/SymbolicTensors.html

$Assumptions = M ∈ Arrays[{3, 3}] && a ∈ Complexes;

TensorContract[a*M, {{1, 2}}]
(* a TensorContract[M, {{1, 2}}] *)
  • $\begingroup$ Thank you very much! How come this doesn't work with the function Tr, since it's just a specific case of TensorContract? $\endgroup$ Jun 16, 2019 at 12:50
  • 1
    $\begingroup$ @MahathiVempati I don't know, I'm not experienced in this area. But TensorContract was introduced specifically for symbolic tensor manipulations. Not every function supports this functionality. $\endgroup$
    – Szabolcs
    Jun 16, 2019 at 13:10

Your Answer

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

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