ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 18 Jul 2019 18:11:57 +0200Concatenate two sparse matrices over a Polynomial Ringhttps://ask.sagemath.org/question/47205/concatenate-two-sparse-matrices-over-a-polynomial-ring/ Hi all,
I am wondering if there is a way to concatenate two sparse matrices over a polynomial ring. Here is a toy example that I am testing with:
S.<x,y> = PolynomialRing(QQ,2)
udict = {(0,0):1, (0,1):x, (1,1):y}
vdict = {(0,0):2*x,(1,0):y}
U = matrix(4,4,ud,sparse=True)
V = matrix(4,2,vd,sparse=True)
I would like to make a larger matrix that is $4 \times 6$, that has the block form $[U|V]$. I attempted to use the block_matrix command, but it said that the given number of rows and columns were incompatible with the number of submatrices.
In Python, for a sparse matrix (csr_matrix or coo_matrix), there is the command hstack from scipy.sparse. Is there a similar command where I can concatenate a list of these sparse matrices? This would be ideal for my general problem.
Thank you for your time!Thu, 18 Jul 2019 14:00:49 +0200https://ask.sagemath.org/question/47205/concatenate-two-sparse-matrices-over-a-polynomial-ring/Answer by FrédéricC for <p>Hi all,</p>
<p>I am wondering if there is a way to concatenate two sparse matrices over a polynomial ring. Here is a toy example that I am testing with:</p>
<pre><code>S.<x,y> = PolynomialRing(QQ,2)
udict = {(0,0):1, (0,1):x, (1,1):y}
vdict = {(0,0):2*x,(1,0):y}
U = matrix(4,4,ud,sparse=True)
V = matrix(4,2,vd,sparse=True)
</code></pre>
<p>I would like to make a larger matrix that is $4 \times 6$, that has the block form $[U|V]$. I attempted to use the block_matrix command, but it said that the given number of rows and columns were incompatible with the number of submatrices.</p>
<p>In Python, for a sparse matrix (csr_matrix or coo_matrix), there is the command hstack from scipy.sparse. Is there a similar command where I can concatenate a list of these sparse matrices? This would be ideal for my general problem.</p>
<p>Thank you for your time!</p>
https://ask.sagemath.org/question/47205/concatenate-two-sparse-matrices-over-a-polynomial-ring/?answer=47209#post-id-47209Like that
sage: U.augment(V)
[ 1 x 0 0 2*x 0]
[ 0 y 0 0 y 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]
Thu, 18 Jul 2019 18:11:57 +0200https://ask.sagemath.org/question/47205/concatenate-two-sparse-matrices-over-a-polynomial-ring/?answer=47209#post-id-47209