# Revision history [back]

Hello,

It is sad that in Sage, it is not yet possible to play with the vector space of matrices. One workaround is to use tensorial product which does exactly what you want. Let us take the following example

sage: A = matrix([[0,0,1],[1,1,0],[1,0,-3]])
sage: B = matrix([[1,0,-1],[0,0,1],[0,2,2]])

sage: AA = A.tensor_product(identity_matrix(3))
sage: BB = identity_matrix(3).tensor_product(B.transpose())


The matrices AA and BB are 9x9 matrices that correspond to matrix multiplication seen on vectors:

sage: print AA
[ 0  0  0| 0  0  0| 1  0  0]
[ 0  0  0| 0  0  0| 0  1  0]
[ 0  0  0| 0  0  0| 0  0  1]
[--------+--------+--------]
[ 1  0  0| 1  0  0| 0  0  0]
[ 0  1  0| 0  1  0| 0  0  0]
[ 0  0  1| 0  0  1| 0  0  0]
[--------+--------+--------]
[ 1  0  0| 0  0  0|-3  0  0]
[ 0  1  0| 0  0  0| 0 -3  0]
[ 0  0  1| 0  0  0| 0  0 -3]


You can check

sage: X = matrix([[1,0,0],[1,1,1],[1,2,-1]])
sage: v = vector(X.list())
sage: (A*X).list() == (AA*v).list()
True
sage: (X*B).list() == (BB*v).list()
True


Then you can solve your problem with standard linear algebra

sage: C = AA - BB
sage: V = [matrix(3,3,v.list()) for v in C.right_kernel().basis()]
sage: m = V[0]
sage: print m
[0 0 0]
[3 2 1]
[0 0 0]
sage: A * m == m * B
True


Vincent

Hello,

It is sad that in Sage, it is not yet possible to play with the vector space of matrices. One workaround is to use tensorial product which does exactly what you want. Let us take the following example

sage: A = matrix([[0,0,1],[1,1,0],[1,0,-3]])
sage: B = matrix([[1,0,-1],[0,0,1],[0,2,2]])

sage: AA = A.tensor_product(identity_matrix(3))
sage: BB = identity_matrix(3).tensor_product(B.transpose())


The matrices AA and BB are 9x9 matrices that correspond to matrix multiplication seen on vectors:

sage: print AA
[ 0  0  0| 0  0  0| 1  0  0]
[ 0  0  0| 0  0  0| 0  1  0]
[ 0  0  0| 0  0  0| 0  0  1]
[--------+--------+--------]
[ 1  0  0| 1  0  0| 0  0  0]
[ 0  1  0| 0  1  0| 0  0  0]
[ 0  0  1| 0  0  1| 0  0  0]
[--------+--------+--------]
[ 1  0  0| 0  0  0|-3  0  0]
[ 0  1  0| 0  0  0| 0 -3  0]
[ 0  0  1| 0  0  0| 0  0 -3]


You can check

sage: X = matrix([[1,0,0],[1,1,1],[1,2,-1]])
sage: v = vector(X.list())
sage: (A*X).list() == (AA*v).list()
True
sage: (X*B).list() == (BB*v).list()
True


Then you can solve your problem with standard linear algebra

sage: C = AA - BB
sage: V = [matrix(3,3,v.list()) for v in C.right_kernel().basis()]
sage: m = V[0]
sage: print m
[0 0 0]
[3 2 1]
[0 0 0]
sage: A * m == m * B
True


Of course you still have to play with the basis to find an invertible solution... but it might not exists as in my example above.

Vincent