-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Numeric Linear Algebra -- -- Linear systems, matrix decompositions, and other numerical -- computations based on BLAS and LAPACK. -- -- Standard interface: Numeric.LinearAlgebra. -- -- Safer interface with statically checked dimensions: -- Numeric.LinearAlgebra.Static. -- -- Code examples: http://dis.um.es/~alberto/hmatrix/hmatrix.html @package hmatrix @version 0.20.2 -- | The library can be easily extended using the tools in this module. module Numeric.LinearAlgebra.Devel createVector :: Storable a => Int -> IO (Vector a) createMatrix :: Storable a => MatrixOrder -> Int -> Int -> IO (Matrix a) class TransArray c where { type family Trans c b; type family TransRaw c b; } apply :: TransArray c => c -> (b -> IO r) -> Trans c b -> IO r applyRaw :: TransArray c => c -> (b -> IO r) -> TransRaw c b -> IO r infixl 1 `apply` infixl 1 `applyRaw` data MatrixOrder RowMajor :: MatrixOrder ColumnMajor :: MatrixOrder 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 -- | O(1) Create a vector from a ForeignPtr with an offset -- and a length. -- -- The data may not be modified through the ForeignPtr afterwards. -- -- If your offset is 0 it is more efficient to use -- unsafeFromForeignPtr0. unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a -- | O(1) Yield the underlying ForeignPtr together with the -- offset to the data and its length. The data may not be modified -- through the ForeignPtr. unsafeToForeignPtr :: Storable a => Vector a -> (ForeignPtr a, Int, Int) -- | check the error code check :: String -> IO CInt -> IO () -- | postfix function application (flip ($)) (//) :: x -> (x -> y) -> y infixl 0 // -- | postfix error code check (#|) :: IO CInt -> String -> IO () infixl 0 #| -- | access to Vector elements without range checking at' :: Storable a => Vector a -> Int -> a atM' :: Storable t => Matrix t -> Int -> Int -> t -- | specialized fromIntegral fi :: Int -> CInt -- | specialized fromIntegral 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 -> RowOper t SCAL :: t -> RowRange -> ColRange -> RowOper t SWAP :: Int -> Int -> ColRange -> RowOper t data RowRange AllRows :: RowRange RowRange :: Int -> Int -> RowRange Row :: Int -> RowRange FromRow :: Int -> RowRange data ColRange AllCols :: ColRange ColRange :: Int -> Int -> ColRange Col :: Int -> ColRange FromCol :: Int -> ColRange gemmm :: Element t => t -> Slice s t -> t -> Slice s t -> Slice s t -> ST s () -- | r0 c0 height width data Slice s t Slice :: STMatrix s t -> Int -> Int -> Int -> Int -> Slice s t 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 -- | zip for Vectors zipVector :: (Storable a, Storable b, Storable (a, b)) => Vector a -> Vector b -> Vector (a, b) -- | zipWith for Vectors zipVectorWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c -- | unzip for Vectors unzipVector :: (Storable a, Storable b, Storable (a, b)) => Vector (a, b) -> (Vector a, Vector b) -- | unzipWith for Vectors unzipVectorWith :: (Storable (a, b), Storable c, Storable d) => ((a, b) -> (c, d)) -> Vector (a, b) -> (Vector c, Vector d) -- | monadic map over Vectors the monad m must be strict mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b) -- | monadic map over Vectors mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m () -- | monadic map over Vectors with the zero-indexed index passed to the -- mapping function the monad m must be strict mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b) -- | monadic map over Vectors with the zero-indexed index passed to the -- mapping function 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 (\(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 ] --mapMatrixWithIndex :: (Element a, Storable b) => ((Int, Int) -> a -> b) -> Matrix a -> Matrix b -- |
-- >>> 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, Storable b, Monad m) => ((Int, Int) -> a -> m b) -> Matrix a -> m (Matrix b) -- |
-- >>> 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 --mapMatrixWithIndexM_ :: (Element a, Num a, Monad m) => ((Int, Int) -> a -> m ()) -> Matrix a -> m () -- | application of a vector function on the flattened matrix elements liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b -- | application of a vector function on the flattened matrices elements liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t -- | A version of liftMatrix2 which automatically adapt matrices -- with a single row or column to match the dimensions of the other -- matrix. liftMatrix2Auto :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t data CSR CSR :: Vector Double -> Vector CInt -> Vector CInt -> Int -> Int -> CSR [csrVals] :: CSR -> Vector Double [csrCols] :: CSR -> Vector CInt [csrRows] :: CSR -> Vector CInt [csrNRows] :: CSR -> Int [csrNCols] :: CSR -> Int fromCSR :: CSR -> GMatrix -- | Produce a CSR sparse matrix from a association matrix. mkCSR :: AssocMatrix -> CSR -- | 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) --impureCSR :: PrimMonad m => (forall x. (x -> (IndexOf Matrix, Double) -> m x) -> m x -> (x -> m CSR) -> r) -> r -- | 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}
--
data GMatrix
SparseR :: CSR -> Int -> Int -> GMatrix
[gmCSR] :: GMatrix -> CSR
[nRows] :: GMatrix -> Int
[nCols] :: GMatrix -> Int
SparseC :: CSC -> Int -> Int -> GMatrix
[gmCSC] :: GMatrix -> CSC
[nRows] :: GMatrix -> Int
[nCols] :: GMatrix -> Int
Diag :: Vector Double -> Int -> Int -> GMatrix
[diagVals] :: GMatrix -> Vector Double
[nRows] :: GMatrix -> Int
[nCols] :: GMatrix -> Int
Dense :: Matrix Double -> Int -> Int -> GMatrix
[gmDense] :: GMatrix -> Matrix Double
[nRows] :: GMatrix -> Int
[nCols] :: GMatrix -> Int
toByteString :: Storable t => Vector t -> ByteString
fromByteString :: Storable t => ByteString -> Vector t
showInternal :: Storable t => Matrix t -> IO ()
-- | 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.
reorderVector :: Element a => Vector CInt -> Vector CInt -> Vector a -> Vector a
-- | This module provides functions for creation and manipulation of
-- vectors and matrices, IO, and other utilities.
module Numeric.LinearAlgebra.Data
type R = Double
type C = Complex Double
type I = CInt
type Z = Int64
type (./.) x n = Mod n x
infixr 5 ./.
-- | O(n) Convert a list to a vector
fromList :: Storable a => [a] -> Vector a
toList :: Storable a => Vector a -> [a]
-- | Create a vector from a list of elements and explicit dimension. The
-- input list is truncated if it is too long, so it may safely be used,
-- for instance, with infinite lists.
--
-- -- >>> 5 |> [1..] -- [1.0,2.0,3.0,4.0,5.0] -- it :: (Enum a, Num a, Foreign.Storable.Storable a) => Vector a --(|>) :: Storable a => Int -> [a] -> Vector a infixl 9 |> -- | Create a real vector. -- --
-- >>> vector [1..5] -- [1.0,2.0,3.0,4.0,5.0] -- it :: Vector R --vector :: [R] -> Vector R -- |
-- >>> range 5 -- [0,1,2,3,4] -- it :: Vector I --range :: Int -> Vector I -- | Create a vector of indexes, useful for matrix extraction using -- (??) idxs :: [Int] -> Vector I -- | Create a matrix from a list of elements -- --
-- >>> (2><3) [2, 4, 7+2*iC, -3, 11, 0] -- (2><3) -- [ 2.0 :+ 0.0, 4.0 :+ 0.0, 7.0 :+ 2.0 -- , (-3.0) :+ (-0.0), 11.0 :+ 0.0, 0.0 :+ 0.0 ] ---- -- The input list is explicitly truncated, so that it can safely be used -- with lists that are too long (like infinite lists). -- --
-- >>> (2><3)[1..] -- (2><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 ] ---- -- This is the format produced by the instances of Show (Matrix a), which -- can also be used for input. (><) :: Storable a => Int -> Int -> [a] -> Matrix a -- | Create a real matrix. -- --
-- >>> matrix 5 [1..15] -- (3><5) -- [ 1.0, 2.0, 3.0, 4.0, 5.0 -- , 6.0, 7.0, 8.0, 9.0, 10.0 -- , 11.0, 12.0, 13.0, 14.0, 15.0 ] --matrix :: Int -> [R] -> Matrix R -- | conjugate transpose tr :: Transposable m mt => m -> mt -- | transpose tr' :: Transposable m mt => m -> mt -- |
-- >>> size $ vector [1..10] -- 10 -- -- >>> size $ (2><5)[1..10::Double] -- (2,5) --size :: Container c t => c t -> IndexOf c rows :: Matrix t -> Int cols :: Matrix t -> Int -- | Creates a Matrix from a list of lists (considered as rows). -- --
-- >>> fromLists [[1,2],[3,4],[5,6]] -- (3><2) -- [ 1.0, 2.0 -- , 3.0, 4.0 -- , 5.0, 6.0 ] --fromLists :: Element t => [[t]] -> Matrix t -- | the inverse of fromLists toLists :: Element t => Matrix t -> [[t]] -- | create a single row real matrix from a list -- --
-- >>> row [2,3,1,8] -- (1><4) -- [ 2.0, 3.0, 1.0, 8.0 ] --row :: [Double] -> Matrix Double -- | create a single column real matrix from a list -- --
-- >>> col [7,-2,4] -- (3><1) -- [ 7.0 -- , -2.0 -- , 4.0 ] --col :: [Double] -> Matrix Double -- | Creates a vector by concatenation of rows. If the matrix is -- ColumnMajor, this operation requires a transpose. -- --
-- >>> flatten (ident 3) -- [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0] -- it :: (Num t, Element t) => Vector t --flatten :: Element t => Matrix t -> Vector t -- | Creates a matrix from a vector by grouping the elements in rows with -- the desired number of columns. (GNU-Octave groups by columns. To do it -- you can define reshapeF r = tr' . reshape r where r is the -- desired number of rows.) -- --
-- >>> reshape 4 (fromList [1..12]) -- (3><4) -- [ 1.0, 2.0, 3.0, 4.0 -- , 5.0, 6.0, 7.0, 8.0 -- , 9.0, 10.0, 11.0, 12.0 ] --reshape :: Storable t => Int -> Vector t -> Matrix t -- | creates a 1-row matrix from a vector -- --
-- >>> asRow (fromList [1..5]) -- (1><5) -- [ 1.0, 2.0, 3.0, 4.0, 5.0 ] --asRow :: Storable a => Vector a -> Matrix a -- | creates a 1-column matrix from a vector -- --
-- >>> asColumn (fromList [1..5]) -- (5><1) -- [ 1.0 -- , 2.0 -- , 3.0 -- , 4.0 -- , 5.0 ] --asColumn :: Storable a => Vector a -> Matrix a -- | Create a matrix from a list of vectors. All vectors must have the same -- dimension, or dimension 1, which is are automatically expanded. fromRows :: Element t => [Vector t] -> Matrix t -- | extracts the rows of a matrix as a list of vectors toRows :: Element t => Matrix t -> [Vector t] -- | Creates a matrix from a list of vectors, as columns fromColumns :: Element t => [Vector t] -> Matrix t -- | Creates a list of vectors from the columns of a matrix toColumns :: Element t => Matrix t -> [Vector t] -- | generic indexing function -- --
-- >>> vector [1,2,3] `atIndex` 1 -- 2.0 ---- --
-- >>> matrix 3 [0..8] `atIndex` (2,0) -- 6.0 --atIndex :: Container c e => c e -> IndexOf c -> e -- | Alternative indexing function. -- --
-- >>> vector [1..10] ! 3 -- 4.0 ---- -- On a matrix it gets the k-th row as a vector: -- --
-- >>> matrix 5 [1..15] ! 1 -- [6.0,7.0,8.0,9.0,10.0] -- it :: Vector Double ---- --
-- >>> matrix 5 [1..15] ! 1 ! 3 -- 9.0 --class Indexable c t | c -> t, t -> c (!) :: Indexable c t => c -> Int -> t infixl 9 ! -- | create a structure with a single element -- --
-- >>> let v = fromList [1..3::Double] -- -- >>> v / scalar (norm2 v) -- fromList [0.2672612419124244,0.5345224838248488,0.8017837257372732] --scalar :: Container c e => e -> c e class Konst e d c | d -> c, c -> d -- |
-- >>> konst 7 3 :: Vector Float -- fromList [7.0,7.0,7.0] ---- --
-- >>> konst i (3::Int,4::Int) -- (3><4) -- [ 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 -- , 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 -- , 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 ] --konst :: Konst e d c => e -> d -> c e class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f -- |
-- >>> build 5 (**2) :: Vector Double -- [0.0,1.0,4.0,9.0,16.0] -- it :: Vector Double ---- -- Hilbert matrix of order N: -- --
-- >>> let hilb n = build (n,n) (\i j -> 1/(i+j+1)) :: Matrix Double -- -- >>> putStr . dispf 2 $ hilb 3 -- 3x3 -- 1.00 0.50 0.33 -- 0.50 0.33 0.25 -- 0.33 0.25 0.20 --build :: Build d f c e => d -> f -> c e -- | Create a structure from an association list -- --
-- >>> assoc 5 0 [(3,7),(1,4)] :: Vector Double -- fromList [0.0,4.0,0.0,7.0,0.0] ---- --
-- >>> assoc (2,3) 0 [((0,2),7),((1,0),2*i-3)] :: Matrix (Complex Double) -- (2><3) -- [ 0.0 :+ 0.0, 0.0 :+ 0.0, 7.0 :+ 0.0 -- , (-3.0) :+ 2.0, 0.0 :+ 0.0, 0.0 :+ 0.0 ] --assoc :: Container c e => IndexOf c -> e -> [(IndexOf c, e)] -> c e -- | Modify a structure using an update function -- --
-- >>> accum (ident 5) (+) [((1,1),5),((0,3),3)] :: Matrix Double -- (5><5) -- [ 1.0, 0.0, 0.0, 3.0, 0.0 -- , 0.0, 6.0, 0.0, 0.0, 0.0 -- , 0.0, 0.0, 1.0, 0.0, 0.0 -- , 0.0, 0.0, 0.0, 1.0, 0.0 -- , 0.0, 0.0, 0.0, 0.0, 1.0 ] ---- -- computation of histogram: -- --
-- >>> accum (konst 0 7) (+) (map (flip (,) 1) [4,5,4,1,5,2,5]) :: Vector Double -- fromList [0.0,1.0,1.0,0.0,2.0,3.0,0.0] --accum :: Container c e => c e -> (e -> e -> e) -> [(IndexOf c, e)] -> c e -- | Creates a real vector containing a range of values: -- --
-- >>> linspace 5 (-3,7::Double) -- [-3.0,-0.5,2.0,4.5,7.0] -- it :: Vector Double ---- --
-- >>> linspace 5 (8,3:+2) :: Vector (Complex Double) -- [8.0 :+ 0.0,6.75 :+ 0.5,5.5 :+ 1.0,4.25 :+ 1.5,3.0 :+ 2.0] -- it :: Vector (Complex Double) ---- -- Logarithmic spacing can be defined as follows: -- --
-- logspace n (a,b) = 10 ** linspace n (a,b) --linspace :: (Fractional e, Container Vector e) => Int -> (e, e) -> Vector e -- | creates the identity matrix of given dimension ident :: (Num a, Element a) => Int -> Matrix a -- | Creates a square matrix with a given diagonal. diag :: (Num a, Element a) => Vector a -> Matrix a -- | create a real diagonal matrix from a list -- --
-- >>> diagl [1,2,3] -- (3><3) -- [ 1.0, 0.0, 0.0 -- , 0.0, 2.0, 0.0 -- , 0.0, 0.0, 3.0 ] --diagl :: [Double] -> Matrix Double -- | creates a rectangular diagonal matrix: -- --
-- >>> diagRect 7 (fromList [10,20,30]) 4 5 :: Matrix Double -- (4><5) -- [ 10.0, 7.0, 7.0, 7.0, 7.0 -- , 7.0, 20.0, 7.0, 7.0, 7.0 -- , 7.0, 7.0, 30.0, 7.0, 7.0 -- , 7.0, 7.0, 7.0, 7.0, 7.0 ] --diagRect :: Storable t => t -> Vector t -> Int -> Int -> Matrix t -- | extracts the diagonal from a rectangular matrix takeDiag :: Element t => Matrix t -> Vector t -- | takes a number of consecutive elements from a Vector -- --
-- >>> subVector 2 3 (fromList [1..10]) -- [3.0,4.0,5.0] -- it :: (Enum t, Num t, Foreign.Storable.Storable t) => Vector t --subVector :: Storable t => Int -> Int -> Vector t -> Vector t -- | Extract consecutive subvectors of the given sizes. -- --
-- >>> takesV [3,4] (linspace 10 (1,10::Double)) -- [[1.0,2.0,3.0],[4.0,5.0,6.0,7.0]] -- it :: [Vector Double] --takesV :: Storable t => [Int] -> Vector t -> [Vector t] -- | concatenate a list of vectors -- --
-- >>> vjoin [fromList [1..5::Double], konst 1 3] -- [1.0,2.0,3.0,4.0,5.0,1.0,1.0,1.0] -- it :: Vector Double --vjoin :: Storable t => [Vector t] -> Vector t -- | Specification of indexes for the operator ??. data Extractor All :: Extractor Range :: Int -> Int -> Int -> Extractor Pos :: Vector I -> Extractor PosCyc :: Vector I -> Extractor Take :: Int -> Extractor TakeLast :: Int -> Extractor Drop :: Int -> Extractor DropLast :: Int -> Extractor -- | General matrix slicing. -- --
-- >>> m -- (4><5) -- [ 0, 1, 2, 3, 4 -- , 5, 6, 7, 8, 9 -- , 10, 11, 12, 13, 14 -- , 15, 16, 17, 18, 19 ] ---- --
-- >>> m ?? (Take 3, DropLast 2) -- (3><3) -- [ 0, 1, 2 -- , 5, 6, 7 -- , 10, 11, 12 ] ---- --
-- >>> m ?? (Pos (idxs[2,1]), All) -- (2><5) -- [ 10, 11, 12, 13, 14 -- , 5, 6, 7, 8, 9 ] ---- --
-- >>> m ?? (PosCyc (idxs[-7,80]), Range 4 (-2) 0) -- (2><3) -- [ 9, 7, 5 -- , 4, 2, 0 ] --(??) :: Element t => Matrix t -> (Extractor, Extractor) -> Matrix t infixl 9 ?? -- | extract rows -- --
-- >>> (20><4) [1..] ? [2,1,1] -- (3><4) -- [ 9.0, 10.0, 11.0, 12.0 -- , 5.0, 6.0, 7.0, 8.0 -- , 5.0, 6.0, 7.0, 8.0 ] --(?) :: Element t => Matrix t -> [Int] -> Matrix t infixl 9 ? -- | extract columns -- -- (unicode 0x00bf, inverted question mark, Alt-Gr ?) -- --
-- >>> (3><4) [1..] ยฟ [3,0] -- (3><2) -- [ 4.0, 1.0 -- , 8.0, 5.0 -- , 12.0, 9.0 ] --(ยฟ) :: Element t => Matrix t -> [Int] -> Matrix t infixl 9 ยฟ -- | Reverse columns fliprl :: Element t => Matrix t -> Matrix t -- | Reverse rows flipud :: Element t => Matrix t -> Matrix t -- | reference to a rectangular slice of a matrix (no data copy) subMatrix :: Element a => (Int, Int) -> (Int, Int) -> Matrix a -> Matrix a takeRows :: Element t => Int -> Matrix t -> Matrix t dropRows :: Element t => Int -> Matrix t -> Matrix t takeColumns :: Element t => Int -> Matrix t -> Matrix t dropColumns :: Element t => Int -> Matrix t -> Matrix t -- | Extract elements from positions given in matrices of rows and columns. -- --
-- >>> r -- (3><3) -- [ 1, 1, 1 -- , 1, 2, 2 -- , 1, 2, 3 ] -- -- >>> c -- (3><3) -- [ 0, 1, 5 -- , 2, 2, 1 -- , 4, 4, 1 ] -- -- >>> m -- (4><6) -- [ 0, 1, 2, 3, 4, 5 -- , 6, 7, 8, 9, 10, 11 -- , 12, 13, 14, 15, 16, 17 -- , 18, 19, 20, 21, 22, 23 ] ---- --
-- >>> remap r c m -- (3><3) -- [ 6, 7, 11 -- , 8, 14, 13 -- , 10, 16, 19 ] ---- -- The indexes are autoconformable. -- --
-- >>> c' -- (3><1) -- [ 1 -- , 2 -- , 4 ] -- -- >>> remap r c' m -- (3><3) -- [ 7, 7, 7 -- , 8, 14, 14 -- , 10, 16, 22 ] --remap :: Element t => Matrix I -> Matrix I -> Matrix t -> Matrix t -- | Create a matrix from blocks given as a list of lists of matrices. -- -- Single row-column components are automatically expanded to match the -- corresponding common row and column: -- --
-- disp = putStr . dispf 2 ---- --
-- >>> disp $ fromBlocks [[ident 5, 7, row[10,20]], [3, diagl[1,2,3], 0]] -- 8x10 -- 1 0 0 0 0 7 7 7 10 20 -- 0 1 0 0 0 7 7 7 10 20 -- 0 0 1 0 0 7 7 7 10 20 -- 0 0 0 1 0 7 7 7 10 20 -- 0 0 0 0 1 7 7 7 10 20 -- 3 3 3 3 3 1 0 0 0 0 -- 3 3 3 3 3 0 2 0 0 0 -- 3 3 3 3 3 0 0 3 0 0 --fromBlocks :: Element t => [[Matrix t]] -> Matrix t -- | horizontal concatenation -- --
-- >>> ident 3 ||| konst 7 (3,4) -- (3><7) -- [ 1.0, 0.0, 0.0, 7.0, 7.0, 7.0, 7.0 -- , 0.0, 1.0, 0.0, 7.0, 7.0, 7.0, 7.0 -- , 0.0, 0.0, 1.0, 7.0, 7.0, 7.0, 7.0 ] --(|||) :: Element t => Matrix t -> Matrix t -> Matrix t infixl 3 ||| -- | vertical concatenation (===) :: Element t => Matrix t -> Matrix t -> Matrix t infixl 2 === -- | create a block diagonal matrix -- --
-- >>> disp 2 $ diagBlock [konst 1 (2,2), konst 2 (3,5), col [5,7]] -- 7x8 -- 1 1 0 0 0 0 0 0 -- 1 1 0 0 0 0 0 0 -- 0 0 2 2 2 2 2 0 -- 0 0 2 2 2 2 2 0 -- 0 0 2 2 2 2 2 0 -- 0 0 0 0 0 0 0 5 -- 0 0 0 0 0 0 0 7 ---- --
-- >>> diagBlock [(0><4)[], konst 2 (2,3)] :: Matrix Double -- (2><7) -- [ 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0 -- , 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0 ] --diagBlock :: (Element t, Num t) => [Matrix t] -> Matrix t -- | creates matrix by repetition of a matrix a given number of rows and -- columns -- --
-- >>> repmat (ident 2) 2 3 -- (4><6) -- [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 -- , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 -- , 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 -- , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ] --repmat :: Element t => Matrix t -> Int -> Int -> Matrix t -- | Partition a matrix into blocks with the given numbers of rows and -- columns. The remaining rows and columns are discarded. toBlocks :: Element t => [Int] -> [Int] -> Matrix t -> [[Matrix t]] -- | Fully partition a matrix into blocks of the same size. If the -- dimensions are not a multiple of the given size the last blocks will -- be smaller. toBlocksEvery :: Element t => Int -> Int -> Matrix t -> [[Matrix t]] -- | complex conjugate conj :: Container c e => c e -> c e -- | like fmap (cannot implement instance Functor because of Element -- class constraint) cmap :: (Element b, Container c e) => (e -> b) -> c e -> c b -- | mod for integer arrays -- --
-- >>> cmod 3 (range 5) -- fromList [0,1,2,0,1] --cmod :: (Integral e, Container c e) => e -> c e -> c e -- | A more efficient implementation of cmap (\x -> if x>0 then 1 -- else 0) -- --
-- >>> step $ linspace 5 (-1,1::Double) -- 5 |> [0.0,0.0,0.0,1.0,1.0] --step :: (Ord e, Container c e) => c e -> c e -- | Element by element version of case compare a b of {LT -> l; EQ -- -> e; GT -> g}. -- -- Arguments with any dimension = 1 are automatically expanded: -- --
-- >>> cond ((1><4)[1..]) ((3><1)[1..]) 0 100 ((3><4)[1..]) :: Matrix Double -- (3><4) -- [ 100.0, 2.0, 3.0, 4.0 -- , 0.0, 100.0, 7.0, 8.0 -- , 0.0, 0.0, 100.0, 12.0 ] ---- --
-- >>> let chop x = cond (abs x) 1E-6 0 0 x --cond :: (Ord e, Container c e, Container c x) => c e -> c e -> c x -> c x -> c x -> c x -- | Find index of elements which satisfy a predicate -- --
-- >>> find (>0) (ident 3 :: Matrix Double) -- [(0,0),(1,1),(2,2)] --find :: Container c e => (e -> Bool) -> c e -> [IndexOf c] -- | index of maximum element maxIndex :: Container c e => c e -> IndexOf c -- | index of minimum element minIndex :: Container c e => c e -> IndexOf c -- | value of maximum element maxElement :: Container c e => c e -> e -- | value of minimum element minElement :: Container c e => c e -> e sortVector :: (Ord t, Element t) => Vector t -> Vector t -- |
-- >>> m <- randn 4 10 -- -- >>> disp 2 m -- 4x10 -- -0.31 0.41 0.43 -0.19 -0.17 -0.23 -0.17 -1.04 -0.07 -1.24 -- 0.26 0.19 0.14 0.83 -1.54 -0.09 0.37 -0.63 0.71 -0.50 -- -0.11 -0.10 -1.29 -1.40 -1.04 -0.89 -0.68 0.35 -1.46 1.86 -- 1.04 -0.29 0.19 -0.75 -2.20 -0.01 1.06 0.11 -2.09 -1.58 ---- --
-- >>> disp 2 $ m ?? (All, Pos $ sortIndex (m!1)) -- 4x10 -- -0.17 -1.04 -1.24 -0.23 0.43 0.41 -0.31 -0.17 -0.07 -0.19 -- -1.54 -0.63 -0.50 -0.09 0.14 0.19 0.26 0.37 0.71 0.83 -- -1.04 0.35 1.86 -0.89 -1.29 -0.10 -0.11 -0.68 -1.46 -1.40 -- -2.20 0.11 -1.58 -0.01 0.19 -0.29 1.04 1.06 -2.09 -0.75 --sortIndex :: (Ord t, Element t) => Vector t -> Vector I type AssocMatrix = [(IndexOf Matrix, Double)] toDense :: AssocMatrix -> Matrix Double mkSparse :: AssocMatrix -> GMatrix mkDiagR :: Int -> Int -> Vector Double -> GMatrix mkDense :: Matrix Double -> GMatrix -- | print a real matrix with given number of digits after the decimal -- point -- --
-- >>> disp 5 $ ident 2 / 3 -- 2x2 -- 0.33333 0.00000 -- 0.00000 0.33333 --disp :: Int -> Matrix Double -> IO () -- | load a matrix from an ASCII file formatted as a 2D table. loadMatrix :: FilePath -> IO (Matrix Double) loadMatrix' :: FilePath -> IO (Maybe (Matrix Double)) -- | save a matrix as a 2D ASCII table saveMatrix :: FilePath -> String -> Matrix Double -> IO () -- | Tool to display matrices with latex syntax. -- --
-- >>> latexFormat "bmatrix" (dispf 2 $ ident 2)
-- "\\begin{bmatrix}\n1 & 0\n\\\\\n0 & 1\n\\end{bmatrix}"
--
latexFormat :: String -> String -> String
-- | Show a matrix with a given number of decimal places.
--
-- -- >>> dispf 2 (1/3 + ident 3) -- "3x3\n1.33 0.33 0.33\n0.33 1.33 0.33\n0.33 0.33 1.33\n" ---- --
-- >>> putStr . dispf 2 $ (3><4)[1,1.5..] -- 3x4 -- 1.00 1.50 2.00 2.50 -- 3.00 3.50 4.00 4.50 -- 5.00 5.50 6.00 6.50 ---- --
-- >>> putStr . unlines . tail . lines . dispf 2 . asRow $ linspace 10 (0,1) -- 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 --dispf :: Int -> Matrix Double -> String -- | Show a matrix with "autoscaling" and a given number of decimal places. -- --
-- >>> putStr . disps 2 $ 120 * (3><4) [1..] -- 3x4 E3 -- 0.12 0.24 0.36 0.48 -- 0.60 0.72 0.84 0.96 -- 1.08 1.20 1.32 1.44 --disps :: Int -> Matrix Double -> String -- | Pretty print a complex matrix with at most n decimal digits. dispcf :: Int -> Matrix (Complex Double) -> String -- | Creates a string from a matrix given a separator and a function to -- show each entry. Using this function the user can easily define any -- desired display function: -- --
-- import Text.Printf(printf) ---- --
-- disp = putStr . format " " (printf "%.2f") --format :: Element t => String -> (t -> String) -> Matrix t -> String dispDots :: Int -> Matrix Double -> IO () dispBlanks :: Int -> Matrix Double -> IO () dispShort :: Int -> Int -> Int -> Matrix Double -> IO () class Convert t real :: (Convert t, Complexable c) => c (RealOf t) -> c t complex :: (Convert t, Complexable c) => c t -> c (ComplexOf t) single :: (Convert t, Complexable c) => c t -> c (SingleOf t) double :: (Convert t, Complexable c) => c t -> c (DoubleOf t) toComplex :: (Convert t, Complexable c, RealElement t) => (c t, c t) -> c (Complex t) fromComplex :: (Convert t, Complexable c, RealElement t) => c (Complex t) -> (c t, c t) roundVector :: Vector Double -> Vector Double -- |
-- >>> fromInt ((2><2) [0..3]) :: Matrix (Complex Double) -- (2><2) -- [ 0.0 :+ 0.0, 1.0 :+ 0.0 -- , 2.0 :+ 0.0, 3.0 :+ 0.0 ] --fromInt :: Container c e => c I -> c e toInt :: Container c e => c e -> c I fromZ :: Container c e => c Z -> c e toZ :: Container c e => c e -> c Z arctan2 :: (Fractional e, Container c e) => c e -> c e -> c e -- | matrix computation implemented as separated vector operations by rows -- and columns. separable :: Element t => (Vector t -> Vector t) -> Matrix t -> Matrix t fromArray2D :: Storable e => Array (Int, Int) e -> Matrix e -- | Wrapper with a phantom integer for statically checked modular -- arithmetic. data Mod (n :: Nat) t -- | Storable-based vectors data Vector a -- | Matrix representation suitable for BLAS/LAPACK computations. data Matrix t -- | 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}
--
data GMatrix
nRows :: GMatrix -> Int
nCols :: GMatrix -> Int
module Numeric.LinearAlgebra
dot :: Numeric t => Vector t -> Vector t -> t
-- | An infix synonym for dot
--
-- -- >>> vector [1,2,3,4] <.> vector [-2,0,1,1] -- 5.0 ---- --
-- >>> let ๐ = 0:+1 :: C -- -- >>> fromList [1+๐,1] <.> fromList [1,1+๐] -- 2.0 :+ 0.0 --(<.>) :: Numeric t => Vector t -> Vector t -> t infixr 8 <.> -- | dense matrix-vector product -- --
-- >>> let m = (2><3) [1..] -- -- >>> m -- (2><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 ] ---- --
-- >>> let v = vector [10,20,30] ---- --
-- >>> m #> v -- [140.0,320.0] -- it :: Vector Numeric.LinearAlgebra.Data.R --(#>) :: Numeric t => Matrix t -> Vector t -> Vector t infixr 8 #> -- | dense vector-matrix product (<#) :: Numeric t => Vector t -> Matrix t -> Vector t infixl 8 <# -- | general matrix - vector product -- --
-- >>> let m = mkSparse [((0,999),1.0),((1,1999),2.0)] -- m :: GMatrix -- -- >>> m !#> vector [1..2000] -- [1000.0,4000.0] -- it :: Vector Double --(!#>) :: GMatrix -> Vector Double -> Vector Double infixr 8 !#> -- | dense matrix product -- --
-- >>> let a = (3><5) [1..] -- -- >>> a -- (3><5) -- [ 1.0, 2.0, 3.0, 4.0, 5.0 -- , 6.0, 7.0, 8.0, 9.0, 10.0 -- , 11.0, 12.0, 13.0, 14.0, 15.0 ] ---- --
-- >>> let b = (5><2) [1,3, 0,2, -1,5, 7,7, 6,0] -- -- >>> b -- (5><2) -- [ 1.0, 3.0 -- , 0.0, 2.0 -- , -1.0, 5.0 -- , 7.0, 7.0 -- , 6.0, 0.0 ] ---- --
-- >>> a <> b -- (3><2) -- [ 56.0, 50.0 -- , 121.0, 135.0 -- , 186.0, 220.0 ] --(<>) :: Numeric t => Matrix t -> Matrix t -> Matrix t infixr 8 <> -- | Outer product of two vectors. -- --
-- >>> fromList [1,2,3] `outer` fromList [5,2,3] -- (3><3) -- [ 5.0, 2.0, 3.0 -- , 10.0, 4.0, 6.0 -- , 15.0, 6.0, 9.0 ] --outer :: Product t => Vector t -> Vector t -> Matrix t -- | Kronecker product of two matrices. -- --
-- m1=(2><3) -- [ 1.0, 2.0, 0.0 -- , 0.0, -1.0, 3.0 ] -- m2=(4><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 -- , 7.0, 8.0, 9.0 -- , 10.0, 11.0, 12.0 ] ---- --
-- >>> kronecker m1 m2 -- (8><9) -- [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0 -- , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0 -- , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0 -- , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0 -- , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0 -- , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0 -- , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0 -- , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ] --kronecker :: Product t => Matrix t -> Matrix t -> Matrix t -- | cross product (for three-element vectors) cross :: Product t => Vector t -> Vector t -> Vector t scale :: Linear t c => t -> c t -> c t add :: Additive c => c -> c -> c -- | the sum of elements sumElements :: Container c e => c e -> e -- | the product of elements prodElements :: Container c e => c e -> e -- | Least squares solution of a linear system, similar to the \ operator -- of Matlab/Octave (based on linearSolveSVD) -- --
-- a = (3><2) -- [ 1.0, 2.0 -- , 2.0, 4.0 -- , 2.0, -1.0 ] ---- --
-- v = vector [13.0,27.0,1.0] ---- --
-- >>> let x = a <\> v -- -- >>> x -- [3.0799999999999996,5.159999999999999] -- it :: Vector Numeric.LinearAlgebra.Data.R ---- --
-- >>> a #> x -- [13.399999999999999,26.799999999999997,0.9999999999999991] -- it :: Vector Numeric.LinearAlgebra.Data.R ---- -- It also admits multiple right-hand sides stored as columns in a -- matrix. (<\>) :: (LSDiv c, Field t) => Matrix t -> c t -> c t infixl 7 <\> -- | Least squared error solution of an overconstrained linear system, or -- the minimum norm solution of an underconstrained system. For -- rank-deficient systems use linearSolveSVD. linearSolveLS :: Field t => Matrix t -> Matrix t -> Matrix t -- | Minimum norm solution of a general linear least squares problem Ax=B -- using the SVD. Admits rank-deficient systems but it is slower than -- linearSolveLS. The effective rank of A is determined by -- treating as zero those singular valures which are less than eps -- times the largest singular value. linearSolveSVD :: Field t => Matrix t -> Matrix t -> Matrix t -- | Solve a linear system (for square coefficient matrix and several -- right-hand sides) using the LU decomposition, returning Nothing for a -- singular system. For underconstrained or overconstrained systems use -- linearSolveLS or linearSolveSVD. -- --
-- a = (2><2) -- [ 1.0, 2.0 -- , 3.0, 5.0 ] ---- --
-- b = (2><3) -- [ 6.0, 1.0, 10.0 -- , 15.0, 3.0, 26.0 ] ---- --
-- >>> linearSolve a b -- Just (2><3) -- [ -1.4802973661668753e-15, 0.9999999999999997, 1.999999999999997 -- , 3.000000000000001, 1.6653345369377348e-16, 4.000000000000002 ] ---- --
-- >>> let Just x = it -- -- >>> disp 5 x -- 2x3 -- -0.00000 1.00000 2.00000 -- 3.00000 0.00000 4.00000 ---- --
-- >>> a <> x -- (2><3) -- [ 6.0, 1.0, 10.0 -- , 15.0, 3.0, 26.0 ] --linearSolve :: Field t => Matrix t -> Matrix t -> Maybe (Matrix t) -- | Solution of a linear system (for several right hand sides) from the -- precomputed LU factorization obtained by luPacked. luSolve :: Field t => LU t -> Matrix t -> Matrix t -- | Obtains the LU decomposition of a matrix in a compact data structure -- suitable for luSolve. luPacked :: Field t => Matrix t -> LU t -- | Experimental implementation of luSolve for any Fractional -- element type, including Mod n I and Mod n -- Z. -- --
-- >>> let a = (2><2) [1,2,3,5] :: Matrix (Z ./. 13) -- (2><2) -- [ 1, 2 -- , 3, 5 ] -- -- >>> b -- (2><3) -- [ 5, 1, 3 -- , 8, 6, 3 ] ---- --
-- >>> luSolve' (luPacked' a) b -- (2><3) -- [ 4, 7, 4 -- , 7, 10, 6 ] --luSolve' :: (Fractional t, Container Vector t) => LU t -> Matrix t -> Matrix t -- | Experimental implementation of luPacked for any Fractional -- element type, including Mod n I and Mod n -- Z. -- --
-- >>> let m = ident 5 + (5><5) [0..] :: Matrix (Z ./. 17) -- (5><5) -- [ 1, 1, 2, 3, 4 -- , 5, 7, 7, 8, 9 -- , 10, 11, 13, 13, 14 -- , 15, 16, 0, 2, 2 -- , 3, 4, 5, 6, 8 ] ---- --
-- >>> let (l,u,p,s) = luFact $ luPacked' m -- -- >>> l -- (5><5) -- [ 1, 0, 0, 0, 0 -- , 6, 1, 0, 0, 0 -- , 12, 7, 1, 0, 0 -- , 7, 10, 7, 1, 0 -- , 8, 2, 6, 11, 1 ] -- -- >>> u -- (5><5) -- [ 15, 16, 0, 2, 2 -- , 0, 13, 7, 13, 14 -- , 0, 0, 15, 0, 11 -- , 0, 0, 0, 15, 15 -- , 0, 0, 0, 0, 1 ] --luPacked' :: (Container Vector t, Fractional t, Normed (Vector t), Num (Vector t)) => Matrix t -> LU t -- | Solution of a linear system (for several right hand sides) from a -- precomputed LDL factorization obtained by ldlPacked. -- -- Note: this can be slower than the general solver based on the LU -- decomposition. ldlSolve :: Field t => LDL t -> Matrix t -> Matrix t -- | Obtains the LDL decomposition of a matrix in a compact data structure -- suitable for ldlSolve. ldlPacked :: Field t => Herm t -> LDL t -- | Solve a symmetric or Hermitian positive definite linear system using a -- precomputed Cholesky decomposition obtained by chol. cholSolve :: Field t => Matrix t -> Matrix t -> Matrix t data UpLo Lower :: UpLo Upper :: UpLo -- | Solve a triangular linear system. If Upper is specified then -- all elements below the diagonal are ignored; if Lower is -- specified then all elements above the diagonal are ignored. triSolve :: Field t => UpLo -> Matrix t -> Matrix t -> Matrix t -- | Solve a tridiagonal linear system. Suppose you wish to solve -- <math> where -- -- <math> -- -- then -- --
-- dL = fromList [3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0] -- d = fromList [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] -- dU = fromList [4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0] -- -- b = (9><3) -- [ -- 1.0, 1.0, 1.0, -- 1.0, -1.0, 2.0, -- 1.0, 1.0, 3.0, -- 1.0, -1.0, 4.0, -- 1.0, 1.0, 5.0, -- 1.0, -1.0, 6.0, -- 1.0, 1.0, 7.0, -- 1.0, -1.0, 8.0, -- 1.0, 1.0, 9.0 -- ] -- -- x = triDiagSolve dL d dU b --triDiagSolve :: Field t => Vector t -> Vector t -> Vector t -> Matrix t -> Matrix t -- | Solve a sparse linear system using the conjugate gradient method with -- default parameters. cgSolve :: Bool -> GMatrix -> Vector R -> Vector R -- | Solve a sparse linear system using the conjugate gradient method with -- default parameters. cgSolve' :: Bool -> R -> R -> Int -> GMatrix -> Vector R -> Vector R -> [CGState] -- | Inverse of a square matrix. See also invlndet. inv :: Field t => Matrix t -> Matrix t -- | Pseudoinverse of a general matrix with default tolerance -- (pinvTol 1, similar to GNU-Octave). pinv :: Field t => Matrix t -> Matrix t -- | pinvTol r computes the pseudoinverse of a matrix with -- tolerance tol=r*g*eps*(max rows cols), where g is the -- greatest singular value. -- --
-- m = (3><3) [ 1, 0, 0 -- , 0, 1, 0 -- , 0, 0, 1e-10] :: Matrix Double ---- --
-- >>> pinv m -- 1. 0. 0. -- 0. 1. 0. -- 0. 0. 10000000000. ---- --
-- >>> pinvTol 1E8 m -- 1. 0. 0. -- 0. 1. 0. -- 0. 0. 1. --pinvTol :: Field t => Double -> Matrix t -> Matrix t -- | Reciprocal of the 2-norm condition number of a matrix, computed from -- the singular values. rcond :: Field t => Matrix t -> Double -- | Number of linearly independent rows or columns. See also ranksv rank :: Field t => Matrix t -> Int -- | Determinant of a square matrix. To avoid possible overflow or -- underflow use invlndet. det :: Field t => Matrix t -> t -- | Joint computation of inverse and logarithm of determinant of a square -- matrix. invlndet :: Field t => Matrix t -> (Matrix t, (t, t)) -- | p-norm for vectors, operator norm for matrices class Normed a norm_0 :: Normed a => a -> R norm_1 :: Normed a => a -> R norm_2 :: Normed a => a -> R norm_Inf :: Normed a => a -> R -- | Frobenius norm (Schatten p-norm with p=2) norm_Frob :: (Normed (Vector t), Element t) => Matrix t -> R -- | Sum of singular values (Schatten p-norm with p=1) norm_nuclear :: Field t => Matrix t -> R -- | return an orthonormal basis of the range space of a matrix. See also -- orthSVD. orth :: Field t => Matrix t -> Matrix t -- | return an orthonormal basis of the null space of a matrix. See also -- nullspaceSVD. nullspace :: Field t => Matrix t -> Matrix t -- | solution of overconstrained homogeneous linear system null1 :: Matrix R -> Vector R -- | solution of overconstrained homogeneous symmetric linear system null1sym :: Herm R -> Vector R -- | Full singular value decomposition. -- --
-- a = (5><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 -- , 7.0, 8.0, 9.0 -- , 10.0, 11.0, 12.0 -- , 13.0, 14.0, 15.0 ] :: Matrix Double ---- --
-- >>> let (u,s,v) = svd a ---- --
-- >>> disp 3 u -- 5x5 -- -0.101 0.768 0.614 0.028 -0.149 -- -0.249 0.488 -0.503 0.172 0.646 -- -0.396 0.208 -0.405 -0.660 -0.449 -- -0.543 -0.072 -0.140 0.693 -0.447 -- -0.690 -0.352 0.433 -0.233 0.398 ---- --
-- >>> s -- [35.18264833189422,1.4769076999800903,1.089145439970417e-15] -- it :: Vector Double ---- --
-- >>> disp 3 v -- 3x3 -- -0.519 -0.751 0.408 -- -0.576 -0.046 -0.816 -- -0.632 0.659 0.408 ---- --
-- >>> let d = diagRect 0 s 5 3 -- -- >>> disp 3 d -- 5x3 -- 35.183 0.000 0.000 -- 0.000 1.477 0.000 -- 0.000 0.000 0.000 -- 0.000 0.000 0.000 ---- --
-- >>> disp 3 $ u <> d <> tr v -- 5x3 -- 1.000 2.000 3.000 -- 4.000 5.000 6.000 -- 7.000 8.000 9.000 -- 10.000 11.000 12.000 -- 13.000 14.000 15.000 --svd :: Field t => Matrix t -> (Matrix t, Vector Double, Matrix t) -- | A version of svd which returns only the min (rows m) (cols -- m) singular vectors of m. -- -- If (u,s,v) = thinSVD m then m == u <> diag s -- <> tr v. -- --
-- a = (5><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 -- , 7.0, 8.0, 9.0 -- , 10.0, 11.0, 12.0 -- , 13.0, 14.0, 15.0 ] :: Matrix Double ---- --
-- >>> let (u,s,v) = thinSVD a ---- --
-- >>> disp 3 u -- 5x3 -- -0.101 0.768 0.614 -- -0.249 0.488 -0.503 -- -0.396 0.208 -0.405 -- -0.543 -0.072 -0.140 -- -0.690 -0.352 0.433 ---- --
-- >>> s -- [35.18264833189422,1.4769076999800903,1.089145439970417e-15] -- it :: Vector Double ---- --
-- >>> disp 3 v -- 3x3 -- -0.519 -0.751 0.408 -- -0.576 -0.046 -0.816 -- -0.632 0.659 0.408 ---- --
-- >>> disp 3 $ u <> diag s <> tr v -- 5x3 -- 1.000 2.000 3.000 -- 4.000 5.000 6.000 -- 7.000 8.000 9.000 -- 10.000 11.000 12.000 -- 13.000 14.000 15.000 --thinSVD :: Field t => Matrix t -> (Matrix t, Vector Double, Matrix t) -- | Similar to thinSVD, returning only the nonzero singular values -- and the corresponding singular vectors. -- --
-- a = (5><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 -- , 7.0, 8.0, 9.0 -- , 10.0, 11.0, 12.0 -- , 13.0, 14.0, 15.0 ] :: Matrix Double ---- --
-- >>> let (u,s,v) = compactSVD a ---- --
-- >>> disp 3 u -- 5x2 -- -0.101 0.768 -- -0.249 0.488 -- -0.396 0.208 -- -0.543 -0.072 -- -0.690 -0.352 ---- --
-- >>> s -- [35.18264833189422,1.476907699980091] -- it :: Vector Double ---- --
-- >>> disp 3 u -- 5x2 -- -0.101 0.768 -- -0.249 0.488 -- -0.396 0.208 -- -0.543 -0.072 -- -0.690 -0.352 ---- --
-- >>> disp 3 $ u <> diag s <> tr v -- 5x3 -- 1.000 2.000 3.000 -- 4.000 5.000 6.000 -- 7.000 8.000 9.000 -- 10.000 11.000 12.000 -- 13.000 14.000 15.000 --compactSVD :: Field t => Matrix t -> (Matrix t, Vector Double, Matrix t) -- | compactSVDTol r is similar to compactSVD (for which -- r=1), but uses tolerance tol=r*g*eps*(max rows cols) -- to distinguish nonzero singular values, where g is the -- greatest singular value. If g<r*eps, then only one -- singular value is returned. compactSVDTol :: Field t => Double -> Matrix t -> (Matrix t, Vector Double, Matrix t) -- | Singular values only. singularValues :: Field t => Matrix t -> Vector Double -- | Singular values and all left singular vectors (as columns). leftSV :: Field t => Matrix t -> (Matrix t, Vector Double) -- | Singular values and all right singular vectors (as columns). rightSV :: Field t => Matrix t -> (Vector Double, Matrix t) -- | Eigenvalues (not ordered) and eigenvectors (as columns) of a general -- square matrix. -- -- If (s,v) = eig m then m <> v == v <> diag -- s -- --
-- a = (3><3) -- [ 3, 0, -2 -- , 4, 5, -1 -- , 3, 1, 0 ] :: Matrix Double ---- --
-- >>> let (l, v) = eig a ---- --
-- >>> putStr . dispcf 3 . asRow $ l -- 1x3 -- 1.925+1.523i 1.925-1.523i 4.151 ---- --
-- >>> putStr . dispcf 3 $ v -- 3x3 -- -0.455+0.365i -0.455-0.365i 0.181 -- 0.603 0.603 -0.978 -- 0.033+0.543i 0.033-0.543i -0.104 ---- --
-- >>> putStr . dispcf 3 $ complex a <> v -- 3x3 -- -1.432+0.010i -1.432-0.010i 0.753 -- 1.160+0.918i 1.160-0.918i -4.059 -- -0.763+1.096i -0.763-1.096i -0.433 ---- --
-- >>> putStr . dispcf 3 $ v <> diag l -- 3x3 -- -1.432+0.010i -1.432-0.010i 0.753 -- 1.160+0.918i 1.160-0.918i -4.059 -- -0.763+1.096i -0.763-1.096i -0.433 --eig :: Field t => Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) -- | Generalized eigenvalues (not ordered) and eigenvectors (as columns) of -- a pair of nonsymmetric matrices. Eigenvalues are represented as pairs -- of alpha, beta, where eigenvalue = alpha / beta. Alpha is always -- complex, but betas has the same type as the input matrix. -- -- If (alphas, betas, v) = geig a b, then a <> v == b -- <> v <> diag (alphas / betas) -- -- Note that beta can be 0 and that has reasonable interpretation. geig :: Field t => Matrix t -> Matrix t -> (Vector (Complex Double), Vector t, Matrix (Complex Double)) -- | Eigenvalues and eigenvectors (as columns) of a complex hermitian or -- real symmetric matrix, in descending order. -- -- If (s,v) = eigSH m then m == v <> diag s <> -- tr v -- --
-- a = (3><3) -- [ 1.0, 2.0, 3.0 -- , 2.0, 4.0, 5.0 -- , 3.0, 5.0, 6.0 ] ---- --
-- >>> let (l, v) = eigSH a ---- --
-- >>> l -- [11.344814282762075,0.17091518882717918,-0.5157294715892575] ---- --
-- >>> disp 3 $ v <> diag l <> tr v -- 3x3 -- 1.000 2.000 3.000 -- 2.000 4.000 5.000 -- 3.000 5.000 6.000 --eigSH :: Field t => Herm t -> (Vector Double, Matrix t) -- | Eigenvalues (not ordered) of a general square matrix. eigenvalues :: Field t => Matrix t -> Vector (Complex Double) -- | Generalized eigenvalues of a pair of matrices. Represented as pairs of -- alpha, beta, where eigenvalue is alpha / beta as in geig. geigenvalues :: Field t => Matrix t -> Matrix t -> (Vector (Complex Double), Vector t) -- | Eigenvalues (in descending order) of a complex hermitian or real -- symmetric matrix. eigenvaluesSH :: Field t => Herm t -> Vector Double -- | Generalized symmetric positive definite eigensystem Av = lBv, for A -- and B symmetric, B positive definite. geigSH :: Field t => Herm t -> Herm t -> (Vector Double, Matrix t) -- | QR factorization. -- -- If (q,r) = qr m then m == q <> r, where q is -- unitary and r is upper triangular. Note: the current implementation is -- very slow for large matrices. thinQR is much faster. qr :: Field t => Matrix t -> (Matrix t, Matrix t) -- | A version of qr which returns only the min (rows m) (cols -- m) columns of q and rows of r. thinQR :: Field t => Matrix t -> (Matrix t, Matrix t) -- | RQ factorization. -- -- If (r,q) = rq m then m == r <> q, where q is -- unitary and r is upper triangular. Note: the current implementation is -- very slow for large matrices. thinRQ is much faster. rq :: Field t => Matrix t -> (Matrix t, Matrix t) -- | A version of rq which returns only the min (rows m) (cols -- m) columns of r and rows of q. thinRQ :: Field t => Matrix t -> (Matrix t, Matrix t) -- | Compute the QR decomposition of a matrix in compact form. qrRaw :: Field t => Matrix t -> QR t -- | generate a matrix with k orthogonal columns from the compact QR -- decomposition obtained by qrRaw. qrgr :: Field t => Int -> QR t -> Matrix t -- | Cholesky factorization of a positive definite hermitian or symmetric -- matrix. -- -- If c = chol m then c is upper triangular and m -- == tr c <> c. chol :: Field t => Herm t -> Matrix t -- | Similar to chol, but instead of an error (e.g., caused by a -- matrix not positive definite) it returns Nothing. mbChol :: Field t => Herm t -> Maybe (Matrix t) -- | Explicit LU factorization of a general matrix. -- -- If (l,u,p,s) = lu m then m == p <> l <> -- u, where l is lower triangular, u is upper triangular, p is a -- permutation matrix and s is the signature of the permutation. lu :: Field t => Matrix t -> (Matrix t, Matrix t, Matrix t, t) -- | Compute the explicit LU decomposition from the compact one obtained by -- luPacked. luFact :: Numeric t => LU t -> (Matrix t, Matrix t, Matrix t, t) -- | Hessenberg factorization. -- -- If (p,h) = hess m then m == p <> h <> tr -- p, where p is unitary and h is in upper Hessenberg form (it has -- zero entries below the first subdiagonal). hess :: Field t => Matrix t -> (Matrix t, Matrix t) -- | Schur factorization. -- -- If (u,s) = schur m then m == u <> s <> tr -- u, where u is unitary and s is a Shur matrix. A complex Schur -- matrix is upper triangular. A real Schur matrix is upper triangular in -- 2x2 blocks. -- -- "Anything that the Jordan decomposition can do, the Schur -- decomposition can do better!" (Van Loan) schur :: Field t => Matrix t -> (Matrix t, Matrix t) -- | Matrix exponential. It uses a direct translation of Algorithm 11.3.1 -- in Golub & Van Loan, based on a scaled Pade approximation. expm :: Field t => Matrix t -> Matrix t -- | Matrix square root. Currently it uses a simple iterative algorithm -- described in Wikipedia. It only works with invertible matrices that -- have a real solution. -- --
-- m = (2><2) [4,9 -- ,0,4] :: Matrix Double ---- --
-- >>> sqrtm m -- (2><2) -- [ 2.0, 2.25 -- , 0.0, 2.0 ] ---- -- For diagonalizable matrices you can try matFunc sqrt: -- --
-- >>> matFunc sqrt ((2><2) [1,0,0,-1]) -- (2><2) -- [ 1.0 :+ 0.0, 0.0 :+ 0.0 -- , 0.0 :+ 0.0, 0.0 :+ 1.0 ] --sqrtm :: Field t => Matrix t -> Matrix t -- | Generic matrix functions for diagonalizable matrices. For instance: -- --
-- logm = matFunc log --matFunc :: (Complex Double -> Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) -- | correlation -- --
-- >>> corr (fromList[1,2,3]) (fromList [1..10]) -- [14.0,20.0,26.0,32.0,38.0,44.0,50.0,56.0] -- it :: (Enum t, Product t, Container Vector t) => Vector t --corr :: (Container Vector t, Product t) => Vector t -> Vector t -> Vector t -- | convolution (corr with reversed kernel and padded input, -- equivalent to polynomial product) -- --
-- >>> conv (fromList[1,1]) (fromList [-1,1]) -- [-1.0,0.0,1.0] -- it :: (Product t, Container Vector t) => Vector t --conv :: (Container Vector t, Product t, Num t) => Vector t -> Vector t -> Vector t -- | similar to corr, using min instead of (*) corrMin :: (Container Vector t, RealElement t, Product t) => Vector t -> Vector t -> Vector t -- | 2D correlation (without padding) -- --
-- >>> disp 5 $ corr2 (konst 1 (3,3)) (ident 10 :: Matrix Double) -- 8x8 -- 3 2 1 0 0 0 0 0 -- 2 3 2 1 0 0 0 0 -- 1 2 3 2 1 0 0 0 -- 0 1 2 3 2 1 0 0 -- 0 0 1 2 3 2 1 0 -- 0 0 0 1 2 3 2 1 -- 0 0 0 0 1 2 3 2 -- 0 0 0 0 0 1 2 3 --corr2 :: Product a => Matrix a -> Matrix a -> Matrix a -- | 2D convolution -- --
-- >>> disp 5 $ conv2 (konst 1 (3,3)) (ident 10 :: Matrix Double) -- 12x12 -- 1 1 1 0 0 0 0 0 0 0 0 0 -- 1 2 2 1 0 0 0 0 0 0 0 0 -- 1 2 3 2 1 0 0 0 0 0 0 0 -- 0 1 2 3 2 1 0 0 0 0 0 0 -- 0 0 1 2 3 2 1 0 0 0 0 0 -- 0 0 0 1 2 3 2 1 0 0 0 0 -- 0 0 0 0 1 2 3 2 1 0 0 0 -- 0 0 0 0 0 1 2 3 2 1 0 0 -- 0 0 0 0 0 0 1 2 3 2 1 0 -- 0 0 0 0 0 0 0 1 2 3 2 1 -- 0 0 0 0 0 0 0 0 1 2 2 1 -- 0 0 0 0 0 0 0 0 0 1 1 1 --conv2 :: (Num (Matrix a), Product a, Container Vector a) => Matrix a -> Matrix a -> Matrix a type Seed = Int data RandDist -- | uniform distribution in [0,1) Uniform :: RandDist -- | normal distribution with mean zero and standard deviation one Gaussian :: RandDist -- | Obtains a vector of pseudorandom elements (use randomIO to get a -- random seed). randomVector :: Seed -> RandDist -> Int -> Vector Double -- | pseudorandom matrix with uniform elements between 0 and 1 rand :: Int -> Int -> IO (Matrix Double) -- | pseudorandom matrix with normal elements -- --
-- >>> disp 3 =<< randn 3 5 -- 3x5 -- 0.386 -1.141 0.491 -0.510 1.512 -- 0.069 -0.919 1.022 -0.181 0.745 -- 0.313 -0.670 -0.097 -1.575 -0.583 --randn :: Int -> Int -> IO (Matrix Double) -- | Obtains a matrix whose rows are pseudorandom samples from a -- multivariate Gaussian distribution. gaussianSample :: Seed -> Int -> Vector Double -> Herm Double -> Matrix Double -- | Obtains a matrix whose rows are pseudorandom samples from a -- multivariate uniform distribution. uniformSample :: Seed -> Int -> [(Double, Double)] -> Matrix Double -- | Compute mean vector and covariance matrix of the rows of a matrix. -- --
-- >>> meanCov $ gaussianSample 666 1000 (fromList[4,5]) (trustSym $ diagl [2,3]) -- ([3.9933155655086696,5.061409102770331],Herm (2><2) -- [ 1.9963242906624408, -4.227815571404954e-2 -- , -4.227815571404954e-2, 3.2003833097832857 ]) -- it :: (Vector Double, Herm Double) --meanCov :: Matrix Double -> (Vector Double, Herm Double) -- | outer products of rows -- --
-- >>> a -- (3><2) -- [ 1.0, 2.0 -- , 10.0, 20.0 -- , 100.0, 200.0 ] -- -- >>> b -- (3><3) -- [ 1.0, 2.0, 3.0 -- , 4.0, 5.0, 6.0 -- , 7.0, 8.0, 9.0 ] ---- --
-- >>> rowOuters a (b ||| 1) -- (3><8) -- [ 1.0, 2.0, 3.0, 1.0, 2.0, 4.0, 6.0, 2.0 -- , 40.0, 50.0, 60.0, 10.0, 80.0, 100.0, 120.0, 20.0 -- , 700.0, 800.0, 900.0, 100.0, 1400.0, 1600.0, 1800.0, 200.0 ] --rowOuters :: Matrix Double -> Matrix Double -> Matrix Double -- | Matrix of pairwise squared distances of row vectors (using the matrix -- product trick in blog.smola.org) pairwiseD2 :: Matrix Double -> Matrix Double -> Matrix Double -- | Obtains a vector in the same direction with 2-norm=1 normalize :: (Normed (Vector t), Num (Vector t), Field t) => Vector t -> Vector t -- | 1 + 0.5*peps == 1, 1 + 0.6*peps /= 1 peps :: RealFloat x => x relativeError :: Num a => (a -> Double) -> a -> a -> Double -- | Check if the absolute value or complex magnitude is greater than a -- given threshold -- --
-- >>> magnit 1E-6 (1E-12 :: R) -- False -- -- >>> magnit 1E-6 (3+iC :: C) -- True -- -- >>> magnit 0 (3 :: I ./. 5) -- True --magnit :: (Element t, Normed (Vector t)) => R -> t -> Bool haussholder :: Field a => a -> Vector a -> Matrix a optimiseMult :: Monoid (Matrix t) => [Matrix t] -> Matrix t -- | unconjugated dot product udot :: Product e => Vector e -> Vector e -> e -- | The nullspace of a matrix from its precomputed SVD decomposition. nullspaceSVD :: Field t => Either Double Int -> Matrix t -> (Vector Double, Matrix t) -> Matrix t -- | The range space a matrix from its precomputed SVD decomposition. orthSVD :: Field t => Either Double Int -> Matrix t -> (Matrix t, Vector Double) -> Matrix t -- | Numeric rank of a matrix from its singular values. ranksv :: Double -> Int -> [Double] -> Int -- | imaginary unit iC :: C -- | Compute the complex Hermitian or real symmetric part of a square -- matrix ((x + tr x)/2). sym :: Field t => Matrix t -> Herm t -- | Compute the contraction tr x <> x of a general matrix. mTm :: Numeric t => Matrix t -> Herm t -- | At your own risk, declare that a matrix is complex Hermitian or real -- symmetric for usage in chol, eigSH, etc. Only a -- triangular part of the matrix will be used. trustSym :: Matrix t -> Herm t -- | Extract the general matrix from a Herm structure, forgetting -- its symmetric or Hermitian property. unSym :: Herm t -> Matrix t -- | Supported matrix elements. class (Storable a) => Element a -- | Basic element-by-element functions for numeric containers class Element e => Container c e -- | Matrix product and related functions class (Num e, Element e) => Product e class (Container Vector t, Container Matrix t, Konst t Int Vector, Konst t (Int, Int) Matrix, CTrans t, Product t, Additive (Vector t), Additive (Matrix t), Linear t Vector, Linear t Matrix) => Numeric t class LSDiv c -- | A matrix that, by construction, it is known to be complex Hermitian or -- real symmetric. -- -- It can be created using sym, mTm, or trustSym, -- and the matrix can be extracted using unSym. data Herm t -- | Structures that may contain complex numbers class Complexable c -- | Supported real types class (Element t, Element (Complex t), RealFloat t) => RealElement t type family RealOf x type ComplexOf x = Complex (RealOf x) type family SingleOf x type family DoubleOf x type family IndexOf (c :: * -> *) -- | Generic linear algebra functions for double precision real and complex -- matrices. -- -- (Single precision data can be converted using single and -- double). class (Numeric t, Convert t, Normed Matrix t, Normed Vector t, Floating t, Linear t Vector, Linear t Matrix, Additive (Vector t), Additive (Matrix t), RealOf t ~ Double) => Field t class Linear t c class Additive c class Transposable m mt | m -> mt, mt -> m -- | LU decomposition of a matrix in a compact format. data LU t LU :: Matrix t -> [Int] -> LU t -- | LDL decomposition of a complex Hermitian or real symmetric matrix in a -- compact format. data LDL t LDL :: Matrix t -> [Int] -> LDL t -- | QR decomposition of a matrix in compact form. (The orthogonal matrix -- is not explicitly formed.) data QR t QR :: Matrix t -> Vector t -> QR t data CGState CGState :: Vector R -> Vector R -> R -> Vector R -> R -> CGState -- | conjugate gradient [cgp] :: CGState -> Vector R -- | residual [cgr] :: CGState -> Vector R -- | squared norm of residual [cgr2] :: CGState -> R -- | current solution [cgx] :: CGState -> Vector R -- | normalized size of correction [cgdx] :: CGState -> R class Testable t checkT :: Testable t => t -> (Bool, IO ()) ioCheckT :: Testable t => t -> IO (Bool, IO ()) -- | compatibility with previous version, to be removed module Numeric.LinearAlgebra.HMatrix -- | a synonym for (|||) (unicode 0x00a6, broken bar) (ยฆ) :: Matrix Double -> Matrix Double -> Matrix Double infixl 3 ยฆ -- | a synonym for (===) (unicode 0x2014, em dash) (โโ) :: Matrix Double -> Matrix Double -> Matrix Double infixl 2 โโ type โ = Double type โ = Complex Double (<ยท>) :: Numeric t => Vector t -> Vector t -> t infixr 8 <ยท> app :: Numeric t => Matrix t -> Vector t -> Vector t mul :: Numeric t => Matrix t -> Matrix t -> Matrix t -- | Similar to chol, without checking that the input matrix is -- hermitian or symmetric. It works with the upper triangular part. cholSH :: Field t => Matrix t -> Matrix t -- | Similar to cholSH, but instead of an error (e.g., caused by a -- matrix not positive definite) it returns Nothing. mbCholSH :: Field t => Matrix t -> Maybe (Matrix t) -- | Similar to eigSH without checking that the input matrix is -- hermitian or symmetric. It works with the upper triangular part. eigSH' :: Field t => Matrix t -> (Vector Double, Matrix t) -- | Similar to eigenvaluesSH without checking that the input matrix -- is hermitian or symmetric. It works with the upper triangular part. eigenvaluesSH' :: Field t => Matrix t -> Vector Double geigSH' :: Field t => Matrix t -> Matrix t -> (Vector Double, Matrix t) -- | Experimental interface with statically checked dimensions. -- -- See code examples at -- http://dis.um.es/~alberto/hmatrix/static.html. module Numeric.LinearAlgebra.Static type โ = Double data R n vec2 :: โ -> โ -> R 2 vec3 :: โ -> โ -> โ -> R 3 vec4 :: โ -> โ -> โ -> โ -> R 4 (&) :: forall n. KnownNat n => R n -> โ -> R (n + 1) infixl 4 & (#) :: forall n m. (KnownNat n, KnownNat m) => R n -> R m -> R (n + m) infixl 4 # split :: forall p n. (KnownNat p, KnownNat n, p <= n) => R n -> (R p, R (n - p)) headTail :: (KnownNat n, 1 <= n) => R n -> (โ, R (n - 1)) vector :: KnownNat n => [โ] -> R n linspace :: forall n. KnownNat n => (โ, โ) -> R n range :: forall n. KnownNat n => R n dim :: forall n. KnownNat n => R n data L m n type Sq n = L n n build :: forall m n. (KnownNat n, KnownNat m) => (โ -> โ -> โ) -> L m n row :: R n -> L 1 n col :: forall (n :: Nat). KnownNat n => R n -> L n 1 (|||) :: forall (c :: Nat) (r1 :: Nat) (r2 :: Nat). (KnownNat c, KnownNat (r1 + r2), KnownNat r1, KnownNat r2) => L c r1 -> L c r2 -> L c (r1 + r2) infixl 3 ||| (===) :: (KnownNat r1, KnownNat r2, KnownNat c) => L r1 c -> L r2 c -> L (r1 + r2) c infixl 2 === splitRows :: forall p m n. (KnownNat p, KnownNat m, KnownNat n, p <= m) => L m n -> (L p n, L (m - p) n) splitCols :: forall p m n. (KnownNat p, KnownNat m, KnownNat n, KnownNat (n - p), p <= n) => L m n -> (L m p, L m (n - p)) unrow :: L 1 n -> R n uncol :: forall (n :: Nat). KnownNat n => L n 1 -> R n -- | conjugate transpose tr :: Transposable m mt => m -> mt eye :: KnownNat n => Sq n diag :: KnownNat n => R n -> Sq n blockAt :: forall m n. (KnownNat m, KnownNat n) => โ -> Int -> Int -> Matrix Double -> L m n matrix :: (KnownNat m, KnownNat n) => [โ] -> L m n type โ = Complex Double data C n data M m n data Her n her :: KnownNat n => M n n -> Her n ๐ :: Sized โ s c => s toComplex :: KnownNat n => (R n, R n) -> C n fromComplex :: KnownNat n => C n -> (R n, R n) complex :: KnownNat n => R n -> C n real :: KnownNat n => C n -> R n imag :: KnownNat n => C n -> R n sqMagnitude :: KnownNat n => C n -> R n magnitude :: KnownNat n => C n -> R n (<>) :: forall m k n. (KnownNat m, KnownNat k, KnownNat n) => L m k -> L k n -> L m n infixr 8 <> (#>) :: (KnownNat m, KnownNat n) => L m n -> R n -> R m infixr 8 #> (<.>) :: KnownNat n => R n -> R n -> โ infixr 8 <.> linSolve :: (KnownNat m, KnownNat n) => L m m -> L m n -> Maybe (L m n) (<\>) :: (KnownNat m, KnownNat n, KnownNat r) => L m n -> L m r -> L n r svd :: (KnownNat m, KnownNat n) => L m n -> (L m m, R n, L n n) withCompactSVD :: forall m n z. (KnownNat m, KnownNat n) => L m n -> (forall k. KnownNat k => (L m k, R k, L n k) -> z) -> z svdTall :: (KnownNat m, KnownNat n, n <= m) => L m n -> (L m n, R n, L n n) svdFlat :: (KnownNat m, KnownNat n, m <= n) => L m n -> (L m m, R m, L n m) class Eigen m l v | m -> l, m -> v eigensystem :: Eigen m l v => m -> (l, v) eigenvalues :: Eigen m l v => m -> l withNullspace :: forall m n z. (KnownNat m, KnownNat n) => L m n -> (forall k. KnownNat k => L n k -> z) -> z withOrth :: forall m n z. (KnownNat m, KnownNat n) => L m n -> (forall k. KnownNat k => L n k -> z) -> z qr :: (KnownNat m, KnownNat n) => L m n -> (L m m, L m n) chol :: KnownNat n => Sym n -> Sq n -- | p-norm for vectors, operator norm for matrices class Normed a norm_0 :: Normed a => a -> R norm_1 :: Normed a => a -> R norm_2 :: Normed a => a -> R norm_Inf :: Normed a => a -> R type Seed = Int data RandDist -- | uniform distribution in [0,1) Uniform :: RandDist -- | normal distribution with mean zero and standard deviation one Gaussian :: RandDist randomVector :: forall n. KnownNat n => Seed -> RandDist -> R n rand :: forall m n. (KnownNat m, KnownNat n) => IO (L m n) randn :: forall m n. (KnownNat m, KnownNat n) => IO (L m n) gaussianSample :: forall m n. (KnownNat m, KnownNat n) => Seed -> R n -> Sym n -> L m n uniformSample :: forall m n. (KnownNat m, KnownNat n) => Seed -> R n -> R n -> L m n mean :: (KnownNat n, 1 <= n) => R n -> โ meanCov :: forall m n. (KnownNat m, KnownNat n, 1 <= m) => L m n -> (R n, Sym n) class Disp t disp :: Disp t => Int -> t -> IO () class Domain field vec mat | mat -> vec field, vec -> mat field, field -> mat vec mul :: forall m k n. (Domain field vec mat, KnownNat m, KnownNat k, KnownNat n) => mat m k -> mat k n -> mat m n app :: forall m n. (Domain field vec mat, KnownNat m, KnownNat n) => mat m n -> vec n -> vec m dot :: forall n. (Domain field vec mat, KnownNat n) => vec n -> vec n -> field cross :: Domain field vec mat => vec 3 -> vec 3 -> vec 3 diagR :: forall m n k. (Domain field vec mat, KnownNat m, KnownNat n, KnownNat k) => field -> vec k -> mat m n dvmap :: forall n. (Domain field vec mat, KnownNat n) => (field -> field) -> vec n -> vec n dmmap :: forall n m. (Domain field vec mat, KnownNat m, KnownNat n) => (field -> field) -> mat n m -> mat n m outer :: forall n m. (Domain field vec mat, KnownNat m, KnownNat n) => vec n -> vec m -> mat n m zipWithVector :: forall n. (Domain field vec mat, KnownNat n) => (field -> field -> field) -> vec n -> vec n -> vec n det :: forall n. (Domain field vec mat, KnownNat n) => mat n n -> field invlndet :: forall n. (Domain field vec mat, KnownNat n) => mat n n -> (mat n n, (field, field)) expm :: forall n. (Domain field vec mat, KnownNat n) => mat n n -> mat n n sqrtm :: forall n. (Domain field vec mat, KnownNat n) => mat n n -> mat n n inv :: forall n. (Domain field vec mat, KnownNat n) => mat n n -> mat n n withVector :: forall z. Vector โ -> (forall n. KnownNat n => R n -> z) -> z withMatrix :: forall z. Matrix โ -> (forall m n. (KnownNat m, KnownNat n) => L m n -> z) -> z -- | Useful for constraining two dependently typed vectors to match each -- other in length when they are unknown at compile-time. exactLength :: forall n m. (KnownNat n, KnownNat m) => R m -> Maybe (R n) -- | Useful for constraining two dependently typed matrices to match each -- other in dimensions when they are unknown at compile-time. exactDims :: forall n m j k. (KnownNat n, KnownNat m, KnownNat j, KnownNat k) => L m n -> Maybe (L j k) toRows :: forall m n. (KnownNat m, KnownNat n) => L m n -> [R n] toColumns :: forall m n. (KnownNat m, KnownNat n) => L m n -> [R m] withRows :: forall n z. KnownNat n => [R n] -> (forall m. KnownNat m => L m n -> z) -> z withColumns :: forall m z. KnownNat m => [R m] -> (forall n. KnownNat n => L m n -> z) -> z class Num t => Sized t s d | s -> t, s -> d konst :: Sized t s d => t -> s unwrap :: Sized t s d => s -> d t fromList :: Sized t s d => [t] -> s extract :: Sized t s d => s -> d t create :: Sized t s d => d t -> Maybe s size :: Sized t s d => s -> IndexOf d class Diag m d | m -> d takeDiag :: Diag m d => m -> d data Sym n sym :: KnownNat n => Sq n -> Sym n mTm :: (KnownNat m, KnownNat n) => L m n -> Sym n unSym :: Sym n -> Sq n (<ยท>) :: KnownNat n => R n -> R n -> โ infixr 8 <ยท> instance GHC.TypeNats.KnownNat n => GHC.Show.Show (Numeric.LinearAlgebra.Static.Sym n) instance Numeric.LinearAlgebra.Static.Domain Internal.Static.โ Internal.Static.R Internal.Static.L instance Numeric.LinearAlgebra.Static.Domain Internal.Static.โ Internal.Static.C Internal.Static.M instance GHC.TypeNats.KnownNat n => Internal.Static.Disp (Numeric.LinearAlgebra.Static.Her n) instance GHC.TypeNats.KnownNat n => Internal.Numeric.Transposable (Numeric.LinearAlgebra.Static.Her n) (Numeric.LinearAlgebra.Static.Her n) instance GHC.TypeNats.KnownNat n => Internal.Static.Disp (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => Numeric.LinearAlgebra.Static.Eigen (Numeric.LinearAlgebra.Static.Sym n) (Internal.Static.R n) (Internal.Static.L n n) instance GHC.TypeNats.KnownNat n => GHC.Num.Num (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => GHC.Real.Fractional (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => GHC.Float.Floating (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => Internal.Numeric.Additive (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => Internal.Numeric.Transposable (Numeric.LinearAlgebra.Static.Sym n) (Numeric.LinearAlgebra.Static.Sym n) instance GHC.TypeNats.KnownNat n => Numeric.LinearAlgebra.Static.Eigen (Numeric.LinearAlgebra.Static.Sq n) (Internal.Static.C n) (Internal.Static.M n n) instance GHC.TypeNats.KnownNat n => Numeric.LinearAlgebra.Static.Diag (Internal.Static.L n n) (Internal.Static.R n) instance GHC.TypeNats.KnownNat n => Numeric.LinearAlgebra.Static.Diag (Internal.Static.M n n) (Internal.Static.C n) instance (GHC.TypeNats.KnownNat n', GHC.TypeNats.KnownNat m') => Internal.Numeric.Testable (Internal.Static.L n' m') instance GHC.TypeNats.KnownNat n => Internal.Util.Normed (Internal.Static.R n) instance (GHC.TypeNats.KnownNat m, GHC.TypeNats.KnownNat n) => Internal.Util.Normed (Internal.Static.L m n)