hmatrix-0.20.2: Numeric Linear Algebra

Copyright | (c) Alberto Ruiz 2014
License | BSD3
Maintainer | Alberto Ruiz
Stability | provisional
Safe Haskell | None
Language | Haskell2010

Numeric.LinearAlgebra.Devel

Contents

- FFI tools
- ST
  - Mutable Vectors
  - Mutable Matrices
  - Unsafe functions
- Special maps and zips
- Sparse representation
- Misc

Description

The library can be easily extended using the tools in this module.

Synopsis

- createVector :: Storable a => Int -> IO (Vector a)
- createMatrix :: Storable a => MatrixOrder -> Int -> Int -> IO (Matrix a)
- class TransArray c where
  - type Trans c b
  - type TransRaw c b
  - apply :: c -> (b -> IO r) -> Trans c b -> IO r
  - applyRaw :: c -> (b -> IO r) -> TransRaw c b -> IO r
- data MatrixOrder
  - = RowMajor
  - | ColumnMajor
- orderOf :: Matrix t -> MatrixOrder
- cmat :: Element t => Matrix t -> Matrix t
- fmat :: Element t => Matrix t -> Matrix t
- matrixFromVector :: Storable t => MatrixOrder -> Int -> Int -> Vector t -> Matrix t
- unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a
- unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int)
- check :: String -> IO CInt -> IO ()
- (//) :: x -> (x -> y) -> y
- (#|) :: IO CInt -> String -> IO ()
- at' :: Storable a => Vector a -> Int -> a
- atM' :: Storable t => Matrix t -> Int -> Int -> t
- fi :: Int -> CInt
- ti :: CInt -> Int
- data STVector s t
- newVector :: Storable t => t -> Int -> ST s (STVector s t)
- thawVector :: Storable t => Vector t -> ST s (STVector s t)
- freezeVector :: Storable t => STVector s t -> ST s (Vector t)
- runSTVector :: Storable t => (forall s. ST s (STVector s t)) -> Vector t
- readVector :: Storable t => STVector s t -> Int -> ST s t
- writeVector :: Storable t => STVector s t -> Int -> t -> ST s ()
- modifyVector :: Storable t => STVector s t -> Int -> (t -> t) -> ST s ()
- liftSTVector :: Storable t => (Vector t -> a) -> STVector s t -> ST s a
- data STMatrix s t
- newMatrix :: Storable t => t -> Int -> Int -> ST s (STMatrix s t)
- thawMatrix :: Element t => Matrix t -> ST s (STMatrix s t)
- freezeMatrix :: Element t => STMatrix s t -> ST s (Matrix t)
- runSTMatrix :: Storable t => (forall s. ST s (STMatrix s t)) -> Matrix t
- readMatrix :: Storable t => STMatrix s t -> Int -> Int -> ST s t
- writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s ()
- modifyMatrix :: Storable t => STMatrix s t -> Int -> Int -> (t -> t) -> ST s ()
- liftSTMatrix :: Element t => (Matrix t -> a) -> STMatrix s t -> ST s a
- mutable :: Element t => (forall s. (Int, Int) -> STMatrix s t -> ST s u) -> Matrix t -> (Matrix t, u)
- extractMatrix :: Element a => STMatrix t a -> RowRange -> ColRange -> ST s (Matrix a)
- setMatrix :: Element t => STMatrix s t -> Int -> Int -> Matrix t -> ST s ()
- rowOper :: (Num t, Element t) => RowOper t -> STMatrix s t -> ST s ()
- data RowOper t
  - = AXPY t Int Int ColRange
  - | SCAL t RowRange ColRange
  - | SWAP Int Int ColRange
- data RowRange
  - = AllRows
  - | RowRange Int Int
  - | Row Int
  - | FromRow Int
- data ColRange
  - = AllCols
  - | ColRange Int Int
  - | Col Int
  - | FromCol Int
- gemmm :: Element t => t -> Slice s t -> t -> Slice s t -> Slice s t -> ST s ()
- data Slice s t = Slice (STMatrix s t) Int Int Int Int
- newUndefinedVector :: Storable t => Int -> ST s (STVector s t)
- unsafeReadVector :: Storable t => STVector s t -> Int -> ST s t
- unsafeWriteVector :: Storable t => STVector s t -> Int -> t -> ST s ()
- unsafeThawVector :: Storable t => Vector t -> ST s (STVector s t)
- unsafeFreezeVector :: Storable t => STVector s t -> ST s (Vector t)
- newUndefinedMatrix :: Storable t => MatrixOrder -> Int -> Int -> ST s (STMatrix s t)
- unsafeReadMatrix :: Storable t => STMatrix s t -> Int -> Int -> ST s t
- unsafeWriteMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s ()
- unsafeThawMatrix :: Storable t => Matrix t -> ST s (STMatrix s t)
- unsafeFreezeMatrix :: Storable t => STMatrix s t -> ST s (Matrix t)
- mapVectorWithIndex :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
- zipVector :: (Storable a, Storable b, Storable (a, b)) => Vector a -> Vector b -> Vector (a, b)
- zipVectorWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c
- unzipVector :: (Storable a, Storable b, Storable (a, b)) => Vector (a, b) -> (Vector a, Vector b)
- unzipVectorWith :: (Storable (a, b), Storable c, Storable d) => ((a, b) -> (c, d)) -> Vector (a, b) -> (Vector c, Vector d)
- mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b)
- mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m ()
- mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b)
- mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m ()
- foldLoop :: (Int -> t -> t) -> t -> Int -> t
- foldVector :: Storable a => (a -> b -> b) -> b -> Vector a -> b
- foldVectorG :: Storable t1 => (Int -> (Int -> t1) -> t -> t) -> t -> Vector t1 -> t
- foldVectorWithIndex :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
- mapMatrixWithIndex :: (Element a, Storable b) => ((Int, Int) -> a -> b) -> Matrix a -> Matrix b
- mapMatrixWithIndexM :: (Element a, Storable b, Monad m) => ((Int, Int) -> a -> m b) -> Matrix a -> m (Matrix b)
- mapMatrixWithIndexM_ :: (Element a, Num a, Monad m) => ((Int, Int) -> a -> m ()) -> Matrix a -> m ()
- liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b
- liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
- liftMatrix2Auto :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
- data CSR = CSR {
  - csrVals :: Vector Double
  - csrCols :: Vector CInt
  - csrRows :: Vector CInt
  - csrNRows :: Int
  - csrNCols :: Int
  }
- fromCSR :: CSR -> GMatrix
- mkCSR :: AssocMatrix -> CSR
- impureCSR :: PrimMonad m => (forall x. (x -> (IndexOf Matrix, Double) -> m x) -> m x -> (x -> m CSR) -> r) -> r
- data GMatrix
  - = SparseR {
    - gmCSR :: CSR
    - nRows :: Int
    - nCols :: Int
    }
  - | SparseC {
    - gmCSC :: CSC
    - nRows :: Int
    - nCols :: Int
    }
  - | Diag {
    - diagVals :: Vector Double
    - nRows :: Int
    - nCols :: Int
    }
  - | Dense {
    - gmDense :: Matrix Double
    - nRows :: Int
    - nCols :: Int
    }
- toByteString :: Storable t => Vector t -> ByteString
- fromByteString :: Storable t => ByteString -> Vector t
- showInternal :: Storable t => Matrix t -> IO ()
- reorderVector :: Element a => Vector CInt -> Vector CInt -> Vector a -> Vector a

# FFI tools

See `examples/devel` in the repository.

createVector :: Storable a => Int -> IO (Vector a) Source #

createMatrix :: Storable a => MatrixOrder -> Int -> Int -> IO (Matrix a) Source #

class TransArray c where Source #

Associated Types

type Trans c b Source #

type TransRaw c b Source #

Methods

apply :: c -> (b -> IO r) -> Trans c b -> IO r infixl 1 Source #

applyRaw :: c -> (b -> IO r) -> TransRaw c b -> IO r infixl 1 Source #

#### Instances

Instances details

