-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Eigen C++ library (linear algebra: matrices, vectors, numerical solvers). -- @package eigen @version 2.0.1 module Data.Eigen.Matrix.Mutable -- | Mutable matrix. You can modify elements data MMatrix a b s MMatrix :: Int -> Int -> MVector s b -> MMatrix a b s mm_rows :: MMatrix a b s -> Int mm_cols :: MMatrix a b s -> Int mm_vals :: MMatrix a b s -> MVector s b -- | Alias for single precision mutable matrix type MMatrixXf = MMatrix Float CFloat -- | Alias for double precision mutable matrix type MMatrixXd = MMatrix Double CDouble -- | Alias for single previsiom mutable matrix of complex numbers type MMatrixXcf = MMatrix (Complex Float) (CComplex CFloat) -- | Alias for double prevision mutable matrix of complex numbers type MMatrixXcd = MMatrix (Complex Double) (CComplex CDouble) type IOMatrix a b = MMatrix a b RealWorld type STMatrix a b s = MMatrix a b s -- | Create a mutable matrix of the given size and fill it with 0 as an -- initial value. new :: (PrimMonad m, Elem a b) => Int -> Int -> m (MMatrix a b (PrimState m)) -- | Create a mutable matrix of the given size and fill it with as an -- initial value. replicate :: (PrimMonad m, Elem a b) => Int -> Int -> m (MMatrix a b (PrimState m)) -- | Verify matrix dimensions and memory layout valid :: Elem a b => MMatrix a b s -> Bool -- | Yield the element at the given position. read :: (PrimMonad m, Elem a b) => MMatrix a b (PrimState m) -> Int -> Int -> m a -- | Replace the element at the given position. write :: (PrimMonad m, Elem a b) => MMatrix a b (PrimState m) -> Int -> Int -> a -> m () -- | Yield the element at the given position. No bounds checks are -- performed. unsafeRead :: (PrimMonad m, Elem a b) => MMatrix a b (PrimState m) -> Int -> Int -> m a -- | Replace the element at the given position. No bounds checks are -- performed. unsafeWrite :: (PrimMonad m, Elem a b) => MMatrix a b (PrimState m) -> Int -> Int -> a -> m () -- | Set all elements of the matrix to the given value set :: (PrimMonad m, Elem a b) => (MMatrix a b (PrimState m)) -> a -> m () -- | Copy a matrix. The two matrices must have the same length and may not -- overlap. copy :: (PrimMonad m, Elem a b) => (MMatrix a b (PrimState m)) -> (MMatrix a b (PrimState m)) -> m () -- | Pass a pointer to the matrix's data to the IO action. Modifying data -- through the pointer is unsafe if the matrix could have been frozen -- before the modification. unsafeWith :: Elem a b => IOMatrix a b -> (Ptr b -> CInt -> CInt -> IO c) -> IO c -- | Some Eigen's algorithms can exploit the multiple cores present in your -- hardware. To this end, it is enough to enable OpenMP on your compiler, -- for instance: GCC: -fopenmp. You can control the number of thread that -- will be used using either the OpenMP API or Eiegn's API using the -- following priority: -- --
-- >>> 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 --map :: Elem a b => (a -> a) -> Matrix a b -> Matrix a b -- | 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 --imap :: Elem a b => (Int -> Int -> a -> a) -> Matrix a b -> Matrix a b -- | Filter elements in the matrix. Filtered elements will be replaced by 0 filter :: Elem a b => (a -> Bool) -> Matrix a b -> Matrix a b -- | Filter elements in the matrix. Filtered elements will be replaced by 0 ifilter :: Elem a b => (Int -> Int -> a -> Bool) -> Matrix a b -> Matrix a b -- | Diagonal of the matrix diagonal :: Elem a b => Matrix a b -> Matrix a b -- | Transpose of the matrix transpose :: Elem a b => Matrix a b -> Matrix a b -- | Inverse of the matrix -- -- For small fixed sizes up to 4x4, this method uses cofactors. In the -- general case, this method uses PartialPivLU decomposition inverse :: Elem a b => Matrix a b -> Matrix a b -- | Adjoint of the matrix adjoint :: Elem a b => Matrix a b -> Matrix a b -- | Conjugate of the matrix conjugate :: Elem a b => Matrix a b -> Matrix a b -- | Nomalize the matrix by deviding it on its norm normalize :: Elem a b => Matrix a b -> Matrix a b -- | 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. modify :: Elem a b => (forall s. MMatrix a b s -> ST s ()) -> Matrix a b -> Matrix a b -- | Upper trinagle of the matrix upperTriangle :: Elem a b => Matrix a b -> Matrix a b -- | Lower trinagle of the matrix lowerTriangle :: Elem a b => Matrix a b -> Matrix a b -- | Yield a mutable copy of the immutable matrix thaw :: Elem a b => PrimMonad m => Matrix a b -> m (MMatrix a b (PrimState m)) -- | Yield an immutable copy of the mutable matrix freeze :: Elem a b => PrimMonad m => MMatrix a b (PrimState m) -> m (Matrix a b) -- | Unsafely convert an immutable matrix to a mutable one without copying. -- The immutable matrix may not be used after this operation. unsafeThaw :: Elem a b => PrimMonad m => Matrix a b -> m (MMatrix a b (PrimState m)) -- | Unsafe convert a mutable matrix to an immutable one without copying. -- The mutable matrix may not be used after this operation. unsafeFreeze :: Elem a b => PrimMonad m => MMatrix a b (PrimState m) -> m (Matrix a b) -- | Pass a pointer to the matrix's data to the IO action. The data may not -- be modified through the pointer. unsafeWith :: Elem a b => Matrix a b -> (Ptr b -> CInt -> CInt -> IO c) -> IO c instance Elem a b => Num (Matrix a b) instance (Elem a b, Show a) => Show (Matrix a b) -- | The problem: You have a system of equations, that you have written as -- a single matrix equation -- --
-- Ax = b ---- -- Where A and b are matrices (b could be a vector, as a special case). -- You want to find a solution x. -- -- The solution: You can choose between various decompositions, depending -- on what your matrix A looks like, and depending on whether you favor -- speed or accuracy. However, let's start with an example that works in -- all cases, and is a good compromise: -- --
-- import Data.Eigen.Matrix -- import Data.Eigen.LA -- -- main = do -- let -- a = fromList [[1,2,3], [4,5,6], [7,8,10]] -- b = fromList [[3],[3],[4]] -- x = solve ColPivHouseholderQR a b -- putStrLn "Here is the matrix A:" >> print a -- putStrLn "Here is the vector b:" >> print b -- putStrLn "The solution is:" >> print x ---- -- produces the following output -- --
-- Here is the matrix A: -- Matrix 3x3 -- 1.0 2.0 3.0 -- 4.0 5.0 6.0 -- 7.0 8.0 10.0 -- -- Here is the vector b: -- Matrix 3x1 -- 3.0 -- 3.0 -- 4.0 -- -- The solution is: -- Matrix 3x1 -- -2.0000000000000004 -- 1.0000000000000018 -- 0.9999999999999989 ---- -- Checking if a solution really exists: Only you know what error margin -- you want to allow for a solution to be considered valid. -- -- You can compute relative error using norm (ax - b) / -- norm b formula or use relativeError function which -- provides the same calculation implemented slightly more efficient. module Data.Eigen.LA -- |
-- Decomposition Requirements on the matrix Speed Accuracy Rank Kernel Image -- -- PartialPivLU Invertible ++ + - - - -- FullPivLU None - +++ + + + -- HouseholderQR None ++ + - - - -- ColPivHouseholderQR None + ++ + - - -- FullPivHouseholderQR None - +++ + - - -- LLT Positive definite +++ + - - - -- LDLT Positive or negative semidefinite +++ ++ - - - -- JacobiSVD None - +++ + - - ---- -- The best way to do least squares solving for square matrices is with a -- SVD decomposition (JacobiSVD) data Decomposition -- | LU decomposition of a matrix with partial pivoting. PartialPivLU :: Decomposition -- | LU decomposition of a matrix with complete pivoting. FullPivLU :: Decomposition -- | Householder QR decomposition of a matrix. HouseholderQR :: Decomposition -- | Householder rank-revealing QR decomposition of a matrix with -- column-pivoting. ColPivHouseholderQR :: Decomposition -- | Householder rank-revealing QR decomposition of a matrix with full -- pivoting. FullPivHouseholderQR :: Decomposition -- | Standard Cholesky decomposition (LL^T) of a matrix. LLT :: Decomposition -- | Robust Cholesky decomposition of a matrix with pivoting. LDLT :: Decomposition -- | Two-sided Jacobi SVD decomposition of a rectangular matrix. JacobiSVD :: Decomposition -- |
-- import Data.Eigen.LA -- main = print $ linearRegression [ -- [-4.32, 3.02, 6.89], -- [-3.79, 2.01, 5.39], -- [-4.01, 2.41, 6.01], -- [-3.86, 2.09, 5.55], -- [-4.10, 2.58, 6.32]] ---- -- produces the following output -- --
-- ([-2.3466569233817127,-0.2534897541434826,-0.1749653335680988],1.8905965120153139e-3) --linearRegression :: [[Double]] -> ([Double], Double) instance Show Decomposition instance Enum Decomposition