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 RR. 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 {(xi)iRNxi=0 for almost all i}\{ (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_rowMethod
sparse_row(R::Ring, J::Vector{Tuple{Int, T}}) -> SRow{T}

Constructs the sparse row (ai)i(a_i)_i with aij=xja_{i_j} = x_j, where J=(ij,xj)jJ = (i_j, x_j)_j. The elements xix_i must belong to the ring RR.

sparse_rowMethod
sparse_row(R::Ring, J::Vector{Tuple{Int, T}}) -> SRow{T}

Constructs the sparse row (ai)i(a_i)_i with aij=xja_{i_j} = x_j, where J=(ij,xj)jJ = (i_j, x_j)_j. The elements xix_i must belong to the ring RR.

sparse_row(R::Ring, J::Vector{Tuple{Int, Int}}) -> SRow

Constructs the sparse row (ai)i(a_i)_i over RR with aij=xja_{i_j} = x_j, where J=(ij,xj)jJ = (i_j, x_j)_j.

sparse_rowMethod
sparse_row(R::Ring, J::Vector{Int}, V::Vector{T}) -> SRow{T}

Constructs the sparse row (ai)i(a_i)_i over RR with aij=xja_{i_j} = x_j, where J=(ij)jJ = (i_j)_j and V=(xj)jV = (x_j)_j.

Basic operations

Rows support the usual operations:

  • +, -, ==
  • multiplication by scalars
  • div, divexact
getindexMethod
getindex(A::SRow, j::Int) -> RingElem

Given a sparse row (ai)i(a_i)_{i} and an index jj return aja_j.

add_scaled_rowMethod
add_scaled_row(A::SRow{T}, B::SRow{T}, c::T) -> SRow{T}

Returns the row cA+Bc A + B.

add_scaled_rowMethod
add_scaled_row(A::SRow{T}, B::SRow{T}, c::T) -> SRow{T}

Returns the row cA+Bc A + B.

transform_rowMethod
transform_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)(aA + bB, cA + dB).

lengthMethod
length(A::SRow)

Returns the number of nonzero entries of AA.

Change of base ring

change_base_ringMethod
change_base_ring(R::Ring, A::SRow) -> SRow

Create a new sparse row by coercing all elements into the ring RR.

Maximum, minimum and 2-norm

maximumMethod
maximum(A::SRow{T}) -> T

Returns the largest entry of AA.

maximumMethod
maximum(A::SRow{T}) -> T

Returns the largest entry of AA.

minimumMethod
minimum(A::SRow{T}) -> T

Returns the smallest entry of AA.

  minimum(A::NfRelOrdIdl) -> NfOrdIdl
  minimum(A::NfRelOrdIdl) -> NfRelOrdIdl

Returns the ideal AOA \cap O where OO is the maximal order of the coefficient ideals of AA.

minimumMethod
minimum(A::SRow{T}) -> T

Returns the smallest entry of AA.

norm2Method
norm2(A::SRow{T} -> T

Returns AAtA \cdot A^t.

Functionality for integral sparse rows

liftMethod
lift(A::SRow{zzModRingElem}) -> SRow{ZZRingElem}

Return the sparse row obtained by lifting all entries in AA.

mod!Method
mod!(A::SRow{ZZRingElem}, n::ZZRingElem) -> SRow{ZZRingElem}

Inplace reduction of all entries of AA modulo nn to the positive residue system.

mod_sym!Method
mod_sym!(A::SRow{ZZRingElem}, n::ZZRingElem) -> SRow{ZZRingElem}

Inplace reduction of all entries of AA modulo nn to the symmetric residue system.

mod_sym!Method
mod_sym!(A::SRow{ZZRingElem}, n::Integer) -> SRow{ZZRingElem}

Inplace reduction of all entries of AA modulo nn to the symmetric residue system.

maximumMethod
maximum(abs, A::SRow{ZZRingElem}) -> ZZRingElem

Returns the largest, in absolute value, entry of AA.

Sparse matrices

Let RR be a commutative ring. Sparse matrices with base ring RR 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 Matn×m(R)\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_matrixMethod
sparse_matrix(R::Ring) -> SMat

Return an empty sparse matrix with base ring RR.

sparse_matrixMethod
sparse_matrix(R::Ring, n::Int, m::Int) -> SMat

Return a sparse nn times mm zero matrix over RR.

Sparse matrices can also be created from dense matrices as well as from julia arrays:

sparse_matrixMethod
sparse_matrix(A::MatElem; keepzrows::Bool = true)

Constructs the sparse matrix corresponding to the dense matrix AA. If keepzrows is false, then the constructor will drop any zero row of AA.

sparse_matrixMethod
sparse_matrix(R::Ring, A::Matrix{T}) -> SMat

Constructs the sparse matrix over RR corresponding to AA.

sparse_matrixMethod
sparse_matrix(R::Ring, A::Matrix{T}) -> SMat

Constructs the sparse matrix over RR corresponding to AA.

The normal way however, is to add rows:

push!Method
push!(A::SMat{T}, B::SRow{T}) where T

Appends the sparse row B to A.

Sparse matrices can also be concatenated to form larger ones:

vcat!Method
vcat!(A::SMat, B::SMat) -> SMat

Vertically joins AA and BB inplace, that is, the rows of BB are appended to AA.

vcatMethod
vcat(A::SMat, B::SMat) -> SMat

Vertically joins AA and BB.

hcat!Method
hcat!(A::SMat, B::SMat) -> SMat

Horizontally concatenates AA and BB, inplace, changing AA.

hcatMethod
hcat(A::SMat, B::SMat) -> SMat

Horizontally concatenates AA and BB.

(Normal julia catcat is also supported)

There are special constructors:

identity_matrixMethod
identity_matrix(::Type{SMat}, R::Ring, n::Int)

Return a sparse nn times nn identity matrix over RR.

zero_matrixMethod
zero_matrix(::Type{SMat}, R::Ring, n::Int)

Return a sparse nn times nn zero matrix over RR.

zero_matrixMethod
zero_matrix(::Type{SMat}, R::Ring, n::Int, m::Int)

Return a sparse nn times mm zero matrix over RR.

Slices:

subMethod
sub(A::SMat, r::UnitRange, c::UnitRange) -> SMat

Return the submatrix of AA, where the rows correspond to rr and the columns correspond to cc.

Transpose:

transposeMethod
transpose(A::SMat) -> SMat

Returns the transpose of AA.

Elementary Properties

sparsityMethod
sparsity(A::SMat) -> Float64

Return the sparsity of A, that is, the number of zero-valued elements divided by the number of all elements.

densityMethod
density(A::SMat) -> Float64

Return the density of A, that is, the number of nonzero-valued elements divided by the number of all elements.

nnzMethod
nnz(A::SMat) -> Int

Return the number of non-zero entries of AA.

nrowsMethod
nrows(A::SMat) -> Int

Return the number of rows of AA.

ncolsMethod
ncols(A::SMat) -> Int

Return the number of columns of AA.

isoneMethod
isone(A::SMat)

Tests if AA is an identity matrix.

iszeroMethod
iszero(A::SMat)

Tests if AA is a zero matrix.

isupper_triangularMethod
isupper_triangular(A::SMat)

Returns true if and only if AA is upper (right) triangular.

maximumMethod
maximum(A::SMat{T}) -> T

Finds the largest entry of AA.

minimumMethod
minimum(A::SMat{T}) -> T

Finds the smallest entry of AA.

maximumMethod
maximum(abs, A::SMat{ZZRingElem}) -> ZZRingElem

Finds the largest, in absolute value, entry of AA.

elementary_divisorsMethod
elementary_divisors(A::SMat{ZZRingElem}) -> Vector{ZZRingElem}

The elementary divisors of AA, i.e. the diagonal elements of the Smith normal form of AA.

solve_dixon_sfMethod
solve_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}, ZZRingElem

For a sparse square matrix AA of full rank and a sparse matrix (row), find a sparse matrix (row) xx and an integer dd s.th. xA=bdx A = bd holds. The algorithm is a Dixon-based linear p-adic lifting method. If \code{is_int} is given, then dd is assumed to be 11. In this case rational reconstruction is avoided.

hadamard_bound2Method
hadamard_bound2(A::SMat{T}) -> T

The square of the product of the norms of the rows of AA.

echelon_with_transformMethod
echelon_with_transform(A::SMat{zzModRingElem}) -> SMat, SMat

Find a unimodular matrix TT and an upper-triangular EE s.th. TA=ETA = E holds.

reduce_fullMethod
reduce_full(A::SMat{ZZRingElem}, g::SRow{ZZRingElem},
                      trafo = Val{false}) -> SRow{ZZRingElem}, Vector{Int}

Reduces gg modulo AA and assumes that AA is upper triangular.

The second return value is the array of pivot elements of AA that changed.

If trafo is set to Val{true}, then additionally an array of transformations is returned.

hnf!Method
hnf!(A::SMat{ZZRingElem})

Inplace transform of AA into upper right Hermite normal form.

hnfMethod
hnf(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}

Return the upper right Hermite normal form of AA.

snfMethod
snf(A::SMat{ZZRingElem})

The Smith normal form (snf) of AA.

hnf_extend!Method
hnf_extend!(A::SMat{ZZRingElem}, b::SMat{ZZRingElem}, offset::Int = 0) -> SMat{ZZRingElem}

Given a matrix AA in HNF, extend this to get the HNF of the concatenation with bb.

is_diagonalMethod
is_diagonal(A::SMat) -> Bool

True iff only the i-th entry in the i-th row is non-zero.

detMethod
det(A::SMat{ZZRingElem})

The determinant of AA using a modular algorithm. Uses the dense (zzModMatrix) determinant on AA for various primes pp.

det_mcMethod
det_mc(A::SMat{ZZRingElem})

Computes the determinant of AA using a LasVegas style algorithm, i.e. the result is not proven to be correct. Uses the dense (zzModMatrix) determinant on AA for various primes pp.

valence_mcMethod
valence_mc{T}(A::SMat{T}; extra_prime = 2, trans = Vector{SMatSLP_add_row{T}}()) -> T

Uses a Monte-Carlo algorithm to compute the valence of AA. The valence is the valence of the minimal polynomial ff of transpose(A)Atranspose(A)*A, thus the last non-zero coefficient, typically f(0)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 * AA is computed instead.

saturateMethod
saturate(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}

Computes the saturation of AA, that is, a basis for $\mathbf{Q}\otimes M \meet \mathbf{Z}^n$, where MM is the row span of AA and nn the number of rows of AA.

Equivalently, return TATA for an invertible rational matrix TT, such that TATA is integral and the elementary divisors of TATA are all trivial.

hnf_kannan_bachemMethod
hnf_kannan_bachem(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}

Compute the Hermite normal form of AA using the Kannan-Bachem algorithm.

diagonal_formMethod
diagonal_form(A::SMat{ZZRingElem}) -> SMat{ZZRingElem}

A matrix DD that is diagonal and obtained via unimodular row and column operations. Like a snf without the divisibility condition.

Manipulation/ Access

getindexMethod
getindex(A::SMat, i::Int, j::Int)

Given a sparse matrix A=(aij)i,jA = (a_{ij})_{i, j}, return the entry aija_{ij}.

getindexMethod
getindex(A::SMat, i::Int) -> SRow

Given a sparse matrix AA and an index ii, return the ii-th row of AA.

setindex!Method
setindex!(A::SMat, b::SRow, i::Int)

Given a sparse matrix AA, a sparse row bb and an index ii, set the ii-th row of AA equal to bb.

swap_rows!Method
swap_rows!(A::SMat{T}, i::Int, j::Int)

Swap the ii-th and jj-th row of AA inplace.

swap_cols!Method
swap_cols!(A::SMat, i::Int, j::Int)

Swap the ii-th and jj-th column of AA inplace.

scale_row!Method
scale_row!(A::SMat{T}, i::Int, c::T)

Multiply the ii-th row of AA by cc inplace.

add_scaled_col!Method
add_scaled_col!(A::SMat{T}, i::Int, j::Int, c::T)

Add cc times the ii-th column to the jj-th column of AA inplace, that is, AjAj+cAiA_j \rightarrow A_j + c \cdot A_i, where (Ai)i(A_i)_i denote the columns of AA.

add_scaled_row!Method
add_scaled_row!(A::SMat{T}, i::Int, j::Int, c::T)

Add cc times the ii-th row to the jj-th row of AA inplace, that is, AjAj+cAiA_j \rightarrow A_j + c \cdot A_i, where (Ai)i(A_i)_i denote the rows of AA.

transform_row!Method
transform_row!(A::SMat{T}, i::Int, j::Int, a::T, b::T, c::T, d::T)

Applies the transformation (Ai,Aj)(aAi+bAj,cAi+dAj)(A_i, A_j) \rightarrow (aA_i + bA_j, cA_i + dA_j) to AA, where (Ai)i(A_i)_i are the rows of AA.

diagonalMethod
diagonal(A::SMat) -> ZZRingElem[]

The diagonal elements of AA in an array.

reverse_rows!Method
reverse_rows!(A::SMat)

Inplace inversion of the rows of AA.

mod_sym!Method
mod_sym!(A::SMat{ZZRingElem}, n::ZZRingElem)

Inplace reduction of all entries of AA modulo nn to the symmetric residue system.

find_row_starting_withMethod
find_row_starting_with(A::SMat, p::Int) -> Int

Tries to find the index ii such that Ai,p0A_{i,p} \neq 0 and Ai,pj=0A_{i, p-j} = 0 for all j>1j > 1. It is assumed that AA is upper triangular. If such an index does not exist, find the smallest index larger.

reduceMethod
reduce(A::SMat{ZZRingElem}, g::SRow{ZZRingElem}, m::ZZRingElem) -> SRow{ZZRingElem}

Given an upper triangular matrix AA over the integers, a sparse row gg and an integer mm, this function reduces gg modulo AA and returns gg modulo mm with respect to the symmetric residue system.

reduceMethod
reduce(A::SMat{ZZRingElem}, g::SRow{ZZRingElem}) -> SRow{ZZRingElem}

Given an upper triangular matrix AA over a field and a sparse row gg, this function reduces gg modulo AA.

reduceMethod
reduce(A::SMat{T}, g::SRow{T}) -> SRow{T}

Given an upper triangular matrix AA over a field and a sparse row gg, this function reduces gg modulo AA.

rand_rowMethod
rand_row(A::SMat) -> SRow

Return a random row of the sparse matrix AA.

Changing of the ring:

map_entriesMethod
map_entries(f, A::SMat) -> SMat

Given a sparse matrix AA and a callable object ff, this function will construct a new sparse matrix by applying ff to all elements of AA.

change_base_ringMethod
change_base_ring(R::Ring, A::SMat)

Create a new sparse matrix by coercing all elements into the ring RR.

Arithmetic

Matrices support the usual operations as well

  • +, -, ==
  • div, divexact by scalars
  • multiplication by scalars

Various products:

mulMethod
mul(A::SMat{T}, b::AbstractVector{T}) -> Vector{T}

Return the product AbA \cdot b as a dense vector.

mulMethod
mul(A::SMat{T}, b::AbstractMatrix{T}) -> Matrix{T}

Return the product AbA \cdot b as a dense array.

mulMethod
mul(A::SMat{T}, b::MatElem{T}) -> MatElem

Return the product AbA \cdot b as a dense matrix.

mulMethod
mul(A::SRow, B::SMat) -> SRow

Return the product ABA\cdot B as a sparse row.

Other:

sparseMethod
sparse(A::SMat) -> SparseMatrixCSC

The same matrix, but as a sparse matrix of julia type SparseMatrixCSC.

ZZMatrixMethod
ZZMatrix(A::SMat{ZZRingElem})

The same matrix AA, but as an ZZMatrix.

ZZMatrixMethod
ZZMatrix(A::SMat{T}) where {T <: Integer}

The same matrix AA, but as an ZZMatrix. Requires a conversion from the base ring of AA to ZZ\mathbb ZZ.

ArrayMethod
Array(A::SMat{T}) -> Matrix{T}

The same matrix, but as a two-dimensional julia array.

This documentation is not for the latest stable release, but for either the development version or an older release.
Click here to go to the documentation for the latest stable release.