Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
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. –  Oleksandr R. Jul 14 '13 at 18:41
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. –  Kevin S. Van Horn Jul 15 '13 at 4:00
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. –  Oleksandr R. Jul 15 '13 at 4:21

1 Answer 1

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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