2
$\begingroup$

Why does this:

Simplify[Abs[x + I], Element[x, Reals]]

give me

Abs[x + i]

Is there a way to force Mathematica to give me the following answer?

$\sqrt{x^2+1}$

$\endgroup$
  • $\begingroup$ @Kuba Thanks for the tip. But I'll wait for someone to tell me why the above doesn't work $\endgroup$ – Priyatham Mar 29 '14 at 8:39
  • 1
    $\begingroup$ The fastes way is to use ComplexExpand which assumes that all constants are real: ComplexExpand@Abs[I + x]. Default ComplexityFunction is LeafCount which gives 6 for Abs form and 9 for Sqrt, that's why it is left. $\endgroup$ – Kuba Mar 29 '14 at 8:55
  • 1
    $\begingroup$ closely related $\endgroup$ – Kuba Mar 29 '14 at 9:00
  • $\begingroup$ Also related: 23867 $\endgroup$ – Michael E2 Apr 8 '15 at 11:50
5
$\begingroup$

If you steal the wizard's explanation an apply it to your case

cf[e_] := 100 Count[e, _Abs, {0, Infinity}] + LeafCount[e]

Then

FullSimplify[Abs[x + I], x \[Element] Reals, ComplexityFunction -> cf]

(* Sqrt[x^2+1] *)

but once again I just copied and vaguely adapted the link provided by kuba

$\endgroup$
  • $\begingroup$ I understand that LeafCount gives the number of trees in the expression tree. Is there a place where I can read more about it and the ComplexityFunction? $\endgroup$ – Priyatham Mar 29 '14 at 13:58
  • 1
    $\begingroup$ How about reference.wolfram.com/mathematica/ref/ComplexityFunction.html $\endgroup$ – chris Mar 29 '14 at 14:27
  • $\begingroup$ how about if you Count[..,I,..]? may be more general if it works (sorry cant try frm here) $\endgroup$ – george2079 Mar 29 '14 at 15:17
  • $\begingroup$ @george2079 cf[e_] := 100 Count[e, _Complex, {0, Infinity}] + LeafCount[e] works too $\endgroup$ – chris Mar 29 '14 at 15:33
  • $\begingroup$ You can also type ComplexityFunction in the find button on the left above in the menu bar... $\endgroup$ – chris Mar 29 '14 at 20:03
3
$\begingroup$

How about using ComplexExpand

ComplexExpand[Abs[x + I]]

Gives:

Sqrt[1 + x^2]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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