module Data.Matrix.Class.IMatrixBase (
IMatrix(..),
row,
col,
rows,
cols,
(<*>),
(<**>),
sapply,
sapplyMat,
unsafeApply,
unsafeApplyMat,
) where
import Data.Elem.BLAS( BLAS3 )
import BLAS.Internal ( checkedRow, checkedCol, checkMatVecMult,
checkMatMatMult )
import Data.Matrix.Class
import Data.Tensor.Class
import Data.Vector.Dense
import Data.Vector.Dense.ST( runSTVector )
import Data.Matrix.Herm
import Data.Matrix.TriBase
import Data.Matrix.Dense.Base
import Data.Matrix.Dense.ST( runSTMatrix )
infixr 7 <*>, <**>
class (MatrixShaped a, BLAS3 e) => IMatrix a e where
unsafeSApply :: e -> a (m,n) e -> Vector n e -> Vector m e
unsafeSApplyMat :: e -> a (m,k) e -> Matrix (k,n) e -> Matrix (m,n) e
unsafeRow :: a (m,n) e -> Int -> Vector n e
unsafeRow a i = let
e = basisVector (numRows a) i
in conj $ unsafeApply (herm a) e
unsafeCol :: a (m,n) e -> Int -> Vector m e
unsafeCol a j = let
e = basisVector (numCols a) j
in unsafeApply a e
row :: (IMatrix a e) => a (m,n) e -> Int -> Vector n e
row a = checkedRow (shape a) (unsafeRow a)
col :: (IMatrix a e) => a (m,n) e -> Int -> Vector m e
col a = checkedCol (shape a) (unsafeCol a)
rows :: (IMatrix a e) => a (m,n) e -> [Vector n e]
rows a = [ unsafeRow a i | i <- [0..numRows a 1] ]
cols :: (IMatrix a e) => a (m,n) e -> [Vector m e]
cols a = [ unsafeCol a j | j <- [0..numCols a 1] ]
(<*>) :: (IMatrix a e) => a (m,n) e -> Vector n e -> Vector m e
(<*>) a x = checkMatVecMult (shape a) (dim x) $ unsafeApply a x
(<**>) :: (IMatrix a e) => a (m,k) e -> Matrix (k,n) e -> Matrix (m,n) e
(<**>) a b = checkMatMatMult (shape a) (shape b) $ unsafeApplyMat a b
sapply :: (IMatrix a e) => e -> a (m,n) e -> Vector n e -> Vector m e
sapply k a x = checkMatVecMult (shape a) (dim x) $ unsafeSApply k a x
sapplyMat :: (IMatrix a e) => e -> a (m,k) e -> Matrix (k,n) e -> Matrix (m,n) e
sapplyMat k a b = checkMatMatMult (shape a) (shape b) $ unsafeSApplyMat k a b
unsafeApply :: (IMatrix a e) => a (m,n) e -> Vector n e -> Vector m e
unsafeApply = unsafeSApply 1
unsafeApplyMat :: (IMatrix a e) => a (m,k) e -> Matrix (k,n) e -> Matrix (m,n) e
unsafeApplyMat = unsafeSApplyMat 1
instance (BLAS3 e) => IMatrix Matrix e where
unsafeSApply alpha a x = runSTVector $ unsafeGetSApply alpha a x
unsafeSApplyMat alpha a b = runSTMatrix $ unsafeGetSApplyMat alpha a b
unsafeRow = unsafeRowView
unsafeCol = unsafeColView
instance (BLAS3 e) => IMatrix (Herm Matrix) e where
unsafeSApply alpha a x = runSTVector $ unsafeGetSApply alpha a x
unsafeSApplyMat alpha a b = runSTMatrix $ unsafeGetSApplyMat alpha a b
unsafeRow a i = runSTVector $ unsafeGetRow a i
unsafeCol a j = runSTVector $ unsafeGetCol a j
instance (BLAS3 e) => IMatrix (Tri Matrix) e where
unsafeSApply alpha a x = runSTVector $ unsafeGetSApply alpha a x
unsafeSApplyMat alpha a b = runSTMatrix $ unsafeGetSApplyMat alpha a b
unsafeRow a i = runSTVector $ unsafeGetRow a i
unsafeCol a j = runSTVector $ unsafeGetCol a j