简体   繁体   English

在sympy中使用符号形式和向量形式的变量?

[英]Using a variable in symbol form and vector form in sympy?

If R = [0, 0, 1] , there are some cases when I would like to use Symbol('R') and others when Matrix([0, 0, 1]) is more useful in equations.如果R = [0, 0, 1] ,在某些情况下我想使用Symbol('R')而在其他情况下Matrix([0, 0, 1])在方程式中更有用。 How do I pick and choose when to use each form without defining too many variables?如何在不定义太多变量的情况下选择何时使用每种形式? (eg R = Symbol('R') and R_vec = Matrix([0, 0, 1]) is bad) (例如R = Symbol('R')R_vec = Matrix([0, 0, 1])是错误的)

One way of approaching this problem is to define R as a symbol, which you can substitute, via subs() , when needed, eg:解决此问题的一种方法是将R定义为一个符号,您可以在需要时通过subs()替换它,例如:

import sympy as sym


R = sym.Symbol('R')
R.subs({'R': sym.Matrix([1, 0, 0])})                                                                                                                    
# Matrix([
# [1],
# [0],
# [0]])

As per @OscarBenjamin comment, you may possibly prefer a MatrixSymbol() over a regular Symbol() as it will play more nicely with matrix operations.根据@OscarBenjamin 的评论,您可能更喜欢MatrixSymbol()而不是常规的Symbol() ,因为它可以更好地处理矩阵运算。

Thank you for the suggestions.感谢您的建议。 For my particular project I think it's suitable to store both sides of the equation in a tuple (LHS, RHS) from the get-go and just use each side R[i] when needed.对于我的特定项目,我认为从一开始就将等式的两边存储在一个元组 (LHS, RHS) 中并在需要时只使用每一边 R[i] 是合适的。 I understand I asked you for how to deal with this in sympy specifically, so thank you for your sympy specific answers我知道我问过你如何在 sympy 中具体处理这个问题,所以感谢你对 sympy 的具体回答

R = (Symbol('R'), Matrix([0, 0, 1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM