sympy - 如何更改 sympy 向量的乳胶名称?
问题描述
创建 sympy 向量时,首先创建坐标系,例如圆柱坐标:
from sympy.vector import CoordSys3D
from IPython.display import display
R = CoordSys3D('', transformation='cylindrical', variable_names=("r", "theta", "z"),vector_names=('i', 'j', 'k'))
我想更改变量和向量的乳胶名称。当我搜索时,CoordSys3D 有这些输入:
class sympy.vector.coordsysrect.CoordSys3D(name, transformation=None, parent=None, location=None, rotation_matrix=None, vector_names=None, variable_names=None)[source]
如果我打印 R.theta :
display(R.theta)
我得到 $theta$,但我想要 $\theta$
然而,在它的 __init__ 里面,有latex_scalars和latex_vects
__init__(name, location=None, rotation_matrix=None, parent=None, vector_names=None, variable_names=None, latex_vects=None, pretty_vects=None, latex_scalars=None, pretty_scalars=None, transformation=None)
我可以给它一个价值吗?
解决方案
我遇到了同样的问题,我找到了答案。
R = CoordSys3D('R', transformation='cylindrical', variable_names=("r", "theta", "z"),vector_names=('i', 'j', 'k'))
R.theta._latex_form = '\\theta'
R.j._latex_form = '\\vectorunit{\\theta}'
._latex_form 属性在定义 sympy 函数时也以相同的方式工作:
E_phi = smp.Function('E_phi')()
E_phi._latex_form = 'E_{\\phi}'
但是,符号没有 ._latex_form 属性。但我找到了另一种方法:
E_phi = Symbol('E_{\\phi}')
在 python 中输入乳胶代码时,请记住使用双反斜杠。
这是我编写的一些示例代码。输出被格式化为 MathJax。输出如下所示:
$$ \require{physics} $$
$$ \vb{E} = (E_{r})\vectorunit{r} + (7 e^{i \omega t})\vectorunit{\phi} + (E_{z})\vectorunit{z} $$
$$ \curl{\vb{E}} = (\frac{7 e^{i \omega t}}{r})\vectorunit{z} $$
$$ \vb{B} = (\begin{cases} \frac{7 i e^{i \omega t}}{r \omega} & \text{for}\: r \omega \neq 0 \\- \frac{7 t}{r} & \text{otherwise} \end{cases})\vectorunit{z} $$
import numpy as np
import sympy as smp
import matplotlib.pyplot as plt
from sympy import *
from sympy import symbols
from sympy.vector import divergence
from sympy.vector import curl
from sympy.vector import gradient
from sympy import diff
from sympy import exp
from sympy import integrate
from sympy import I, pi
from sympy.vector import CoordSys3D
N = CoordSys3D('N', transformation='cylindrical',
vector_names=("r", "phi", "z"),
variable_names=("R", "PHI", "Z"))
# Variables
R = N.R
PHI = N.PHI
Z = N.Z
R._latex_form = 'r'
PHI._latex_form = '\\phi'
Z._latex_form = 'z'
# Basis Unit Vectors
rhat = N.r
phihat = N.phi
zhat = N.z
rhat._latex_form = '\\vectorunit{r}'
phihat._latex_form = '\\vectorunit{\\phi}'
zhat._latex_form = '\\vectorunit{z}'
# The r, phi, and z components of the Electric Field
E_r = Symbol('E_{r}')
E_phi = Symbol('E_{\\phi}')
E_z = Symbol('E_{z}')
# Symbols
x, y, z, t = symbols('x y z t')
wavenumber = symbols('k')
E_0 = symbols('E_0') # Amplitude of E field
w = symbols('omega' , real=True, positive=True)
# Define E_phi(r,phi,z,t)
# Note: I = sqrt(-1)
# E_phi = (1/R) * smp.exp( I * ( w*t ))
E_phi = 7 * smp.exp( I * w * t)
# The Electric Field
E = (E_r * rhat) + (E_phi * phihat) + (E_z * zhat)
# Compute B by integrating the curl of E wrt time
# curl E = - dB/dt
# integrate( (curl E) , t ) = - B
jimmy = curl( E )
B = -integrate( jimmy, t )
# Display the answers as LATEX
# init_printing(use_unicode=True, wrap_line=False)
init_printing( use_latex='mathjax' )
print("$$ \\require{physics} $$")
print("$$ \\vb{E} = " + latex(E) + " $$")
print("$$ \\curl{\\vb{E}} = " + latex(jimmy) + " $$")
print("$$ \\vb{B} = " + latex(B.simplify()) + " $$")
# Alternative Way of defining the components of
# the Electric Field:
# The r, phi, and z components of the Electric Field
# E_r = smp.Function('E_r')()
# E_phi = smp.Function('E_phi')()
# E_z = smp.Function('E_z')()
# E_r._latex_form = 'E_{r}'
# E_phi._latex_form = 'E_{\\phi}'
# E_z._latex_form = 'E_{z}'
# Note that if we define these variables as symbols()
# Then we have to give the latex form this way:
# E_phi = symbol('E_{\\phi}')
# symbols have no attribute ._latex_form
# Whereas Functions do.
推荐阅读
- python - 获取 FileNotFoundError:尝试读取 csv 文件时
- python - 将python for循环变成一行代码
- android - 尝试解决在 AndroidManifest.xml 中检测到的重复包
- javascript - onClick 按钮功能,用于在复选框上“全选”切换
- java - java中关闭程序后如何存储变量的值
- android - 解析类似于矢量可绘制的路径并创建路径对象以复制相同的android
- apache-camel - Camel 在运行时配置重新交付警察
- javascript - 嘿!我试图让十字准线跟随我的鼠标,但 e.mouseX 和 e.mouseY 不能正常工作
- python - 将多个数据库与单个 Flask Web 服务器一起使用
- html - 无法在Angular的innerHTML中呈现routerLink