Storable t => TransArray (Matrix t) Source # |
Instance details Defined in Internal.Matrix Methods apply :: Matrix t -> (b -> IO r) -> Trans (Matrix t) b -> IO r Source # applyRaw :: Matrix t -> (b -> IO r) -> TransRaw (Matrix t) b -> IO r Source #
Storable t => TransArray (Vector t) Source # |
Instance details Defined in Internal.Devel Methods apply :: Vector t -> (b -> IO r) -> Trans (Vector t) b -> IO r Source # applyRaw :: Vector t -> (b -> IO r) -> TransRaw (Vector t) b -> IO r Source #

data MatrixOrder Source #

Constructors

RowMajor |
ColumnMajor |

#### Instances

Instances details

Show MatrixOrder Source # |
Instance details Defined in Internal.Matrix Methods showsPrec :: Int -> MatrixOrder -> ShowS Source # show :: MatrixOrder -> String Source # showList :: [MatrixOrder] -> ShowS Source #
Eq MatrixOrder Source # |
Instance details Defined in Internal.Matrix Methods (==) :: MatrixOrder -> MatrixOrder -> Bool Source # (/=) :: MatrixOrder -> MatrixOrder -> Bool Source #

orderOf :: Matrix t -> MatrixOrder Source #

cmat :: Element t => Matrix t -> Matrix t Source #

fmat :: Element t => Matrix t -> Matrix t Source #

matrixFromVector :: Storable t => MatrixOrder -> Int -> Int -> Vector t -> Matrix t Source #

unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a #

unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int) #

check :: String -> IO CInt -> IO () Source #

check the error code

(//) :: x -> (x -> y) -> y infixl 0 Source #

postfix function application (`flip ($)`)

(#|) :: IO CInt -> String -> IO () infixl 0 Source #

postfix error code check

at' :: Storable a => Vector a -> Int -> a Source #

access to Vector elements without range checking

atM' :: Storable t => Matrix t -> Int -> Int -> t Source #

fi :: Int -> CInt Source #

specialized fromIntegral

ti :: CInt -> Int Source #

specialized fromIntegral

# ST

In-place manipulation inside the ST monad. See `examples/inplace.hs` in the repository.

## Mutable Vectors

data STVector s t Source #

newVector :: Storable t => t -> Int -> ST s (STVector s t) Source #

thawVector :: Storable t => Vector t -> ST s (STVector s t) Source #

freezeVector :: Storable t => STVector s t -> ST s (Vector t) Source #

runSTVector :: Storable t => (forall s. ST s (STVector s t)) -> Vector t Source #

readVector :: Storable t => STVector s t -> Int -> ST s t Source #

writeVector :: Storable t => STVector s t -> Int -> t -> ST s () Source #

modifyVector :: Storable t => STVector s t -> Int -> (t -> t) -> ST s () Source #

liftSTVector :: Storable t => (Vector t -> a) -> STVector s t -> ST s a Source #

## Mutable Matrices

data STMatrix s t Source #

newMatrix :: Storable t => t -> Int -> Int -> ST s (STMatrix s t) Source #

thawMatrix :: Element t => Matrix t -> ST s (STMatrix s t) Source #

freezeMatrix :: Element t => STMatrix s t -> ST s (Matrix t) Source #

runSTMatrix :: Storable t => (forall s. ST s (STMatrix s t)) -> Matrix t Source #

readMatrix :: Storable t => STMatrix s t -> Int -> Int -> ST s t Source #

writeMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s () Source #

modifyMatrix :: Storable t => STMatrix s t -> Int -> Int -> (t -> t) -> ST s () Source #

liftSTMatrix :: Element t => (Matrix t -> a) -> STMatrix s t -> ST s a Source #

mutable :: Element t => (forall s. (Int, Int) -> STMatrix s t -> ST s u) -> Matrix t -> (Matrix t, u) Source #

extractMatrix :: Element a => STMatrix t a -> RowRange -> ColRange -> ST s (Matrix a) Source #

setMatrix :: Element t => STMatrix s t -> Int -> Int -> Matrix t -> ST s () Source #

rowOper :: (Num t, Element t) => RowOper t -> STMatrix s t -> ST s () Source #

data RowOper t Source #

Constructors

AXPY t Int Int ColRange |
SCAL t RowRange ColRange |
SWAP Int Int ColRange |

data RowRange Source #

Constructors

AllRows |
RowRange Int Int |
Row Int |
FromRow Int |

data ColRange Source #

Constructors

AllCols |
ColRange Int Int |
Col Int |
FromCol Int |

gemmm :: Element t => t -> Slice s t -> t -> Slice s t -> Slice s t -> ST s () Source #

data Slice s t Source #

r0 c0 height width

Constructors

Slice (STMatrix s t) Int Int Int Int |

## Unsafe functions

newUndefinedVector :: Storable t => Int -> ST s (STVector s t) Source #

unsafeReadVector :: Storable t => STVector s t -> Int -> ST s t Source #

unsafeWriteVector :: Storable t => STVector s t -> Int -> t -> ST s () Source #

unsafeThawVector :: Storable t => Vector t -> ST s (STVector s t) Source #

unsafeFreezeVector :: Storable t => STVector s t -> ST s (Vector t) Source #

newUndefinedMatrix :: Storable t => MatrixOrder -> Int -> Int -> ST s (STMatrix s t) Source #

unsafeReadMatrix :: Storable t => STMatrix s t -> Int -> Int -> ST s t Source #

unsafeWriteMatrix :: Storable t => STMatrix s t -> Int -> Int -> t -> ST s () Source #

unsafeThawMatrix :: Storable t => Matrix t -> ST s (STMatrix s t) Source #

unsafeFreezeMatrix :: Storable t => STMatrix s t -> ST s (Matrix t) Source #

# Special maps and zips

mapVectorWithIndex :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b Source #

zipVector :: (Storable a, Storable b, Storable (a, b)) => Vector a -> Vector b -> Vector (a, b) Source #

zip for Vectors

zipVectorWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c Source #

zipWith for Vectors

unzipVector :: (Storable a, Storable b, Storable (a, b)) => Vector (a, b) -> (Vector a, Vector b) Source #

unzip for Vectors

unzipVectorWith :: (Storable (a, b), Storable c, Storable d) => ((a, b) -> (c, d)) -> Vector (a, b) -> (Vector c, Vector d) Source #

unzipWith for Vectors

mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b) Source #

monadic map over Vectors the monad `m` must be strict

mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m () Source #

monadic map over Vectors

mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b) Source #

