Ask Your Question
1

Change of Coords. in Symbolic Comp.

asked 2024-10-01 22:31:42 +0100

Afandi_MohAlsh gravatar image

updated 2024-10-02 19:48:47 +0100

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}}$

edit retag flag offensive close merge delete

Comments

1

This is partially answered in https://ask.sagemath.org/question/971...

f.substitute(z^2  == r^2-x^2-y^2 )
achrzesz gravatar imageachrzesz ( 2024-10-02 10:41:55 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2024-10-02 13:01:33 +0100

Emmanuel Charpentier gravatar image

I re-ran your code, replacing ϕ withphi. After :

f = function('f')(x,y,z) == phi_t.expand();

we have :

sage: f
f(x, y, z) == k*q/(a^2 + x^2 + y^2 + 2*a*z + z^2) + k*q/(a^2 + x^2 + y^2 - 2*a*z + z^2) - 2*k*q/(x^2 + y^2 + z^2)

meaning that f is a Python variable whose value is an equality. Probably not what you wanted.

I am guessing that your intent was :

sage: reset("f")
sage: f(x,y,z) = phi_t.expand(); f
(x, y, z) |--> k*q/(a^2 + x^2 + y^2 + 2*a*z + z^2) + k*q/(a^2 + x^2 + y^2 - 2*a*z + z^2) - 2*k*q/(x^2 + y^2 + z^2)

f is now (a Python variable whose value is) a symbolic function (a. k. a. a callable symbolic expression).

sage: f(x, y, z).subs(z^2+x^2+y^2+w0==r^2+w0)
k*q/(a^2 + r^2 + 2*a*z) + k*q/(a^2 + r^2 - 2*a*z) - 2*k*q/(x^2 + y^2 + z^2)

which seems to be closer to what you seem to want.

But writing yourself your conversion functions may be unnecessary : you may be interested by this tutorial, then by this one, using this (large) Sage module.

HTH,

edit flag offensive delete link more

Comments

Thank you for the suggestions. I did stumble on all these tutorials actually, but thought this was a trivial enough problem to warrant an easier solution (doesn't seem like it now).

I tried to implement some material from the first tutorial you mentioned (as seen in the first couple lines), but wasn't too useful unless I'm defining a scalar field. Which leads me to another question: is there a way to define a single valued scalar function on E^3 the same way for a scalar field ? Plus, would this allow you to "display" the single-valued function in multiple coord systems the same way you could for the field ? It seems like the module may come in handy for this.

--Moh

Afandi_MohAlsh gravatar imageAfandi_MohAlsh ( 2024-10-02 20:13:03 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2024-10-01 22:30:44 +0100

Seen: 243 times

Last updated: Oct 02