Safe Haskell | None |
---|---|

Language | Haskell2010 |

## Synopsis

- newtype Matrix :: Nat -> Nat -> Type -> Type where
- newtype Vec :: Nat -> Type -> Type where
- type MatrixXf n m = Matrix n m Float
- type MatrixXd n m = Matrix n m Double
- type MatrixXcf n m = Matrix n m (Complex Float)
- type MatrixXcd n m = Matrix n m (Complex Double)
- class (Num a, Cast a, Storable a, Storable (C a), Code (C a)) => Elem a
- type family C a = (result :: Type) | result -> a
- natToInt :: forall n. KnownNat n => Int
- data Row (r :: Nat) = Row
- data Col (c :: Nat) = Col
- encode :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> ByteString
- decode :: (Elem a, KnownNat n, KnownNat m) => ByteString -> Matrix n m a
- null :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Bool
- square :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Bool
- rows :: forall n m a. KnownNat n => Matrix n m a -> Int
- cols :: forall n m a. KnownNat m => Matrix n m a -> Int
- dims :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> (Int, Int)
- empty :: Elem a => Matrix 0 0 a
- constant :: forall n m a. (Elem a, KnownNat n, KnownNat m) => a -> Matrix n m a
- zero :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a
- ones :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a
- identity :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a
- random :: forall n m a. (Elem a, KnownNat n, KnownNat m) => IO (Matrix n m a)
- diagonal :: (Elem a, KnownNat n, KnownNat m, r ~ Min n m, KnownNat r) => Matrix n m a -> Matrix r 1 a
- (!) :: forall n m a r c. (Elem a, KnownNat n, KnownNat r, KnownNat c, r <= n, c <= m) => Row r -> Col c -> Matrix n m a -> a
- coeff :: forall n m a r c. (Elem a, KnownNat n, KnownNat r, KnownNat c, r <= n, c <= m) => Row r -> Col c -> Matrix n m a -> a
- generate :: forall n m a. (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a) -> Matrix n m a
- sum :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- prod :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- mean :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- trace :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- all :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Bool
- any :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Bool
- count :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Int
- norm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- squaredNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- blueNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- hypotNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a
- determinant :: forall n a. (Elem a, KnownNat n) => Matrix n n a -> a
- add :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a -> Matrix n m a
- sub :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a -> Matrix n m a
- mul :: (Elem a, KnownNat p, KnownNat q, KnownNat r) => Matrix p q a -> Matrix q r a -> Matrix p r a
- map :: Elem a => (a -> a) -> Matrix n m a -> Matrix n m a
- imap :: (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a -> a) -> Matrix n m a -> Matrix n m a
- data TriangularMode
- triangularView :: (Elem a, KnownNat n, KnownNat m) => TriangularMode -> Matrix n m a -> Matrix n m a
- filter :: Elem a => (a -> Bool) -> Matrix n m a -> Matrix n m a
- ifilter :: (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a -> Bool) -> Matrix n m a -> Matrix n m a
- length :: forall n m a r. (Elem a, KnownNat n, KnownNat m, r ~ (n * m), KnownNat r) => Matrix n m a -> Int
- foldl :: (Elem a, KnownNat n, KnownNat m) => (b -> a -> b) -> b -> Matrix n m a -> b
- foldl' :: Elem a => (b -> a -> b) -> b -> Matrix n m a -> b
- inverse :: forall n a. (Elem a, KnownNat n) => Matrix n n a -> Matrix n n a
- adjoint :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix m n a
- transpose :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix m n a
- conjugate :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a
- normalize :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a
- modify :: (Elem a, KnownNat n, KnownNat m) => (forall s. MMatrix n m s a -> ST s ()) -> Matrix n m a -> Matrix n m a
- block :: forall sr sc br bc n m a. (Elem a, KnownNat sr, KnownNat sc, KnownNat br, KnownNat bc, KnownNat n, KnownNat m) => (sr <= n, sc <= m, br <= n, bc <= m) => Row sr -> Col sc -> Row br -> Col bc -> Matrix n m a -> Matrix br bc a
- unsafeFreeze :: (Elem a, KnownNat n, KnownNat m, PrimMonad p) => MMatrix n m (PrimState p) a -> p (Matrix n m a)
- unsafeWith :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> (Ptr (C a) -> CInt -> CInt -> IO b) -> IO b
- fromList :: forall n m a. (Elem a, KnownNat n, KnownNat m) => [[a]] -> Maybe (Matrix n m a)
- toList :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> [[a]]

# Types

newtype Matrix :: Nat -> Nat -> Type -> Type where Source #

Matrix to be used in pure computations.

- Uses column majour memory layout.
- Has a copy-free FFI using the Eigen library.

newtype Vec :: Nat -> Type -> Type where Source #

Used internally to track the size and corresponding C type of the matrix.

