solving matrix equations

610 views
Skip to first unread message

mb

unread,
Feb 7, 2009, 12:45:54 PM2/7/09
to sage-support
Hi,

Say I want to compute the centralizer of a matrix.

sage: P.<a,b,c,d>=PolynomialRing(QQ)
sage: A=matrix(P,2,2,[2,1,1,1])
sage: B=matrix(P,2,2,[a,b,c,d])
sage: C=A*B-B*A
sage: C

[ -b + c -a + b + d]
[ a - c - d b - c]
sage: var('a b c d')
(a, b, c, d)
sage: solve([-b + c==0,-a + b + d==0,a - c - d==0,b - c==0],[a,b,c,d])
[[a == r2 + r1, b == r2, c == r2, d == r1]]

Here I had to manually copy the entries of C into solve, which is a
problem for larger matrices. Ideally, something like

solve(C==0,[a,b,c,d])

should work, but of course it doesn't. Is there any way of doing this?

Mladen

Craig Citro

unread,
Feb 7, 2009, 1:31:08 PM2/7/09
to sage-s...@googlegroups.com
Hi,

Yep, there are definitely easy ways of doing this. Here's one way:

sage: var('a b c d')
(a, b, c, d)
sage: A = matrix(2,[2,1,1,1])
sage: B = matrix(2,[a,b,c,d])
sage: C = A*B - B*A

sage: [ e == 0 for e in C.list() ]
[c - b == 0, d + b - a == 0, -d - c + a == 0, b - c == 0]

sage: solve([ e == 0 for e in C.list() ], N.list())
[[a == r2 + r1, b == r2, c == r2, d == r1]]

-cc

Jason Grout

unread,
Feb 7, 2009, 2:19:55 PM2/7/09
to sage-s...@googlegroups.com
Craig Citro wrote:
> Hi,
>
> Yep, there are definitely easy ways of doing this. Here's one way:
>
> sage: var('a b c d')
> (a, b, c, d)
> sage: A = matrix(2,[2,1,1,1])
> sage: B = matrix(2,[a,b,c,d])
> sage: C = A*B - B*A
>
> sage: [ e == 0 for e in C.list() ]
> [c - b == 0, d + b - a == 0, -d - c + a == 0, b - c == 0]
>
> sage: solve([ e == 0 for e in C.list() ], N.list())
> [[a == r2 + r1, b == r2, c == r2, d == r1]]

Just to point out for those that need a guide through the above, Craig
is basically creating a list of equations from each entry in the matrix.
He doesn't show it, but I suppose that N is a matrix of the variables.
You could also do:

solve([ e == 0 for e in C.list() ], C.variables())

That said, I think it would be a great thing if solve could recognize
matrices and that two matrices are equal if each entry is equal. I
believe MMA does this (but it's easier there; matrices are nothing more
than nested lists). It'd certainly make certain things I do more
natural if I could do:

solve(matrixA==matrixB)

and that was equivalent to:

solve([i==j for i,j in zip(matrixA.list(), matrixB.list())])

if the matrices were of the same dimensions.

Okay, so now that I've written my piece, I suppose the next step is to
open a trac ticket, write a patch to implement it, and post it for
review :).

The ticket is http://trac.sagemath.org/sage_trac/ticket/5201

I won't cry if someone submits a patch before I get to it :).

Jason

mb

unread,
Feb 7, 2009, 3:15:16 PM2/7/09
to sage-support
Thanks to both of you!

Mladen
> The ticket ishttp://trac.sagemath.org/sage_trac/ticket/5201
Reply all
Reply to author
Forward
0 new messages