MyNixOS website logo
Description

A lightweight Haskell implementation of Smith normal form over the integers.

This package contains an implementation of Smith normal form for arbitrary matrices over the integers. In particular, it does not assume that the matrix is invertible or square.

SmithNormalForm

This is a simple Haskell implementation of Smith normal form for matrices over the integers, built upon the matrix library, and so emphasizes elegance and minimal dependencies over pure speed. In particular, it has no dependencies beyond those needed for the matrix library.

Linear algebra packages usually assume that you are working over a field, such as the rational numbers or the real numbers. Matrices over fields also have a Smith normal form, but as scaling a row by an arbitrary nonzero constant corresponds to an elementary matrix in linear algebra over a field, the resulting diagonal matrix is equivalent to one with r 1's and all other entries 0, where r is the rank of the matrix. More interesting information can be gleaned if we restrict ourselves to "linear algebra over the integers."

Example Usage

In ghci:

> import Data.Matrix
Data.Matrix> import Data.Matrix.SmithNormalForm 
Data.Matrix Data.Matrix.SmithNormalForm> a = fromList 4 4 [-2, 1, 1, 0, 1, -2, 1, 0, 1, 1, -3, 1, 0, 0, 1, -1] :: Matrix Integer
Data.Matrix Data.Matrix.SmithNormalForm> a
┌             ┐
│ -2  1  1  0 │
│  1 -2  1  0 │
│  1  1 -3  1 │
│  0  0  1 -1 │
└             ┘
Data.Matrix Data.Matrix.SmithNormalForm> smithNormalForm a
┌         ┐
│ 1 0 0 0 │
│ 0 1 0 0 │
│ 0 0 3 0 │
│ 0 0 0 0 │
└         ┘

Warning: For dense matrices beyond a small size, calculating the Smith normal form of a matrix can result in "coefficient explosion," that is, during the computation, the entries of the matrices involved in the computation can become very large, even when those of the initial matrix are small. So if your matrix has entries whose type consists of bounded integers (e.g. Int) as opposed to unbounded integers (e.g. Integer), you are likely to encounter integer overflow errors.


We recommend that you make sure that the matrices to which you're applying smithNormalForm have type Matrix Integer, especially if you found the remarks above baffling or if you're not sure what to do here.


In general, the main theorem regarding the existence and uniqueness of Smith normal form holds over any principal ideal domain (PID). The present implementation immediately extends to any Euclidean domain, but would require modifications for the general case.

What is Smith Normal Form?

The Smith normal form a matrix A can be thought of as the simplest matrix that is equivalent to A, in the sense that it can obtained by applying the kinds of transformations used in Gauss–Jordan elimination to both rows (probably the first thing you learn in linear algebra, in getting to reduced row echelon form) and columns (i.e. doing the same thing to the transposed matrix).

More precisely, the Smith normal form of a matrix A with entries in the integers is the unique matrix D (also with entries in the integers) with the following properties:

  • We have A = RDC where R and C are products of elementary matrices; here R and C can be taken to correspond to sequences of row and column operations, respectively. (Recall that the elementary matrices over the integers are matrices of determinant +1 or -1 that correspond to the operations of (i) multiplying a row or column by -1, (ii) switching two rows or two columns, or (iii) adding a scalar integer multiple of one row to another row or one column to another column.)

  • The entries of D are zero outside of the diagonal entries (i.e. D_{i,j} = 0 for i not equal to j).

  • The diagonal entries [d(1) = D_{1,1}, d(2) = D_{2,2},..,d(n) = D_{n,n}] of D are nonnegative and d(i) divides d(i+1) for i = 1, 2,..., n-1. (These numbers are called the invariant factors of the matrix A.)

Some Applications of Smith Normal Form

  • Compute the homology of a chain complex of finitely generated abelian groups.
  • Determine the structure of a finitely generated module over the integers.
  • Calculate the (abelian) sandpile group (a.k.a. critcal group, Jacobian group, Picard group) of a graph.

A number of combinatorial applications and related questions can be found in this 2016 survey by Stanley.

Metadata

Version

0.1.0.0

License

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows