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?
ExpandtoComplexExpandinpolarForm? – Bill Watts Jun 15 '21 at 23:57In[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 variablet" To some degree, you're right, but$Assumptionsdoesn't have any influence onNumericQbecauseNumericQdoesn't have the optionAssumptions. 4. A even simpler example showing the limitation ofpolarForm:Clear[a,b]; a + b I // polarForm.NumericQ. – emnha Jun 19 '21 at 12:17NumericQfrom thepolarFormfunction.polarForm = Expand[# /. z :> Abs[z] Exp[I Arg[z]]] &and then applied it to the expression above. – emnha Jun 19 '21 at 12:46polarForm1 = 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 // polarForm1and 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:25z :>should bez_ :>– xzczd Jun 19 '21 at 13:26E^(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:29Arg? Or something else? – xzczd Jun 19 '21 at 13:40Abs[z]andArg[z]evaluate to something doesn't explicitly involveI. ThenComplexExpandas 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:16a+b Iis 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 functionpolarFormfrom another post, you don't give the link, this leads to the impression the question isn't well prepared. 4. The code sampleIn[381]is wrong, this again leads to the impression the question isn't well prepared.