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()) # needs sage.libs.pari
[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
>>> from sage.all import *
>>> R = PolynomialRing(QQ, names=('x',)); (x,) = R._first_ngens(1)
>>> M = MatrixSpace(QQ['x'], Integer(2), Integer(3), sparse=True); M
Full MatrixSpace of 2 by 3 sparse matrices over
Univariate Polynomial Ring in x over Rational Field
>>> a = M(range(Integer(6))); a
[0 1 2]
[3 4 5]
>>> b = M([x**n for n in range(Integer(6))]); b
[ 1 x x^2]
[x^3 x^4 x^5]
>>> 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]
>>> pari(a)*pari(b.transpose()) # needs sage.libs.pari
[2*x^2 + x, 2*x^5 + x^4; 5*x^2 + 4*x + 3, 5*x^5 + 4*x^4 + 3*x^3]
>>> c = copy(b); c
[ 1 x x^2]
[x^3 x^4 x^5]
>>> c[Integer(0),Integer(0)] = Integer(5); c
[ 5 x x^2]
[x^3 x^4 x^5]
>>> b[Integer(0),Integer(0)]
1
>>> c.dict()
{(0, 0): 5, (0, 1): x, (0, 2): x^2, (1, 0): x^3, (1, 1): x^4, (1, 2): x^5}
>>> c.list()
[5, x, x^2, x^3, x^4, x^5]
>>> c.rows()
[(5, x, x^2), (x^3, x^4, x^5)]
>>> TestSuite(c).run()
>>> d = c.change_ring(CC['x']); d
[5.00000000000000 x x^2]
[ x^3 x^4 x^5]
>>> latex(c)
\left(\begin{array}{rrr}
5 & x & x^{2} \\
x^{3} & x^{4} & x^{5}
\end{array}\right)
>>> c.sparse_rows()
[(5, x, x^2), (x^3, x^4, x^5)]
>>> d = c.dense_matrix(); d
[ 5 x x^2]
[x^3 x^4 x^5]
>>> parent(d)
Full MatrixSpace of 2 by 3 dense matrices
over Univariate Polynomial Ring in x over Rational Field
>>> c.sparse_matrix() is c
True
>>> c.is_sparse()
True
R.<x> = PolynomialRing(QQ) M = MatrixSpace(QQ['x'], 2, 3, sparse=True); M a = M(range(6)); a b = M([x^n for n in range(6)]); b a * b.transpose() pari(a)*pari(b.transpose()) # needs sage.libs.pari c = copy(b); c c[0,0] = 5; c b[0,0] c.dict() c.list() c.rows() TestSuite(c).run() d = c.change_ring(CC['x']); d latex(c) c.sparse_rows() d = c.dense_matrix(); d parent(d) c.sparse_matrix() is c c.is_sparse()
- class sage.matrix.matrix_generic_sparse.Matrix_generic_sparse[source]¶
Bases:
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]
>>> from sage.all import * >>> R = PolynomialRing(ZZ,'a,b', names=('a', 'b',)); (a, b,) = R._first_ngens(2) >>> M = MatrixSpace(R,Integer(5),Integer(5),sparse=True) >>> M({(Integer(0),Integer(0)):Integer(5)*a+Integer(2)*b, (Integer(3),Integer(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] >>> M(Integer(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] >>> V = FreeModule(ZZ, Integer(5),sparse=True) >>> m = M([V({Integer(0):Integer(3)}), V({Integer(2):Integer(2), Integer(4):-Integer(1)}), V(Integer(0)), V(Integer(0)), V({Integer(1):Integer(2)})]) >>> 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]
R.<a,b> = PolynomialRing(ZZ,'a,b') M = MatrixSpace(R,5,5,sparse=True) M({(0,0):5*a+2*b, (3,4): -a}) M(3) V = FreeModule(ZZ, 5,sparse=True) m = M([V({0:3}), V({2:2, 4:-1}), V(0), V(0), V({1:2})]) m
Note
The datastructure can potentially be optimized. Firstly, as noticed in Issue #17663, we lose time in using 2-tuples to store indices. Secondly, there is no fast way to access nonzero elements in a given row/column.
- sage.matrix.matrix_generic_sparse.Matrix_sparse_from_rows(X)[source]¶
INPUT:
X
– nonempty list ofSparseVector
rows
OUTPUT:
Sparse_matrix
with those rowsEXAMPLES:
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]
>>> from sage.all import * >>> V = VectorSpace(QQ,Integer(20),sparse=True) >>> v = V(Integer(0)) >>> v[Integer(9)] = Integer(4) >>> from sage.matrix.matrix_generic_sparse import Matrix_sparse_from_rows >>> 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] >>> Matrix_sparse_from_rows([v, v, v, V(Integer(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]
V = VectorSpace(QQ,20,sparse=True) v = V(0) v[9] = 4 from sage.matrix.matrix_generic_sparse import Matrix_sparse_from_rows Matrix_sparse_from_rows([v]) Matrix_sparse_from_rows([v, v, v, V(0)])