Sunday, September 20, 2020

vectors in SymPy and use of dot cross and the Laplacian

Converting "\vec{\psi}(r, t)" into SymPy is feasible
Function('vecpsi')(Symbol('r'), Symbol('t'))
but I can't figure out how to apply the dot product with a vector:

>>> import sympy
>>> from sympy import *
>>> from sympy.vector import CoordSys3D, Del, curl, divergence, gradient
>>> Symbol('vecp').dot( Function('vecpsi')(Symbol('r'), Symbol('t')) )
Traceback (most recent call last):
AttributeError: 'Symbol' object has no attribute 'dot'

The issue is that a vector needs to be specified in a specific dimension (e.g., 3) and have specific coefficients with respect to the basis.

>>> N = CoordSys3D('N')
>>> v1 = Symbol('a')*N.i+Symbol('b')*N.j + Symbol('c')*N.k
>>> v2 = Symbol('f')*N.i+Symbol('g')*N.j + Symbol('k')*N.k
>>> v1.dot(v2)
a*f + b*g + c*k
>>> v1.cross(v2)
(b*k - c*g)*N.i + (-a*k + c*f)*N.j + (a*g - b*f)*N.k

see https://en.wikipedia.org/wiki/Del
>>> delop = Del()
>>> delop(Symbol('a'))
0
>>> delop(v1)
(Derivative(a*N.i + b*N.j + c*N.k, N.x))*N.i + (Derivative(a*N.i + b*N.j + c*N.k, N.y))*N.j + (Derivative(a*N.i + b*N.j + c*N.k, N.z))*N.k
>>> v1
a*N.i + b*N.j + c*N.k
>>> curl(v1)
0
>>> divergence(v1)
0
>>> Laplacian(v1)
Laplacian(a*N.i + b*N.j + c*N.k)

Also, operators can't be defined since using Laplacian requires an argument:
>>> Laplacian()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() missing 1 required positional argument: 'expr'

No comments:

Post a Comment