6
$\begingroup$

For a general (complex) number $z$ I sometimes get terms that explicitly writes (for instance)

$$2zz^*+|z|^2. $$ I should add that these expressions come from long calculations involving packages (FeynCalc). How come FullSimplify[] does not simply put this expression to $3|z|^2$ instead?

$\endgroup$
2
  • 2
    $\begingroup$ ComplexExpand is intended for this sort of transformation. But it is a bit dull in this case and benefits from postprocessing by Simplify. Simplify[ComplexExpand[z*Conjugate[z], z, TargetFunctions->{Abs,Arg}]] gives the desired result. $\endgroup$ Nov 25, 2015 at 18:59
  • $\begingroup$ @DanielLichtblau that was cool, thanks. $\endgroup$ Nov 27, 2015 at 10:30

1 Answer 1

14
$\begingroup$

Running FullSimplify I get

FullSimplify[z Conjugate[z] + 2 Abs[z]^2]
3 z Conjugate[z]

This is actually more simple than 3 Abs[z]^2 in the eyes of FullSimplify. See here for more info.

Simplify`SimplifyCount[3 z Conjugate[z]]
5
Simplify`SimplifyCount[3 Abs[z]^2]
6

Luckily there are many ways to guide FullSimplify. Here is one such way.

FullSimplify[z Conjugate[z] + 2 Abs[z]^2, ExcludedForms -> {_Abs}]
3 Abs[z]^2

Edit

A more general way is to use a combination of ComplexityFunction and TransformationFunctions. Basically we'll assign a penalty whenever the unwanted pattern is present and teach FullSimplify the rule we want to use.

ToAbs[a_. z_ Conjugate[z_]] := a Abs[z]^2
ToAbs[e_] := e

FullSimplify[a1 Conjugate[a1] + a1 Conjugate[a1] + b1 Conjugate[b1], 
  ComplexityFunction -> (LeafCount[#] + 100 Count[#, _Conjugate, ∞] &), 
  TransformationFunctions -> {ToAbs, Automatic}
]
2 Abs[a1]^2 + Abs[b1]^2
$\endgroup$
7
  • $\begingroup$ OK thanks. let me try that. $\endgroup$ Nov 25, 2015 at 16:48
  • $\begingroup$ When I write FullSimplify[a1 Conjugate[a1] + a1 Conjugate[a1] + b1 Conjugate[b1], ExcludedForms -> {_Abs}] I get Abs[b1]^2 + 2 a1 Conjugate[a1] $\endgroup$ Nov 25, 2015 at 16:51
  • $\begingroup$ Is there a simply way of telling Mathe to write everything in absolute bars? It just looks nicer that's all... $\endgroup$ Nov 25, 2015 at 16:52
  • $\begingroup$ @Faq see my edit. Also you could try TraditionalForm to get the vertical bars. $\endgroup$
    – Greg Hurst
    Nov 25, 2015 at 17:00
  • 2
    $\begingroup$ It will only work when you explicitly pass these options and I don't think there's much of a slowdown. $\endgroup$
    – Greg Hurst
    Nov 25, 2015 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.