Change of Coords. in Symbolic Comp.
I'd like to change cartesian symbols to spherical ones through a neat and simple method. I discovered that subbing in spherical symbols is problematic due to how SAGE's subbing method fails to treat (without wildcard) some expressions as equivalent because ordering of variables in each of those expressions is different (noncommutative ?).....meaning you may end up with one part of the function looking as intended and the other not.
I've tried using wildcards and saw significant improvement, but still struggle to reach my desired outcome. In particular, I'm solving prob. 3.6 (a) in Class. Elec. Jackson. Below is an attempt by (A) firstly setting variables with respective coord. system, (B) expanding the factors of potential function, and (C) finally subbing in r^2 along with a wildcard. Notice that the last term has failed to sub in r^2 (easily fixed by subbing in another time).
What I'd like to know is if there's a way to directly convert the cartesian function into a spherical one without tedious substitutions.
%display latex
E.<x,y,z> = EuclideanSpace()
cartesian = E.cartesian_coordinates()
spherical.<r,\theta,ϕ> = E.spherical_coordinates()
k, q, a, Q, = var('k, q, a, Q')
w0 = SR.wild(0)
ϕ_1 = k * -2 * q / (x^2 + y^2 + z^2);
ϕ_2 = k * q / (x^2 + y^2 + (z + a)^2);
ϕ_3 = k * q / (x^2 + y^2 + (z - a)^2);
ϕ_t = ϕ_1 + ϕ_2 + ϕ_3;
f = function('f')(x,y,z) == ϕ_t.expand();
f.subs(z^2 + x^2 + y^2 + w0 == r^2 + w0)
output : $f\left(x, y, z\right) = \frac{k q}{a^{2} + r^{2} + 2 \, a z} + \frac{k q}{a^{2} + r^{2} - 2 \, a z} - \frac{2 \, k q}{x^{2} + y^{2} + z^{2}}$
This is partially answered in https://ask.sagemath.org/question/971...