HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

ModuleAgda-2.8.0.2Haskell2010

Agda.Termination.SparseMatrix

Sparse matrices.

We assume the matrices to be very sparse, so we just implement them as sorted association lists.

Most operations are linear in the number of non-zero elements.

An exception is transposition, which needs to sort the association list again; it has the complexity of sorting: n log n where n is the number of non-zero elements.

Another exception is matrix multiplication, of course.

Contents
  1. Basic data types
  2. Generating and creating matrices
  3. Combining and querying matrices
  4. Modifying matrices
Synopsis
  • PackageAgda-2.8.0.2
  • Exports23
  • LanguageHaskell2010
  • LicenceMIT
  • SourceSparseMatrix.hs

Basic data types

datadata Matrix i b

Type of matrices, parameterised on the type of values.

Sparse matrices are implemented as an ordered association list, mapping coordinates to values.

Constructors

Instances11Functor, Foldable, Traversable, Eq, Ord, Show, …
  • Functor (Matrix i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Foldable (Matrix i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Traversable (Matrix i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • (Eq i, Eq b) => Eq (Matrix i b)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • (Ord i, Ord b) => Ord (Matrix i b)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • (Integral i, HasZero b, Show i, Show b) => Show (Matrix i b)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • (Integral i, HasZero b, Pretty b) => Pretty (Matrix i b)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • (Ord i, PartialOrd a) => PartialOrd (Matrix i a)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix

    Pointwise comparison. Only matrices with the same dimension are comparable.

  • (Ord i, HasZero o, NotWorse o) => NotWorse (Matrix i o)Defined in Agda-2.8.0.2 · Agda.Termination.Order

    We assume the matrices have the same dimension.

  • Ord i => Transpose (Matrix i b)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix

    Matrix transposition.

    O(n log n) where n is the number of non-zero elements in the matrix.

  • (Integral i, HasZero b) => Diagonal (Matrix i b) bDefined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix

    Diagonal of sparse matrix.

    O(n) where n is the number of non-zero elements in the matrix.

datadata Size i

Size of a matrix.

Constructors

  • Size
    • rows :: i

      Number of rows, >= 0.

    • cols :: i

      Number of columns, >= 0.

Instances4Eq, Ord, Show, Transpose
  • Eq i => Eq (Size i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Ord i => Ord (Size i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Show i => Show (Size i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Transpose (Size i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix

    Size of transposed matrix.

datadata MIx i

Type of matrix indices (row, column).

Constructors

  • MIx
    • row :: i

      Row index, 1 <= row <= rows.

    • col :: i

      Column index 1 <= col <= cols.

Instances5Eq, Ord, Show, Ix, Transpose
  • Eq i => Eq (MIx i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Ord i => Ord (MIx i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Show i => Show (MIx i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Ix i => Ix (MIx i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix
  • Transpose (MIx i)Defined in Agda-2.8.0.2 · Agda.Termination.SparseMatrix

    Transposing coordinates.

Generating and creating matrices

valuefromIndexList :: (Ord i, HasZero b) => Size i -> [(MIx i, b)] -> Matrix i b

Constructs a matrix from a list of (index, value)-pairs. O(n) where n is size of the list.

Precondition: indices are unique.

Combining and querying matrices

valuezipMatrices
  1. :: Ord i
  2. => (a -> c)

    Element only present in left matrix.

  3. -> (b -> c)

    Element only present in right matrix.

  4. -> (a -> b -> c)

    Element present in both matrices.

  5. -> (c -> Bool)

    Result counts as zero?

  6. -> Matrix i a
  7. -> Matrix i b
  8. -> Matrix i c

General pointwise combination function for sparse matrices. O(n1 + n2).

valueinterAssocWith :: Ord i => (a -> a -> a) -> [(i, a)] -> [(i, a)] -> [(i, a)]

Association list intersection. O(n1 + n2).

interAssocWith f l l' = { (i, f a b) | (i,a) ∈ l and (i,b) ∈ l' }

Used to combine sparse matrices, it might introduce zero elements if f can return zero for non-zero arguments.

valuemul :: (Ix i, Eq a) => Semiring a -> Matrix i a -> Matrix i a -> Matrix i a

mul semiring m1 m2 multiplies matrices m1 and m2. Uses the operations of the semiring semiring to perform the multiplication.

O(n1 + n2 log n2 + Σ(i <= r1) Σ(j <= c2) d(i,j)) where r1 is the number of non-empty rows in m1 and c2 is the number of non-empty columns in m2 and d(i,j) is the bigger one of the following two quantifies: the length of sparse row i in m1 and the length of sparse column j in m2.

Given dimensions m1 : r1 × c1 and m2 : r2 × c2, a matrix of size r1 × c2 is returned. It is not necessary that c1 == r2, the matrices are implicitly patched with zeros to match up for multiplication. For sparse matrices, this patching is a no-op.

classclass Diagonal m e | m -> e where

diagonal m extracts the diagonal of m.

For non-square matrices, the length of the diagonal is the minimum of the dimensions of the matrix.

Methods

Instances3Diagonal
valuetoSparseRows :: Eq i => Matrix i b -> [(i, [(i, b)])]

Converts a sparse matrix to a sparse list of rows. O(n) where n is the number of non-zero entries of the matrix.

Only non-empty rows are generated.

valuezipAssocWith
  1. :: Ord i
  2. => ([(i, a)] -> [(i, c)])

    Only left map remaining.

  3. -> ([(i, b)] -> [(i, c)])

    Only right map remaining.

  4. -> (a -> Maybe c)

    Element only present in left map.

  5. -> (b -> Maybe c)

    Element only present in right map.

  6. -> (a -> b -> Maybe c)

    Element present in both maps.

  7. -> [(i, a)]
  8. -> [(i, b)]
  9. -> [(i, c)]

General pointwise combination function for association lists. O(n1 + n2) where ni is the number of non-zero element in matrix i.

In zipAssocWith fs gs f g h l l',

fs is possibly more efficient version of mapMaybe ( (i, a) -> (i,) $ f a), and same for gs and g.

Modifying matrices