0

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?

emnha
  • 2,101
  • 13
  • 26
  • Change Expand to ComplexExpand in polarForm? – Bill Watts Jun 15 '21 at 23:57
  • @BillWatts I tried but it doesn't work. – emnha Jun 16 '21 at 08:04
  • 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.
  • – xzczd Jun 19 '21 at 08:05
  • @xzczd It's from this. https://mathematica.stackexchange.com/questions/16414/how-can-i-convert-a-complex-number-into-an-exponent-form?rq=1 It doesn't work even if I remove NumericQ. – emnha Jun 19 '21 at 12:17
  • @anhnha What do you mean by "doesn't work"? Please show us your modified code and obtained output. – xzczd Jun 19 '21 at 12:34
  • @xzczd I just deleted NumericQ from the polarForm function. polarForm = Expand[# /. z :> Abs[z] Exp[I Arg[z]]] & and then applied it to the expression above. – emnha Jun 19 '21 at 12:46
  • Once again, what do you mean by "doesn't work"? – xzczd Jun 19 '21 at 13:21
  • @xzczd polarForm1 = Expand[# /. z :> Abs[z] Exp[I Arg[z]]] &; (320 Cos[1000 t])/12641 + I (-((316 Cos[1000 t])/12641) + (320 Sin[1000 t])/12641) + (316 Sin[ 1000 t])/12641 // polarForm1 and this is the output (320/12641 - (316 I)/12641) Cos[ 1000 t] + (316/12641 + (320 I)/12641) Sin[1000 t] – emnha Jun 19 '21 at 13:25
  • z :> should be z_ :> – xzczd Jun 19 '21 at 13:26
  • @xzczd ok but still not works. this is the output E^(I Arg[(320 Cos[1000 t])/12641 + I (-((316 Cos[1000 t])/12641) + (320 Sin[1000 t])/12641) + ( 316 Sin[1000 t])/12641]) Abs[(320 Cos[1000 t])/12641 + I (-((316 Cos[1000 t])/12641) + (320 Sin[1000 t])/12641) + ( 316 Sin[1000 t])/12641] – emnha Jun 19 '21 at 13:29
  • Once again, what do you mean by "doesn't work"? Because it involves Arg? Or something else? – xzczd Jun 19 '21 at 13:40
  • well I sent the result above. If it works then it should be in polar form but if you look at the result I sent above, it still has imaginary numbers in the result. – emnha Jun 19 '21 at 14:04
  • OK, so by "doesn't work" you mean you want the Abs[z] and Arg[z] evaluate to something doesn't explicitly involve I. Then ComplexExpand as shown in my answer is necessary. BTW, now I guess you probably see why your question doesn't attract much attention. – xzczd Jun 20 '21 at 03:16
  • @xzczd why does it not? – emnha Jun 20 '21 at 07:55
  • 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.
  • – xzczd Jun 20 '21 at 11:56