有没有办法在 SymPy 中使用球坐标? [英] Is there a way of working in spherical coordinates in SymPy?

查看:44
本文介绍了有没有办法在 SymPy 中使用球坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写脚本以在 Python 中进行分析计算.为此,我需要参数的向量表示以及一些基本的向量运算,例如梯度和发散,因此我开始使用 SymPy.但是,我注意到在球坐标中没有一种直接的工作方式.

阅读文档后我发现笛卡尔环境可以简单地定义为

from sympy.vector import CoordSys3DN = CoordSys3D('N')

并直接开始使用酉笛卡尔酉向量 i, j, k

v = 2*N.i + 3*N.j - N.k

有什么方法可以为球坐标初始化这样的环境,在那里我可以访问径向、θ和 phi 酉向量,从而相应地完成基本向量运算?在此先感谢您!

解决方案

您可以将球坐标酉向量访问为 'r'、't'、'p'(或者您可以使用全名,如 'radius'、'theta', 'phi') 而不是 'i', 'j', ,'k' 如果您指出变换是球形":

<预><代码>>>>从 sympy.vector 导入 CoordSys3D>>>P = CoordSys3D('P',transformation='sphereal', variable_names=list('rtp'))>>>公关公关

I am working in a script to do analytical calculations in Python. For it I would need vector representation of my parameters and also some basic vector operations such as Gradient and Divergence, and for this reason I started working in SymPy. However, I noticed there is not a straightforward way of working in spherical coordinates.

After reading the documentation I found out a Cartessian environment can be simply defined as

from sympy.vector import CoordSys3D

N = CoordSys3D('N')

and directly start working with the unitary cartessian unitary vectors i, j, k

v = 2*N.i + 3*N.j - N.k

Is there any way of initializing such an environment for spherical coordinates where I have access to radial, theta and phi unitary vectors, and consequently the basic vector operations are done accordingly? Thank you so much in advance!

解决方案

You can access spherical coordinate unitary vectors as 'r', 't', 'p' (or you can use full names like 'radius', 'theta', 'phi') instead of 'i', 'j', ,'k' if you indicate that the transformation is 'spherical':

>>> from sympy.vector import CoordSys3D
>>> P = CoordSys3D('P', transformation='spherical', variable_names=list('rtp'))
>>> P.r
P.r

这篇关于有没有办法在 SymPy 中使用球坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