# How to do nonlinear substitution?

Suppose, I have an expression. For example:

$$\cos^5(\phi) \sin^3(\theta)$$

And I want to change variables to $(x, y, z)$, knowing that the new variables are related to $\theta$ and $\phi$, via given function, for example

$$x = \cos(\phi) \sin(\theta), y = \sin(\phi) \sin(\theta), z = \cos(\theta)$$.

Can I do it in Mathematica in an automatic way?

The solution is not unique.

coordinates = {x -> Cos[ϕ] Sin[θ], y -> Sin[ϕ] Sin[θ], z -> Cos[θ]};
$Assumptions = 0 < θ < π && 0 < ϕ < π/2;  The expressions x^5/(x^2 + y^2) /. coordinates // FullSimplify x^5/(1 - z^2) /. coordinates // FullSimplify (1 - z^2)^(3/2) ((-1 + y^2 + z^2)/(-1 + z^2))^(5/2) /. coordinates // FullSimplify (y^3 z^5)/((x^2 + y^2)^(3/2) (x^2 + y^2 + z^2)^(5/2)) /. coordinates // FullSimplify x^5/((x^2 + y^2) (x^2 + y^2 + z^2)^(3/2)) /. coordinates // FullSimplify  all return Cos[ϕ]^5 Sin[θ]^3  So does any (properly weighted) linear combination thereof. Needless to say, only the last option is homogeneous in x, y, z, so it is in a sense special. For more general problems (where there is no predefined natural set of coordinates), you can proceed as follows. We assume that the new set of coordinates is three-dimensional, for otherwise the solution is non-unique. If you want to restrict yourself to some two-dimensional submanifold you can always set one of the coordinates to any value of your choice. Take for example the coordinates$a,b,c$defined by $$x=a\log b,\quad y=c\log b,\quad z=a c \log b$$ and say you want to obtain the value of the expression $$a\ e^b+\frac{c}{ab}$$ Then you can use the code coordinates = {x == a Log[b], y == c Log[b], z == a c Log[b]}; inverse = Solve[coordinates, {a, b, c}][] // Normal; a E^b + c/(a b) /. inverse  whose output is (E^(-((x y)/z)) y)/x + (E^E^((x y)/z) z)/y  As a check, % /. ToRules@*And @@ coordinates // Simplify (* c/(a b) + a E^b *)  • I have several concerns about this answer: 1.- I do not know why you emphasise "not unique" and, 2.- why you restrict the angle$\phi$if$0\leq\phi\leq 2\pi\$? – José Antonio Díaz Navas Mar 27 '18 at 16:13
• @JoséAntonioDíazNavas 1) the emphasis on "non unique" is, well, because the question has no unique answer. OP is asking for the expression in terms of x,y,z, but there are an infinite number of non-equivalent expressions. Strictly speaking, the question is ill-posed. 2) the restriction in the angles is to help FullSimplify do its job. Without it, the third and fourth expressions contain factors of Abs[Sin[...]]. (In particular, the fourth expression is the one you give in your post; without the restriction in the angles your expression is not identical to the one in the OP). – AccidentalFourierTransform Mar 27 '18 at 16:18
• @Jose, in fact, your solution implicitly uses coordinate bounds, where Accidental chose to use them explicitly; look at the result of CoordinateChartData["Spherical", "CoordinateRangeAssumptions", {r, θ, ϕ}] – J. M.'s ennui Mar 28 '18 at 0:53
• It is non-unique because we used a transformation from 2-dimensional space to 3-dimensional space. So we can generate many solutions using a relation satisfied by x, y, z (which is x^2+y^2+z^2 = 1 in this case). – Janusz Przewocki Mar 28 '18 at 16:53
• Thank you for your answer. – Janusz Przewocki Apr 4 '18 at 9:50

I am assuming you have a field in Spherical Coordinates given by your function and you want to transform it in Cartesian Coordinates:

CoordinateTransform["Spherical" -> "Cartesian", {r, \[Theta], \[Phi]}]

(* {r Cos[\[Phi]] Sin[\[Theta]], r Sin[\[Theta]] Sin[\[Phi]], r Cos[\[Theta]]} *)


Then, use TransformedField:

TransformedField["Spherical" -> "Cartesian", Cos[\[Phi]]^5 Sin[\[Theta]]^3,
{r, \[Theta], \[Phi]} -> {x, y, z}]


$$\frac{x^5}{\left(x^2+y^2\right) \left(x^2+y^2+z^2\right)^{3/2}}$$

See the documentation for further info.

• Thank you. I see that Mathematica has a large basis of standard coordinate systems built in. But I wonder, what should I do, when I want to use some non-standard (local) coordinates, given by some formulae? I don't see any examplex in the documentation, even though the syntax of TransformedField function suggests it should be possible. – Janusz Przewocki Mar 28 '18 at 16:52
• @Janusz, that sounds like it should be a new question. – J. M.'s ennui Mar 28 '18 at 17:14
• @JanuszPrzewocki I updated my answer. – AccidentalFourierTransform Mar 28 '18 at 17:41