简体   繁体   English

将 Sympy 矩阵代入多项式

[英]Substitute a Sympy matrix into a polynomial

I have a Sympy matrix A and a polynomial expression P, and I would like to compute P(A).我有一个 Sympy 矩阵 A 和一个多项式表达式 P,我想计算 P(A)。

Here is an example:下面是一个例子:

x = Symbol('x')
P = x**2 - 3*x + 5
A = Matrix([ [1,3], [-1,2] ])
P.subs(x, A)

I expect Sympy to compute A**2 - 3*A + 5*eye(2) (in the example, the result is the zero matrix).我希望 Sympy 计算A**2 - 3*A + 5*eye(2) (在示例中,结果是零矩阵)。

But this fails with an error message:但这失败并显示错误消息:

AttributeError: ImmutableMatrix has no attribute as_coeff_Mul.

Is there any way to obtain what I want ?有什么办法可以得到我想要的吗?

Edit: I've tried to convert P into Sympy's polynomial class, and substitute afterwards, but the result is useless:编辑:我尝试将P转换为 Sympy 的多项式类,然后替换,但结果无用:

Poly(P).subs(A)
Poly(Matrix([ [ 1, 3], [-1, 2]])**2 - 3*Matrix([ [ 1, 3],        
    [-1, 2]]) + 5, Matrix([ [ 1, 3], [-1, 2]]), domain='ZZ')

I can get the correct result with the following function :我可以使用以下函数获得正确的结果:

def poly_matrix(P, A):
    coeffs = Poly(P).all_coeffs()[::-1]
    res = zeros(A.rows)
    for i in range(len(coeffs)):
        res += coeffs[i]*(A**i)
    return res

But I'm still looking for a more efficient built-in option.但我仍在寻找更高效的内置选项。

Matrix expressions are still a bit buggy, hopefully they'll get fixed in the future.矩阵表达式仍然有一些问题,希望它们在未来得到修复。

Anyway, here's an alternative way to perform the substitution:无论如何,这是执行替换的另一种方法:

In [1]: x = MatrixSymbol('x', 2, 2)

In [2]: P = x**2 - 3*x + 5*eye(2)

In [3]: P
Out[3]: 
                 2
⎡5  0⎤ + -3⋅x + x 
⎢    ⎥            
⎣0  5⎦            

In [4]: A = Matrix([ [1,3], [-1,2] ])

In [5]: P.subs(x, A)
Out[5]: 
                               2
⎡5  0⎤ + -3⋅⎡1   3⎤ + ⎛⎡1   3⎤⎞ 
⎢    ⎥      ⎢     ⎥   ⎜⎢     ⎥⎟ 
⎣0  5⎦      ⎣-1  2⎦   ⎝⎣-1  2⎦⎠ 

In [6]: P.subs(x, A).doit()
Out[6]: 
                   2
⎡2  -9⎤ + ⎛⎡1   3⎤⎞ 
⎢     ⎥   ⎜⎢     ⎥⎟ 
⎣3  -1⎦   ⎝⎣-1  2⎦⎠ 

Here it appears that MatPow isn't able to perform the .doit() operation.这里似乎MatPow无法执行.doit()操作。 It's probably another bug.这可能是另一个错误。

In output number 5 there's also a bug with the printer ( + -3 should just be -3 ).在输出编号 5 中,打印机也有一个错误( + -3应该只是-3 )。

I really hope that someone will eventually fix all these problems.我真的希望有人最终会解决所有这些问题。

Try to evaluate each term of the polynomial.尝试评估多项式的​​每一项。

(x**2).subs(x,A) - (3*x).subs(x,A) + 5*(eye(2))

This will evaluate your expression.这将评估您的表达式。

To evaluate P at A , you can substitute x with a Matrix and convert the constant term by multiplying by eye(2) :要在A处评估P ,您可以用Matrix替换x并通过乘以eye(2)转换常数项:

P_ = Poly(P, x)
(P_ - P_.coeff_monomial(1)).as_expr().subs(x, A) * eye(2) + P_.coeff_monomial(1) * eye(2) # P(A)

The first multiplication by eye(2) ensures the first term in the sum is a matrix, even if P is just a constant, ie P_ - P_.coeff_monomial(1) == 0 .第一次乘以eye(2)确保总和中的第一项是矩阵,即使P只是一个常数,即P_ - P_.coeff_monomial(1) == 0

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

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