Mathematica programming

Tuesday, July 25, 2006

ExcudedForms for Simplify


We shall define
Simplify2[e_, opts__]
admitting an option
ExcludedForms
, just like
FullSimplify
does.
The idea behind, is to wrap parts matching excluded patterns around with
Hold
, do the simplification and restore held parts back to normal.

In[38]:=
Options[Simplify2] =
Union[Options[Simplify], {ExcludedForms :> {}}];

In[39]:=
Simplify2[e_, (opts___)?OptionQ] :=
Catch[Module[{popts, tag, ef, p},
popts = FilterOptions[Simplify, opts];
ef = ExcludedForms/.{opts}/.Options[Simplify2];
If[ef === {}, Throw[Simplify[e, popts]]];
ef = Alternatives @@ ef;
Simplify[
e /. RuleDelayed @@ {p:ef, Hold[tag[p]]}
, popts] /. {Hold[tag[p_]] :> p}
]]

In[40]:=
Simplify2[Sin[x]^4 + Cos[x]^4 + Sin[x]^2 + Cos[x]^2]

Out[40]=
(1/4)*(7 + Cos[4*x])

In[41]:=
Simplify2[Sin[x]^4 + Cos[x]^4 + Sin[x]^2 + Cos[x]^2,
ExcludedForms -> {(_Sin | _Cos)^4}]

Out[41]=
1 + Cos[x]^4 + Sin[x]^4
Enjoy!

0 Comments:

Post a Comment

<< Home