Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Here is the example:

Simplify[x + y, x + y == a]
Simplify[x + y, x + y == 5]

Mathematica 9 output:


I expect the complexity of a to be lower than complexity of Plus[x,y], and the result of the first line should be a.

Even if I specify ComplexityFunction explicitly:

Simplify[x + y, x + y == a, ComplexityFunction -> LeafCount]

I still get x+y as a result. It's however obvious that LeafCount[x+y] is greater than LeafCount[a].

Why does Simplify ignore x+y==a but uses x+y==5? How can I define the former assumption in a right way?

share|improve this question
It is interesting that Simplify[x y, x y == a] returns a. – whuber Jan 17 '13 at 1:48
As does Simplify[x^2+y^2,x^2+y^2==a], and Simplify[x+y,x+y==Cos[a]] returns Cos[a] – Cassini Jan 17 '13 at 2:03
... and, Simplify[x + y, x + y == Unevaluated[a]] gives a...? – kglr Jan 17 '13 at 2:09
Simplify[x+y, x+y == boo[]] also gives boo[]. Strange. – Szabolcs Jan 17 '13 at 2:15
It's something I've seen posted before, but can't find. It has to do with the fact that Simplify seems to have a preference for alphabetical order in sums. So you can get the expected result by replacing a with any symbol that lexically comes after x and y. For example, use Simplify[x + y, x + y == xPlusY] or just Simplify[x + y,x + y==z]. It probably tries substitutions not in all permutations but only in alphabetical order when sums are involved. – Jens Jan 17 '13 at 7:21

2 Answers 2

up vote 35 down vote accepted

We can see from the examples in the comments to the question that Simplify (and FullSimplify which builds on it) doesn't try all permutations of substitutions. That's probably justified in general to keep the computational effort from exploding, but in your example it leads to the quirky behavior that the variable names affect the result of the simplification.

For example, you get

Clear[a, z];
Simplify[x + y, x + y == a]

(* ==> x + y *)

Simplify[x + y, x + y == z]

(* ==> z *)

The only difference is that the last assumption uses a variable name that comes lexically after the names which you would like to replace.

I think the reason for this is that Mathematica tries substitutions in sums only in a specific sequence dictated by the alphabetical order of the variables it encounters.

My heuristic conclusion from this would be that assumptions in which you would like variables to be substituted by new names should have the new names chosen such that they come lexically after the "old" names.

If this doesn't work for you, the best alternative would be to do the elimination explicitly using Eliminate.

share|improve this answer

Here's a little tool from Adam Strzebonski that deals with the issue that Jens describes.

VOISimplify[vars_, expr_, assum_: True] := 
 Module[{perm, ee, best}, perm = Permutations[vars];
  ee = (FullSimplify @@ ({expr, assum} /. Thread[vars -> #])) & /@ perm;
  best = Sort[Transpose[{LeafCount /@ ee, ee, perm}]][[1]];
  best[[2]] /. Thread[best[[3]] -> vars]]


VOISimplify[{a, x, y}, x + y, x + y == a]
share|improve this answer
Do you have a link to the source of this? – Szabolcs Jan 20 '13 at 1:11
It looks like WRI is aware of this strange behavior for already seven years. Here is the link – Nick Stranniy Jan 20 '13 at 1:26
@NickStranniy That could very well be where I first read about this problem, too. Thanks for the link! – Jens Jan 20 '13 at 1:40
Cute, but it increases execution time by a factor Factorial[Length[vars]]. – Sjoerd C. de Vries Apr 1 at 8:02
@Sjoerd Yes, and Adam explicitly states exactly that. I think it's kind of the point: to consider all variable orderings gets very expensive very quickly. I suppose optimization may be possible but I am just the messenger here. – Mr.Wizard Apr 1 at 13:31

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.