# Evaluate polynomial expression on matrices

How can I construct a map $M_2(\mathbb Q)^3 \to M_2(\mathbb Q), (A,B,C) \mapsto (AB-BA)C$ where $M_2(\mathbb Q)$ denotes the space of 2x2 matrices.

sage: x,y,z = var('x y z')
sage: f=(x*y-y*x)*z
sage: f
0

edit retag close merge delete

1

Like this:

def f(A,B,C):
return (A*B-B*A)*C

( 2019-02-28 09:07:20 +0200 )edit
1

sage: sage: x,y,z = var('x y z')
....: sage: f=(x*y-y*x)*z
....: sage: f
....:
0
sage: f.parent()
Symbolic Ring
sage: [u.parent() for u in [x,y,z]]
[Symbolic Ring, Symbolic Ring, Symbolic Ring]


This means that f is a symbolic function, whose arguments are symbolic variables. Without further assumptions, Sage will treat the value of f as an expression involving members of the symbolic ring, which is roughly equivalent to treat them as complexes. This implies that multiplication is commutative, hence your result.

Frederic's solution will indeed work, because Sage will not attempt to simplify your code for you, and won't simplify x*y-y*x as 0.

( 2019-02-28 10:40:21 +0200 )edit

Thanks for the clarification!

( 2019-02-28 18:01:04 +0200 )edit

Sort by » oldest newest most voted

You can define a function as shown in the comments.

Alternatively you can substitute into a noncommutative polynomial, e.g.:

R.<x,y,z> = FreeAlgebra(QQ, 3)
f = (x*y - y*x)*z # not zero
A = Matrix(QQ, [[0,1],[0,0]])
B = Matrix(QQ, [[1,1],[1,1]])
C = Matrix(QQ, [[0,-1],[-1,0]])
f.subs({x: A, y: B, z: C})

more

1

Nice ! Didn't think of that...

It turns out that you can even use a "function-call" syntax. With a slight change of notations :

sage: R.<a,b,c> = FreeAlgebra(QQbar, 3)
sage: f=(a*b-b*a)*c
sage: f
a*b*c - b*a*c
sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:A = Matrix(QQ, [[0,1],[0,0]])
:B = Matrix(QQ, [[1,1],[1,1]])
:C = Matrix(QQ, [[0,-1],[-1,0]])
:--
sage: f(A,B,C)
[ 0 -1]
[ 1  0]


which is equivalent (and faster to type) to :

sage: f.subs({a:A, b:B, c:C})
[ 0 -1]
[ 1  0]

( 2019-02-28 13:25:08 +0200 )edit