Finding sub-expressions that simplify larger formula
Note, that if you just propose Mathematica additional equalities, it will give them try:
f = (1/(a^2 (b + m))) (-(-1 + a + 2 b) (-1 + 2 b + 2 m) -
4 b ArcTanh[1 - 2 b] + Log[4] +
2 ((2 + a) b + m + a m) Log[1 + a] - a (b + m) Log[4 - 4 b] +
a Log[(2 - 2 b)/(1 + a)] - m Log[1 - b] +
2 Log[(1 - b)/(1 + a)] + (m - a (b + m)) Log[b]);
In[6]:=
LeafCount[f]
fs = FullSimplify[f, {c == 1 + a}];
LeafCount[fs]
Out[6]= 114
Out[8]= 90
Sometimes you can make a good guess from the additional information you have about you problem (in physics you often do).
One way to find repeating sub expressions:
SortBy[Select[
Tally[Level[f, -1]],
LeafCount[First[#]] > 3 &
], Last][[-3 ;;]] // Column
Compile[]
is also able to find common subexpressions if the whole expression is compilable in general. The resulting CompiledFunction
contains a list of imperative program steps that is equal to the original expression.
expr = (1/(a^2 (b+m))) (-(-1+a+2 b) (-1+2 b+2 m)-
4 b ArcTanh[1-2 b]+Log[4]+2 ((2+a) b+m+a m) Log[1+a]-
a (b+m) Log[4-4 b]+a Log[(2-2 b)/(1+a)]-m Log[1-b]+
2 Log[(1-b)/(1+a)]+(m-a (b+m)) Log[b]);
cf = Compile[{{a, _Real}, {b, _Real}, {m, _Real}}, Evaluate@expr]
Clicking on the +
symbol reveals the generated code:
This expression can also be found as second to last part of the CompiledFunction
expression:
cf[[-2]]
Function[{a, b, m},
Block[{Compile`$5, Compile`$2, Compile`$21, Compile`$28, Compile`$33,
Compile`$34}, Compile`$5 = -2 b; Compile`$2 = b + m;
Compile`$21 = 1 + a; Compile`$28 = 1/Compile`$21; Compile`$33 = -b;
Compile`$34 = 1 + Compile`$33; (1/(
a^2 Compile`$2))((1 - a + Compile`$5) (-1 + 2 b + 2 m) -
4 b ArcTanh[1 + Compile`$5] + Log[4] +
2 ((2 + a) b + m + a m) Log[Compile`$21] -
a Compile`$2 Log[4 - 4 b] + a Log[Compile`$28 (2 + Compile`$5)] -
m Log[Compile`$34] +
2 Log[Compile`$28 Compile`$34] + (m - a Compile`$2) Log[b])]]