Sparse rational matrices#

AUTHORS:

  • William Stein (2007-02-21)

  • Soroosh Yazdani (2007-02-21)

class sage.matrix.matrix_rational_sparse.Matrix_rational_sparse[source]#

Bases: Matrix_sparse

Create a sparse matrix over the rational numbers.

INPUT:

  • parent – a matrix space over QQ

  • entries – see matrix()

  • copy – ignored (for backwards compatibility)

  • coerce – if False, assume without checking that the entries are of type Rational.

add_to_entry(i, j, elt)[source]#

Add elt to the entry at position (i, j).

EXAMPLES:

sage: m = matrix(QQ, 2, 2, sparse=True)
sage: m.add_to_entry(0, 0, -1/3)
sage: m
[-1/3    0]
[   0    0]
sage: m.add_to_entry(0, 0, 1/3)
sage: m
[0 0]
[0 0]
sage: m.nonzero_positions()
[]
>>> from sage.all import *
>>> m = matrix(QQ, Integer(2), Integer(2), sparse=True)
>>> m.add_to_entry(Integer(0), Integer(0), -Integer(1)/Integer(3))
>>> m
[-1/3    0]
[   0    0]
>>> m.add_to_entry(Integer(0), Integer(0), Integer(1)/Integer(3))
>>> m
[0 0]
[0 0]
>>> m.nonzero_positions()
[]
denominator()[source]#

Return the denominator of this matrix.

OUTPUT:

  • Sage Integer

EXAMPLES:

sage: b = matrix(QQ,2,range(6)); b[0,0]=-5007/293; b
[-5007/293         1         2]
[        3         4         5]
sage: b.denominator()
293
>>> from sage.all import *
>>> b = matrix(QQ,Integer(2),range(Integer(6))); b[Integer(0),Integer(0)]=-Integer(5007)/Integer(293); b
[-5007/293         1         2]
[        3         4         5]
>>> b.denominator()
293
dense_matrix()[source]#

Return dense version of this matrix.

EXAMPLES:

sage: a = matrix(QQ,2,[1..4],sparse=True); type(a)
<class 'sage.matrix.matrix_rational_sparse.Matrix_rational_sparse'>
sage: type(a.dense_matrix())
<class 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'>
sage: a.dense_matrix()
[1 2]
[3 4]
>>> from sage.all import *
>>> a = matrix(QQ,Integer(2),(ellipsis_range(Integer(1),Ellipsis,Integer(4))),sparse=True); type(a)
<class 'sage.matrix.matrix_rational_sparse.Matrix_rational_sparse'>
>>> type(a.dense_matrix())
<class 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'>
>>> a.dense_matrix()
[1 2]
[3 4]

Check that subdivisions are preserved when converting between dense and sparse matrices:

sage: a.subdivide([1,1], [2])
sage: b = a.dense_matrix().sparse_matrix().dense_matrix()
sage: b.subdivisions() == a.subdivisions()
True
>>> from sage.all import *
>>> a.subdivide([Integer(1),Integer(1)], [Integer(2)])
>>> b = a.dense_matrix().sparse_matrix().dense_matrix()
>>> b.subdivisions() == a.subdivisions()
True
echelon_form(algorithm='default', height_guess=None, proof=True, **kwds)[source]#

INPUT:

height_guess, proof, **kwds – all passed to the multimodular algorithm; ignored by the p-adic algorithm.

OUTPUT:

self is no in reduced row echelon form.

EXAMPLES:

sage: a = matrix(QQ, 4, range(16), sparse=True); a[0,0] = 1/19; a[0,1] = 1/5; a
[1/19  1/5    2    3]
[   4    5    6    7]
[   8    9   10   11]
[  12   13   14   15]
sage: a.echelon_form()
[      1       0       0 -76/157]
[      0       1       0  -5/157]
[      0       0       1 238/157]
[      0       0       0       0]
>>> from sage.all import *
>>> a = matrix(QQ, Integer(4), range(Integer(16)), sparse=True); a[Integer(0),Integer(0)] = Integer(1)/Integer(19); a[Integer(0),Integer(1)] = Integer(1)/Integer(5); a
[1/19  1/5    2    3]
[   4    5    6    7]
[   8    9   10   11]
[  12   13   14   15]
>>> a.echelon_form()
[      1       0       0 -76/157]
[      0       1       0  -5/157]
[      0       0       1 238/157]
[      0       0       0       0]
echelonize(height_guess=None, proof=True, **kwds)[source]#

Transform the matrix self into reduced row echelon form in place.

INPUT:

height_guess, proof, **kwds – all passed to the multimodular algorithm; ignored by the p-adic algorithm.

OUTPUT:

Nothing. The matrix self is transformed into reduced row echelon form in place.

ALGORITHM: a multimodular algorithm.

EXAMPLES:

sage: a = matrix(QQ, 4, range(16), sparse=True); a[0,0] = 1/19; a[0,1] = 1/5; a
[1/19  1/5    2    3]
[   4    5    6    7]
[   8    9   10   11]
[  12   13   14   15]
sage: a.echelonize(); a
[      1       0       0 -76/157]
[      0       1       0  -5/157]
[      0       0       1 238/157]
[      0       0       0       0]
>>> from sage.all import *
>>> a = matrix(QQ, Integer(4), range(Integer(16)), sparse=True); a[Integer(0),Integer(0)] = Integer(1)/Integer(19); a[Integer(0),Integer(1)] = Integer(1)/Integer(5); a
[1/19  1/5    2    3]
[   4    5    6    7]
[   8    9   10   11]
[  12   13   14   15]
>>> a.echelonize(); a
[      1       0       0 -76/157]
[      0       1       0  -5/157]
[      0       0       1 238/157]
[      0       0       0       0]

Issue #10319 has been fixed:

sage: m = Matrix(QQ, [1], sparse=True); m.echelonize()
sage: m = Matrix(QQ, [1], sparse=True); m.echelonize(); m
[1]
>>> from sage.all import *
>>> m = Matrix(QQ, [Integer(1)], sparse=True); m.echelonize()
>>> m = Matrix(QQ, [Integer(1)], sparse=True); m.echelonize(); m
[1]
height()[source]#

Return the height of this matrix, which is the least common multiple of all numerators and denominators of elements of this matrix.

OUTPUT:

– Integer

EXAMPLES:

sage: b = matrix(QQ,2,range(6), sparse=True); b[0,0]=-5007/293; b
[-5007/293         1         2]
[        3         4         5]
sage: b.height()
5007
>>> from sage.all import *
>>> b = matrix(QQ,Integer(2),range(Integer(6)), sparse=True); b[Integer(0),Integer(0)]=-Integer(5007)/Integer(293); b
[-5007/293         1         2]
[        3         4         5]
>>> b.height()
5007
set_row_to_multiple_of_row(i, j, s)[source]#

Set row i equal to s times row j.

EXAMPLES:

sage: a = matrix(QQ,2,3,range(6), sparse=True); a
[0 1 2]
[3 4 5]
sage: a.set_row_to_multiple_of_row(1,0,-3)
sage: a
[ 0  1  2]
[ 0 -3 -6]
>>> from sage.all import *
>>> a = matrix(QQ,Integer(2),Integer(3),range(Integer(6)), sparse=True); a
[0 1 2]
[3 4 5]
>>> a.set_row_to_multiple_of_row(Integer(1),Integer(0),-Integer(3))
>>> a
[ 0  1  2]
[ 0 -3 -6]