# Simplify double minus signs

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)?

• That's because Simplify (mostly) minimizes the LeafCount of expressions. The LeafCount of those two is the same. See their FullForm to understand why. – Szabolcs Apr 11 '17 at 21:42
• Why not just enter -(-1 + c) a b without Simplify as the output is a b (1 - c)? – Edmund Apr 11 '17 at 21:43
• 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). – Muk Apr 11 '17 at 21:50
• @Szabolcs Any suggestions, then, how to fix this? – Muk Apr 11 '17 at 22:14

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.