# Converting polar coordinate expression to cartesian coordinates

An intermediate step in my analysis requires me to work in polar coordinates, but I would like to convert the results back into cartesian coordinates. The conversion is very simple but tedious for complex expressions. I expected to use substitutions, but it isn't panning out for me

polarf[{r_, phi_}] := r^6 Sin[phi]^2 Cos[phi]^2
cartesianf[{k1_, k2_}] :=
polarf[{r, phi}] /. r^2 Sin[phi]^2 -> k2^2 /.
r^2 Cos[phi]^2 -> k1^2 /. r^2 -> k1^2 + k2^2
cartesianf[{k1, k2}]


gives me back my original expression:

r^6 Cos[phi]^2 Sin[phi]^2


I'm pretty sure there is a way to get Mathematica to spit out

(k1^2 + k2^2) k1^2 k2^2


or something similar. But how? My searches of Google, Mathematica documentation, and this site have not turned up an answer.

• I don't know if it solves your problem, but are you aware of CoordinatesToCartesian and CoordinatesFromCartesian? – VLC Oct 2 '12 at 16:05
• It came up in my searches, but appears to transform point coordinates rather than functions of coordinates. – James Oct 2 '12 at 16:14
• Is there a reason why you use replacement rules instead of simply inserting the formulas directly? That is, cartesianf[{k1_,k2_}] := polarf[{Sqrt[k1^2+k2^2],ArcTan[k1,k2]}] (however you then need a FullSimplify to arrive at the form you want: FullSimplify[cartesianf[{k1,k2}]]) – celtschk Oct 2 '12 at 16:14
• If you want to use replacement rules, you probably should first get rid of phi, then of r: polarf[{r, phi}] /. { Cos[phi] -> k1/r, Sin[phi] -> k2/r } /. { r -> Sqrt[k1^2+k2^2] }. Then you also don't need FullSimplify. – celtschk Oct 2 '12 at 16:18
• That works quite well in my sample code, but threw up warnings when used for my actual result. After a few minutes I aborted the evaluation. – James Oct 2 '12 at 16:25

You obviously misunderstand replacement rules. Replacement rules work on the structure level, and don't care about the mathematical meaning (they do take into accounts attributes like Orderless, however). In your case, you are saying with your first replacement rule that whereever your result contains the structure (more exactly, the pattern) r^2 Sin[phi]^2 (that is, Times[Power[r, 2], Power[Sin[phi], 2]]), you want that replaced with the expression k2^2. However the result of your function does not contain this structure. It contains r^6 Sin[phi]^2 which doesn't match your pattern because 6 is not 2. For the same reason, your second replacement rule doesn't do anything, either.
polarf[{r, phi}] /. { Cos[phi] -> k1/r, Sin[phi] -> k2/r } /.

This works because the expression generated by polarf, r^6 Cos[phi]^2 Sin[phi]^2, does contain Cos[phi] and Sin[phi], which in the first step get replaced by k1/r and k2/r. Thus you get r^6 (k1/r)^2 (k2/r)^2 which gets further evaluated to r^2 k1^2 k2^2. Now the last replacement rule kicks in, replacing r by Sqrt[k1^2 + k2^2]. Thus you get Sqrt[k1^2 + k2^2]^2 k1^2 k2^2 which evaluates to (k1^2 + k2^2) k1^2 k2^2, which is the expression you want.