# Multiplying matrices with different parents?

I want to conjugate a symbolic matrix, Sigma, by a matrix, garbage, over Z/9Z. If I define both matrices as symbolic matrices, I get the right answer. If I define garbage over Z/9Z, I get confusing answers. Can anyone explain my results?

Sigma=matrix(SR,2,[[1+3*A,3*B],[3*C,1+3*D]])
garbage=matrix(SR,2,[[2,1],[2,6]]);garbageinverse=matrix(SR,2,[[6,8],[7,2]])
expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
R=Integers(9)
garbage=matrix(R,2,[[2,1],[2,6]]);garbageinverse=matrix(R,2,[[6,8],[7,2]])
expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]

[  36*A + 42*B + 18*C + 21*D + 19    48*A + 12*B + 24*C + 6*D + 18]
[36*A + 42*B + 108*C + 126*D + 54  48*A + 12*B + 144*C + 36*D + 28]
36*A + 42*B + 108*C + 126*D + 54
[        6*B + 3*D + 1 3*A + 3*B + 6*C + 6*D]
[            0*A + 0*D         3*A + 0*D + 1]
6*B + 0*D

edit retag close merge delete

Sort by » oldest newest most voted

There is indeed something wrong with how the Symbolic Ring deals with integer modulo 9.

Here is the actual issue:

sage: _ = var('A,B')
sage: (A + 3*B)*Zmod(9)(6)
0*B


while the result should be 6*A. Thanks for pointing this issue, i opened trac ticket 18787.

What is even more weird is that i found some non-determinism, since i once get the following result without being able to reproduce (on Sage 6.8.beta5):

sage: var('A,B,C,D')
(A, B, C, D)
sage: Sigma=matrix(SR,2,[[1+3*A,3*B],[3*C,1+3*D]])
sage: garbage=matrix(SR,2,[[2,1],[2,6]]);garbageinverse=matrix(SR,2,[[6,8],[7,2]])
sage: expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
[  36*A + 42*B + 18*C + 21*D + 19    48*A + 12*B + 24*C + 6*D + 18]
[36*A + 42*B + 108*C + 126*D + 54  48*A + 12*B + 144*C + 36*D + 28]
36*A + 42*B + 108*C + 126*D + 54
sage: R=Integers(9)
sage: garbage=matrix(R,2,[[2,1],[2,6]]);garbageinverse=matrix(R,2,[[6,8],[7,2]])
sage: expand(garbage*Sigma*garbageinverse);(Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
[        6*B + 3*D + 1 3*A + 3*B + 6*C + 6*D]
[            0*A + 6*B         3*A + 3*B + 1]
6*B + 0*D


That said, for such algebraic questions, i would recommend to work on well defined rings:

sage: R.<a,b,c,d> = PolynomialRing(Integers(9),4)
sage: R
Multivariate Polynomial Ring in a, b, c, d over Ring of integers modulo 9
sage: Sigma = matrix(R,2,[[1+3*a,3*b],[3*c,1+3*d]])
sage: garbage = matrix(R,2,[[2,1],[2,6]])
sage: garbageinverse=matrix(R,2,[[6,8],[7,2]])
sage: garbage*Sigma*garbageinverse
[        6*b + 3*d + 1 3*a + 3*b + 6*c + 6*d]
[                  6*b         3*a + 3*b + 1]
sage: (Sigma*garbageinverse)[0,0]*garbage[1,0]+(Sigma*garbageinverse)[1,0]*garbage[1,1]
6*b

more

1

Thank you for looking into this for me! As a new user, I'm glad to know it wasn't something ridiculous on my part. Also, thanks for the suggestion to use the polynomial ring. That will get me through what I need to do.

( 2015-06-24 16:16:15 -0500 )edit

Thierry, if you can get any smaller examples of this please do open a ticket. But I agree that in general SR is ill-equipped to handle modular stuff, because SR sort of implicitly assumes characteristic zero I think.

( 2015-06-24 22:41:02 -0500 )edit

Pynac has some code that takes the characteristic into account but I have no idea how complete it is.

( 2015-06-25 00:47:57 -0500 )edit

I could not find a smaller example since the direct computation of the coefficients is OK, so there is something with matrices.

( 2015-06-25 06:09:17 -0500 )edit

But Jeroen did !

( 2015-06-25 07:21:44 -0500 )edit

@tmonteil's answer says it all. What I have to add is more a comment than an answer.

For a question on ask-sage, why not use simpler names and provide readable code?

sage: Z9 = Zmod(9)
sage: R.<a,b,c,d> = PolynomialRing(Z9)
sage: m = matrix(R, 2, [[1+3*a,3*b],[3*c,1+3*d]])
sage: g = matrix(R, 2,[[2,1],[2,6]])
sage: gi = matrix(R, 2,[[6,8],[7,2]]) # this is g inverse
sage: g * gi
[1 0]
[0 1]
sage: g * m * gi
[        6*b + 3*d + 1 3*a + 3*b + 6*c + 6*d]
[                  6*b         3*a + 3*b + 1]
sage: (m * gi)[0,0] * g[1,0] + (m * gi)[1,0] * g[1,1]
6*b

more

I think the answer to that is that new users of this (or any) Q&A site may not yet be accustomed to the local customs.

( 2015-06-25 08:20:00 -0500 )edit

Ignore that, you are talking about the answer. Agreed, but perhaps T was just trying to match the OP's names as much as possible - but yours is undoubtedly clearer :)

( 2015-06-25 08:22:40 -0500 )edit