# Sparse rational matrices¶

AUTHORS:

• William Stein (2007-02-21)

• Soroosh Yazdani (2007-02-21)

class sage.matrix.matrix_rational_sparse.Matrix_rational_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 `elt` to the entry at position `(i, j)`.

EXAMPLES:

```sage: m = matrix(QQ, 2, 2, sparse=True)
sage: m
[-1/3    0]
[   0    0]
sage: m
[0 0]
[0 0]
sage: m.nonzero_positions()
[]
```
denominator()

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
```
dense_matrix()

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]
```

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

```sage: a.subdivide([1,1], )
sage: b = a.dense_matrix().sparse_matrix().dense_matrix()
sage: b.subdivisions() == a.subdivisions()
True
```
echelon_form(algorithm='default', height_guess=None, proof=True, **kwds)

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]
```
echelonize(height_guess=None, proof=True, **kwds)

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]
```

trac ticket #10319 has been fixed:

```sage: m = Matrix(QQ, , sparse=True); m.echelonize()
sage: m = Matrix(QQ, , sparse=True); m.echelonize(); m

```
height()

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
```
set_row_to_multiple_of_row(i, j, s)

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]
```