# Applying a polynomial to a matrix

In the quickref for linear algebra, it is writen that, if f(x) = x^2 + 5*x+3 then f(B) is possible.

I have tried

B=matrix([[1/2,2/3],[1/2,1/3]])
f(B)


but it returns an error when B^2 + 5*B + 3 returns

[34 31 33 27]
[31 34 32 28]
[27 25 43 30]
[32 32 31 30]


I'm wondering?

edit retag close merge delete

Sort by ยป oldest newest most voted
R.<x>=QQ[]
p = x^2 + 5*x+3
B=matrix([[1/2,2/3],[1/2,1/3]])
p(B)

[73/12  35/9]
[35/12  46/9]

more

Ok but the quick ref must be corrected

( 2023-01-23 10:43:12 +0200 )edit

FWIW :

sage: f(x) = x^2 + 5*x+3


Note : this is a symbolic function",more properly called a *callable symbolic expression whose components must be symbolic expressions.

sage: B=matrix([[1/2,2/3],[1/2,1/3]])  ; B
[1/2 2/3]
[1/2 1/3]


Note : B is a matrix, which is not a symbolic expression. Demonstration

sage: f(B)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /usr/local/sage-9/src/sage/symbolic/expression.pyx:3804, in sage.symbolic.expression.Expression.coerce_in()
3803 try:
-> 3804     return <Expression?>z
3805 except TypeError:

TypeError: Cannot convert sage.matrix.matrix_rational_dense.Matrix_rational_dense to sage.symbolic.expression.Expression


Q. E. D.

Explanation later in the same sequence :

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In [76], line 1
----> 1 f(B)

File /usr/local/sage-9/src/sage/symbolic/expression.pyx:6182, in sage.symbolic.expression.Expression.__call__()
6180         z^2 + x^y
6181     """
-> 6182     return self._parent._call_element_(self, *args, **kwds)
6183
6184 def variables(self):

File /usr/local/sage-9/src/sage/symbolic/callable.py:476, in CallableSymbolicExpressionRing_class._call_element_(self, _the_element, *args, **kwds)
474 d = dict(zip([repr(_) for _ in self.arguments()], args))
475 d.update(kwds)
--> 476 return SR(_the_element.substitute(**d))

File /usr/local/sage-9/src/sage/symbolic/expression.pyx:5891, in sage.symbolic.expression.Expression.substitute()
5889 for k, v in sdict.iteritems():
5890     smap.insert(make_pair((<Expression>self.coerce_in(k))._gobj,
-> 5891                           (<Expression>self.coerce_in(v))._gobj))
5892 res = self._gobj.subs_map(smap, 0)
5893 return new_Expression_from_GEx(self._parent, res)

File /usr/local/sage-9/src/sage/symbolic/expression.pyx:3806, in sage.symbolic.expression.Expression.coerce_in()
3804         return <Expression?>z
3805     except TypeError:
-> 3806         return self._parent.coerce(z)
3807

File /usr/local/sage-9/src/sage/structure/parent.pyx:1213, in sage.structure.parent.Parent.coerce()
1211         except Exception:
1212             _record_exception()
-> 1213     raise TypeError(_LazyString("no canonical coercion from %s to %s", (parent(x), self), {}))
1214 else:
1215     return (<map.Map>mor)._call_(x)

TypeError: no canonical coercion from Full MatrixSpace of 2 by 2 dense matrices over Rational Field to Callable function ring with argument x


But (proper, Python) functions of B are possible :

sage: def g(x): return x^2+5*x+3
sage: g(B)
[73/12  35/9]
[35/12  46/9]


Q. E. D. again...

Unless you have meant :

sage: B.apply_map(lambda u:f(u))
[23/4 61/9]
[23/4 43/9]


which is something else entirely...

HTH,

more