Target Simplification in Mathematica

I generate very long and complex analytic expressions of a general form:

(...something not so complex...)(...ditto...)(...ditto...)...lots... 

When I try to use Simplify , Mathematica grinds to a halt, I assume that because it is trying to expand brackets and / or simplify in different brackets. Brackets containing long expressions are easily simplified by Mathematica on their own. Is there any way that I can limit the scope of Simplify to one bracket at a time?

Edit: Additional information and progress.

Therefore, using the advice from you guys, I started using something in the spirit

 In[1]:= trouble = Log[(x + I y) (x - I y) + Sqrt[(a + I b) (a - I b)]]; In[2]:= Replace[trouble, form_ /; (Head[form] == Times) :> Simplify[form],{3}] Out[2]= Log[Sqrt[a^2 + b^2] + (x - I y) (x + I y)] 

Changing Times to the appropriate head, such as Plus or Power , allows you to fine-tune the simplification. The problem / question that remains, however, is this: Simplify will still go deeper than the level set in Replace , for example

 In[3]:= Replace[trouble, form_ /; (Head[form] == Plus) :> Simplify[form], {1}] Out[3]= Log[Sqrt[a^2 + b^2] + x^2 + y^2] 

also simplifies the square root.

My plan was to iteratively use Replace from bottom to top one level at a time, but that would obviously lead to a huge amount of repeated work on Simplify and ultimately lead to exactly the same swamping down Mathematica I experienced right at the beginning. Is there a way to limit Simplify to a specific level (s)?

I understand that such a restriction may not give optimal results, but the idea here is to get something that is "good enough."

+6
wolfram-mathematica
source share
3 answers

There are several ways to do this, but it can be a little more complicated and depends on the structure of your actual expression. However, usually the product of several terms in brackets will have a Times head, and you can use FullForm to check this:

 In[1]:= FullForm[(a+b)(c+d)] Out[1]= Times[Plus[a, b], Plus[c, d]] 

You can use the higher-order Map function with expressions with head Times same way you use it with expressions with head List , and this can allow you to Simplify express one member at a time, for example:

 Map[Simplify, yourGinormousExpression] 

You can use the Expand result if you need to expand the brackets afterwards.

EDIT to add:. If you want to specify the forms you want to simplify, you can use Replace or ReplaceAll instead of one of the Map siblings. Replace especially useful because it requires level specification , allowing only factors to be influenced in the topmost product. As a simple example, consider the following:

 In[1]:= expr = Sqrt[(a + 1)/a] Sqrt[(b + 1)/b]; In[2]:= Simplify[expr] Out[2]= Sqrt[1 + 1/a] Sqrt[1 + 1/b] 

If you do not want to simplify factors that depend on a . you can do this instead:

 In[3]:= Replace[expr, form_ /; FreeQ[form, a] :> Simplify[form], {1}] Out[3]= Sqrt[(1 + a)/a] Sqrt[1 + 1/b] 

Only the second term is changed, which depends on b . It should be borne in mind, however, that some conversions are performed automatically using Times or Plus ; for example, a + a will be converted to 2 a even without using Simplify .

+4
source share

I ask you to differ from your colleagues, since the use of Map to apply Simplify to each subexpression may not be saved at any time, since it will still apply to each of them. Instead, try MapAt as follows:

 In[1]:= MapAt[f, SomeHead[a,b,c,d], {4}] Out[1]:= SomeHead[a, b, c, f[d]] 

The difficult part determines the specification of the position. Although, if the expression you want to simplify is on the first level, it should not be harder than what I wrote above.


Now, if you still want to simplify everything, but want to keep some structure, try using the ExcludedForms option. I used to prevent this simplification:

 In[2]:= Simplify[d Exp[I (a + b)] Cos[c/2]] Out[2]:= Exp[I(a + b + c)](d + d Exp[c]) 

which seems to be Mathematica, so I do

 In[3]:= Simplify[d Exp[I (a + b)] Cos[c/2], ExcludedForms -> {_Cos,_Sin}] Out[3]:= d Exp[I (a + b)] Cos[c/2] 

Also, do not forget that the second parameter for Simplify is for speculation and can greatly facilitate your struggle to get your expressions in a useful way.

+2
source share

You should try Map .
In general, Map[foo, G[a, b, c, ...]] gives G[foo[a], foo[b], foo[c], ...] for any head G and any expression foo therefore for

  Map[Simplify, abcde] 

He gives

  Simplify[a] Simplify[b] Simplify[c] Simplify[d] Simplify[e] 

Note that you can designate Map[foo, expr] als foo /@ expr if you find it more convenient.

+1
source share

All Articles