4
$\begingroup$

I have an application where I strongly prefer $1+\frac{3}{2+3x}$ over $\frac{5+3x}{2+3x}$, and $\sin(2x)$ over $2\sin(x)\cos(x)$. In general, I prefer to minimize the number of occurrences of the variables.

Is there a way to set the "cost" function for Simplify to achieve this?

$\endgroup$
7
$\begingroup$

For the example in OP FullSimplify gives the desired result:

FullSimplify [(5 + 3 x)/(2 + 3 x)]

1 + 3/(2 + 3 x)

To take a different example for which neither Simplify nor FullSimplify makes the desired simplification without additional work:

expr = (5 + 2 x)/(2 + 3 x);

Simplify  @ expr

(5 + 2 x)/(2 + 3 x)

FullSimplify @ expr

(5 + 2 x)/(2 + 3 x)

"to minimize the number of occurrences of the variables" you can use a custom ComplexityFunction that penalizes multiple occurences of symbols. For example,

cF = Simplify`SimplifyCount[#] + 100 Count[#, _Symbol, All] &;

Using it with FullSimplify

FullSimplify[expr, ComplexityFunction -> cF]

2/3 + 11/(6 + 9 x)

With Simplify we need to do more:

Simplify[expr, ComplexityFunction -> cF]

(5 + 2 x)/(2 + 3 x)

Adding Apart to the Automatic TransformationFunctions used by Simplify gives the desired result:

Simplify[expr, ComplexityFunction -> cF, TransformationFunctions -> {Automatic, Apart}]

2/3 + 11/(6 + 9 x)

$\endgroup$
  • $\begingroup$ Isn't simple Apart enough? It gives 2/3 + 11/(3 (2+3 x)). One can then also expand denominator with ExpandAll to get 2/3 + 11/(6+9 x). $\endgroup$ – Alx Aug 22 at 5:06
  • $\begingroup$ My actual target functions may have 100s of terms involving some trigonometry, some logarithms, and many rational functions. I wasn't aware of ComplexityFunction, and that's what was missing. $\endgroup$ – Kevin O'Bryant Aug 23 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.