Attached is the code and pdf output for all three cases.
On 10/23/21 2:11 AM, Andreas Schuldei wrote:
I am putting together the components of a vector field (a magnetic field, caused by a current in several conductors) in cartesian coordinates. The field is derived from calculating the rotation of its magnetic vector potential, which can be expressed asA_z = -Const * dot(r,a)(dot(r,r) A =(0,0,A_z and then the rot(A): B = curl(A) gives me (after some simplifications)u = (2 * C * r2 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2) - (C * a2) / (r1*r1 + r2*r2) v = (C * a1) / (r1*r1 + r2*r2) - (2 * C * r1 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2)w = 0 with B(u,v,w) being the vector field I am interested in.My initial question ("How to create a vector") is mostly a sympy syntactical one. I look for a function likeSys = CoordSys3D("Sys") O = Sys.originu = (2 * C * r2 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2) - (C * a2) / (r1*r1 + r2*r2) v = (C * a1) / (r1*r1 + r2*r2) - (2 * C * r1 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2)w = 0 V = O.vector(u,v,w) ^^^^^^^^^^^^^^^^^^^where I can specify a vector, relative to a coordinate system, by its components. The O.vector() function would return a vector that can then safely be transformed into other (resting) coordinate systems.brombo schrieb am Freitag, 22. Oktober 2021 um 19:06:03 UTC+2: You might want to look at this link - https://galgebra.readthedocs.io/en/latest/ <https://galgebra.readthedocs.io/en/latest/> Also if you could show me symbolically (not code) what you are doing perhaps I could give you an example of how to do it in galgebra. On 10/22/21 3:15 AM, Andreas Schuldei wrote:I saw this https://stackoverflow.com/questions/46993819/how-to-create-a-vector-function-in-sympy <https://stackoverflow.com/questions/46993819/how-to-create-a-vector-function-in-sympy> which uses Matrix() as a workaround to create a vector. The author says, that it can not be transformed between coordinate systems, like real vectors, though. I need to transform my input and output vector from one coordinate system to another (and back). How are vector functions done in that case? My function is simple: def B_el(r_vec, I): mu_0 = 4 * np.pi * 1e-7 a1 = -0.05 a2 = 0.0 C = mu_0 * I / np.pi r1 = r_vec.i r2 = r_vec.j u = (2 * C * r2 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2) - (C * a2) / (r1*r1 + r2*r2) v = (C * a1) / (r1*r1 + r2*r2) - (2 * C * r1 * (a1 * r1 + a2 * r2)) / np.square(r1*r1 + r2*r2) return Matrix([u, v, 0])-- You received this message because you are subscribed to theGoogle Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/8db840a8-20f9-47ac-a1c3-11b6658f00bfn%40googlegroups.com <https://groups.google.com/d/msgid/sympy/8db840a8-20f9-47ac-a1c3-11b6658f00bfn%40googlegroups.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 sympy+unsubscr...@googlegroups.com <mailto:sympy+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/9797a914-06a6-4f8e-a99f-5403504862bfn%40googlegroups.com <https://groups.google.com/d/msgid/sympy/9797a914-06a6-4f8e-a99f-5403504862bfn%40googlegroups.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 sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/451c0f63-cdbf-caca-017f-7a5910ad4eda%40gmail.com.
from sympy import Symbol, symbols, sin, cos, Rational, expand, simplify, collect from galgebra.printer import Format, Print_Function, Fmt from galgebra.gprinter import gFormat, gxpdf, gprint from galgebra.ga import Ga, one, zero from galgebra.mv import Nga, cross HALF = Rational(1,2) def derivatives_in_rectangular_coordinates(): #Print_Function() X = (x,y,z) = symbols('x y z') o3d = Ga('e_x e_y e_z',g=[1,1,1],coords=X) (ex,ey,ez) = o3d.mv() grad = o3d.grad f = o3d.mv('f','scalar',f=True) A = o3d.mv('A','vector',f=True) B = o3d.mv('B','bivector',f=True) C = o3d.mv('C','mv') gprint('f =',f) gprint('A =',A) gprint('B =',B) gprint('C =',C) gprint('\\nabla f =',grad*f) gprint('\\nabla\\cdot A =',grad|A) gprint('\\nabla A =',grad*A) gprint('\\nabla\\times A = -I*(\\nabla\\wedge A) =',-o3d.I()*(grad^A)) gprint('\\nabla B =',grad*B) gprint('\\nabla\\wedge B =',grad^B) gprint('\\nabla\\cdot B =',grad|B) return def derivatives_in_cylindrical_coordinates(): #Print_Function() X = (r,th,z) = symbols('r theta z') c3d = Ga('e_r e_theta e_z',g=[1,r**2,1],coords=X,norm=True) (er,eth,ez) = c3d.mv() grad = c3d.grad f = c3d.mv('f','scalar',f=True) A = c3d.mv('A','vector',f=True) B = c3d.mv('B','bivector',f=True) gprint('f =',f) gprint('A =',A) gprint('B =',B) gprint('\\nabla f =',(grad*f).simplify()) gprint('\\nabla\\cdot A =',(grad|A).simplify()) gprint('\\nabla\\times A = -I*(\\nabla\\wedge A) =',(-c3d.E()*(grad^A)).simplify()) gprint('\\nabla\\wedge B =',(grad^B).simplify()) 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) def vector_potential_in_cylindrical_coordinates(): #Print_Function() X = (r,th,z) = symbols('r theta z') (k,ax,ay,az) = symbols('k a_x a_y a_z') gprint(r'\text{Cylindrical Coordinates}') c3d = Ga('e_r e_theta e_z',g=[1,r**2,1],coords=X,norm=True) gprint('g =',c3d.g_raw) (er,eth,ez) = c3d.mv() grad = c3d.grad R = r*er+z*ez gprint(r'\bs{R} =',R) a = (ax*cos(th)+ay*sin(th))*er+(ay*cos(th)-ax*sin(th))*eth+az*ez gprint(r'\bs{a} =',a) A = (-k*(a|R)*(R|R)*ez).simplify() gprint(r'\bs{A} = -k(\bs{a}\cdot \bs{R})\bs{R}^{2}\bs{e}_{z} =',A) B = (-c3d.E()*(grad^A)).simplify() gprint(r'\bs{B} = \nabla\times \bs{A} = -I*(\nabla\wedge \bs{A}) =',B) def vector_potential_in_rectangular_coordinates(): #Print_Function() X = (x,y,z) = symbols('x y z') k = symbols('k') gprint(r'\text{Rectangular Coordinates}') r3d = Ga('e_x e_y e_z',g=[1,1,1],coords=X,norm=True) gprint('g =',r3d.g_raw) (ex,ey,ez) = r3d.mv() grad = r3d.grad R = x*ex+y*ey+z*ez gprint(r'\bs{R} =',R) a = r3d.mv('a','vector') gprint(r'\bs{a} =',a) A = (-k*(a|R)*(R|R)*ez).simplify() gprint(r'\bs{A} = -k(\bs{a}\cdot \bs{R})\bs{R}^{2}\bs{e}_{z} =',A) B = (-r3d.E()*(grad^A)).simplify() gprint(r'\bs{B} = \nabla\times \bs{A} = -I*(\nabla\wedge \bs{A}) =',B) def vector_potential_in_nonorthogonal_coordinates(): #Print_Function() X = (xa,xp,z) = symbols('x_a x_p z') (k,a,th) = symbols('k a theta') gprint(r'\text{Nonorthogonal Coordinates}') g = [[1,0,cos(th)],[0,1,0],[cos(th),0,1]] no3d = Ga('e_a e_p e_z',g=g,coords=X) gprint('g =',no3d.g_raw) (ea,ep,ez) = no3d.mv() grad = no3d.grad R = xa*ea+xp*ep+z*ez gprint(r'\bs{R} =',R) a = a*ea gprint(r'\bs{a} =',a) A = (-k*(a|R)*(R|R)*ez).simplify() gprint(r'\bs{A} = -k(\bs{a}\cdot \bs{R})\bs{R}^{2}\bs{e}_{z} =',A) B = (-no3d.E()*(grad^A)).simplify() gprint(r'\bs{B} = \nabla\times \bs{A} = -I*(\nabla\wedge \bs{A}) =',B) def main(): gFormat() #derivatives_in_rectangular_coordinates() #derivatives_in_cylindrical_coordinates() #derivatives_in_spherical_coordinates() vector_potential_in_rectangular_coordinates() vector_potential_in_cylindrical_coordinates() vector_potential_in_nonorthogonal_coordinates() gxpdf() return if __name__ == "__main__": main()
Vector_Potential.pdf
Description: Adobe PDF document