# How can I change Simplify

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?

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 = SimplifySimplifyCount[#] + 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)

• 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). – Alx Aug 22 at 5:06
• 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. – Kevin O'Bryant Aug 23 at 20:14