monadic map over Vectors with the zero-indexed index passed to the mapping function the monad `m` must be strict

mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m () Source #

monadic map over Vectors with the zero-indexed index passed to the mapping function

foldLoop :: (Int -> t -> t) -> t -> Int -> t Source #

foldVector :: Storable a => (a -> b -> b) -> b -> Vector a -> b Source #

foldVectorG :: Storable t1 => (Int -> (Int -> t1) -> t -> t) -> t -> Vector t1 -> t Source #

foldVectorWithIndex :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b Source #

mapMatrixWithIndex :: (Element a, Storable b) => ((Int, Int) -> a -> b) -> Matrix a -> Matrix b Source #

    >>> mapMatrixWithIndex (\(i,j) v -> 100*v + 10*fromIntegral i + fromIntegral j) (ident 3:: Matrix Double)
    (3><3)
     [ 100.0,   1.0,   2.0
     ,  10.0, 111.0,  12.0
     ,  20.0,  21.0, 122.0 ]

mapMatrixWithIndexM :: (Element a, Storable b, Monad m) => ((Int, Int) -> a -> m b) -> Matrix a -> m (Matrix b) Source #

    >>> mapMatrixWithIndexM (\(i,j) v -> Just $ 100*v + 10*fromIntegral i + fromIntegral j) (ident 3:: Matrix Double)
    Just (3><3)
     [ 100.0,   1.0,   2.0
     ,  10.0, 111.0,  12.0
     ,  20.0,  21.0, 122.0 ]

mapMatrixWithIndexM_ :: (Element a, Num a, Monad m) => ((Int, Int) -> a -> m ()) -> Matrix a -> m () Source #

    >>> mapMatrixWithIndexM_ (\(i,j) v -> printf "m[%d,%d] = %.f\n" i j v :: IO()) ((2><3)[1 :: Double ..])
    m[0,0] = 1
    m[0,1] = 2
    m[0,2] = 3
    m[1,0] = 4
    m[1,1] = 5
    m[1,2] = 6

liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b Source #

application of a vector function on the flattened matrix elements

liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t Source #

application of a vector function on the flattened matrices elements

liftMatrix2Auto :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t Source #

A version of `liftMatrix2` which automatically adapt matrices with a single row or column to match the dimensions of the other matrix.

# Sparse representation

data CSR Source #

Constructors

CSR |

Fields

- csrVals :: Vector Double
- csrCols :: Vector CInt
- csrRows :: Vector CInt
- csrNRows :: Int
- csrNCols :: Int

#### Instances

Instances details

Show CSR Source # |
Instance details Defined in Internal.Sparse Methods showsPrec :: Int -> CSR -> ShowS Source # show :: CSR -> String Source # showList :: [CSR] -> ShowS Source #

fromCSR :: CSR -> GMatrix Source #

mkCSR :: AssocMatrix -> CSR Source #

Produce a CSR sparse matrix from a association matrix.

impureCSR :: PrimMonad m => (forall x. (x -> (IndexOf Matrix, Double) -> m x) -> m x -> (x -> m CSR) -> r) -> r Source #

Produce a CSR sparse matrix by applying a generic folding function.

This allows one to build a CSR from an effectful streaming source when combined with libraries like pipes, io-streams, or streaming.

For example

    impureCSR Pipes.Prelude.foldM :: PrimMonad m => Producer AssocEntry m () -> m CSR
    impureCSR Streaming.Prelude.foldM :: PrimMonad m => Stream (Of AssocEntry) m r -> m (Of CSR r)

data GMatrix Source #

General matrix with specialized internal representations for dense, sparse, diagonal, banded, and constant elements.

    >>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)]
    >>> m
    SparseR {gmCSR = CSR {csrVals = fromList [1.0,2.0],
                          csrCols = fromList [1000,2000],
                          csrRows = fromList [1,2,3],
                          csrNRows = 2,
                          csrNCols = 2000},
                          nRows = 2,
                          nCols = 2000}

    >>> let m = mkDense (mat 2 [1..4])
    >>> m
    Dense {gmDense = (2><2)
     [ 1.0, 2.0
     , 3.0, 4.0 ], nRows = 2, nCols = 2}

Constructors

SparseR |

Fields

- gmCSR :: CSR
- nRows :: Int
- nCols :: Int

SparseC |

Fields

- gmCSC :: CSC
- nRows :: Int
- nCols :: Int

Diag |

Fields

- diagVals :: Vector Double
- nRows :: Int
- nCols :: Int

Dense |

Fields

- gmDense :: Matrix Double
- nRows :: Int
- nCols :: Int

#### Instances

Instances details

Show GMatrix Source # |
Instance details Defined in Internal.Sparse Methods showsPrec :: Int -> GMatrix -> ShowS Source # show :: GMatrix -> String Source # showList :: [GMatrix] -> ShowS Source #
Testable GMatrix Source # |
Instance details Defined in Internal.CG Methods checkT :: GMatrix -> (Bool, IO ()) Source # ioCheckT :: GMatrix -> IO (Bool, IO ()) Source #
Transposable GMatrix GMatrix Source # |
Instance details Defined in Internal.Sparse Methods tr :: GMatrix -> GMatrix Source # tr' :: GMatrix -> GMatrix Source #

# Misc

toByteString :: Storable t => Vector t -> ByteString Source #

fromByteString :: Storable t => ByteString -> Vector t Source #

showInternal :: Storable t => Matrix t -> IO () Source #

reorderVector Source #

Arguments

:: Element a |
=> Vector CInt | `strides`: array strides
-> Vector CInt | `dims`: array dimensions of new array `v`
-> Vector a | `v`: flattened input array
-> Vector a | `v'`: flattened output array

Transpose an array with dimensions `dims` by making a copy using `strides`. For example, for an array with 3 indices, `(reorderVector strides dims v) ! ((i * dims ! 1 + j) * dims ! 2 + k) == v ! (i * strides ! 0 + j * strides ! 1 + k * strides ! 2)` This function is intended to be used internally by tensor libraries.