From cfd75ac234ef7d769f8a6f86949a18ee5f37fa00 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 5 Jun 2008 12:04:40 +0000 Subject: first version of Data.Packed.ST --- lib/Data/Packed/Convert.hs | 61 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 9 deletions(-) (limited to 'lib/Data/Packed') diff --git a/lib/Data/Packed/Convert.hs b/lib/Data/Packed/Convert.hs index 55fdf8f..206fa06 100644 --- a/lib/Data/Packed/Convert.hs +++ b/lib/Data/Packed/Convert.hs @@ -1,3 +1,5 @@ +{-# OPTIONS -XTypeOperators -XRank2Types -XFlexibleContexts #-} + ----------------------------------------------------------------------------- -- | -- Module : Data.Packed.Convert @@ -9,18 +11,26 @@ -- Portability : portable -- -- Conversion of Vectors and Matrices to and from the standard Haskell arrays. +-- (provisional) -- ----------------------------------------------------------------------------- module Data.Packed.Convert ( - vectorToStorableArray, - storableArrayToVector, --- unsafeUpdateVector, + arrayFromVector, vectorFromArray, + mArrayFromVector, vectorFromMArray, + vectorToStorableArray, storableArrayToVector, + arrayFromMatrix, matrixFromArray, + mArrayFromMatrix, matrixFromMArray, +-- matrixToStorableArray, storableArrayToMatrix ) where import Data.Packed.Internal import Data.Array.Storable import Foreign +import Control.Monad.ST +import Data.Array.ST +import Data.Array.IArray +import Data.Array.Unboxed -- | Creates a StorableArray indexed from 0 to dim -1. -- (Memory is efficiently copied, so you can then freely modify the obtained array) @@ -49,11 +59,44 @@ unsafeVectorToStorableArray v = unsafeForeignPtrToStorableArray (fptr v) (0,dim --unsafeStorableArrayToVector s = undefined -- the foreign ptr of Storable Array is not available? ----------------------------------------------------------------- +-- provisional, we need Unboxed arrays for Complex Double + + +unsafeFreeze' :: (MArray a e m, Ix i) => a i e -> m (Array i e) +unsafeFreeze' = unsafeFreeze + +-- | creates an immutable Array from an hmatrix Vector (to do: unboxed) +arrayFromVector :: (Storable t) => Vector t -> Array Int t +arrayFromVector x = runSTArray (mArrayFromVector x) + +-- | creates a mutable array from an hmatrix Vector (to do: unboxed) +mArrayFromVector :: (MArray b t (ST s), Storable t) => Vector t -> ST s (b Int t) +mArrayFromVector v = unsafeThaw =<< unsafeIOToST ( unsafeFreeze' =<< (vectorToStorableArray $ v)) + + +-- (creates an hmatrix Vector from an immutable array (to do: unboxed)) +vectorFromArray :: (Storable t) => Array Int t -> Vector t +vectorFromArray a = unsafePerformIO $ storableArrayToVector =<< unsafeThaw a + +-- | creates a mutable Array from an hmatrix Vector for manipulation with runSTUArray (to do: unboxed) +vectorFromMArray :: (Storable t, MArray a t (ST s)) => a Int t -> ST s (Vector t) +vectorFromMArray x = fmap vectorFromArray (unsafeFreeze' x) + +-------------------------------------------------------------------- +-- provisional + +matrixFromArray :: UArray (Int, Int) Double -> Matrix Double +matrixFromArray m = reshape c . fromList . elems $ m + where ((r1,c1),(r2,c2)) = bounds m + r = r2-r1+1 + c = c2-c1+1 + +arrayFromMatrix :: Matrix Double -> UArray (Int, Int) Double +arrayFromMatrix m = listArray ((0,0),(rows m -1, cols m -1)) (toList $ flatten m) + +mArrayFromMatrix :: (MArray b Double m) => Matrix Double -> m (b (Int, Int) Double) +mArrayFromMatrix = unsafeThaw . arrayFromMatrix -unsafeUpdateVector :: Storable t => Int -- ^ position - -> (t->t) -- ^ modification function - -> Vector t -- ^ source - -> IO () -- ^ result -unsafeUpdateVector k h v = do - withForeignPtr (fptr v) $ \s -> pokeElemOff s k (h (v`at'`k)) +matrixFromMArray :: (MArray a Double (ST s)) => a (Int,Int) Double -> ST s (Matrix Double) +matrixFromMArray x = fmap matrixFromArray (unsafeFreeze x) -- cgit v1.2.3