0
$\begingroup$

The function from another post is to convert a complex number to polar form.

polarForm = Expand[# /. z_?NumericQ :> Abs[z] Exp[I Arg[z]]] &

Now if I apply that function to this expression it works.

In[381]:= (320/12641-(316 I)/12641) E^(1000 I t)  //ComplexExpand
Out[381]= (320 Cos[1000 t])/12641+I (-((316 Cos[1000 t])/12641)+(320 Sin[1000 t])/12641)+(316 Sin[1000 t])/12641

However if I apply it the the complex number below (the same one but just expand it in real and imaginary parts) it doesn't work.

In[382]:= (320 Cos[1000 t])/12641+I (-((316 Cos[1000 t])/12641)+(320 Sin[1000 t])/12641)+(316 Sin[1000 t])/12641//polarForm
Out[382]= (320/12641-(316 I)/12641) Cos[1000 t]+(316/12641+(320 I)/12641) Sin[1000 t]

I thought the problem is due to the variable t so I added an assumption that t is real but it doesn't work either.

In[383]:= $Assumptions=t\[Element]Reals
(320 Cos[1000 t])/12641+I (-((316 Cos[1000 t])/12641)+(320 Sin[1000 t])/12641)+(316 Sin[1000 t])/12641//polarForm
Out[383]= t\[Element]\[DoubleStruckCapitalR]
Out[384]= (320/12641-(316 I)/12641) Cos[1000 t]+(316/12641+(320 I)/12641) Sin[1000 t]

Question: how can I modify the polarForm function to assume all variables in the expression are real so the function works for any expression?

$\endgroup$
15
  • $\begingroup$ Change Expand to ComplexExpand in polarForm? $\endgroup$
    – Bill Watts
    Jun 15, 2021 at 23:57
  • $\begingroup$ @BillWatts I tried but it doesn't work. $\endgroup$
    – emnha
    Jun 16, 2021 at 8:04
  • $\begingroup$ 1. What's the "another post"? 2. The sample In[381] doesn't match the description "I apply that function to this expression it works", please double check it. 3. "I thought the problem is due to the variable t" To some degree, you're right, but $Assumptions doesn't have any influence on NumericQ because NumericQ doesn't have the option Assumptions. 4. A even simpler example showing the limitation of polarForm: Clear[a,b]; a + b I // polarForm. $\endgroup$
    – xzczd
    Jun 19, 2021 at 8:05
  • $\begingroup$ @xzczd It's from this. mathematica.stackexchange.com/questions/16414/… It doesn't work even if I remove NumericQ. $\endgroup$
    – emnha
    Jun 19, 2021 at 12:17
  • 1
    $\begingroup$ 1. The most important part: the code sample isn't simplified enough. (As you can see, a+b I is capable of reproducing the issue. ) Lengthier sample is less attractive. 2. The description "doesn't work" is vague and is more likely to push people away. 3. Though you quote function polarForm from another post, you don't give the link, this leads to the impression the question isn't well prepared. 4. The code sample In[381] is wrong, this again leads to the impression the question isn't well prepared. $\endgroup$
    – xzczd
    Jun 20, 2021 at 11:56

1 Answer 1

2
+100
$\begingroup$

Why not AbsArg?:

(320 Cos[1000 t])/12641 + 
    I (-((316 Cos[1000 t])/12641) + (320 Sin[1000 t])/12641) + (316 Sin[1000 t])/12641//
    AbsArg // ComplexExpand[#, TargetFunctions -> {Re, Im}] & // #[[1]] Exp[I #[[2]]] &

(*
E^(I ArcTan[(320 Cos[1000 t])/12641 + (316 Sin[1000 t])/
    12641, -((316 Cos[1000 t])/12641) + (320 Sin[1000 t])/12641]) Sqrt[((
    320 Cos[1000 t])/12641 + (316 Sin[1000 t])/12641)^2 + (-((316 Cos[1000 t])/12641) +(
    320 Sin[1000 t])/12641)^2]
 *)
$\endgroup$

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.