type MatrixXcf n m = Matrix n m (Complex Float) Source #

Alias for single precision matrix of complex numbers

type MatrixXcd n m = Matrix n m (Complex Double) Source #

Alias for double precision matrix of complex numbers

# Common API

class (Num a, Cast a, Storable a, Storable (C a), Code (C a)) => Elem a Source #

`Elem`

is a closed typeclass that encompasses the properties
eigen expects its values to possess, and simplifies the external
API quite a bit.

# Encode/Decode a Matrix

encode :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> ByteString Source #

Encode the sparse matrix as a lazy bytestring

decode :: (Elem a, KnownNat n, KnownNat m) => ByteString -> Matrix n m a Source #

Decode the sparse matrix from a lazy bytestring

# Querying a Matrix

square :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Bool Source #

Is matrix square?

dims :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> (Int, Int) Source #

Return Matrix size as a pair of (rows, cols)

# Constructing a Matrix

constant :: forall n m a. (Elem a, KnownNat n, KnownNat m) => a -> Matrix n m a Source #

Matrix where all coeffs are filled with the given value

zero :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a Source #

Matrix where all coeffs are filled with 0

ones :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a Source #

Matrix where all coeffs are filled with 1

identity :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a Source #

The identity matrix (not necessarily square)

random :: forall n m a. (Elem a, KnownNat n, KnownNat m) => IO (Matrix n m a) Source #

The random matrix of a given size

diagonal :: (Elem a, KnownNat n, KnownNat m, r ~ Min n m, KnownNat r) => Matrix n m a -> Matrix r 1 a Source #

Return the diagonal of a matrix.

(!) :: forall n m a r c. (Elem a, KnownNat n, KnownNat r, KnownNat c, r <= n, c <= m) => Row r -> Col c -> Matrix n m a -> a Source #

Return the value at the given position.

coeff :: forall n m a r c. (Elem a, KnownNat n, KnownNat r, KnownNat c, r <= n, c <= m) => Row r -> Col c -> Matrix n m a -> a Source #

Return the value at the given position.

generate :: forall n m a. (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a) -> Matrix n m a Source #

Given a generation function `f :: Int -> Int -> a`, construct a Matrix of known size using points in the matrix as inputs.

sum :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

The sum of all coefficients in the matrix

prod :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

The product of all coefficients in the matrix

mean :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

The arithmetic mean of all coefficients in the matrix

all :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Bool Source #

Given a predicate p, determine if all values in the Matrix satisfy p.

any :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Bool Source #

Given a predicate p, determine if any values in the Matrix satisfy p.

count :: (Elem a, KnownNat n, KnownNat m) => (a -> Bool) -> Matrix n m a -> Int Source #

Given a predicate p, determine how many values in the Matrix satisfy p.

norm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

For vectors, the l2 norm, and for matrices the Frobenius norm. In both cases, it consists in the square root of the sum of the square of all the matrix entries. For vectors, this is also equals to the square root of the dot product of this with itself.

squaredNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

For vectors, the squared l2 norm, and for matrices the Frobenius norm. In both cases, it consists in the sum of the square of all the matrix entries. For vectors, this is also equals to the dot product of this with itself.

blueNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

The l2 norm of the matrix using the Blue's algorithm. A Portable Fortran Program to Find the Euclidean Norm of a Vector, ACM TOMS, Vol 4, Issue 1, 1978.

hypotNorm :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> a Source #

The l2 norm of the matrix avoiding undeflow and overflow. This version use a concatenation of hypot calls, and it is very slow.

determinant :: forall n a. (Elem a, KnownNat n) => Matrix n n a -> a Source #

The determinant of the matrix

add :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a -> Matrix n m a Source #

Add two matrices.

sub :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a -> Matrix n m a Source #

Subtract two matrices.

mul :: (Elem a, KnownNat p, KnownNat q, KnownNat r) => Matrix p q a -> Matrix q r a -> Matrix p r a Source #

Multiply two matrices.

map :: Elem a => (a -> a) -> Matrix n m a -> Matrix n m a Source #

Apply a given function to each element of the matrix. Here is an example how to implement scalar matrix multiplication:

`>>>`

`let a = fromList [[1,2],[3,4]] :: MatrixXf 2 2`

`>>>`

Matrix 2x2 1.0 2.0 3.0 4.0`a`

`>>>`

Matrix 2x2 10.0 20.0 30.0 40.0`map (*10) a`

imap :: (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a -> a) -> Matrix n m a -> Matrix n m a Source #

Apply a given function to each element of the matrix. Here is an example how upper triangular matrix can be implemented: >>> let a = fromList [[1,2,3],[4,5,6],[7,8,9]] :: MatrixXf >>> a Matrix 3x3 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 >>> imap (row col val -> if row <= col then val else 0) a Matrix 3x3 1.0 2.0 3.0 0.0 5.0 6.0 0.0 0.0 9.0

