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.Tue, 30 Sep 2014 03:29:27 +0200Applying a two-variable polynomial to matriceshttps://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/A naive question:
I have in sage two commuting square matrices X and Y over a ring R, and a polynomial P
in two variables x and y over R. How can I ask sage to compute P(X,Y)?
Thanks.
Sun, 27 Nov 2011 22:25:08 +0100https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/Answer by DSM for <p>A naive question:
I have in sage two commuting square matrices X and Y over a ring R, and a polynomial P
in two variables x and y over R. How can I ask sage to compute P(X,Y)?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?answer=12946#post-id-12946Couldn't you simply substitute X and Y into the polynomial P as the values of x and y? (Throwing worries about ordering to the wind because we have that they commute.)
sage: # set things up
sage: R.<x,y> = QQ[]
sage: P = R.random_element(10,12)
sage: M = MatrixSpace(QQ, 5)
sage: X = M.random_element()
sage: Y = M.random_element()
sage:
sage: # check the inputs
sage: P
-x^3*y^7 + 1/2*x^2*y^6 + 1/2*x^7 - 13*x^5 - 1/2*y^5 + 2*x*y^2 + y^3
sage: X
[ 1/2 0 -1 0 -1/2]
[ -2 0 -1/2 1 -1/2]
[ -1 -2 -2 0 -2]
[ 1 2 0 -2 2]
[ 2 1 -1 0 0]
sage: Y
[ 0 -2 0 1 -2]
[ 0 0 0 0 -1]
[ -1 -1 0 -1 2]
[ 0 -2 -2 -2 1]
[ -1 -1 1/2 0 0]
sage:
sage: parent(P)
Multivariate Polynomial Ring in x, y over Rational Field
sage:
sage: # do the substitution
sage: q = P.subs(x=X, y=Y)
sage: q
[-170339/256 221009/128 337381/128 90749/64 -27463/256]
[-131901/128 461983/64 574233/64 226241/32 -367357/128]
[-339165/128 441951/64 663751/64 184439/32 -89985/128]
[ 167421/128 -917935/64 -1076103/64 -456607/32 823585/128]
[ 8761/64 -3571/32 -6061/32 -815/8 3597/64]
sage: parent(q)
Full MatrixSpace of 5 by 5 dense matrices over Rational Field
sage: parent(q) is M
True
sage:
sage: # sanity check that it's what we expected
sage: q2 = sage_eval(str(P).replace('x','X').replace('y','Y'),locals=locals())
sage: q == q2
TrueSun, 27 Nov 2011 23:05:09 +0100https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?answer=12946#post-id-12946Comment by DSM for <p>Couldn't you simply substitute X and Y into the polynomial P as the values of x and y? (Throwing worries about ordering to the wind because we have that they commute.)</p>
<pre><code>sage: # set things up
sage: R.<x,y> = QQ[]
sage: P = R.random_element(10,12)
sage: M = MatrixSpace(QQ, 5)
sage: X = M.random_element()
sage: Y = M.random_element()
sage:
sage: # check the inputs
sage: P
-x^3*y^7 + 1/2*x^2*y^6 + 1/2*x^7 - 13*x^5 - 1/2*y^5 + 2*x*y^2 + y^3
sage: X
[ 1/2 0 -1 0 -1/2]
[ -2 0 -1/2 1 -1/2]
[ -1 -2 -2 0 -2]
[ 1 2 0 -2 2]
[ 2 1 -1 0 0]
sage: Y
[ 0 -2 0 1 -2]
[ 0 0 0 0 -1]
[ -1 -1 0 -1 2]
[ 0 -2 -2 -2 1]
[ -1 -1 1/2 0 0]
sage:
sage: parent(P)
Multivariate Polynomial Ring in x, y over Rational Field
sage:
sage: # do the substitution
sage: q = P.subs(x=X, y=Y)
sage: q
[-170339/256 221009/128 337381/128 90749/64 -27463/256]
[-131901/128 461983/64 574233/64 226241/32 -367357/128]
[-339165/128 441951/64 663751/64 184439/32 -89985/128]
[ 167421/128 -917935/64 -1076103/64 -456607/32 823585/128]
[ 8761/64 -3571/32 -6061/32 -815/8 3597/64]
sage: parent(q)
Full MatrixSpace of 5 by 5 dense matrices over Rational Field
sage: parent(q) is M
True
sage:
sage: # sanity check that it's what we expected
sage: q2 = sage_eval(str(P).replace('x','X').replace('y','Y'),locals=locals())
sage: q == q2
True
</code></pre>
https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=20809#post-id-20809@Joel B. Cool, glad that helped!Mon, 28 Nov 2011 11:02:20 +0100https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=20809#post-id-20809Comment by nbruin for <p>Couldn't you simply substitute X and Y into the polynomial P as the values of x and y? (Throwing worries about ordering to the wind because we have that they commute.)</p>
<pre><code>sage: # set things up
sage: R.<x,y> = QQ[]
sage: P = R.random_element(10,12)
sage: M = MatrixSpace(QQ, 5)
sage: X = M.random_element()
sage: Y = M.random_element()
sage:
sage: # check the inputs
sage: P
-x^3*y^7 + 1/2*x^2*y^6 + 1/2*x^7 - 13*x^5 - 1/2*y^5 + 2*x*y^2 + y^3
sage: X
[ 1/2 0 -1 0 -1/2]
[ -2 0 -1/2 1 -1/2]
[ -1 -2 -2 0 -2]
[ 1 2 0 -2 2]
[ 2 1 -1 0 0]
sage: Y
[ 0 -2 0 1 -2]
[ 0 0 0 0 -1]
[ -1 -1 0 -1 2]
[ 0 -2 -2 -2 1]
[ -1 -1 1/2 0 0]
sage:
sage: parent(P)
Multivariate Polynomial Ring in x, y over Rational Field
sage:
sage: # do the substitution
sage: q = P.subs(x=X, y=Y)
sage: q
[-170339/256 221009/128 337381/128 90749/64 -27463/256]
[-131901/128 461983/64 574233/64 226241/32 -367357/128]
[-339165/128 441951/64 663751/64 184439/32 -89985/128]
[ 167421/128 -917935/64 -1076103/64 -456607/32 823585/128]
[ 8761/64 -3571/32 -6061/32 -815/8 3597/64]
sage: parent(q)
Full MatrixSpace of 5 by 5 dense matrices over Rational Field
sage: parent(q) is M
True
sage:
sage: # sanity check that it's what we expected
sage: q2 = sage_eval(str(P).replace('x','X').replace('y','Y'),locals=locals())
sage: q == q2
True
</code></pre>
https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=24332#post-id-24332Equivalently, you can just "evaluate" the polynomial at the matrices by calling P(X,Y) . This is potentially even a little more efficient than P.subs(x=X,y=Y) because it is clear from the start that it is not a partial evaluation, e.g., P(x=X)(y=Y) gives a rather incomprehensible result by comparison.Tue, 30 Sep 2014 03:29:27 +0200https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=24332#post-id-24332Comment by Joel B. for <p>Couldn't you simply substitute X and Y into the polynomial P as the values of x and y? (Throwing worries about ordering to the wind because we have that they commute.)</p>
<pre><code>sage: # set things up
sage: R.<x,y> = QQ[]
sage: P = R.random_element(10,12)
sage: M = MatrixSpace(QQ, 5)
sage: X = M.random_element()
sage: Y = M.random_element()
sage:
sage: # check the inputs
sage: P
-x^3*y^7 + 1/2*x^2*y^6 + 1/2*x^7 - 13*x^5 - 1/2*y^5 + 2*x*y^2 + y^3
sage: X
[ 1/2 0 -1 0 -1/2]
[ -2 0 -1/2 1 -1/2]
[ -1 -2 -2 0 -2]
[ 1 2 0 -2 2]
[ 2 1 -1 0 0]
sage: Y
[ 0 -2 0 1 -2]
[ 0 0 0 0 -1]
[ -1 -1 0 -1 2]
[ 0 -2 -2 -2 1]
[ -1 -1 1/2 0 0]
sage:
sage: parent(P)
Multivariate Polynomial Ring in x, y over Rational Field
sage:
sage: # do the substitution
sage: q = P.subs(x=X, y=Y)
sage: q
[-170339/256 221009/128 337381/128 90749/64 -27463/256]
[-131901/128 461983/64 574233/64 226241/32 -367357/128]
[-339165/128 441951/64 663751/64 184439/32 -89985/128]
[ 167421/128 -917935/64 -1076103/64 -456607/32 823585/128]
[ 8761/64 -3571/32 -6061/32 -815/8 3597/64]
sage: parent(q)
Full MatrixSpace of 5 by 5 dense matrices over Rational Field
sage: parent(q) is M
True
sage:
sage: # sanity check that it's what we expected
sage: q2 = sage_eval(str(P).replace('x','X').replace('y','Y'),locals=locals())
sage: q == q2
True
</code></pre>
https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=20810#post-id-20810Thank you very much. I didn't know the method "subs". Mon, 28 Nov 2011 10:17:48 +0100https://ask.sagemath.org/question/8487/applying-a-two-variable-polynomial-to-matrices/?comment=20810#post-id-20810