5
$\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$
0

1 Answer 1

8
$\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$
2
  • $\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
    Commented Aug 22, 2019 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$ Commented Aug 23, 2019 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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