# ComplexExpand absolute squared

ComplexExpand[Abs[a + b I]]


Gives

$\sqrt{a^2 + b^2 }$

ComplexExpand[Abs[a + b I]^2]


On the other hand gives

Abs[a + I b]^2

How can I let it evaluate to $a^2 + b^2$ instead?

-
FullSimplify[Abs[a + b I]^2, (a | b) \[Element] Reals] works too. –  Szabolcs Apr 23 '13 at 15:54
I have found that the TargetFunctions approach usually works better for more complicated arguments. Sometimes, running things through FullSimplify is slow and doesn't get you much. –  Kevin Driscoll Apr 23 '13 at 18:39

One way is to use

ComplexExpand[Abs[a + b I]^2, TargetFunctions -> {Re, Im}]
(* a^2 + b^2 *)

-
I should have mentioned that last year. ComplexExpand with TargetFunctions -> {Re, Im} will make an effort to do things like what you show above. –  Daniel Lichtblau Apr 28 at 13:51

You can also get a slightly better result for Abs[a + I b]^2 by using an artificial limiting process:

ComplexExpand[Limit[Abs[t (a + I b)]^2, t -> 1]]

(* ==> Abs[a]^2 + Abs[b]^2 *)


If you don't like this, and also don't like having to specify the TargetFunctions option just to get Abs to simplify, maybe you'd be better off defining a custom absolute value function that acts like the built-in one but gets simplified more readily:

abs[x_] := Sqrt[x Conjugate[x]]

SetAttributes[abs, {NumericFunction, Listable}]

ComplexExpand[abs[a + b I]^2]

(* ==> a^2 + b^2 *)


The SetAttributes is added just to make the abs function act like Abs as much as possible, but you could also omit that line.

In general, I don't like using Abs because it doesn't always give useful results when you try to take derivatives of variables inside of an Abs. You end up with ugly-looking Abs' derivatives.

-