diff options
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 21 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 59 |
2 files changed, 15 insertions, 65 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index b8ed18d..255009c 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -47,6 +47,7 @@ import Data.Complex(Complex) | |||
47 | import Foreign.C.Types | 47 | import Foreign.C.Types |
48 | import Foreign.C.String(newCString) | 48 | import Foreign.C.String(newCString) |
49 | import System.IO.Unsafe(unsafePerformIO) | 49 | import System.IO.Unsafe(unsafePerformIO) |
50 | import Control.DeepSeq | ||
50 | 51 | ||
51 | ----------------------------------------------------------------- | 52 | ----------------------------------------------------------------- |
52 | 53 | ||
@@ -131,12 +132,10 @@ mat a f = | |||
131 | let m g = do | 132 | let m g = do |
132 | g (fi (rows a)) (fi (cols a)) p | 133 | g (fi (rows a)) (fi (cols a)) p |
133 | f m | 134 | f m |
134 | 135 | -- | Creates a vector by concatenation of rows. If the matrix is ColumnMajor, this operation requires a transpose. | |
135 | {- | Creates a vector by concatenation of rows | 136 | -- |
136 | 137 | -- @\> flatten ('ident' 3) | |
137 | @\> flatten ('ident' 3) | 138 | -- 9 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@ |
138 | 9 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@ | ||
139 | -} | ||
140 | flatten :: Element t => Matrix t -> Vector t | 139 | flatten :: Element t => Matrix t -> Vector t |
141 | flatten = xdat . cmat | 140 | flatten = xdat . cmat |
142 | 141 | ||
@@ -459,3 +458,13 @@ size m = (rows m, cols m) | |||
459 | 458 | ||
460 | shSize m = "(" ++ show (rows m) ++"><"++ show (cols m)++")" | 459 | shSize m = "(" ++ show (rows m) ++"><"++ show (cols m)++")" |
461 | 460 | ||
461 | ---------------------------------------------------------------------- | ||
462 | |||
463 | instance (Storable t, NFData t) => NFData (Matrix t) | ||
464 | where | ||
465 | rnf m | d > 0 = rnf (v @> 0) | ||
466 | | otherwise = () | ||
467 | where | ||
468 | d = dim v | ||
469 | v = xdat m | ||
470 | |||
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index d3b80ff..5892e67 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -55,48 +55,17 @@ import GHC.Base | |||
55 | import GHC.IOBase hiding (liftIO) | 55 | import GHC.IOBase hiding (liftIO) |
56 | #endif | 56 | #endif |
57 | 57 | ||
58 | #ifdef VECTOR | ||
59 | import qualified Data.Vector.Storable as Vector | 58 | import qualified Data.Vector.Storable as Vector |
60 | import Data.Vector.Storable(Vector, | 59 | import Data.Vector.Storable(Vector, |
61 | unsafeToForeignPtr, | 60 | unsafeToForeignPtr, |
62 | unsafeFromForeignPtr, | 61 | unsafeFromForeignPtr, |
63 | unsafeWith) | 62 | unsafeWith) |
64 | #else | ||
65 | import Foreign.ForeignPtr(withForeignPtr) | ||
66 | #endif | ||
67 | 63 | ||
68 | #ifdef VECTOR | ||
69 | 64 | ||
70 | -- | Number of elements | 65 | -- | Number of elements |
71 | dim :: (Storable t) => Vector t -> Int | 66 | dim :: (Storable t) => Vector t -> Int |
72 | dim = Vector.length | 67 | dim = Vector.length |
73 | 68 | ||
74 | #else | ||
75 | |||
76 | -- | One-dimensional array of objects stored in a contiguous memory block. | ||
77 | data Vector t = | ||
78 | V { ioff :: {-# UNPACK #-} !Int -- ^ offset of first element | ||
79 | , idim :: {-# UNPACK #-} !Int -- ^ number of elements | ||
80 | , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block | ||
81 | } | ||
82 | |||
83 | unsafeToForeignPtr :: Storable a => Vector a -> (ForeignPtr a, Int, Int) | ||
84 | unsafeToForeignPtr v = (fptr v, ioff v, idim v) | ||
85 | |||
86 | -- | Same convention as in Roman Leshchinskiy's vector package. | ||
87 | unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a | ||
88 | unsafeFromForeignPtr fp i n | n > 0 = V {ioff = i, idim = n, fptr = fp} | ||
89 | | otherwise = error "unsafeFromForeignPtr with dim < 1" | ||
90 | |||
91 | unsafeWith (V i _ fp) m = withForeignPtr fp $ \p -> m (p `advancePtr` i) | ||
92 | {-# INLINE unsafeWith #-} | ||
93 | |||
94 | -- | Number of elements | ||
95 | dim :: (Storable t) => Vector t -> Int | ||
96 | dim = idim | ||
97 | |||
98 | #endif | ||
99 | |||
100 | 69 | ||
101 | -- C-Haskell vector adapter | 70 | -- C-Haskell vector adapter |
102 | -- vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r | 71 | -- vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r |
@@ -204,36 +173,8 @@ subVector :: Storable t => Int -- ^ index of the starting element | |||
204 | -> Int -- ^ number of elements to extract | 173 | -> Int -- ^ number of elements to extract |
205 | -> Vector t -- ^ source | 174 | -> Vector t -- ^ source |
206 | -> Vector t -- ^ result | 175 | -> Vector t -- ^ result |
207 | |||
208 | #ifdef VECTOR | ||
209 | |||
210 | subVector = Vector.slice | 176 | subVector = Vector.slice |
211 | 177 | ||
212 | {- | ||
213 | subVector k l v | ||
214 | | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" | ||
215 | | otherwise = unsafeFromForeignPtr fp (i+k) l | ||
216 | where | ||
217 | (fp, i, n) = unsafeToForeignPtr v | ||
218 | -} | ||
219 | |||
220 | #else | ||
221 | |||
222 | subVector k l v@V{idim = n, ioff = i} | ||
223 | | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" | ||
224 | | otherwise = v {idim = l, ioff = i+k} | ||
225 | |||
226 | {- | ||
227 | subVectorCopy k l (v@V {idim=n}) | ||
228 | | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" | ||
229 | | otherwise = unsafePerformIO $ do | ||
230 | r <- createVector l | ||
231 | let f _ s _ d = copyArray d (advancePtr s k) l >> return 0 | ||
232 | app2 f vec v vec r "subVector" | ||
233 | return r | ||
234 | -} | ||
235 | |||
236 | #endif | ||
237 | 178 | ||
238 | {- | Reads a vector position: | 179 | {- | Reads a vector position: |
239 | 180 | ||