{-# LANGUAGE GADTs, RecordWildCards, ScopedTypeVariables #-}
module Data.Eigen.SparseMatrix.Mutable (
IOSparseMatrix(..),
IOSparseMatrixXf,
IOSparseMatrixXd,
IOSparseMatrixXcf,
IOSparseMatrixXcd,
new,
reserve,
rows,
cols,
innerSize,
outerSize,
nonZeros,
compressed,
compress,
uncompress,
read,
write,
setZero,
setIdentity,
resize,
conservativeResize
) where
import Prelude hiding (read)
import Data.Complex
import Foreign.C.String
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import qualified Foreign.Concurrent as FC
import qualified Data.Eigen.Internal as I
data IOSparseMatrix a b where
IOSparseMatrix :: I.Elem a b => !(ForeignPtr (I.CSparseMatrix a b)) -> IOSparseMatrix a b
type IOSparseMatrixXf = IOSparseMatrix Float CFloat
type IOSparseMatrixXd = IOSparseMatrix Double CDouble
type IOSparseMatrixXcf = IOSparseMatrix (Complex Float) (I.CComplex CFloat)
type IOSparseMatrixXcd = IOSparseMatrix (Complex Double) (I.CComplex CDouble)
new :: I.Elem a b => Int -> Int -> IO (IOSparseMatrix a b)
new rows cols = alloca $ \pm -> do
I.call $ I.sparse_new (I.cast rows) (I.cast cols) pm
m <- peek pm
fm <- FC.newForeignPtr m $ I.call $ I.sparse_free m
return $! IOSparseMatrix fm
rows :: I.Elem a b => IOSparseMatrix a b -> IO Int
rows = _prop I.sparse_rows (return . I.cast)
cols :: I.Elem a b => IOSparseMatrix a b -> IO Int
cols = _prop I.sparse_cols (return . I.cast)
innerSize :: I.Elem a b => IOSparseMatrix a b -> IO Int
innerSize = _prop I.sparse_innerSize (return . I.cast)
outerSize :: I.Elem a b => IOSparseMatrix a b -> IO Int
outerSize = _prop I.sparse_outerSize (return . I.cast)
compressed :: I.Elem a b => IOSparseMatrix a b -> IO Bool
compressed = _prop I.sparse_isCompressed (return . (==1))
compress :: I.Elem a b => IOSparseMatrix a b -> IO ()
compress = _inplace I.sparse_compressInplace
uncompress :: I.Elem a b => IOSparseMatrix a b -> IO ()
uncompress = _inplace I.sparse_uncompressInplace
read :: I.Elem a b => IOSparseMatrix a b -> Int -> Int -> IO a
read (IOSparseMatrix fm) row col = withForeignPtr fm $ \m -> alloca $ \px -> do
I.call $ I.sparse_coeff m (I.cast row) (I.cast col) px
I.cast <$> peek px
write :: I.Elem a b => IOSparseMatrix a b -> Int -> Int -> a -> IO ()
write (IOSparseMatrix fm) row col x = withForeignPtr fm $ \m -> alloca $ \px -> do
I.call $ I.sparse_coeffRef m (I.cast row) (I.cast col) px
peek px >>= (`poke` I.cast x)
setIdentity :: I.Elem a b => IOSparseMatrix a b -> IO ()
setIdentity = _inplace I.sparse_setIdentity
setZero :: I.Elem a b => IOSparseMatrix a b -> IO ()
setZero = _inplace I.sparse_setZero
nonZeros :: I.Elem a b => IOSparseMatrix a b -> IO Int
nonZeros = _prop I.sparse_nonZeros (return . I.cast)
reserve :: I.Elem a b => IOSparseMatrix a b -> Int -> IO ()
reserve m s = _inplace (\p -> I.sparse_reserve p (I.cast s)) m
resize :: I.Elem a b => IOSparseMatrix a b -> Int -> Int -> IO ()
resize m rows cols = _inplace (\p -> I.sparse_resize p (I.cast rows) (I.cast cols)) m
conservativeResize :: I.Elem a b => IOSparseMatrix a b -> Int -> Int -> IO ()
conservativeResize m rows cols = _inplace (\p -> I.sparse_conservativeResize p (I.cast rows) (I.cast cols)) m
_inplace :: I.Elem a b => (Ptr (I.CSparseMatrix a b) -> IO CString) -> IOSparseMatrix a b -> IO ()
_inplace f (IOSparseMatrix fm) = withForeignPtr fm $ \m -> I.call $ f m
_prop :: Storable c => (I.CSparseMatrixPtr a b -> Ptr c -> IO CString) -> (c -> IO d) -> IOSparseMatrix a b -> IO d
_prop f g (IOSparseMatrix fp) =
withForeignPtr fp $ \p ->
alloca $ \pq -> do
I.call (f p pq)
peek pq >>= g