ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 19 May 2016 04:06:31 +0200How to define a polynomial which can take matrix ?https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/ I want to define a matrix valued function. for example..
If I have a polynomial of a matrix with me say $f(x)$ and I want to check $f(A)$. What can be done better?
The following will work for numbers but not for matrix.
var('x')
f(x)=2x^2+x+3
print f(A)# this is what I want as an answer..
**
In the above if I want to replace my x by a matrix what I have to do?
----------------------------------------
**
So, ultimate aim is to find define a polynomial which can take matrix
Thanks in advance...
Wed, 18 May 2016 20:33:49 +0200https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/Answer by tmonteil for <p>I want to define a matrix valued function. for example..</p>
<p>If I have a polynomial of a matrix with me say $f(x)$ and I want to check $f(A)$. What can be done better?
The following will work for numbers but not for matrix.</p>
<pre><code>var('x')
f(x)=2x^2+x+3
print f(A)# this is what I want as an answer..
</code></pre>
<p>**</p>
<h2>In the above if I want to replace my x by a matrix what I have to do?</h2>
<p>**</p>
<p>So, ultimate aim is to find define a polynomial which can take matrix
Thanks in advance...</p>
https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/?answer=33442#post-id-33442You should work with polynomials, not symbolic expressions.
Here is an example, let us assume that your matrix is defined over the field `QQ`:
sage: A = random_matrix(QQ,3)
sage: A
[ 2 0 -2]
[ 2 2 0]
[-1/2 0 0]
Then you can define the polynomial ring with one variable over the rationals, and define your polynomial:
sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: f = 2*x^2 + x + 3
sage: f.parent()
Univariate Polynomial Ring in x over Rational Field
And apply it to your matrix:
sage: f(A)
[ 15 0 -10]
[ 18 13 -8]
[-5/2 0 5]
Wed, 18 May 2016 20:52:03 +0200https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/?answer=33442#post-id-33442Comment by daviddgl for <p>You should work with polynomials, not symbolic expressions.</p>
<p>Here is an example, let us assume that your matrix is defined over the field <code>QQ</code>:</p>
<pre><code>sage: A = random_matrix(QQ,3)
sage: A
[ 2 0 -2]
[ 2 2 0]
[-1/2 0 0]
</code></pre>
<p>Then you can define the polynomial ring with one variable over the rationals, and define your polynomial:</p>
<pre><code>sage: R.<x> = PolynomialRing(QQ)
sage: R
Univariate Polynomial Ring in x over Rational Field
sage: f = 2*x^2 + x + 3
sage: f.parent()
Univariate Polynomial Ring in x over Rational Field
</code></pre>
<p>And apply it to your matrix:</p>
<pre><code>sage: f(A)
[ 15 0 -10]
[ 18 13 -8]
[-5/2 0 5]
</code></pre>
https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/?comment=33446#post-id-33446Thank you so much..Thu, 19 May 2016 04:06:31 +0200https://ask.sagemath.org/question/33441/how-to-define-a-polynomial-which-can-take-matrix/?comment=33446#post-id-33446