LinAlg-0.2.0.0: An interface for specifying linear algebra computations

Safe HaskellNone
LanguageHaskell2010

Numeric.LinAlg

Description

This module provides an interface for specifying immutable linear algebra computations.

This is done with the Matr typeclass.

Synopsis

Documentation

data Dim

Constructors

V Nat 
M Nat Nat 

Instances

class Solve n dim | dim -> n where

Infix, overloaded versions of functions for solving linear equations

Methods

(<\>) :: Matr k arr => arr (M n n) k -> arr dim k -> arr dim k infixl 7

(^\) :: Matr k arr => arr (M n n) k -> arr dim k -> arr dim k infixl 7

(.\) :: Matr k arr => arr (M n n) k -> arr dim k -> arr dim k infixl 7

(\\) :: Matr k arr => arr (M n n) k -> arr dim k -> arr dim k infixl 7

Instances

Solve n (V n) 
Solve n (M n p) 

class Product a b where

Associated Types

type Prod a b :: Dim

Methods

(><) :: Matr k arr => arr a k -> arr b k -> arr (Prod a b) k infixl 7

Instances

Product (V n) (M n p) 
Product (M m n) (V n) 
Product (M m n) (M n p) 

class (Floating k, Scale k arr) => Matr k arr where

An instance of Matr k v m means that v k is a vector type and m k is a matrix type over the field k.

Methods

fromVect :: Vect n k -> arr (V n) k

Convert a list of elements to a vector.

toVect :: arr (V n) k -> Vect n k

Convert a vector to a list of its elements.

fromVects :: Vect m (Vect n k) -> arr (M m n) k

Convert a row-major list of lists of elements (which should all have the same length) to a matrix containing those elements.

toVects :: arr (M m n) k -> Vect m (Vect n k)

Convert a matrix to a list of its rows, each given as a list of elements.

toRows :: arr (M m n) k -> Vect m (arr (V n) k)

Convert a matrix to a list of its rows.

toColumns :: arr (M m n) k -> Vect n (arr (V m) k)

Convert a matrix to a list of its columns.

fromRows :: Vect m (arr (V n) k) -> arr (M m n) k

Convert a list of vectors to a matrix having those vectors as rows.

fromColumns :: Vect n (arr (V m) k) -> arr (M m n) k

Convert a list of vectors to a matrix having those vectors as columns.

asColMat :: arr (V n) k -> arr (M n 1) k

Regard a vector as a matrix with a single column.

asColVec :: arr (M n 1) k -> arr (V n) k

Convert a matrix which has only one column to a vector. This function may have undefined behavior if the input matrix has more than one column.

fromDiag :: arr (V n) k -> arr (M n n) k

Produce a diagonal matrix with the given vector along its diagonal (and zeros elsewhere).

takeDiag :: arr (M n n) k -> arr (V n) k

Return a vector of elements along the diagonal of the matrix. Does not necessarily fail if the matrix is not square.

dim :: arr (M m n) k -> (SNat m, SNat n)

Dimension of a matrix (rows, columns).

rows :: arr (M m n) k -> SNat m

The number of rows in a matrix.

cols :: arr (M m n) k -> SNat n

The number of columns in a matrix.

len :: arr (V n) k -> SNat n

The length of a vector.

trans :: arr (M m n) k -> arr (M n m) k

Transpose a matrix.

ident :: SNat n -> arr (M n n) k

Construct the identity matrix of a given dimension.

outer :: arr (V m) k -> arr (V n) k -> arr (M m n) k

Compute the outer product of two vectors.

(>.<) :: arr (V n) k -> arr (V n) k -> k infixl 7

Compute the dot product (i.e., inner product) of two vectors.

mXm :: arr (M m n) k -> arr (M n p) k -> arr (M m p) k

elementwiseprod :: arr (M m n) k -> arr (M m n) k -> arr (M m n) k

Compute the elementwise product (i.e., Hadamard product) of two matrices.

inv :: arr (M n n) k -> arr (M n n) k

General matrix inverse.

invL :: arr (M n n) k -> arr (M n n) k

Inverse of a lower-triangular matrix.

invU :: arr (M n n) k -> arr (M n n) k

Inverse of an upper-triangular matrix.

chol :: arr (M n n) k -> arr (M n n) k

Cholesky decomposition of a positive-definite symmetric matrix. Returns lower triangular matrix of the decomposition. May not necessarily zero out the upper portion of the matrix.

cholInv :: arr (M n n) k -> arr (M n n) k

Invert a positive-definite symmetric system using a precomputed Cholesky decomposition. That is, if l == chol a and b == cholInv l , then b is the inverse of a .

cholLnDet :: arr (M n n) k -> k

Compute the log-determinant of a positive-definite symmetric matrix using its precomputed Cholesky decomposition. That is, if l == chol a and d == cholLnDet l , then d is the log-determinant of a .

trsymprod :: arr (M n n) k -> arr (M n n) k -> k

If matrices a and b are symmetric, then trsymprod a b is the trace of a >< b . Note that in this case, the trace of a >< b is just the sum of the elements in the element-wise product of a and b .

constant :: k -> SNat n -> arr (V n) k

Create a vector of a given length whose elements all have the same value.

linearSolve :: arr (M n n) k -> arr (M n p) k -> arr (M n p) k

Solve a general system. If there is some x such that m >< x == b , then x == 'linearSolve m b' .

ltriSolve :: arr (M n n) k -> arr (M n p) k -> arr (M n p) k

Solve a lower-triangular system. If l is a lower-triangular matrix, then x == ltriSolve l b means that l >< x == b .

utriSolve :: arr (M n n) k -> arr (M n p) k -> arr (M n p) k

Solve a upper-triangular system. If u is a upper-triangular matrix, then x == utriSolve u b means that u >< x == b .

posdefSolve :: arr (M n n) k -> arr (M n p) k -> arr (M n p) k

Solve a positive-definite symmetric system. That is, if a is a positive-definite symmetric matrix and x == posdefSolve a b , then a >< x == b .

cholSolve :: arr (M n n) k -> arr (M n p) k -> arr (M n p) k

Solve a positive-definite symmetric system using a precomputed Cholesky decomposition. If l == chol a and x == l `'cholSolve'\` b , then a >< x == b .

Instances

(Floating k, Num (Matrix k), Num (Vector k), Field k, Numeric k) => Matr k HArr 

square :: Matr k arr => arr (M m n) k -> Maybe (m :~: n)

If the matrix is square, return Just its dimension; otherwise, Nothing.

showMat :: (Show k, Matr k arr) => arr (M m n) k -> String

Pretty-print a matrix. (Actual prettiness not guaranteed!)

toLists :: Matr k arr => arr (M m n) k -> [[k]]

class Scale k arr where

Scalar multiplication for vector spaces.

Methods

(.*) :: k -> arr dim k -> arr dim k infixl 7

Instances