How can I get Mathematica to simplify the following expressions, with $Assumptions including Element[n,Integers], n > 0, and Element[n, Vectors[Reals]]:



Neither TensorReduce nor FullSimplify simplifies the above expressions.

Context for this question: I am frequently implementing new Bayesian statistical models that automated tools such as OpenBUGS can't handle, and so I need to work out expressions for various full conditional probability densities from the (large and complex) joint probability distribution. These expressions need to be simplified as much as possible, for computational efficiency. I want to see if I can use Mathematica to do this more quickly and reliably, instead of always doing it by hand. One of my needs is to be able to manipulate and/or simplify various linear algebra expressions that often arise; the dimensionality of these matrices and vectors is itself a parameter of the problem, not a fixed number.

  • $\begingroup$ As I found here, Dot does not seem to be understood in this context, and must be expressed as the contraction of a tensor product in order for the simplifications to proceed. This is inconvenient but it seems to be a fact of life as of Mathematica 9. $\endgroup$ Commented Jul 14, 2013 at 18:41
  • $\begingroup$ That doesn't seem to be enough. I can't find any way to get Mathematica to simplify TensorContract[TensorProduct[IdentityMatrix[n],x],{{2,3}}], nor even TensorContract[IdentityMatrix[n],{{1,2}}]. It's beginning to look to me as if Mathematica has no built-in rules relating various ways of creating symbolic vectors and matrices whose dimensions are symbolic expressions with the tensor operations. $\endgroup$ Commented Jul 15, 2013 at 4:00
  • $\begingroup$ I think that is correct in the sense that the only operations that work in this context (so far, anyway) are the ones whose names begin with Tensor. There doesn't seem to be a way to define a tensor that is the identity matrix; the best one can apparently do is to have a symmetric matrix. $\endgroup$ Commented Jul 15, 2013 at 4:21

2 Answers 2


I agree that it's a bit odd that Mathematica doesn't simplify these expressions with its built-in functions, especially in the symbolic tensor language (i.e. using TensorContract and TensorReduce). Nonetheless, we can teach it how to simplify the identity matrix ourselves.

I've chosen to implement this for Dot, since that is what you initially asked. I do think that the symbolic tensor language is the appropriate context though. But implementing it there would have been a good deal more complicated. For Dot, it's a reasonably simple single function:

SimplifyIdentityMatrix[expr_] := expr //.
     Dot[before___, IdentityMatrix[n_], tensor_, after___] /; 
       TensorDimensions[tensor][[1]]  === n :> Dot[before, tensor, after],
     Dot[before___, tensor_, IdentityMatrix[n_], after___] /; 
       TensorDimensions[tensor][[-1]] === n :> Dot[before, tensor, after]

We then have:

$Assumptions = v ∈ Vectors[n]

IdentityMatrix[n].IdentityMatrix[n] // SimplifyIdentityMatrix
IdentityMatrix[n].v // SimplifyIdentityMatrix

In M10+, your expected simplifications work:

TensorReduce[IdentityMatrix[n].x, Assumptions->x ∈ Vectors[n]]




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.