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: loads(dumps(c)) == c
True
sage: d = c.change_ring(CC['x']); d
[    5.00000000000000   1.00000000000000*x 1.00000000000000*x^2]
[1.00000000000000*x^3 1.00000000000000*x^4 1.00000000000000*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.

__copy__()
__eq__()
x.__eq__(y) <==> x==y
__ge__()
x.__ge__(y) <==> x>=y
__gt__()
x.__gt__(y) <==> x>y
__hash__()
x.__hash__() <==> hash(x)
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
__le__()
x.__le__(y) <==> x<=y
__lt__()
x.__lt__(y) <==> x<y
__ne__()
x.__ne__(y) <==> x!=y
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_add_()

EXAMPLES:

sage: a = matrix([[1,10],[3,4]],sparse=True); a
[ 1 10]
[ 3  4]
sage: a+a
[ 2 20]
[ 6  8]
sage: a = matrix([[1,10,-5/3],[2/8,3,4]],sparse=True); a
[   1   10 -5/3]
[ 1/4    3    4]
sage: a+a
[    2    20 -10/3]
[  1/2     6     8]
_dict()
Return the underlying dictionary of self.
_list()
Return all entries of self as a list of numbers of rows times number of columns entries.
_nonzero_positions_by_column()
_nonzero_positions_by_row()
_pickle()
_unpickle()

EXAMPLES:

sage: a = matrix([[1,10],[3,4]],sparse=True); a
[ 1 10]
[ 3  4]
sage: loads(dumps(a)) == a
True
sage.matrix.matrix_generic_sparse.Matrix_sparse_from_rows()

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]
sage.matrix.matrix_generic_sparse._cmp_backward()

Previous topic

Dense Matrices over a general ring

Next topic

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

This Page