如何将变量定义为圣人中的矩阵?

How to define the variable as a matrix in sage?

我想定义一个处理矩阵的函数..

如果我有一个矩阵的特征多项式并且我想检查凯莱哈密尔顿定理.. 有什么可以做得更好?

var('x')
f(x)=2x^2+x+3 # this the characteristic polynomial of $A$ (say)
print f(A)# this is what I want as an answer..

在上面如果我想用矩阵替换我的 x 我必须做什么?

所以,最终目的是找到定义一个可以取矩阵的多项式 提前致谢...

令人惊奇的是,尽管已经提到过 six years ago,但显然这个问题并没有经常出现,所以我们还没有修复它。

sage: M = matrix([[1,2],[3,4]])
sage: g(x) = x^2-5*x-2
sage: g(M)
TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Integer Ring to Callable function ring with argument x

(至少为此做点什么是 Trac 15487。)

但是,请尝试 using this trick。问题仅在于符号表达式,而不是多项式。

sage: M = matrix([[1,2],[3,4]])
sage: f = M.charpoly()
sage: f.subs(x=M)
[0 0] 
[0 0] 

编辑:一般来说,尝试一下 like this

M = matrix([[1,2],[3,4]])
R.<t> = PolynomialRing(SR)
f = t^2+t+1
f(M)