# Sparse Matrices over a general ring¶

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, 1): x, (1, 2): x^5, (0, 0): 5, (1, 0): x^3, (0, 2): x^2, (1, 1): x^4}
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

The Matrix_generic_sparse class derives from Matrix, and defines functionality for sparse matrices over any base ring. A generic sparse matrix is represented using a dictionary with keys pairs $$(i,j)$$ and values in the base ring.

The values of the dictionary must never be zero.

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]


#### Previous topic

Dense Matrices over a general ring

#### Next topic

Dense matrices over $$\ZZ/n\ZZ$$ for $$n$$ small