Mathematica : Log Handling/Simplification

In summary, the ComplexityFunction controls how Simplify behaves. It is driven by a measure of complexity, usually LeafCount, and can be tuned to prefer smaller numbers.
  • #1
Hepth
Gold Member
464
40
I'm having a simple problem with simplifying of logs. Given the input :

FullSimplify[8 Log[m1] - 8 Log[m2] + 4, m1 > 0 && m2 > 0]
FullSimplify[8 Log[m1] - 8 Log[m2], m1 > 0 && m2 > 0]

I get out:

8 log(m1)-8 log(m2)+4

8 log(m1/m2)


So for the second command it combines the logs using the division rule, but in the first, where there is an extra addition, it does not. Is there an option that I can turn on to always force MM to simplify that log?
 
Physics news on Phys.org
  • #2
Ah, finally, a somewhat interesting problem.

Simplify is driven by a measure of the complexity of the function, usually LeafCount. Google it.

In[3]:= LeafCount[4+8 Log[m1]-8 Log[m2]]
Out[3]= 10
In[4]:= LeafCount[4+8*Log[m1/m2]]
Out[4]= 10

So the "complexity" of those two are equal and Simplify isn't driven one direction or the other.

The mechanism for you to control this is the ComplexityFunction option. Google it.

In[5]:= logExpensive[e_]:=10*Count[e,_Log,{0,Infinity}]+LeafCount[e];
FullSimplify[8 Log[m1]-8 Log[m2]+4,m1>0&&m2>0,ComplexityFunction->logExpensive]
Out[6]= (4 + Log[m1^8/m2^8]

which is odd.

In[7]:= LeafCount[4 + Log[m1^8/m2^8]]
Out[7]= 10
In[8]:= LeafCount[4 + 8*Log[m1/m2]]
Out[17]= 10

So making Log really expensive drove those togther. But for some reason it drove the 8 inside too.

Getting Mathematica to do things the way you want and it doesn't can be very challenging. Some days it just isn't worth the fight.
 
  • #3
Either write some manual replacement rules, e.g.
Code:
a_ Log[x_] + b_ Log[y_] :> a Log[x/y] /; a == -b
which can include checks on positivity of x and y if need be.

Or use a custom ComplexityFunction to tune FullSimplify.
The default ComplexityFunction is given in the documentation as
Code:
SimplifyCount[p_] :=
 Which[Head[p] === Symbol, 1,
  IntegerQ[p], 
  If[p == 0, 1, Floor[N[Log[2, Abs[p]]/Log[2, 10]]] + If[p > 0, 1, 2]],
  Head[p] === Rational, 
  SimplifyCount[Numerator[p]] + SimplifyCount[Denominator[p]] + 1,
  Head[p] === Complex, 
  SimplifyCount[Re[p]] + SimplifyCount[Im[p]] + 1, NumberQ[p], 2,
  True, SimplifyCount[Head[p]] + 
   If[Length[p] == 0, 0, Plus @@ (SimplifyCount /@ (List @@ p))]]
which is just a LeafCount modified to prefer small numbers.

If you define something like (which says it doesn't like logs)
Code:
LogSimplifyCount =  SimplifyCount[#] 
    + Count[#, Log, Infinity, Heads -> True] &;
then things simplify like you want:

Code:
In[88]:= FullSimplify[8 Log[m1]-8 Log[m2]+8,m1>0&&m2>0,ComplexityFunction->LogSimplifyCount]
         FullSimplify[8 Log[m1]-8 Log[m2],m1>0&&m2>0,ComplexityFunction->LogSimplifyCount]
Out[88]= 8 (1+Log[m1/m2])
Out[89]= 8 Log[m1/m2]
 
  • #4
Hi Bill, you beat me too it!

I also forgot that I had changed
8 Log[m1]-8 Log[m2]+4
into
8 Log[m1]-8 Log[m2]+8
since I was having the same problems as you.

A simple fix would be to use something like
Code:
LogSimplifyCount=SimplifyCount[#]
    +Count[#,Log,Infinity,Heads->True]
    -.5Count[#,Log[_?(!FreeQ[#,Power]&)],Infinity]&

Then everything works properly. Although, it would probably be more efficient to modify SimplifyCount[] directly...
 
  • #5
Thanks guys! I love learning new things about Mathematica. I'll play around with it!
 
  • #6
I said long ago that Mathematica and computer algebra software in general needs to implement a "mash that result into a form that looks kind of like this" command which behaves in a fairly predictable fashion. So for this problem it might be as simple as

Mash[%, _ Log[ _ ]+_ ]

to get the result he wanted.

That might be far easier for a user. If it didn't work then the user could make the pattern a little more specific, instead of trying to craft some incomprehensible function on LeafCount.

I believe there was a paper published in the Mathematica Journal once that could be interpreted to be related to this idea, but I can't locate that now.
 

1. What is the purpose of log handling and simplification in Mathematica?

Log handling and simplification in Mathematica is used to manipulate and simplify mathematical expressions involving logarithmic functions by applying rules and identities to reduce the expression to a more compact and easily understandable form.

2. How does Mathematica handle logarithms in calculations?

Mathematica uses a built-in algorithm to automatically simplify and evaluate logarithmic expressions, taking into account the properties of logarithms such as log rules, logarithmic identities, and logarithmic differentiation.

3. Can I customize the log handling and simplification in Mathematica?

Yes, Mathematica allows users to define their own rules and properties for log handling and simplification using the LogExpand and LogSimplify functions. This gives users more control over how logarithmic expressions are handled and simplified in their calculations.

4. Are there any limitations to log handling and simplification in Mathematica?

While Mathematica's built-in algorithms are powerful and can handle most logarithmic expressions, there may be some cases where manual intervention is needed for more complex expressions. In these cases, users can use the FullSimplify function to apply more extensive simplification techniques.

5. Can I visualize logarithmic functions and their simplification in Mathematica?

Yes, Mathematica has a variety of built-in functions and tools for visualizing logarithmic functions and their simplification. The LogPlot and LogLogPlot functions can be used to plot logarithmic functions on a logarithmic scale, and the Simplify function can be used to visualize the simplification of a logarithmic expression step by step.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • Introductory Physics Homework Help
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Introductory Physics Homework Help
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Math Guides, Tutorials and Articles
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
Back
Top