8
$\begingroup$

I am puzzled as to why Simplify won't remove "double minus signs" in certain expressions.

Here it does:

Simplify[-(-1 - c) a b]

a b (1 + c)

Here it doesn't:

Simplify[-(-1 + c) a b]

-a b (-1 + c)

How, in the second example, can I get Simplify to output a b (1 - c)?

$\endgroup$
  • 1
    $\begingroup$ That's because Simplify (mostly) minimizes the LeafCount of expressions. The LeafCount of those two is the same. See their FullForm to understand why. $\endgroup$ – Szabolcs Apr 11 '17 at 21:42
  • $\begingroup$ Why not just enter -(-1 + c) a b without Simplify as the output is a b (1 - c)? $\endgroup$ – Edmund Apr 11 '17 at 21:43
  • 1
    $\begingroup$ This was just a minimalistic example. I have longer expressions I need to simplify, but Simplify often outputs expressions of the form -a b (-1 + c) instead of a b (1 - c). $\endgroup$ – Muk Apr 11 '17 at 21:50
  • 1
    $\begingroup$ @Szabolcs Any suggestions, then, how to fix this? $\endgroup$ – Muk Apr 11 '17 at 22:14
16
$\begingroup$

Simplify tries different transformations and calculates ComplexityFunction of transformed expression after each try. Expression with minimal ComplexityFunction is returned.

By default, Simplify uses ComplexityFunction based on LeafCount, but you can override this.

For example, you can add some penalty points for negative integers in the resulting expression. In this case simplify will try to avoid them.

f[e_] := 100 Count[e, _Integer?Negative, {0, Infinity}] + LeafCount[e];
Simplify[-(-1 + c) a b, ComplexityFunction -> f]

a b (1 - c)

You can tune ComplexityFunction to your needs by adding or subtracting points for different subexpressions.

Alternatively, you can define a ComplexityFunction based on a string length of resulting expression.

f[e_] := StringLength[ToString[InputForm[e]]];
Simplify[-(-1 + c) a b, ComplexityFunction -> f]

a b (1 - c)

This will also make double minus signs less attractive for Simplify.

Default complexity function and more examples can be found in the documentation center for ComplexityFunction.

$\endgroup$

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.