Mathematica : Log Handling/Simplification

  • Context: Mathematica 
  • Thread starter Thread starter Hepth
  • Start date Start date
  • Tags Tags
    Log Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the simplification of logarithmic expressions in Mathematica, specifically addressing the behavior of the FullSimplify function when handling logs with additional constants. Participants explore various methods to achieve desired simplifications and the underlying mechanisms that influence these outcomes.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant notes that FullSimplify does not combine logs with an additional constant, while it does so when only logs are present.
  • Another participant explains that the complexity of expressions, measured by LeafCount, influences the behavior of Simplify, suggesting that the ComplexityFunction option can be adjusted.
  • A proposed custom ComplexityFunction is shared, which aims to prioritize the simplification of logarithmic terms.
  • Participants discuss the effectiveness of manual replacement rules to enforce specific simplifications in logarithmic expressions.
  • One participant suggests a potential command, Mash, to achieve a more predictable simplification outcome, indicating a desire for more user-friendly features in Mathematica.

Areas of Agreement / Disagreement

Participants express varying opinions on the best approach to achieve the desired simplification of logarithmic expressions. There is no consensus on a single method, as multiple strategies are proposed and explored.

Contextual Notes

Some participants mention the need for additional checks on the positivity of variables involved in logarithmic expressions, indicating that assumptions may affect the simplification process. The discussion also highlights the limitations of current Mathematica functions in handling specific user needs.

Who May Find This Useful

This discussion may be useful for Mathematica users interested in advanced simplification techniques for logarithmic expressions, particularly those facing challenges with the software's default behavior.

Hepth
Science Advisor
Gold Member
Messages
458
Reaction score
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
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.
 
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]
 
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...
 
Thanks guys! I love learning new things about Mathematica. I'll play around with it!
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 6 ·
Replies
6
Views
5K