Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica does not calculate absolute values of a complex number with real coefficients

Using code FullSimplify[Abs[q + I*w], Element[{q, w}, Reals]] results in

Abs[q + I w]

and not

Sqrt[q^2 + w^2]

What am I missing?

P.S. Assuming[{q \[Element] Reals, w \[Element] Reals}, Abs[q + I*w]] does not work either. Note: Simplify[Abs[w]^2, Element[{q, w}, Reals]] and Simplify[Abs[I*q]^2, Element[{q, w}, Reals]] work.

like image 692
shadesofdarkred Avatar asked Mar 07 '12 16:03

shadesofdarkred


2 Answers

The problem is that what you assume to be "Simple" and what MMA assumes to be simple are two different things. Taking a look at ComplexityFunction indicates that MMA primarily looks at "LeafCount". Applying LeafCount gives:

In[3]:= Abs[q + I w] // LeafCount
Out[3]= 8

In[4]:= Sqrt[q^2 + w^2] // LeafCount    
Out[4]= 11

So, MMA considers the Abs form to be better. (One can visually explore the simplicity using either TreeForm or FullForm). What we need to do is tell MMA to treat MMA as more expensive. To do this, we take the example from ComplexityFunction and write:

In[7]:= f[e_] := 100 Count[e, _Abs, {0, Infinity}] + LeafCount[e]
FullSimplify[Abs[q + I w], Element[{q, w}, Reals], 
 ComplexityFunction -> f]

Out[8]= Sqrt[q^2 + w^2]

As requested. Basically, we are telling MMA through f[e] that the count of all parts of the form Abs should count as 100 leaves.

EDIT: As mentioned by Brett, you can also make it more general, and use _Complex as the rule to look for:

In[20]:= f[e_] := 100 Count[e, _Complex, {0, Infinity}] + LeafCount[e]
FullSimplify[Abs[q + I w], Element[{q, w}, Reals], 
 ComplexityFunction -> f]

Out[21]= Sqrt[q^2 + w^2]
like image 178
tkott Avatar answered Nov 15 '22 12:11

tkott


I suggest using ComplexExpand, which tells the system that all variables are real.

In[28]:= Abs[q + I*w] // ComplexExpand

Out[28]= Sqrt[q^2 + w^2]
like image 29
Szabolcs Avatar answered Nov 15 '22 13:11

Szabolcs