       "Best" ComplexityFunction Setting ?

• To: mathgroup at smc.vnet.net
• Subject: [mg24306] "Best" ComplexityFunction Setting ?
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Fri, 7 Jul 2000 00:11:50 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Below I give three examples where the default setting of ComplexityFunction
doesn't return the expression in the "simplest" form.

In:=
FullSimplify[1-Cosh[x]+Sinh[x]]

Out=
1 - Cosh[x] + Sinh[x]

-------

In:=
FullSimplify[8(-1+x)(1+x)]

Out=
8*(-1 + x)*(1 + x)

-------

In[:=
n1=59^5*1579^7;
n2=3^5*19^7*113^5;
FullSimplify[Log[n1*n2]]

Out=
7*Log + 5*Log + 5*Log + Log +
7*Log

--------

Next I define functions used for a new ComplexityFunction setting. This new
setting gives results that are "simpler" or "just as simple" as those we get
using the default setting. Well at least it does in all cases I looked at so
far.

In:=
ClearAll[DigitsLength,Digits1,Digits2,VariableCount];
DigitsLength[0|1]=1/10;
DigitsLength[-1]=1/8;
DigitsLength[n_]:=Length[IntegerDigits[n]];
Digits1[expr_]:=2*Plus@@(DigitsLength[#]&/@Cases[expr,_Integer,-1]);
Digits2[expr_]:=2*Plus@@Flatten[
Map[DigitsLength[#]&,
({Numerator[#],Denominator[#]}& /@Cases[expr,_Rational,{-1}]),
{-1}
]
];
VariableCount[expr_]:=Count[expr,_Symbol?(Not[NumericQ[#]]&),{-1}];

SetOptions[
FullSimplify,ComplexityFunction->
(LeafCount[#]+Digits1[#]+Digits2[#]+3*VariableCount[#]&)
];

Below we see that using the new setting for ComplexityFunction,
FullSimplify gives a "simpler" result in each case considered above.

---------

In:=
FullSimplify[1-Cosh[x]+Sinh[x]]

Out=
1 - E^(-x)

---------

In:=
FullSimplify[8(-1+x)(1+x)]

Out=
8*(-1 + x^2)

---------

In:=
n1=59^5*1579^7;
n2=3^5*19^7*113^5;
FullSimplify[Log[n1*n2]]

Out=
5*Log + 7*Log

---------
Notice the last example takes a surprising amount of time using my
ComplexityFunction. The time needed to simplify a similar but large
expression would be very long. It seems this is a case where we have to
trade off the extent of the search with the time needed to complete the
search.

My ComplexityFunction above doesn't take into account the number of digits
in Re[z] or Im[z] when  Re[z] or Im[z] are Integer or Rational. I could find
a way to do that, but it's a bit messy. Without exploiting that short coming
can anyone find an example where
ComplexityFunction->Automatic can find a form "more simple" than using my
ComplexityFunction. For me the search for the "best" ComplexityFunction is
interesting.

--------------------
Regards,
Ted Ersek