Sparse linear algebra
Introduction
This chapter deals with sparse linear algebra over commutative rings and fields.
Sparse linear algebra, that is, linear algebra with sparse matrices, plays an important role in various algorithms in algebraic number theory. For example, it is one of the key ingredients in the computation of class groups and discrete logarithms using index calculus methods.
Sparse rows
Building blocks for sparse matrices are sparse rows, which are modelled by objects of type SRow. More precisely, the type is of parametrized form SRow{T}, where T is the element type of the base ring $R$. For example, SRow{ZZRingElem} is the type for sparse rows over the integers.
It is important to note that sparse rows do not have a fixed number of columns, that is, they represent elements of $\{ (x_i)_i \in R^{\mathbb{N}} \mid x_i = 0 \text{ for almost all }i\}$. In particular any two sparse rows over the same base ring can be added.
Creation
sparse_row — Methodsparse_row(R::Ring, J::Vector{Tuple{Int, T}}) -> SRow{T}Constructs the sparse row $(a_i)_i$ with $a_{i_j} = x_j$, where $J = (i_j, x_j)_j$. The elements $x_i$ must belong to the ring $R$.
sparse_row — Methodsparse_row(R::Ring, J::Vector{Tuple{Int, Int}}) -> SRowConstructs the sparse row $(a_i)_i$ over $R$ with $a_{i_j} = x_j$, where $J = (i_j, x_j)_j$.
sparse_row — Methodsparse_row(R::NCRing, J::Vector{Int}, V::Vector{T}) -> SRow{T}Constructs the sparse row $(a_i)_i$ over $R$ with $a_{i_j} = x_j$, where $J = (i_j)_j$ and $V = (x_j)_j$.
Basic operations
Rows support the usual operations:
+,-,==- multiplication by scalars
div,divexact
getindex — Methodgetindex(A::SRow, j::Int) -> RingElemGiven a sparse row $(a_i)_{i}$ and an index $j$ return $a_j$.
add_scaled_row — Methodadd_scaled_row(A::SRow{T}, B::SRow{T}, c::T) -> SRow{T}Returns the row $c A + B$.
add_scaled_row — Methodadd_scaled_row(A::SRow{T}, B::SRow{T}, c::T) -> SRow{T}Returns the row $c A + B$.
transform_row — Methodtransform_row(A::SRow{T}, B::SRow{T}, i::Int, j::Int, a::T, b::T, c::T, d::T)Returns the tuple $(aA + bB, cA + dB)$.
length — Methodlength(A::SRow)Returns the number of nonzero entries of $A$.
Change of base ring
change_base_ring — Methodchange_base_ring(R::Ring, A::SRow) -> SRowCreate a new sparse row by coercing all elements into the ring $R$.
Maximum, minimum and 2-norm
maximum — Methodmaximum(A::SRow{T}) -> TReturns the largest entry of $A$.
maximum — Methodmaximum(A::SRow{T}) -> TReturns the largest entry of $A$.
minimum — Methodminimum(A::SRow{T}) -> TReturns the smallest entry of $A$.
minimum(A::RelNumFieldOrderIdeal) -> AbsNumFieldOrderIdeal{AbsSimpleNumField, AbsSimpleNumFieldElem}
minimum(A::RelNumFieldOrderIdeal) -> RelNumFieldOrderIdealReturns the ideal $A \cap O$ where $O$ is the maximal order of the coefficient ideals of $A$.
minimum — Methodminimum(A::SRow{T}) -> TReturns the smallest entry of $A$.
norm2 — Methodnorm2(A::SRow{T} -> TReturns $A \cdot A^t$.
Functionality for integral sparse rows
lift — Methodlift(A::SRow{zzModRingElem}) -> SRow{ZZRingElem}Return the sparse row obtained by lifting all entries in $A$.
mod! — Methodmod!(A::SRow{ZZRingElem}, n::ZZRingElem) -> SRow{ZZRingElem}Inplace reduction of all entries of $A$ modulo $n$ to the positive residue system.
mod_sym! — Methodmod_sym!(A::SRow{ZZRingElem}, n::ZZRingElem) -> SRow{ZZRingElem}Inplace reduction of all entries of $A$ modulo $n$ to the symmetric residue system.
mod_sym! — Methodmod_sym!(A::SRow{ZZRingElem}, n::Integer) -> SRow{ZZRingElem}Inplace reduction of all entries of $A$ modulo $n$ to the symmetric residue system.
maximum — Methodmaximum(abs, A::SRow{ZZRingElem}) -> ZZRingElemReturns the largest, in absolute value, entry of $A$.
Conversion to/from julia and AbstractAlgebra types
Vector — MethodVector(a::SMat{T}, n::Int) -> Vector{T}The first n entries of a, as a julia vector.
sparse_row — Methodsparse_row(A::MatElem)Convert A to a sparse row. nrows(A) == 1 must hold.
dense_row — Methoddense_row(r::SRow, n::Int)Convert r[1:n] to a dense row, that is an AbstractAlgebra matrix.
Sparse matrices
Let $R$ be a commutative ring. Sparse matrices with base ring $R$ are modelled by objects of type SMat. More precisely, the type is of parametrized form SRow{T}, where T is the element type of the base ring. For example, SMat{ZZRingElem} is the type for sparse matrices over the integers.
In contrast to sparse rows, sparse matrices have a fixed number of rows and columns, that is, they represent elements of the matrices space $\mathrm{Mat}_{n\times m}(R)$. Internally, sparse matrices are implemented as an array of sparse rows. As a consequence, unlike their dense counterparts, sparse matrices have a mutable number of rows and it is very performant to add additional rows.
Construction
sparse_matrix — Methodsparse_matrix(R::Ring) -> SMatReturn an empty sparse matrix with base ring $R$.
sparse_matrix — Methodsparse_matrix(R::Ring, n::Int, m::Int) -> SMatReturn a sparse $n$ times $m$ zero matrix over $R$.
Sparse matrices can also be created from dense matrices as well as from julia arrays:
sparse_matrix — Methodsparse_matrix(A::MatElem; keepzrows::Bool = true)Constructs the sparse matrix corresponding to the dense matrix $A$. If keepzrows is false, then the constructor will drop any zero row of $A$.
sparse_matrix — Methodsparse_matrix(R::Ring, A::Matrix{T}) -> SMatConstructs the sparse matrix over $R$ corresponding to $A$.
sparse_matrix — Methodsparse_matrix(R::Ring, A::Matrix{T}) -> SMatConstructs the sparse matrix over $R$ corresponding to $A$.
The normal way however, is to add rows:
push! — Methodpush!(A::SMat{T}, B::SRow{T}) where TAppends the sparse row B to A.
Sparse matrices can also be concatenated to form larger ones:
vcat! — Methodvcat!(A::SMat, B::SMat) -> SMatVertically joins $A$ and $B$ inplace, that is, the rows of $B$ are appended to $A$.
vcat — Methodvcat(A::SMat, B::SMat) -> SMatVertically joins $A$ and $B$.
hcat! — Methodhcat!(A::SMat, B::SMat) -> SMatHorizontally concatenates $A$ and $B$, inplace, changing $A$.
hcat — Methodhcat(A::SMat, B::SMat) -> SMatHorizontally concatenates $A$ and $B$.
(Normal julia $cat$ is also supported)
There are special constructors:
identity_matrix — Methodidentity_matrix(::Type{SMat}, R::Ring, n::Int)Return a sparse $n$ times $n$ identity matrix over $R$.
zero_matrix — Methodzero_matrix(::Type{SMat}, R::Ring, n::Int)Return a sparse $n$ times $n$ zero matrix over $R$.
zero_matrix — Methodzero_matrix(::Type{SMat}, R::Ring, n::Int, m::Int)Return a sparse $n$ times $m$ zero matrix over $R$.
block_diagonal_matrix — Methodblock_diagonal_matrix(xs::Vector{SMat})Return the block diagonal matrix with the matrices in xs on the diagonal. Requires all blocks to have the same base ring.
Slices:
sub — Methodsub(A::SMat, r::AbstractUnitRange, c::AbstractUnitRange) -> SMatReturn the submatrix of $A$, where the rows correspond to $r$ and the columns correspond to $c$.
Transpose:
transpose — Methodtranspose(A::SMat) -> SMatReturns the transpose of $A$.
Elementary Properties
sparsity — Methodsparsity(A::SMat) -> Float64Return the sparsity of A, that is, the number of zero-valued elements divided by the number of all elements.
density — Methoddensity(A::SMat) -> Float64Return the density of A, that is, the number of nonzero-valued elements divided by the number of all elements.
nnz — Methodnnz(A::SMat) -> IntReturn the number of non-zero entries of $A$.
number_of_rows — Methodnumber_of_rows(A::SMat) -> IntReturn the number of rows of $A$.
number_of_columns — Methodnumber_of_columns(A::SMat) -> IntReturn the number of columns of $A$.
isone — Methodisone(A::SMat)Tests if $A$ is an identity matrix.
iszero — Methodiszero(A::SMat)Tests if $A$ is a zero matrix.
is_upper_triangular — Methodis_upper_triangular(A::SMat)Returns true if and only if $A$ is upper (right) triangular.
maximum — Methodmaximum(A::SMat{T}) -> TFinds the largest entry of $A$.
minimum — Methodminimum(A::SMat{T}) -> TFinds the smallest entry of $A$.
maximum — Methodmaximum(abs, A::SMat{ZZRingElem}) -> ZZRingElemFinds the largest, in absolute value, entry of $A$.
elementary_divisors — Methodelementary_divisors(A::SMat{ZZRingElem}) -> Vector{ZZRingElem}The elementary divisors of $A$, i.e. the diagonal elements of the Smith normal form of $A$.
solve_dixon_sf — Methodsolve_dixon_sf(A::SMat{ZZRingElem}, b::SRow{ZZRingElem}, is_int::Bool = false) -> SRow{ZZRingElem}, ZZRingElem
solve_dixon_sf(A::SMat{ZZRingElem}, B::SMat{ZZRingElem}, is_int::Bool = false) -> SMat{ZZRingElem}, ZZRingElemFor a sparse square matrix $A$ of full rank and a sparse matrix (row), find a sparse matrix (row) $x$ and an integer $d$ s.th. $x A = bd$ holds. The algorithm is a Dixon-based linear p-adic lifting method. If \code{is_int} is given, then $d$ is assumed to be $1$. In this case rational reconstruction is avoided.
hadamard_bound2 — Methodhadamard_bound2(A::SMat{T}) -> TThe square of the product of the norms of the rows of $A$.
echelon_with_transform — Methodechelon_with_transform(A::SMat{zzModRingElem}) -> SMat, SMatFind a unimodular matrix $T$ and an upper-triangular $E$ s.th. $TA = E$ holds.
reduce_full — Methodreduce_full(A::SMat{ZZRingElem}, g::SRow{ZZRingElem},
with_transform = Val(false)) -> SRow{ZZRingElem}, Vector{Int}Reduces $g$ modulo $A$ and assumes that $A$ is upper triangular.
The second return value is the array of pivot elements of $A$ that changed.
If with_transform is set to Val(true), then additionally an array of transformations is returned.
hnf! — Methodhnf!(A::SMat{ZZRingElem})Inplace transform of $A$ into upper right Hermite normal form.
hnf — Methodhnf(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}Return the upper right Hermite normal form of $A$.
\[truncate\]
indicates if zero rows should be returned or disgarded, when $full_hnf$ is set to $false$ only an upper triangular matrix is computed, ie. the upwards reduction is not performed.
\[auto\]
if set to true selects a different elemination strategy - here the upwards reduction is temporarily switched off.
\[limit\]
marks the last column where size reduction happens, so calling $hnf$ on $hcat(A, identity_matrix(SMat, ZZ, nrows(A)))$ with $limit$ set to $ncols(A)$ should produce a non-reduced transformation matrix in the right. If $limit$ is not set, the transformation matrix will also be partly triangular.
snf — Methodsnf(A::SMat{ZZRingElem})The Smith normal form (snf) of $A$.
hnf_extend! — Methodhnf_extend!(A::SMat{ZZRingElem}, b::SMat{ZZRingElem}, offset::Int = 0) -> SMat{ZZRingElem}Given a matrix $A$ in HNF, extend this to get the HNF of the concatenation with $b$.
is_diagonal — Methodis_diagonal(A::SMat) -> BoolTrue iff only the i-th entry in the i-th row is non-zero.
det — Methoddet(A::SMat{ZZRingElem})The determinant of $A$ using a modular algorithm. Uses the dense (zzModMatrix) determinant on $A$ for various primes $p$.
det_mc — Methoddet_mc(A::SMat{ZZRingElem})Computes the determinant of $A$ using a LasVegas style algorithm, i.e. the result is not proven to be correct. Uses the dense (zzModMatrix) determinant on $A$ for various primes $p$.
valence_mc — Methodvalence_mc{T}(A::SMat{T}; extra_prime = 2, trans = Vector{SMatSLP_add_row{T}}()) -> TUses a Monte-Carlo algorithm to compute the valence of $A$. The valence is the valence of the minimal polynomial $f$ of $transpose(A)*A$, thus the last non-zero coefficient, typically $f(0)$.
The valence is computed modulo various primes until the computation stabilises for extra_prime many.
trans, if given, is a SLP (straight-line-program) in GL(n, Z). Then the valence of trans * $A$ is computed instead.
saturate — Methodsaturate(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}Computes the saturation of $A$, that is, a basis for $\mathbf{Q}\otimes M \cap \mathbf{Z}^n$, where $M$ is the row span of $A$ and $n$ the number of rows of $A$.
Equivalently, return $TA$ for an invertible rational matrix $T$, such that $TA$ is integral and the elementary divisors of $TA$ are all trivial.
hnf_kannan_bachem — Methodhnf_kannan_bachem(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}Compute the Hermite normal form of $A$ using the Kannan-Bachem algorithm.
diagonal_form — Methoddiagonal_form(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}A matrix $D$ that is diagonal and obtained via unimodular row and column operations. Like a snf without the divisibility condition.
Manipulation/ Access
getindex — Methodgetindex(A::SMat, i::Int, j::Int)Given a sparse matrix $A = (a_{ij})_{i, j}$, return the entry $a_{ij}$.
getindex — Methodgetindex(A::SMat, i::Int) -> SRowGiven a sparse matrix $A$ and an index $i$, return the $i$-th row of $A$.
setindex! — Methodsetindex!(A::SMat, b::SRow, i::Int)Given a sparse matrix $A$, a sparse row $b$ and an index $i$, set the $i$-th row of $A$ equal to $b$.
swap_rows! — Methodswap_rows!(A::SMat{T}, i::Int, j::Int)Swap the $i$-th and $j$-th row of $A$ inplace.
swap_cols! — Methodswap_cols!(A::SMat, i::Int, j::Int)Swap the $i$-th and $j$-th column of $A$ inplace.
scale_row! — Methodscale_row!(A::SMat{T}, i::Int, c::T)Multiply the $i$-th row of $A$ by $c$ inplace.
add_scaled_col! — Methodadd_scaled_col!(A::SMat{T}, i::Int, j::Int, c::T)Add $c$ times the $i$-th column to the $j$-th column of $A$ inplace, that is, $A_j \rightarrow A_j + c \cdot A_i$, where $(A_i)_i$ denote the columns of $A$.
add_scaled_row! — Methodadd_scaled_row!(A::SMat{T}, i::Int, j::Int, c::T)Add $c$ times the $i$-th row to the $j$-th row of $A$ inplace, that is, $A_j \rightarrow A_j + c \cdot A_i$, where $(A_i)_i$ denote the rows of $A$.
transform_row! — Methodtransform_row!(A::SMat{T}, i::Int, j::Int, a::T, b::T, c::T, d::T)Applies the transformation $(A_i, A_j) \rightarrow (aA_i + bA_j, cA_i + dA_j)$ to $A$, where $(A_i)_i$ are the rows of $A$.
diagonal — Methoddiagonal(A::SMat) -> ZZRingElem[]The diagonal elements of $A$ in an array.
reverse_rows! — Methodreverse_rows!(A::SMat)Inplace inversion of the rows of $A$.
mod_sym! — Methodmod_sym!(A::SMat{ZZRingElem}, n::ZZRingElem)Inplace reduction of all entries of $A$ modulo $n$ to the symmetric residue system.
find_row_starting_with — Methodfind_row_starting_with(A::SMat, p::Int) -> IntTries to find the index $i$ such that $A_{i,p} \neq 0$ and $A_{i, p-j} = 0$ for all $j > 1$. It is assumed that $A$ is upper triangular. If such an index does not exist, find the smallest index larger.
reduce — Methodreduce(A::SMat{T}, g::SRow{T}, m::T) -> SRow{T}Given an upper triangular matrix $A$ over the integers, a sparse row $g$ and an integer $m$, this function reduces $g$ modulo $A$ and returns $g$ modulo $m$ with respect to the symmetric residue system.
reduce — Methodreduce(A::SMat{T}, g::SRow{T}) -> SRow{T}Given an upper triangular matrix $A$ over a field and a sparse row $g$, this function reduces $g$ modulo $A$.
reduce — Methodreduce(A::SMat{T}, g::SRow{T}) -> SRow{T}Given an upper triangular matrix $A$ over a field and a sparse row $g$, this function reduces $g$ modulo $A$.
rand_row — Methodrand_row(A::SMat) -> SRowReturn a random row of the sparse matrix $A$.
Changing of the ring:
map_entries — Methodmap_entries(f, A::SMat) -> SMatGiven a sparse matrix $A$ and a callable object $f$, this function will construct a new sparse matrix by applying $f$ to all elements of $A$.
change_base_ring — Methodchange_base_ring(R::Ring, A::SMat)Create a new sparse matrix by coercing all elements into the ring $R$.
Arithmetic
Matrices support the usual operations as well
+,-,==div,divexactby scalars- multiplication by scalars
Various products:
* — Method*(A::SMat{T}, b::AbstractVector{T}) -> Vector{T}Return the product $A \cdot b$ as a dense vector.
* — Method*(A::SMat{T}, b::AbstractMatrix{T}) -> Matrix{T}Return the product $A \cdot b$ as a dense matrix.
* — Method*(A::SMat{T}, b::MatElem{T}) -> MatElemReturn the product $A \cdot b$ as a dense matrix.
* — Method*(A::SRow, B::SMat) -> SRowReturn the product $A\cdot B$ as a sparse row.
mul_sparse — Methodmul_sparse(A::SMat{T}, B::SMat{T}) -> SMat{T}Return the product $A\cdot B$ as a sparse matrix.
The product of two sparse matrices is, in general, not sparse, so depending on the context, it might be more efficient to use mul_dense(::SMat{T}, ::SMat{T}) where {T} instead.
mul_dense — Methodmul_dense(A::SMat{T}, B::SMat{T}) -> MatElem{T}Return the product $A\cdot B$ as a dense matrix.
dot — Methoddot(x::SRow{T}, A::SMat{T}, y::SRow{T}) where T -> TReturn the generalized dot product dot(x, A*y).
dot — Methoddot(x::MatrixElem{T}, A::SMat{T}, y::MatrixElem{T}) where T -> TReturn the generalized dot product dot(x, A*y).
dot — Methoddot(x::AbstractVector{T}, A::SMat{T}, y::AbstractVector{T}) where T -> TReturn the generalized dot product dot(x, A*y).
Other:
sparse — Methodsparse(A::SMat) -> SparseMatrixCSCThe same matrix, but as a sparse matrix of julia type SparseMatrixCSC.
ZZMatrix — MethodZZMatrix(A::SMat{ZZRingElem})The same matrix $A$, but as an ZZMatrix.
ZZMatrix — MethodZZMatrix(A::SMat{T}) where {T <: Integer}The same matrix $A$, but as an ZZMatrix. Requires a conversion from the base ring of $A$ to $\mathbb ZZ$.
Matrix — MethodMatrix(A::SMat{T}) -> Matrix{T}The same matrix, but as a julia matrix.
Array — MethodArray(A::SMat{T}) -> Matrix{T}The same matrix, but as a two-dimensional julia array.