50
$\begingroup$

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?

$\endgroup$
8
  • 2
    $\begingroup$ It is interesting that Simplify[x y, x y == a] returns a. $\endgroup$
    – whuber
    Commented Jan 17, 2013 at 1:48
  • $\begingroup$ As does Simplify[x^2+y^2,x^2+y^2==a], and Simplify[x+y,x+y==Cos[a]] returns Cos[a] $\endgroup$
    – Cassini
    Commented Jan 17, 2013 at 2:03
  • $\begingroup$ ... and, Simplify[x + y, x + y == Unevaluated[a]] gives a...? $\endgroup$
    – kglr
    Commented Jan 17, 2013 at 2:09
  • $\begingroup$ Simplify[x+y, x+y == boo[]] also gives boo[]. Strange. $\endgroup$
    – Szabolcs
    Commented Jan 17, 2013 at 2:15
  • 5
    $\begingroup$ 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. $\endgroup$
    – Jens
    Commented Jan 17, 2013 at 7:21

2 Answers 2

43
$\begingroup$

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.

$\endgroup$
23
$\begingroup$

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
$\endgroup$
4
  • 1
    $\begingroup$ It looks like WRI is aware of this strange behavior for already seven years. Here is the link $\endgroup$ Commented Jan 20, 2013 at 1:26
  • $\begingroup$ @NickStranniy That could very well be where I first read about this problem, too. Thanks for the link! $\endgroup$
    – Jens
    Commented Jan 20, 2013 at 1:40
  • 1
    $\begingroup$ Cute, but it increases execution time by a factor Factorial[Length[vars]]. $\endgroup$ Commented Apr 1, 2015 at 8:02
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Apr 1, 2015 at 13:31

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.