data TriangularMode Source #

Provide a view of the matrix for extraction of a subset.

Lower | View matrix as a lower triangular matrix. |

Upper | View matrix as an upper triangular matrix. |

StrictlyLower | View matrix as a lower triangular matrix with zeros on the diagonal. |

StrictlyUpper | View matrix as an upper triangular matrix with zeros on the diagonal. |

UnitLower | View matrix as a lower triangular matrix with ones on the diagonal. |

UnitUpper | View matrix as an upper triangular matrix with ones on the diagonal. |

## Instances

Enum TriangularMode Source # | |

Defined in Eigen.Matrix succ :: TriangularMode -> TriangularMode # pred :: TriangularMode -> TriangularMode # toEnum :: Int -> TriangularMode # fromEnum :: TriangularMode -> Int # enumFrom :: TriangularMode -> [TriangularMode] # enumFromThen :: TriangularMode -> TriangularMode -> [TriangularMode] # enumFromTo :: TriangularMode -> TriangularMode -> [TriangularMode] # enumFromThenTo :: TriangularMode -> TriangularMode -> TriangularMode -> [TriangularMode] # | |

Eq TriangularMode Source # | |

Defined in Eigen.Matrix (==) :: TriangularMode -> TriangularMode -> Bool # (/=) :: TriangularMode -> TriangularMode -> Bool # | |

Read TriangularMode Source # | |

Defined in Eigen.Matrix readsPrec :: Int -> ReadS TriangularMode # readList :: ReadS [TriangularMode] # | |

Show TriangularMode Source # | |

Defined in Eigen.Matrix showsPrec :: Int -> TriangularMode -> ShowS # show :: TriangularMode -> String # showList :: [TriangularMode] -> ShowS # |

triangularView :: (Elem a, KnownNat n, KnownNat m) => TriangularMode -> Matrix n m a -> Matrix n m a Source #

Triangular view extracted from the current matrix

filter :: Elem a => (a -> Bool) -> Matrix n m a -> Matrix n m a Source #

Filter elements in the matrix. Filtered elements will be replaced by 0.

ifilter :: (Elem a, KnownNat n, KnownNat m) => (Int -> Int -> a -> Bool) -> Matrix n m a -> Matrix n m a Source #

Filter elements in the matrix with an indexed predicate. Filtered elements will be replaces by 0.

length :: forall n m a r. (Elem a, KnownNat n, KnownNat m, r ~ (n * m), KnownNat r) => Matrix n m a -> Int Source #

The length of the matrix.

foldl :: (Elem a, KnownNat n, KnownNat m) => (b -> a -> b) -> b -> Matrix n m a -> b Source #

Left fold of a matrix, where accumulation is lazy.

foldl' :: Elem a => (b -> a -> b) -> b -> Matrix n m a -> b Source #

Right fold of a matrix, where accumulation is strict.

inverse :: forall n a. (Elem a, KnownNat n) => Matrix n n a -> Matrix n n a Source #

Inverse of the matrix For small fixed sizes up to 4x4, this method uses cofactors. In the general case, this method uses PartialPivLU decomposition

adjoint :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix m n a Source #

Adjoint of the matrix

transpose :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix m n a Source #

Transpose of the matrix

conjugate :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a Source #

Conjugate of the matrix

normalize :: forall n m a. (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> Matrix n m a Source #

Normalise the matrix by dividing it on its `norm`

modify :: (Elem a, KnownNat n, KnownNat m) => (forall s. MMatrix n m s a -> ST s ()) -> Matrix n m a -> Matrix n m a Source #

Apply a destructive operation to a matrix. The operation will be performed in-place, if it is safe to do so - otherwise, it will create a copy of the matrix.

:: (Elem a, KnownNat sr, KnownNat sc, KnownNat br, KnownNat bc, KnownNat n, KnownNat m) | |

=> (sr <= n, sc <= m, br <= n, bc <= m) | |

=> Row sr | starting row |

-> Col sc | starting col |

-> Row br | block of rows |

-> Col bc | block of cols |

-> Matrix n m a | extract from this |

-> Matrix br bc a | extraction |

Extract rectangular block from matrix defined by startRow startCol blockRows blockCols

unsafeFreeze :: (Elem a, KnownNat n, KnownNat m, PrimMonad p) => MMatrix n m (PrimState p) a -> p (Matrix n m a) Source #

Turn a mutable matrix into an immutable matrix without copying. The mutable matrix should not be modified after this conversion.

unsafeWith :: (Elem a, KnownNat n, KnownNat m) => Matrix n m a -> (Ptr (C a) -> CInt -> CInt -> IO b) -> IO b Source #

Pass a pointer to the matrix's data to the IO action. The data may not be modified through the pointer.