# How does Maple simplify algebraic expressions?

I use Maple to simplify messy algebraic expressions. My problem is that sometimes Maple does not do it and I do not know why.

For example, I know that a given expression is identically zero (checked it by hand several times) and it only contains elementary functions so there is no fancy mathematics.

I type the expression in, apply "simplify", Maple does transform it somewhat but it is still far from zero.

Then I transform a small part of the expression, doing the job for Maple, it goes "Aha!" and finally simplifies to zero.

So for some reason Maple sometimes just does not transform certain very simple expressions. Any idea why does it not?

Here is the example. Expressions L and R call function Q several times. L-R should simplify to zero.

restart;

Q := proc (x1, x2) options operator, arrow; -ln(exp(x1)+exp(x2))-ln(exp(-x1)+exp(-x2)) end proc;

Q := proc (x1, x2) options operator, arrow; -ln(2+exp(x1-x2)+exp(x2-x1)) end proc;

L := ln(exp(x1)+exp(ln(exp(x2)+exp(x3))+Q(x2, x3)))+Q(x1, ln(exp(x2)+exp(x3))+Q(x2, x3));

R := ln(exp(ln(exp(x1)+exp(x2))+Q(x1, x2))+exp(x3))+Q(ln(exp(x1)+exp(x2))+Q(x1, x2), x3);

simplify(L); simplify(R); simplify(L-R);

The first function Q and the second function Q are identical, the only difference is one line elementary transformation going from the first to the second.

If you use the first Q, and then execute the expressions L, R,..., simplify(L-R), that call function Q, you don't get answer 0.

If you use the second Q (after restart of course), the expression simplify(L-R) will become zero, as it should be, checked by manual calculations.

• Could you please give an example? Jul 12, 2019 at 13:24
• Well it already fails to simplify $Q1(x,y)-Q2(x,y)$ to $0$. To make even simpler example, with $y=0$ we get $\ln(2+e^x+e^{-x})-\ln(e^x+1)-\ln(e^{-x}+1))$, which it also fails to simplify...
– Sil
Jul 12, 2019 at 17:38
• I did not notice that. thank you. Jul 12, 2019 at 18:33

restart;

kernelopts(version);

Maple 2019.1, X86 64 LINUX, May 21 2019, Build ID 1399874

Q := (x1,x2) -> -ln(exp(x1)+exp(x2))-ln(exp(-x1)+exp(-x2)):

L := ln(exp(x1)+exp(ln(exp(x2)+exp(x3))+Q(x2, x3)))
+Q(x1, ln(exp(x2)+exp(x3))+Q(x2, x3)):
R := ln(exp(ln(exp(x1)+exp(x2))+Q(x1, x2))+exp(x3))
+Q(ln(exp(x1)+exp(x2))+Q(x1, x2), x3):

simplify(L-R);

0


And now the other Q,

restart;

Q := (x1,x2) -> -ln(2+exp(x1-x2)+exp(x2-x1)):

L := ln(exp(x1)+exp(ln(exp(x2)+exp(x3))+Q(x2, x3)))
+Q(x1, ln(exp(x2)+exp(x3))+Q(x2, x3)):
R := ln(exp(ln(exp(x1)+exp(x2))+Q(x1, x2))+exp(x3))
+Q(ln(exp(x1)+exp(x2))+Q(x1, x2), x3):

ln(normal(expand(exp(L-R))));

0

ln(factor(expand(exp(combine(L-R)))));

0

subsindets(combine(L-R),specfunc(ln),
u->ln((expand@numer/expand@denom)(op(u))));

0

is(exp(L-R)=1);

true

is(L-R=0); # oof

false


The third way above, using subsindets, does not entail manually applying exp to the expression L-R up front. The call to subsindets forces Maple to expand numerator and denominator within any ln call, as a distinct action.

And, comparing just Q1 and Q2,

restart;

Q1 := (x1,x2) -> -ln(exp(x1)+exp(x2))-ln(exp(-x1)+exp(-x2)):
Q2 := (x1,x2) -> -ln(2+exp(x1-x2)+exp(x2-x1)):

ln(normal(expand(exp( Q1(x,y) -Q2(x,y) ))));

0


Of course, applying exp, manipulating, and then applying ln, is a specific trick.

• Could you please describe the range of expressions that can be successfully acted upon with this amazing specific trick? And is there any guiding principle of applying it? Jul 13, 2019 at 5:55
• In general zero recognition of a symbolic expression is undecidable (that's a mathematical aspect, not just something about Maple or Mathematica). Simplification is a heuristic art, not a foolproof algorithm. This trick is only one more rule to try. A heuristic is that it might help for sums of ln calls (of which your L-R is an example). I doubt it's possible to delineate sharply for which classes of problem this trick is guaranteed to either work or not work, or even just to work.
– acer
Jul 14, 2019 at 19:17
• My Answer's 3rd example, using subsindets, forced an expansion of both numerator and denominator (of rational expression involving products of sums exp calls) within all ln calls. It seems that Maple's heuristics don't try that, instead either expanding only partially, or preferentially expanding in some other way. I was lucky to notice this. Robust programmatic symbolic simplification is very hard to implement well. In principle it is impossible to do perfectly (in any language).
– acer
Jul 14, 2019 at 19:24

All computer algebra systems can "simplify" algebraic expressions and perhaps beyond. Some of the methods used include distributing products over sums and factoring. There is no standard set of simplification algorithms and precise definition of simplicity of an expression. Some simplifications are dubious, such as, simplifying $$\,\sqrt{x^2}\,$$ or $$\,\ln(e^x)\,$$ to $$\,x,\,$$ or $$\,x/x\,$$ to $$1$$ without any assumptions on $$\,x.\,$$ In many difficult cases you have to "help" the system by giving explicit transformation rules. Simplification is not an easy subject.

In the particular case of your expressions, you may be able to show that the two expressions are the same by first exponentiating, then factoring. The following Mathematica code illustrates this:

ln = Log; exp = Exp;
Q1 = Function[{x1, x2}, -ln[exp[x1] + exp[x2]] - ln[exp[-x1] + exp[-x2]]];
Q2 = Function[{x1, x2}, -ln[2 + exp[x1 - x2] + exp[x2 - x1]]];
L = ln[exp[x1] + exp[ln[exp[x2] + exp[x3]] + Q[x2, x3]]] +
Q[x1, ln[exp[x2] + exp[x3]] + Q[x2, x3]];
R = ln[exp[ln[exp[x1] + exp[x2]] + Q[x1, x2]] + exp[x3]] +
Q[ln[exp[x1] + exp[x2]] + Q[x1, x2], x3];
exp /@ (L == R /. Q -> Q1)
Factor[exp /@ (L == R /. Q -> Q2)]


which returns the result True twice. Notice the Factor required for Q2. The key fact about logarithms is that they are multi-valued and this makes even simple transformations such as $$\,\ln(x y) \to \ln(x) + \ln(y)\,$$ problematic except in special cases. Thus, by exponentiating, you can eliminate potential problems and change sums of logarithm terms into a product of factors.

• It is given that Q1 and Q2 are the same. What I need Maple to do is to check that L and R are equal. Jul 12, 2019 at 15:43
• what is it about exponentiating and factoring that helps it muddle through? Jul 13, 2019 at 5:49