Sparse Matrices over a general ring¶
EXAMPLES:
sage: R.<x> = PolynomialRing(QQ)
sage: M = MatrixSpace(QQ['x'],2,3,sparse=True); M
Full MatrixSpace of 2 by 3 sparse matrices over Univariate Polynomial Ring in x over Rational Field
sage: a = M(range(6)); a
[0 1 2]
[3 4 5]
sage: b = M([x^n for n in range(6)]); b
[ 1 x x^2]
[x^3 x^4 x^5]
sage: a * b.transpose()
[ 2*x^2 + x 2*x^5 + x^4]
[ 5*x^2 + 4*x + 3 5*x^5 + 4*x^4 + 3*x^3]
sage: pari(a)*pari(b.transpose())
[2*x^2 + x, 2*x^5 + x^4; 5*x^2 + 4*x + 3, 5*x^5 + 4*x^4 + 3*x^3]
sage: c = copy(b); c
[ 1 x x^2]
[x^3 x^4 x^5]
sage: c[0,0] = 5; c
[ 5 x x^2]
[x^3 x^4 x^5]
sage: b[0,0]
1
sage: c.dict()
{(0, 0): 5, (0, 1): x, (0, 2): x^2, (1, 0): x^3, (1, 1): x^4, (1, 2): x^5}
sage: c.list()
[5, x, x^2, x^3, x^4, x^5]
sage: c.rows()
[(5, x, x^2), (x^3, x^4, x^5)]
sage: TestSuite(c).run()
sage: d = c.change_ring(CC['x']); d
[5.00000000000000 x x^2]
[ x^3 x^4 x^5]
sage: latex(c)
\left(\begin{array}{rrr}
5 & x & x^{2} \\
x^{3} & x^{4} & x^{5}
\end{array}\right)
sage: c.sparse_rows()
[(5, x, x^2), (x^3, x^4, x^5)]
sage: d = c.dense_matrix(); d
[ 5 x x^2]
[x^3 x^4 x^5]
sage: parent(d)
Full MatrixSpace of 2 by 3 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage: c.sparse_matrix() is c
True
sage: c.is_sparse()
True
-
class
sage.matrix.matrix_generic_sparse.
Matrix_generic_sparse
¶ Bases:
sage.matrix.matrix_sparse.Matrix_sparse
Generic sparse matrix.
The
Matrix_generic_sparse
class derives fromMatrix_sparse
, and defines functionality for sparse matrices over any base ring. A generic sparse matrix is represented using a dictionary whose keys are pairs of integers \((i,j)\) and values in the base ring. The values of the dictionary must never be zero.EXAMPLES:
sage: R.<a,b> = PolynomialRing(ZZ,'a,b') sage: M = MatrixSpace(R,5,5,sparse=True) sage: M({(0,0):5*a+2*b, (3,4): -a}) [5*a + 2*b 0 0 0 0] [ 0 0 0 0 0] [ 0 0 0 0 0] [ 0 0 0 0 -a] [ 0 0 0 0 0] sage: M(3) [3 0 0 0 0] [0 3 0 0 0] [0 0 3 0 0] [0 0 0 3 0] [0 0 0 0 3] sage: V = FreeModule(ZZ, 5,sparse=True) sage: m = M([V({0:3}), V({2:2, 4:-1}), V(0), V(0), V({1:2})]) sage: m [ 3 0 0 0 0] [ 0 0 2 0 -1] [ 0 0 0 0 0] [ 0 0 0 0 0] [ 0 2 0 0 0]
Note
The datastructure can potentially be optimized. Firstly, as noticed in trac ticket #17663, we lose time in using 2-tuples to store indices. Secondly, there is no fast way to access non-zero elements in a given row/column.
-
sage.matrix.matrix_generic_sparse.
Matrix_sparse_from_rows
(X)¶ INPUT:
X
- nonempty list of SparseVector rows
OUTPUT: Sparse_matrix with those rows.
EXAMPLES:
sage: V = VectorSpace(QQ,20,sparse=True) sage: v = V(0) sage: v[9] = 4 sage: from sage.matrix.matrix_generic_sparse import Matrix_sparse_from_rows sage: Matrix_sparse_from_rows([v]) [0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0] sage: Matrix_sparse_from_rows([v, v, v, V(0)]) [0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]