---
title: "Accessing Sparse Matrices"
canonical: "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/SparseMatrices/access.html"
source: "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/SparseMatrices/access.md"
part: "Matrices and Linear Algebra"
chapter: "Sparse Matrices"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Matrices and Linear Algebra", "source": "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/index-matrices-linear-algebra.md"}
  - {"title": "Sparse Matrices", "source": "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/SparseMatrices/index-sparse-matrices.md"}
prev: {"title": "Creation of Sparse Matrices", "source": "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/SparseMatrices/creation.md"}
next: {"title": "Accessing or Modifying Entries", "source": "https://docs.magma-maths.org/v2.30-1/MatricesLinearAlgebra/SparseMatrices/entries.md"}
intrinsics:
  - {"name": "BaseRing", "signature": "BaseRing(A): MtrxSprs -> Rng"}
  - {"name": "CoefficientRing", "signature": "CoefficientRing(A): MtrxSprs -> Rng"}
  - {"name": "NumberOfRows", "signature": "NumberOfRows(A): MtrxSprs -> RngIntElt"}
  - {"name": "Nrows", "signature": "Nrows(A): MtrxSprs -> RngIntElt"}
  - {"name": "NumberOfColumns", "signature": "NumberOfColumns(A): MtrxSprs -> RngIntElt"}
  - {"name": "Ncols", "signature": "Ncols(A): MtrxSprs -> RngIntElt"}
  - {"name": "ElementToSequence", "signature": "ElementToSequence(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]"}
  - {"name": "Eltseq", "signature": "Eltseq(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]"}
  - {"name": "NumberOfNonZeroEntries", "signature": "NumberOfNonZeroEntries(A): MtrxSprs -> RngIntElt"}
  - {"name": "NNZEntries", "signature": "NNZEntries(A): MtrxSprs -> RngIntElt"}
  - {"name": "Density", "signature": "Density(A): MtrxSprs -> FldRe"}
  - {"name": "Support", "signature": "Support(A, i): MtrxSprs, RngIntElt -> [RngIntElt]"}
  - {"name": "Support", "signature": "Support(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]"}
  - {"name": "RowWeight", "signature": "RowWeight(A, i): MtrxSprs, RngIntElt -> RngIntElt"}
  - {"name": "RowWeights", "signature": "RowWeights(A): MtrxSprs -> [RngIntElt]"}
  - {"name": "ColumnWeight", "signature": "ColumnWeight(A, j): MtrxSprs, RngIntElt -> RngIntElt"}
  - {"name": "ColumnWeights", "signature": "ColumnWeights(A): MtrxSprs -> [RngIntElt]"}
---

# Accessing Sparse Matrices

The following functions access basic properties of sparse matrices.

## Elementary Properties

<a id="function-basering-mtrxsprs"></a>
### `BaseRing(A): MtrxSprs -> Rng`

<a id="function-coefficientring-mtrxsprs"></a>
### `CoefficientRing(A): MtrxSprs -> Rng`

Given a sparse matrix $A$ with entries lying in a ring $R$, return $R$.

<a id="function-numberofrows-mtrxsprs"></a>
### `NumberOfRows(A): MtrxSprs -> RngIntElt`

<a id="function-nrows-mtrxsprs"></a>
### `Nrows(A): MtrxSprs -> RngIntElt`

Given an $m \times n$ sparse matrix $A$, return $m$, the number of rows of $A$.

<a id="function-numberofcolumns-mtrxsprs"></a>
### `NumberOfColumns(A): MtrxSprs -> RngIntElt`

<a id="function-ncols-mtrxsprs"></a>
### `Ncols(A): MtrxSprs -> RngIntElt`

Given an $m \times n$ sparse matrix $A$, return $n$, the number of columns of $A$.

<a id="function-elementtosequence-mtrxsprs"></a>
### `ElementToSequence(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]`

<a id="function-eltseq-mtrxsprs"></a>
### `Eltseq(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]`

Given a sparse matrix $A$ over the ring $R$ having $m$ rows and $n$ columns, return the entries of $A$ as a sequence of all tuples of the form `<i, j, x>` such that the $[i, j]$-th entry of $A$ equals $x$ and $x$ is non-zero. It is always true that `SparseMatrix(Nrows(A), Ncols(A), Eltseq(A))` equals $A$.

<a id="function-numberofnonzeroentries-mtrxsprs"></a>
### `NumberOfNonZeroEntries(A): MtrxSprs -> RngIntElt`

<a id="function-nnzentries-mtrxsprs"></a>
### `NNZEntries(A): MtrxSprs -> RngIntElt`

Given a sparse matrix $A$, return the number of non-zero entries in $A$.

<a id="function-density-mtrxsprs"></a>
### `Density(A): MtrxSprs -> FldRe`

Given a sparse matrix $A$, return the density of $A$ as a real number, which is the number of non-zero entries in $A$ divided by the product of the number of rows of $A$ and the number of columns of $A$ (or zero if $A$ has zero rows or columns).

<a id="function-support-mtrxsprs-rngintelt"></a>
### `Support(A, i): MtrxSprs, RngIntElt -> [RngIntElt]`

Given a sparse matrix $A$ having $r$ rows, and an integer $i$ such that $1\leq i\leq r$, return the support of row $i$ of $A$; i.e., the column numbers of the non-zero entries of row $i$ of $A$.

<a id="function-support-mtrxsprs"></a>
### `Support(A): MtrxSprs -> [ <RngIntElt, RngIntElt, RngElt> ]`

Given a sparse matrix $A$, return the sequence of all pairs `<i, j>` such that the $[i, j]$-th entry of $A$ is non-zero.

## Weights

<a id="function-rowweight-mtrxsprs-rngintelt"></a>
### `RowWeight(A, i): MtrxSprs, RngIntElt -> RngIntElt`

Given a sparse matrix $A$ with $m$ rows and an integer $i$ such that $1\leq i\leq m$, return the weight (number of non-zero entries) of the $i$-th row of $A$.

<a id="function-rowweights-mtrxsprs"></a>
### `RowWeights(A): MtrxSprs -> [RngIntElt]`

Given a sparse matrix $A$ with $m$ rows, return the length $m$ sequence of integers whose $i$-th entry is the weight of the $i$-th row of $A$.

<a id="function-columnweight-mtrxsprs-rngintelt"></a>
### `ColumnWeight(A, j): MtrxSprs, RngIntElt -> RngIntElt`

Given a sparse matrix $A$ with $n$ columns and an integer $j$ such that $1\leq j\leq n$, return the weight (number of non-zero entries) of the $j$-th column of $A$.

<a id="function-columnweights-mtrxsprs"></a>
### `ColumnWeights(A): MtrxSprs -> [RngIntElt]`

Given a sparse matrix $A$ with $n$ columns, return the length $n$ sequence of integers whose $j$-th entry is the weight of the $j$-th column of $A$.
