# Why MMA refuses to simplify the following simple expression?

I have the following simple expression

Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2


that can be shown to simplify to x for positive x (and same for $$x < -1$$ if principal branch of log is taken, $$\log (x) = i \pi + \log (-x)$$). However, none of the following

Simplify[Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2]
FullSimplify[Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2]
Simplify[Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2,
Assumptions -> {x > 0}]
FullSimplify[Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2,
Assumptions -> {x > 0}]


finds this simplification. Why is that? To show that the expression simplifies accordingly, use $$a^2 - b^2 = (a+b)(a-b)$$ formula inside the exponential and in the denominator $$\log (1+1/x) = \log (1+x) - \log(x)$$. Some stuff cancels out and you'll be left with $$\exp \log (x+x^2) - x^2 = x$$. I have no clue why MMA didn't choose to go down this route, I assumed that the common formulas like $$a^n - b^n$$ are known to it. I tried Simplify[Log[1 + 1/x] + Log[x], Assumptions -> {x > 0}] and it correctly returned log[1+x], so this might not be the issue here.

This specific expression came up in some wider context, I frequently use MMA to simplify expressions after integration to get some nicer form and was surprised that this couldn't be simplified.

• I tried with Maple and it can do it. Jan 3, 2021 at 10:09
• While it isn't Simplify at least Reduce[Exp[(Log[1+x]^2-Log[x]^2)/Log[1+1/x]]-x^2==x && x>0,x] returns 0<x<Infinity but it says it cannot accomplish this when given x < -1 which I am guessing is because of the principal branch issue.
– Bill
Jan 3, 2021 at 10:12
• Somewhat relevant mathematica.stackexchange.com/q/207506/9469 Jan 3, 2021 at 10:54
• Marius, I'm not familiar with Maple, sorry :) @Bill this is reassuring, there is a way to verify it after all! The region $x < -1$ is not that important to me, in my application $x$ was positive anyway, I mentioned it only for completeness. However, I'm more interested in applications that yield the result, rather than verifying something I already worked out manually outside of MMA... Jan 3, 2021 at 11:12
• PowerExpand[Factor[#]]&//@ex seems to work, where ex is your expression. Jan 3, 2021 at 11:32

Let y be your expression. Here is the way to establish the desired fact:

a = Assuming[x > 0, D[y, x] // FullSimplify]
(* 1 *)
b = Limit[y, x -> 1]
(* 1 *)
Y = DSolveValue[{z'[x] == a, z[1] == b}, z, x];
Y[x]
(* x *)


Of course, it is an indirect albeit mathematically rigorous way.

Explanation Assume we would like to simplify a function $$y(x)$$.The function $$y(x)$$ is such that direct simplification is not possible. However, the derivative of it is better handled by MA. Denote $$a(x)=y'(x)\\ b=y(p).$$ Then the simplified function can be obtained by solving the ODE $$\frac{\mathrm{d}y(x)}{\mathrm{d}x}=a(x),\quad y(p)=b.$$

Organizing into a function

AltSimplify[f_, x_, p_, assump_] :=
Assuming[assump,
DSolveValue[{z'[x] == FullSimplify[D[f, x]], z[p] == Limit[f, x -> p]}, z, x][x]]

AltSimplify[y, x, 1, x > 0]
(* x *)

• I am sorry, I just fail to see how this is related to my question at all :O What differential equation are you solving, and why? Care to explain? Jan 3, 2021 at 11:15
• @user16320 See edits Jan 3, 2021 at 11:22

Change the complexity function so that it discourages log powers and log sums, which in turn converts $$\log(a)+\log(b)$$ into $$\log(a b)$$ and $$\log(a)^2-\log(b)^2$$ into $$(\log(a)+\log(b))(\log(a)-\log(b))$$:

FullSimplify[E^((-Log[x]^2 + Log[1 + x]^2)/Log[1 + 1/x]) - x^2,
Assumptions -> x > 0,
ComplexityFunction -> Function[LeafCount[#] +
100 Count[#, Log[_] + Log[_], {0, Infinity}] +
100 Count[#, Log[_]^_, {0, Infinity}]
]
]
(* x *)


Note that it works with the assumption $$x<-1$$ as well.

• I'm not familiar with LeafCount, but this sounds promising! Thanks! (also, I just realized that (* x *) looks like a cute face) Jan 5, 2021 at 1:50
• Very nice solution. By the way, you can post it as an answer to my old unanswered question mathematica.stackexchange.com/q/207506/9469. Jan 5, 2021 at 10:12

As stated in the documentation for Simplify:

Simplify tries expanding, factoring, and doing many other transformations on expressions, keeping track of the simplest form obtained.

For some simplifications, such as yours, the required transformations actually increase complexity (as measured by Mathematica) before the cancellations can occur. This can be seen by using the default complexity function SimplifySimplifyCount:

SimplifySimplifyCount[Log[1 + 1/x]]
(* 7 *)

SimplifySimplifyCount[Log[1 + x] - Log[x]]
(* 10 *)

SimplifySimplifyCount[Log[1 + x]^2 - Log[x]^2]
(* 14 *)

SimplifySimplifyCount[(Log[1 + x] - Log[x])(Log[1 + x] + Log[x])]
(* 18 *)


I don't believe the details of Simplify's algorithm are publically documented, but it appears likely that it abandons these transformations due to the increasing complexity, before getting to the point where the simplification can occur.

For more discussion on simplification look at some of the highly rated answers with the tag: https://mathematica.stackexchange.com/search?tab=votes&q=%5bsimplifying-expressions%5d%20is%3aa

expr = Exp[(Log[1 + x]^2 - Log[x]^2)/Log[1 + 1/x]] - x^2;

FullSimplify[expr, x > 0, ComplexityFunction -> StringLength@*ToString]

FullSimplify[expr,
TransformationFunctions -> {Automatic, PowerExpand[#, Assumptions -> x > 0] &}]
`

x
x

• Definitely a very neat solution! Am I understanding it right, ComplexityFunction sets the criterion for what the Simplify should aim as a "good simplified expression"? From Simon's answer it seems that MMA gives up too quickly before even having a chance to discover that "x" is the simplification, because it first has to expand the expression before it shrinks to x ("it has to get worse before it gets better"). However, with the ComplexityFunction you provided it simplified to x. By the way, what is the last command for? For me, Simplify with provided ComplexityFunction did the job already... Jan 6, 2021 at 2:32