Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
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?

share|improve this question
2  
FullSimplify[Abs[a + b I]^2, (a | b) \[Element] Reals] works too. –  Szabolcs Apr 23 '13 at 15:54
2  
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

2 Answers 2

One way is to use

ComplexExpand[Abs[a + b I]^2, TargetFunctions -> {Re, Im}]
(* a^2 + b^2 *)
share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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