[英]SymPy substitute Symbol for Vector
I have an expression of type Mul
called term
in the code below.我在下面的代码中有一个称为term
的Mul
类型的表达式。 Within term
is a type Symbol
called zhat
.在term
中是一个名为zhat
的Symbol
类型。 I want to perform something like term.subs(zhat, vec)
where vec
is of type BaseVector
.我想执行类似term.subs(zhat, vec)
的事情,其中vec
的类型是BaseVector
。 I am subbing a symbol for a vector.我正在用符号代替向量。 The output is assigned to out_actual
in the code. output 在代码中分配给out_actual
。
The issue is that out_actual
is of type Mul
when I need it to be of type VectorMul
.问题是当我需要out_actual
的类型为Mul
时,它的类型为VectorMul
。 The variable out_ideal
is what I would expect from the substitution.变量out_ideal
是我对替换的期望。 Any way to obtain out_ideal
?有什么方法可以获得out_ideal
吗?
import sympy as sp
from sympy.vector import CoordSys3D
N = CoordSys3D('N')
Q, eps, zhat = sp.symbols('Q \epsilon_0 \\hat{\\mathbf{z}}')
vec = N.k
term = Q*eps*zhat
out_ideal = Q*eps*vec
out_actual = term.subs(zhat, vec)
It's not particularly pleasant or generalizable, but you could do the following to convert any Mul
into a VectorMul
whenever appropriate (and leaving it as a Mul
otherwise):它不是特别令人愉快或普遍化,但您可以执行以下操作以在适当的时候将任何Mul
转换为VectorMul
(否则将其保留为Mul
):
import sympy as sp
from sympy.vector import CoordSys3D
from sympy.core.mul import Mul
N = CoordSys3D('N')
def multiply_args(obj):
'''Gets the result of multiplying all of the Mul's args together.'''
if not isinstance(obj, Mul):
raise ValueError('Input must be an instance of Mul')
args = obj.args
prod = args[0]
for e in args[1:]:
prod = prod * e
return prod
Q, eps, zhat = sp.symbols('Q \epsilon_0 \\hat{\\mathbf{z}}')
vec = N.k
term = Q*eps*zhat
sub_result = term.subs(zhat, vec)
sub_result = multiply_args(sub_result)
The reason this is necessary is that subs
, a method which belongs to the Basic
class, simply looks at all of the arguments (eg Q
, eps
, zhat
) of the Mul
object ( term
) and replaces each that matches the substitution target, and gives the result as a Mul
but with an amended list of args
(ie zhat
has been replaced with vec
).这是必要的原因是subs
,一种属于Basic
class 的方法,只需查看Mul
ZA8CFDE6331BD59EB2AC96F8911C4B666 的所有term
(例如Q
, eps
, zhat
),并替换每个替换目标匹配(和)将结果作为Mul
但带有修改的args
列表(即zhat
已替换为vec
)。 It doesn't do any further evaluation on the result, and leaves the argument as a Mul
.它不对结果进行任何进一步的评估,并将参数保留为Mul
。
To convert it to a VectorMul
, you can just multiply the resulting arguments together manually, like you did to get out_ideal
.要将其转换为VectorMul
,您可以手动将生成的 arguments 相乘,就像获得out_ideal
。 All multiply_args
does is manually multiplies the arguments together, which then promotes to a VectorMul
if any of the arguments is a Vector
.所有multiply_args
所做的都是手动将 arguments 相乘,然后如果 arguments 中的任何一个是Vector
,则将其提升为VectorMul
。 Obviously this only works because you know you have started with a Mul
;显然,这只有效,因为您知道您已经开始使用Mul
; you'd need to generalize this to deal with other types if you needed to.如果需要,您需要将其概括为处理其他类型。
If could be worth putting in a feature reqeust to the SymPy Github repository for this functionality.如果值得为此功能向 SymPy Github 存储库添加功能请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.