The following worked for me:
(1) Using the polynomial (only algebraic expressions) $P$ in $x$ as a true polynomial over the base ring / field of the matrix $A$, in the following sample it is $\Bbb Z$:
R.<x> = PolynomialRing(ZZ) # or also R.<x> = ZZ[]
P = x^2007 + 4*x + 1
A = matrix(2, 2, [0, -1, 1, 1])
P(A)
This gives:
sage: P(A)
[ 0 -4]
[ 4 4]
sage: A^2007 + 4*A + 1
[ 0 -4]
[ 4 4]
(2) Using a sage function $P$ of the argument $x$:
def P(x):
return x^2007 + 4*x + 1
A = matrix(2, 2, [0, -1, 1, 1])
P(A)
This gives the same result.
(3) Using an expression (which in a more general setting may contain $\sin$, $\log$, $\exp$, ... but for the code below should not...) in the variable $x$, well we cheat and build the polynomial for $f$, we are now in the case $A$, then plug in the matrix $A$:
x = var('x')
A = matrix(2, 2, [0, -1, 1, 1])
f = x^2007 + 4*x + 1
f.polynomial(ZZ)(A)
Same result.
Could you please provide the code to construct
P
,x
andA
so that we can reproduce your issue ?