Goal: Automated evaluation of line integral $$ \int_{{\bf r}_1}^{{\bf r}_2} {\bf v}({\bf r})\cdot d{\bf l} $$ given
So far works for paths parametrized in terms of Cartesian basis vectors. Will need an additional function to convert paths parametrized in terms of $r$, $\theta$, $\phi$ into paths in terms of $x$, $y$, and $z$.
import sympy as sym
sym.init_printing() # for LaTeX formatted output
import sympy.vector as sv # There is a very similar sympy.physics.vector module
# I think the "physics" version may be more
# transformation-friendly
x, y, z, t = sym.symbols('x,y,z,t')
R= sv.CoordSys3D('R') # cf ReferenceFrame of sympy.physics.vector
def v(x,y,z): # vector field as a function of scalar variables x,y,z
return x*y*R.i + 2*y*z*R.j + 3*x*z*R.k
def voft(l): # vector field along path l as a function of t
x,y,z = (l.dot(R.i),l.dot(R.j),l.dot(R.k)) # x,y,z as functions of t
return v(x,y,z)
dl
; the dt
will be implicit in the integrate
step.) def li(l,v): # dl/dt
dl = sym.diff(l,t)
return sym.integrate(voft(l).dot(dl),(t,0,1))
These are paths from Griffiths problem 1.34.
l1 = 2*t*R.j
l2 = 2*(1-t)*R.j + 2*t*R.k
l3 = 2*(1-t)*R.k
li(l1,v),li(l2,v),li(l3,v)
l1,l2,l3
dl1 = sym.diff(l1,t)
dl2 = sym.diff(l2,t)
dl3 = sym.diff(l3,t)
dl1,dl2,dl3
voft(l1),voft(l2),voft(l3)
voft(l1).dot(l1),voft(l2).dot(l2),voft(l3).dot(l3)
sym.integrate(voft(l2).dot(dl2),(t,0,1))
curl_v = sv.curl(v(R.x,R.y,R.z)).subs(R.x,0) # curl evaluated on surface where x=0
da = R.i # Infinitesimals dx dy implicit in integrate
sym.integrate(curl_v.dot(da),(R.z,0,2-R.y),(R.y,0,2))
version_information
is from J.R. Johansson (jrjohansson at gmail.com); see Introduction to scientific computing with Python for more information and instructions for package installation.
version_information
is installed on the linux network at Bucknell
%load_ext version_information
%version_information sympy