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.

Here is the example:

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

Mathematica 9 output:

x+y
5

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] –  David Skulsky Jan 17 '13 at 2:03
    
... and, Simplify[x + y, x + y == Unevaluated[a]] gives a...? –  kguler Jan 17 '13 at 2:09
    
Simplify[x+y, x+y == boo[]] also gives boo[]. Strange. –  Szabolcs Jan 17 '13 at 2:15
5  
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
show 3 more comments

2 Answers 2

up vote 18 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
add comment

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]]

Now:

VOISimplify[{a, x, y}, x + y, x + y == a]
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
add comment

Your Answer

 
discard

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.