1
q_vec, omega_vec, q1, q2, q3, w1, w2, w3 = \
        sym.symbols('q_vec, omega_vec, q1, q2, q3, w1, w2, w3')

q_0_dot_str = '.5 * dot(q_vec, omega_vec)'
q_0_dot_symp = sym.sympify(q_0_dot_str)
q_0_dot_symp = q_0_dot_symp.subs([(q_vec, sym.Matrix([q1, q2, q3]), \
                               (omega_vec, sym.Matrix([w1, w2, w3]).T))])
q_0_dot_fcn = lambdify((q1, q2, q3, w1, w2, w3), q_0_dot_symp, 'numpy')

-或者-

q_0_dot_str = '.5 * dot(q_vec, omega_vec)'
q_0_dot_symp = sym.sympify(q_0_dot_str)
q_0_dot_symp = q_0_dot_symp.subs([(q_vec, sym.Matrix([q1, q2, q3]), \
                               (omega_vec, sym.Matrix([w1, w2, w3]).T))])
q_0_dot_symp = q_0_dot_symp.xreplace([{q_vec: sym.Matrix([q1, q2, q3])},\
                                   {omega_vec: sym.Matrix([w1, w2, w3]).T}])
q_0_dot_fcn = lambdify((q1, q2, q3, w1, w2, w3), q_0_dot_symp, 'numpy')

但这些都不起作用。当我评估以下内容时:

q_0_dot_fcn(1,2,3,4,5,6)

我得到:

0.5*omega_vec*q_vec

而不是在我为符号矩阵中的符号变量提供的值中进行替换(然后得到一个实际的 m=numerical 结果)。

4

1 回答 1

0

您在 subs 调用中有错字。它应该是

q_0_dot_symp = q_0_dot_symp.subs([(q_vec, sym.Matrix([q1, q2, q3])), \
                               (omega_vec, sym.Matrix([w1, w2, w3]).T)])

代替

q_0_dot_symp = q_0_dot_symp.subs([(q_vec, sym.Matrix([q1, q2, q3]), \
                               (omega_vec, sym.Matrix([w1, w2, w3]).T))])

(注意括号的位置)

进行此修复后,它在 SymPy 1.0 中对我有用

In [1]: import sympy as sym

In [2]: q_vec, omega_vec, q1, q2, q3, w1, w2, w3 = \
   ...:         sym.symbols('q_vec, omega_vec, q1, q2, q3, w1, w2, w3')

In [3]: q_0_dot_str = '.5 * dot(q_vec, omega_vec)'

In [4]: q_0_dot_symp = sym.sympify(q_0_dot_str)

In [5]: q_0_dot_symp = q_0_dot_symp.subs([(q_vec, sym.Matrix([q1, q2, q3])), \
                               (omega_vec, sym.Matrix([w1, w2, w3]).T)])

In [6]: q_0_dot_fcn = lambdify((q1, q2, q3, w1, w2, w3), q_0_dot_symp, 'numpy')

In [7]: q_0_dot_fcn(1,2,3,4,5,6)
Out[7]:
array([[ 2. ,  2.5,  3. ],
       [ 4. ,  5. ,  6. ],
       [ 6. ,  7.5,  9. ]])
于 2016-06-13T22:11:15.393 回答