Safe Haskell | None |
---|---|
Language | Haskell98 |
- data Matrix = Matrix {}
- valid :: Matrix -> Bool
- fromList :: [[Double]] -> Matrix
- toList :: Matrix -> [[Double]]
- generate :: Int -> Int -> (Int -> Int -> Double) -> Matrix
- empty :: Matrix
- null :: Matrix -> Bool
- square :: Matrix -> Bool
- zero :: Int -> Int -> Matrix
- ones :: Int -> Int -> Matrix
- identity :: Int -> Matrix
- constant :: Int -> Int -> Double -> Matrix
- random :: Int -> Int -> IO Matrix
- cols :: Matrix -> Int
- rows :: Matrix -> Int
- (!) :: Matrix -> (Int, Int) -> Double
- coeff :: Int -> Int -> Matrix -> Double
- unsafeCoeff :: Int -> Int -> Matrix -> Double
- col :: Int -> Matrix -> [Double]
- row :: Int -> Matrix -> [Double]
- block :: Int -> Int -> Int -> Int -> Matrix -> Matrix
- topRows :: Int -> Matrix -> Matrix
- bottomRows :: Int -> Matrix -> Matrix
- leftCols :: Int -> Matrix -> Matrix
- rightCols :: Int -> Matrix -> Matrix
- sum :: Matrix -> Double
- prod :: Matrix -> Double
- mean :: Matrix -> Double
- minCoeff :: Matrix -> Double
- maxCoeff :: Matrix -> Double
- trace :: Matrix -> Double
- norm :: Matrix -> Double
- squaredNorm :: Matrix -> Double
- blueNorm :: Matrix -> Double
- hypotNorm :: Matrix -> Double
- determinant :: Matrix -> Double
- fold :: (a -> Double -> a) -> a -> Matrix -> a
- fold' :: (a -> Double -> a) -> a -> Matrix -> a
- ifold :: (Int -> Int -> a -> Double -> a) -> a -> Matrix -> a
- ifold' :: (Int -> Int -> a -> Double -> a) -> a -> Matrix -> a
- all :: (Double -> Bool) -> Matrix -> Bool
- any :: (Double -> Bool) -> Matrix -> Bool
- count :: (Double -> Bool) -> Matrix -> Int
- add :: Matrix -> Matrix -> Matrix
- sub :: Matrix -> Matrix -> Matrix
- mul :: Matrix -> Matrix -> Matrix
- map :: (Double -> Double) -> Matrix -> Matrix
- imap :: (Int -> Int -> Double -> Double) -> Matrix -> Matrix
- filter :: (Double -> Bool) -> Matrix -> Matrix
- ifilter :: (Int -> Int -> Double -> Bool) -> Matrix -> Matrix
- diagonal :: Matrix -> Matrix
- transpose :: Matrix -> Matrix
- inverse :: Matrix -> Matrix
- adjoint :: Matrix -> Matrix
- conjugate :: Matrix -> Matrix
- normalize :: Matrix -> Matrix
- modify :: (forall s. MMatrix s -> ST s ()) -> Matrix -> Matrix
- upperTriangule :: Matrix -> Matrix
- lowerTriangule :: Matrix -> Matrix
- thaw :: PrimMonad m => Matrix -> m (MMatrix (PrimState m))
- freeze :: PrimMonad m => MMatrix (PrimState m) -> m Matrix
- unsafeThaw :: PrimMonad m => Matrix -> m (MMatrix (PrimState m))
- unsafeFreeze :: PrimMonad m => MMatrix (PrimState m) -> m Matrix
- unsafeWith :: Matrix -> (Ptr CDouble -> CInt -> CInt -> IO a) -> IO a
Matrix type
Matrix to be used in pure computations, uses column major memory layout, features copy-free FFI with C++ Eigen library.
Matrix conversions
fromList :: [[Double]] -> Matrix Source
Construct matrix from a list of rows, column count is detected as maximum row length. Missing values are filled with 0
generate :: Int -> Int -> (Int -> Int -> Double) -> Matrix Source
Create matrix using generator function f :: row -> col -> val
Standard matrices and special cases
constant :: Int -> Int -> Double -> Matrix Source
Matrix where all coeffs are filled with given value
Accessing matrix data
unsafeCoeff :: Int -> Int -> Matrix -> Double Source
Unsafe version of coeff function. No bounds check performed so SEGFAULT possible
block :: Int -> Int -> Int -> Int -> Matrix -> Matrix Source
Extract rectangular block from matrix defined by startRow startCol blockRows blockCols
bottomRows :: Int -> Matrix -> Matrix Source
Bottom N
rows of matrix
Matrix properties
trace :: Matrix -> Double Source
The trace of a matrix is the sum of the diagonal coefficients and can also be computed as sum (diagonal m)
norm :: Matrix -> Double 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 :: Matrix -> Double 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 :: Matrix -> Double 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 :: Matrix -> Double 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 :: Matrix -> Double Source
The determinant of the matrix
Generic reductions
fold :: (a -> Double -> a) -> a -> Matrix -> a Source
Reduce matrix using user provided function applied to each element.
fold' :: (a -> Double -> a) -> a -> Matrix -> a Source
Reduce matrix using user provided function applied to each element. This is strict version of fold
ifold :: (Int -> Int -> a -> Double -> a) -> a -> Matrix -> a Source
Reduce matrix using user provided function applied to each element and it's index
ifold' :: (Int -> Int -> a -> Double -> a) -> a -> Matrix -> a Source
Reduce matrix using user provided function applied to each element and it's index. This is strict version of ifold
Boolean reductions
all :: (Double -> Bool) -> Matrix -> Bool Source
Applied to a predicate and a matrix, all determines if all elements of the matrix satisfies the predicate
any :: (Double -> Bool) -> Matrix -> Bool Source
Applied to a predicate and a matrix, any determines if any element of the matrix satisfies the predicate
count :: (Double -> Bool) -> Matrix -> Int Source
Returns the number of coefficients in a given matrix that evaluate to true
Basic matrix algebra
add :: Matrix -> Matrix -> Matrix Source
Adding two matrices by adding the corresponding entries together. You can use (+)
function as well.
sub :: Matrix -> Matrix -> Matrix Source
Subtracting two matrices by subtracting the corresponding entries together. You can use (-)
function as well.
Mapping over elements
map :: (Double -> Double) -> Matrix -> Matrix 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]]
>>>
a
Matrix 2x2 1.0 2.0 3.0 4.0
>>>
map (*10) a
Matrix 2x2 10.0 20.0 30.0 40.0
imap :: (Int -> Int -> Double -> Double) -> Matrix -> Matrix Source
Apply a given function to each element of the matrix.
Here is an example how getting upper triangular matrix can be implemented:
>>>
let a = fromList [[1,2,3],[4,5,6],[7,8,9]]
>>>
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
filter :: (Double -> Bool) -> Matrix -> Matrix Source
Filter elements in the matrix. Filtered elements will be replaced by 0
ifilter :: (Int -> Int -> Double -> Bool) -> Matrix -> Matrix Source
Filter elements in the matrix. Filtered elements will be replaced by 0
Matrix transformations
inverse :: Matrix -> Matrix 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
modify :: (forall s. MMatrix s -> ST s ()) -> Matrix -> Matrix Source
Apply a destructive operation to a matrix. The operation will be performed in place if it is safe to do so and will modify a copy of the matrix otherwise.
upperTriangule :: Matrix -> Matrix Source
Upper trinagle of the matrix
lowerTriangule :: Matrix -> Matrix Source
Lower trinagle of the matrix
Mutable matrices
thaw :: PrimMonad m => Matrix -> m (MMatrix (PrimState m)) Source
Yield a mutable copy of the immutable matrix
freeze :: PrimMonad m => MMatrix (PrimState m) -> m Matrix Source
Yield an immutable copy of the mutable matrix
unsafeThaw :: PrimMonad m => Matrix -> m (MMatrix (PrimState m)) Source
Unsafely convert an immutable matrix to a mutable one without copying. The immutable matrix may not be used after this operation.