sympy:收集符号以获得矩阵系数吗? [英] sympy: Collect symbols for matrix coefficients?

查看:160
本文介绍了sympy:收集符号以获得矩阵系数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> sympy表达式与矩阵系数紧密相关吗?,其中Wild符号与match(form)一起使用以确定其矩阵形式的系数.但是,我无法使用match(form)方法来进行以下操作.

Closely related to Factor sympy expression to matrix coefficients?, where Wild symbols are used with match(form) to determine coefficients for its matrix form. However, I am unable to get the match(form) method to work for the following.

match(form)方法为什么会失败?

Why does match(form) method fail?

有哪些干净的替代方法可以做到这一点?

#Linear Interpolation function: V(x)
v_1, theta_1, v_2, theta_2, x, L = symbols("v_1, theta_1, v_2, theta_2, x, L")
a_1, a_2, a_3, a_4 = symbols("a_1, a_2, a_3, a_4", real=True)
V = a_1*x**0 + a_2*x**1 + a_3*x**2 + a_4*x**3
#Solve for coefficients (a_1, a_2, a_3, a_4) with BC's: V(x) @ x=0, x=L
shape_coeffs = solve([Eq(v_1, V.subs({x:0})), 
                      Eq(theta_1, V.diff(x).subs({x:0})), 
                      Eq(v_2, V.subs({x:L})), 
                      Eq(theta_2, V.diff(x).subs({x:L}))], 
                     (a_1, a_2, a_3, a_4))
V = V.subs(shape_coeffs)
#Factor to matrix
V = sympy.collect(sympy.expand(V), (v_1, theta_1, v_2, theta_2))

并收集项,直到矩阵形式明显为止.要匹配表格:

And collect terms until the matrix form is apparent. To match forms:

C_1, C_2, C_3, C_4 = symbols("C_1, C_2, C_3, C_4", cls=Wild)
form = c_1*v_1 + c_2*theta_1 + c_3*v_2 + c_4*theta_2
mat_coeffs = V.match(form)
N = Matrix([C_1, C_2, C_3, C_4]).transpose()
N = N.subs(mat_coeffs)
v = Matrix([v_1, theta_1, v_2, theta_2])

与引用的问题V.match(form)不同,它返回,而不是包含{C_1:f(x), C_2:f(x), C_3:f(x), C_4:f(x)} dict().为什么失败了? -通过检查,解决方案很明显.

Unlike the referenced question, V.match(form), returns None instead of a dict() containing {C_1:f(x), C_2:f(x), C_3:f(x), C_4:f(x)}. Why does this fail? -- by inspection, the solution is obvious.

推荐答案

由于collect(expand(V), ...)已经将V显示为变量v_1, theta_1, v_2, theta_2中的线性多项式,而不是使用V.match(form),也许更简单,更直接获取系数的方法是使用V.coeff方法:

Since collect(expand(V), ...) already shows V as a linear polynomial in the variables v_1, theta_1, v_2, theta_2, instead of using V.match(form), perhaps an easier, more direct way to get the coefficients is to use the V.coeff method:

N = sy.Matrix([V.coeff(v) for v in (v_1, theta_1, v_2, theta_2)]).transpose()


import sympy as sy
#Linear Interpolation function: V(x)
v_1, theta_1, v_2, theta_2, x, L = sy.symbols(
    "v_1, theta_1, v_2, theta_2, x, L")
a_1, a_2, a_3, a_4 = sy.symbols("a_1, a_2, a_3, a_4", real=True)
V = a_1*x**0 + a_2*x**1 + a_3*x**2 + a_4*x**3
#Solve for coefficients (a_1, a_2, a_3, a_4) with BC's: V(x) @ x=0, x=L
shape_coeffs = sy.solve([sy.Eq(v_1, V.subs({x:0})), 
                      sy.Eq(theta_1, V.diff(x).subs({x:0})), 
                      sy.Eq(v_2, V.subs({x:L})), 
                      sy.Eq(theta_2, V.diff(x).subs({x:L}))], 
                     (a_1, a_2, a_3, a_4))
V = V.subs(shape_coeffs)
V = sy.collect(sy.expand(V), (v_1, theta_1, v_2, theta_2))
N = sy.Matrix([V.coeff(v) for v in (v_1, theta_1, v_2, theta_2)]).transpose()
print(N)

收益

Matrix([[1 - 3*x**2/L**2 + 2*x**3/L**3, x - 2*x**2/L + x**3/L**2, 3*x**2/L**2 - 2*x**3/L**3, -x**2/L + x**3/L**2]])

这篇关于sympy:收集符号以获得矩阵系数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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