Here is galgebra/sympy code for vector derivatives in rectangular and
spherical coordinate with output annotated using latex -
def derivatives_in_rectangular_coordinates():
#Print_Function()
X = (x,y,z) = symbols('x y z') #coordinates
o3d = Ga('e_x e_y e_z',g=[1,1,1],coords=X) #Rectangular coordinate
basis
(ex,ey,ez) = o3d.mv() #basis vectors
grad = o3d.grad #gradient operator
f = o3d.mv('f','scalar',f=True) #scalar function
A = o3d.mv('A','vector',f=True) #vector function
B = o3d.mv('B','bivector',f=True) #bivector function
C = o3d.mv('C','mv') #general multivector function
gprint('f =',f)
gprint('A =',A)
gprint('B =',B)
gprint('C =',C)
gprint('\\nabla f =',grad*f) #gradient of scalar function
gprint('\\nabla\\cdot A =',grad|A) #divergence of vector function
gprint('\\nabla A =',grad*A) #geometric derivative of vector function
gprint('\\nabla\\times A = -I*(\\nabla\\wedge A)
=',-o3d.I()*(grad^A)) #curl of vector function
gprint('\\nabla B =',grad*B) # geometric derivative of bivector
function
gprint('\\nabla\\wedge B =',grad^B) # exterior derivative of
bivector function
gprint('\\nabla\\cdot B =',grad|B) # divergence of bivector function
return
def derivatives_in_spherical_coordinates():
#Print_Function()
X = (r,th,phi) = symbols('r theta phi')
s3d = Ga('e_r e_theta
e_phi',g=[1,r**2,r**2*sin(th)**2],coords=X,norm=True)
(er,eth,ephi) = s3d.mv()
grad = s3d.grad
f = s3d.mv('f','scalar',f=True)
A = s3d.mv('A','vector',f=True)
B = s3d.mv('B','bivector',f=True)
gprint('f =',f)
gprint('A =',A)
gprint('B =',B)
gprint('\\nabla f =',grad*f)
gprint('\\nabla\\cdot A =',grad|A)
gprint('\\nabla\\times A = -I*(\\nabla\\wedge A)
=',(-s3d.E()*(grad^A)).simplify())
gprint('\\nabla\\wedge B =',grad^B)
return
On 5/13/21 10:58 AM, Ryan D Johnson wrote:
Oscar makes a good point here:
Conceptually this is a bit muddled though as it conflates vectors
with points. Formally what is wanted here is to evaluate a scalar
field at a point represented by a particular position vector wrt some
reference frame. I don't know if the vector module has the concept of
a point as opposed to a vector.
It turns out sympy.vector does indeed have a Point class. It occurs to
me that I could also dot with each basis vector. Both of these handle
the case of a zero-valued component without introducing an if clause
to the list comprehension (a problem with my original example, as
Davide pointed out).
But as far as I can tell, there’s no way to get the coordinates in a
format compatible with subs, so however you get the components you
still end up having to zip them with the base vectors from CoordSys3D.
Here are three conceptually more pleasing ways to get a tuple of
components:
In [1]: import sympy
In [2]: import sympy.vector
In [3]: R3 = sympy.vector.CoordSys3D("R3")
In [4]: t = sympy.Symbol("t", real=True)
In [5]: g = t * R3.i - t**2 * R3.j - 0 * R3.k
In [6]: # Three different ways to get the components as a tuple:
In [7]: R3.origin.locate_new(‘P', g).express_coordinates(R3) #
Using Point
Out[7]: (t, -t**2, 0)
In [8]: tuple(b.dot(g) for b in R3.base_vectors()) # dot product
with each basis
Out[8]: (t, -t**2, 0)
In [9]: tuple(g.to_matrix(R3))
Out[9]: (t, -t**2, 0)
If I were just making stuff up, it would be great if there were
operations to (a) get the point at the end of a vector given a
starting point and (b) convert a point to a dictionary or tuple in a
format that subs is happy to consume. So something like:
g.to_point(R3.origin).to_dict(R3)
But even if we just had (b) we could write
R3.origin.locate_new(‘P', g).to_dict(R3)
Seems a shame to keep passing the coordinate system, but Point doesn’t
look like it remembers it.
/rdj
--
You received this message because you are subscribed to the Google
Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/73A0AD28-CA29-4D87-923D-213355CDD1C6%40gmail.com
<https://groups.google.com/d/msgid/sympy/73A0AD28-CA29-4D87-923D-213355CDD1C6%40gmail.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/da4454c6-60a9-a1d3-ef36-5488094aa75a%40gmail.com.