From 6809103cf34a9345f8cb60a0ec3a8f55dd18d5ef Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 13 May 2009 09:39:25 +0000 Subject: added Development module --- lib/Data/Packed/Internal/Common.hs | 38 ++++++++++++++++++++++++++++++++++++++ lib/Data/Packed/Internal/Matrix.hs | 9 ++++++++- lib/Data/Packed/Internal/Vector.hs | 5 ++++- 3 files changed, 50 insertions(+), 2 deletions(-) (limited to 'lib/Data/Packed/Internal') diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs index 2310f5f..879dade 100644 --- a/lib/Data/Packed/Internal/Common.hs +++ b/lib/Data/Packed/Internal/Common.hs @@ -60,6 +60,44 @@ ww2 w1 o1 w2 o2 f = w1 o1 $ \a1 -> w2 o2 $ \a2 -> f a1 a2 ww3 w1 o1 w2 o2 w3 o3 f = w1 o1 $ \a1 -> ww2 w2 o2 w3 o3 (f a1) ww4 w1 o1 w2 o2 w3 o3 w4 o4 f = w1 o1 $ \a1 -> ww3 w2 o2 w3 o3 w4 o4 (f a1) +type Adapt f t r = t -> ((f -> r) -> IO()) -> IO() + +app1 :: f + -> Adapt f t (IO CInt) + -> t + -> String + -> IO() + +app2 :: f + -> Adapt f t1 r + -> t1 + -> Adapt r t2 (IO CInt) + -> t2 + -> String + -> IO() + +app3 :: f + -> Adapt f t1 r1 + -> t1 + -> Adapt r1 t2 r2 + -> t2 + -> Adapt r2 t3 (IO CInt) + -> t3 + -> String + -> IO() + +app4 :: f + -> Adapt f t1 r1 + -> t1 + -> Adapt r1 t2 r2 + -> t2 + -> Adapt r2 t3 r3 + -> t3 + -> Adapt r3 t4 (IO CInt) + -> t4 + -> String + -> IO() + app1 f w1 o1 s = w1 o1 $ \a1 -> f // a1 // check s app2 f w1 o1 w2 o2 s = ww2 w1 o1 w2 o2 $ \a1 a2 -> f // a1 // a2 // check s app3 f w1 o1 w2 o2 w3 o3 s = ww3 w1 o1 w2 o2 w3 o3 $ diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 13ffc34..8a074a6 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -74,6 +74,7 @@ data Matrix t = MC { rows :: {-# UNPACK #-} !Int xdat MC {cdat = d } = d xdat MF {fdat = d } = d +orderOf :: Matrix t -> MatrixOrder orderOf MF{} = ColumnMajor orderOf MC{} = RowMajor @@ -82,12 +83,16 @@ trans :: Matrix t -> Matrix t trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } +cmat :: (Element t) => Matrix t -> Matrix t cmat m@MC{} = m cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} +fmat :: (Element t) => Matrix t -> Matrix t fmat m@MF{} = m fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} +-- C-Haskell matrix adapter +mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r mat = withMatrix withMatrix a f = @@ -156,7 +161,7 @@ MF {rows = r, cols = c, fdat = v} @@> (i,j) | otherwise = v `at` (j*r+i) {-# INLINE (@@>) #-} --- | Unsafe matrix access without range checking +-- Unsafe matrix access without range checking atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) {-# INLINE atM' #-} @@ -173,6 +178,8 @@ matrixFromVector ColumnMajor c v = MF { rows = r, cols = c, fdat = v } r | m==0 = d | otherwise = error "matrixFromVector" +-- allocates memory for a new matrix +createMatrix :: (Storable a) => MatrixOrder -> Int -> Int -> IO (Matrix a) createMatrix order r c = do p <- createVector (r*c) return (matrixFromVector order c p) diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index fc8a6be..1b572a5 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -18,6 +18,7 @@ module Data.Packed.Internal.Vector where import Data.Packed.Internal.Common import Foreign +import Foreign.C.Types(CInt) import Complex import Control.Monad(when) @@ -36,6 +37,8 @@ data Vector t = , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block } +-- C-Haskell vector adapter +vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r vec = withVector withVector (V n fp) f = withForeignPtr fp $ \p -> do @@ -43,7 +46,7 @@ withVector (V n fp) f = withForeignPtr fp $ \p -> do g (fi n) p f v --- | allocates memory for a new vector +-- allocates memory for a new vector createVector :: Storable a => Int -> IO (Vector a) createVector n = do when (n <= 0) $ error ("trying to createVector of dim "++show n) -- cgit v1.2.3