Grzegorz Rut
Grzegorz Rut

Reputation: 215

Simplification of complex expressions

The other day I wanted to simplify a following expression:

a Conjugate[a]//FullSimplify

Great! The answer we receive is Abs[a]^2. Now I tried something like this:

a b Conjugate[a]//FullSimplify (*a Conjugate[a] b will return the same output*)

Unfortunatelly it wasn't simplified. LeafCount prefers the unsimplified expression over Abs[a]^2 b. ComplexityFunction should be able to fix this: ComplexityFunction

I used the function presented in the link above:

f[e_] := StringLength[ToString[InputForm[e]]]
FullSimplify[a Conjugate[a] b, ComplexityFunction -> f]

And I still get the unwanted form.

Is there a workaround for this?

Upvotes: 0

Views: 358

Answers (1)

xslittlegrass
xslittlegrass

Reputation: 4966

I just learned this from my question here at StackExchange. You can do it like this:

f[expr_]:=expr/.x_*Conjugate[x_]:>Abs[x]^2
Simplify[a Conjugate[a] b,TransformationFunctions->{Automatic,f},ComplexityFunction->(StringLength[ToString[InputForm[#]]]&)]

it will give b Abs[a]^2.

Upvotes: 1

Related Questions