ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 17 Jul 2019 14:27:49 -0500Matrix multiplication of sparse matrix with matrix over polynomial ringhttps://ask.sagemath.org/question/47196/matrix-multiplication-of-sparse-matrix-with-matrix-over-polynomial-ring/ Hi all,
I would like to know how to multiply the following matrices I have constructed in Sage. Here is my code for a toy example:
from scipy.sparse import csr_matrix
row = [0,0,1]
col = [0,1,1]
data = [1,-1,1]
A = csr_matrix((data,(row,col)),shape=(4,4))
R.<x,y> = PolynomialRing(QQ,2)
B = matrix(R,2,2,[1,x,1,y])
When I try to multiply the matrices using A.dot(B), I end up with the error: "No supported conversion for types: (dtype('int64'), dtype('O'))"
Is there a way I can multiply these matrices and yield an output that is a csr_matrix? (I suspect I would want to convert the entries of my A matrix to be the same data type, but I am unaware of how to do this)
Thank you for your time!
Wed, 17 Jul 2019 10:49:28 -0500https://ask.sagemath.org/question/47196/matrix-multiplication-of-sparse-matrix-with-matrix-over-polynomial-ring/Answer by vdelecroix for <p>Hi all,</p>
<p>I would like to know how to multiply the following matrices I have constructed in Sage. Here is my code for a toy example:</p>
<pre><code>from scipy.sparse import csr_matrix
row = [0,0,1]
col = [0,1,1]
data = [1,-1,1]
A = csr_matrix((data,(row,col)),shape=(4,4))
R.<x,y> = PolynomialRing(QQ,2)
B = matrix(R,2,2,[1,x,1,y])
</code></pre>
<p>When I try to multiply the matrices using A.dot(B), I end up with the error: "No supported conversion for types: (dtype('int64'), dtype('O'))"</p>
<p>Is there a way I can multiply these matrices and yield an output that is a csr_matrix? (I suspect I would want to convert the entries of my A matrix to be the same data type, but I am unaware of how to do this)</p>
<p>Thank you for your time!</p>
https://ask.sagemath.org/question/47196/matrix-multiplication-of-sparse-matrix-with-matrix-over-polynomial-ring/?answer=47197#post-id-47197The scipy `csr_format` does not support custom datatypes such as Sage polynomials. You should use Sage matrices alone. If you want your matrix to be sparse, you can use
sage: row = [0,0,1]
sage: col = [0,1,1]
sage: data = [1,-1,1]
sage: data_for_sage = {(i,j):v for i,j,v in zip(row,col,data)}
sage: matrix(4, data_for_sage, sparse=True)
[ 1 -1 0]
[ 0 1 0]
[ 0 0 0]Wed, 17 Jul 2019 14:27:49 -0500https://ask.sagemath.org/question/47196/matrix-multiplication-of-sparse-matrix-with-matrix-over-polynomial-ring/?answer=47197#post-id-47197