To simplify based on the visual complexity of an expression (i.e. the complexity of the function as displayed in the notebook instead of the internal form, which in some cases may be substantially different), I've defined the following complexity function:
VisualComplexity:=(Count[ToBoxes[#], Except[" "|"("|")", _String], Infinity]&)
This works well on some simple examples, however not on the following:
FullSimplify[(-1+a)(-1+b) + Abs[c]^2 - Abs[d]^2,
ComplexityFunction->VisualComplexity]
(*
==> 1 - a - b + a*b + Abs[c]^2 - Abs[d]^2
*)
What I had expected (and desired) would have been (1-a)(1-b) + Abs[c]^2 - Abs[d]^2
. Note that with the built-in complexity function, it converts (1-a)(1-b)
to (-1+a)(-1+b)
in that context, so transforming between those two forms is definitely in the capabilities of FullSimplify
.
Now my first thought was that maybe that's really less complex according to my complexity function (which would have meant tweaking that function). However, it turned out that the desired form indeed is computed to be less complex:
VisualComplexity /@ {1 - a - b + a*b + Abs[c]^2 - Abs[d]^2,
(1-a)(1-b) + Abs[c]^2 - Abs[d]^2}
(*
==> {20, 18}
*)
Therefore my question is: How do I get FullSimplify to generate the simpler form?
TransformationFunctions->{Automatic, #/.(-1+x_)(-1+y_):>(1-x)(1-y)&}
), I get the desired result. $\endgroup$