1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
{-# LANGUAGE MagicHash, CPP, UnboxedTuples, BangPatterns, FlexibleContexts #-}
-----------------------------------------------------------------------------
-- |
-- Module : Data.Packed.Internal.Vector
-- Copyright : (c) Alberto Ruiz 2007
-- License : GPL-style
--
-- Maintainer : Alberto Ruiz <aruiz@um.es>
-- Stability : provisional
-- Portability : portable (uses FFI)
--
-- Vector implementation
--
-----------------------------------------------------------------------------
module Data.Packed.Internal.Vector (
Vector, dim,
fromList, toList, (|>),
join, (@>), safe, at, at', subVector, takesV,
mapVector, zipVectorWith, unzipVectorWith,
mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_,
foldVector, foldVectorG, foldLoop, foldVectorWithIndex,
createVector, vec,
asComplex, asReal, float2DoubleV, double2FloatV,
stepF, stepD, condF, condD,
fwriteVector, freadVector, fprintfVector, fscanfVector,
cloneVector,
unsafeToForeignPtr,
unsafeFromForeignPtr,
unsafeWith
) where
import Data.Packed.Internal.Common
import Data.Packed.Internal.Signatures
import Foreign
import Foreign.C.String
import Foreign.C.Types(CInt,CChar)
import Data.Complex
import Control.Monad(when)
#if __GLASGOW_HASKELL__ >= 605
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
#else
import Foreign.ForeignPtr (mallocForeignPtrBytes)
#endif
import GHC.Base
#if __GLASGOW_HASKELL__ < 612
import GHC.IOBase hiding (liftIO)
#endif
#ifdef VECTOR
import qualified Data.Vector.Storable as Vector
import Data.Vector.Storable(Vector,
unsafeToForeignPtr,
unsafeFromForeignPtr,
unsafeWith)
#endif
#ifdef VECTOR
-- | Number of elements
dim :: (Storable t) => Vector t -> Int
dim = Vector.length
#else
-- | One-dimensional array of objects stored in a contiguous memory block.
data Vector t =
V { ioff :: {-# UNPACK #-} !Int -- ^ offset of first element
, idim :: {-# UNPACK #-} !Int -- ^ number of elements
, fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block
}
unsafeToForeignPtr :: Storable a => Vector a -> (ForeignPtr a, Int, Int)
unsafeToForeignPtr v = (fptr v, ioff v, idim v)
-- | Same convention as in Roman Leshchinskiy's vector package.
unsafeFromForeignPtr :: Storable a => ForeignPtr a -> Int -> Int -> Vector a
unsafeFromForeignPtr fp i n | n > 0 = V {ioff = i, idim = n, fptr = fp}
| otherwise = error "unsafeFromForeignPtr with dim < 1"
unsafeWith (V i _ fp) m = withForeignPtr fp $ \p -> m (p `advancePtr` i)
{-# INLINE unsafeWith #-}
-- | Number of elements
dim :: (Storable t) => Vector t -> Int
dim = idim
#endif
-- C-Haskell vector adapter
-- vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r
vec :: (Storable t) => Vector t -> (((CInt -> Ptr t -> t1) -> t1) -> IO b) -> IO b
vec x f = unsafeWith x $ \p -> do
let v g = do
g (fi $ dim x) p
f v
{-# INLINE vec #-}
-- 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)
fp <- doMalloc undefined
return $ unsafeFromForeignPtr fp 0 n
where
--
-- Use the much cheaper Haskell heap allocated storage
-- for foreign pointer space we control
--
doMalloc :: Storable b => b -> IO (ForeignPtr b)
doMalloc dummy = do
#if __GLASGOW_HASKELL__ >= 605
mallocPlainForeignPtrBytes (n * sizeOf dummy)
#else
mallocForeignPtrBytes (n * sizeOf dummy)
#endif
{- | creates a Vector from a list:
@> fromList [2,3,5,7]
4 |> [2.0,3.0,5.0,7.0]@
-}
fromList :: Storable a => [a] -> Vector a
fromList l = unsafePerformIO $ do
v <- createVector (length l)
unsafeWith v $ \ p -> pokeArray p l
return v
safeRead v = inlinePerformIO . unsafeWith v
{-# INLINE safeRead #-}
inlinePerformIO :: IO a -> a
inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
{-# INLINE inlinePerformIO #-}
{- | extracts the Vector elements to a list
@> toList (linspace 5 (1,10))
[1.0,3.25,5.5,7.75,10.0]@
-}
toList :: Storable a => Vector a -> [a]
toList v = safeRead v $ peekArray (dim v)
{- | An alternative to 'fromList' with explicit dimension. The input
list is explicitly truncated if it is too long, so it may safely
be used, for instance, with infinite lists.
This is the format used in the instances for Show (Vector a).
-}
(|>) :: (Storable a) => Int -> [a] -> Vector a
infixl 9 |>
n |> l = if length l' == n
then fromList l'
else error "list too short for |>"
where l' = take n l
-- | access to Vector elements without range checking
at' :: Storable a => Vector a -> Int -> a
at' v n = safeRead v $ flip peekElemOff n
{-# INLINE at' #-}
--
-- turn off bounds checking with -funsafe at configure time.
-- ghc will optimise away the salways true case at compile time.
--
#if defined(UNSAFE)
safe :: Bool
safe = False
#else
safe = True
#endif
-- | access to Vector elements with range checking.
at :: Storable a => Vector a -> Int -> a
at v n
| safe = if n >= 0 && n < dim v
then at' v n
else error "vector index out of range"
| otherwise = at' v n
{-# INLINE at #-}
{- | takes a number of consecutive elements from a Vector
@> subVector 2 3 (fromList [1..10])
3 |> [3.0,4.0,5.0]@
-}
subVector :: Storable t => Int -- ^ index of the starting element
-> Int -- ^ number of elements to extract
-> Vector t -- ^ source
-> Vector t -- ^ result
#ifdef VECTOR
subVector = Vector.slice
#else
subVector k l v@V{idim = n, ioff = i}
| k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range"
| otherwise = v {idim = l, ioff = i+k}
subVectorCopy k l (v@V {idim=n})
| k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range"
| otherwise = unsafePerformIO $ do
r <- createVector l
let f _ s _ d = copyArray d (advancePtr s k) l >> return 0
app2 f vec v vec r "subVector"
return r
#endif
{- | Reads a vector position:
@> fromList [0..9] \@\> 7
7.0@
-}
(@>) :: Storable t => Vector t -> Int -> t
infixl 9 @>
(@>) = at
{- | creates a new Vector by joining a list of Vectors
@> join [fromList [1..5], constant 1 3]
8 |> [1.0,2.0,3.0,4.0,5.0,1.0,1.0,1.0]@
-}
join :: Storable t => [Vector t] -> Vector t
join [] = error "joining zero vectors"
join [v] = v
join as = unsafePerformIO $ do
let tot = sum (map dim as)
r <- createVector tot
unsafeWith r $ \ptr ->
joiner as tot ptr
return r
where joiner [] _ _ = return ()
joiner (v:cs) _ p = do
let n = dim v
unsafeWith v $ \pb -> copyArray p pb n
joiner cs 0 (advancePtr p n)
{- | Extract consecutive subvectors of the given sizes.
@> takesV [3,4] (linspace 10 (1,10))
[3 |> [1.0,2.0,3.0],4 |> [4.0,5.0,6.0,7.0]]@
-}
takesV :: Storable t => [Int] -> Vector t -> [Vector t]
takesV ms w | sum ms > dim w = error $ "takesV " ++ show ms ++ " on dim = " ++ (show $ dim w)
| otherwise = go ms w
where go [] _ = []
go (n:ns) v = subVector 0 n v
: go ns (subVector n (dim v - n) v)
---------------------------------------------------------------
-- | transforms a complex vector into a real vector with alternating real and imaginary parts
asReal :: (RealFloat a, Storable a) => Vector (Complex a) -> Vector a
asReal v = unsafeFromForeignPtr (castForeignPtr fp) (2*i) (2*n)
where (fp,i,n) = unsafeToForeignPtr v
-- | transforms a real vector into a complex vector with alternating real and imaginary parts
asComplex :: (RealFloat a, Storable a) => Vector a -> Vector (Complex a)
asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2)
where (fp,i,n) = unsafeToForeignPtr v
---------------------------------------------------------------
float2DoubleV :: Vector Float -> Vector Double
float2DoubleV v = unsafePerformIO $ do
r <- createVector (dim v)
app2 c_float2double vec v vec r "float2double"
return r
double2FloatV :: Vector Double -> Vector Float
double2FloatV v = unsafePerformIO $ do
r <- createVector (dim v)
app2 c_double2float vec v vec r "double2float2"
return r
foreign import ccall "float2double" c_float2double:: TFV
foreign import ccall "double2float" c_double2float:: TVF
---------------------------------------------------------------
stepF :: Vector Float -> Vector Float
stepF v = unsafePerformIO $ do
r <- createVector (dim v)
app2 c_stepF vec v vec r "stepF"
return r
stepD :: Vector Double -> Vector Double
stepD v = unsafePerformIO $ do
r <- createVector (dim v)
app2 c_stepD vec v vec r "stepD"
return r
foreign import ccall "stepF" c_stepF :: TFF
foreign import ccall "stepD" c_stepD :: TVV
---------------------------------------------------------------
condF :: Vector Float -> Vector Float -> Vector Float -> Vector Float -> Vector Float -> Vector Float
condF x y l e g = unsafePerformIO $ do
r <- createVector (dim x)
app6 c_condF vec x vec y vec l vec e vec g vec r "condF"
return r
condD :: Vector Double -> Vector Double -> Vector Double -> Vector Double -> Vector Double -> Vector Double
condD x y l e g = unsafePerformIO $ do
r <- createVector (dim x)
app6 c_condD vec x vec y vec l vec e vec g vec r "condD"
return r
foreign import ccall "condF" c_condF :: CInt -> PF -> CInt -> PF -> CInt -> PF -> TFFF
foreign import ccall "condD" c_condD :: CInt -> PD -> CInt -> PD -> CInt -> PD -> TVVV
----------------------------------------------------------------
cloneVector :: Storable t => Vector t -> IO (Vector t)
cloneVector v = do
let n = dim v
r <- createVector n
let f _ s _ d = copyArray d s n >> return 0
app2 f vec v vec r "cloneVector"
return r
------------------------------------------------------------------
-- | map on Vectors
mapVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
mapVector f v = unsafePerformIO $ do
w <- createVector (dim v)
unsafeWith v $ \p ->
unsafeWith w $ \q -> do
let go (-1) = return ()
go !k = do x <- peekElemOff p k
pokeElemOff q k (f x)
go (k-1)
go (dim v -1)
return w
{-# INLINE mapVector #-}
-- | zipWith for Vectors
zipVectorWith :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
zipVectorWith f u v = unsafePerformIO $ do
let n = min (dim u) (dim v)
w <- createVector n
unsafeWith u $ \pu ->
unsafeWith v $ \pv ->
unsafeWith w $ \pw -> do
let go (-1) = return ()
go !k = do x <- peekElemOff pu k
y <- peekElemOff pv k
pokeElemOff pw k (f x y)
go (k-1)
go (n -1)
return w
{-# INLINE zipVectorWith #-}
-- | unzipWith for Vectors
unzipVectorWith :: (Storable (a,b), Storable c, Storable d)
=> ((a,b) -> (c,d)) -> Vector (a,b) -> (Vector c,Vector d)
unzipVectorWith f u = unsafePerformIO $ do
let n = dim u
v <- createVector n
w <- createVector n
unsafeWith u $ \pu ->
unsafeWith v $ \pv ->
unsafeWith w $ \pw -> do
let go (-1) = return ()
go !k = do z <- peekElemOff pu k
let (x,y) = f z
pokeElemOff pv k x
pokeElemOff pw k y
go (k-1)
go (n-1)
return (v,w)
{-# INLINE unzipVectorWith #-}
foldVector :: Storable a => (a -> b -> b) -> b -> Vector a -> b
foldVector f x v = unsafePerformIO $
unsafeWith v $ \p -> do
let go (-1) s = return s
go !k !s = do y <- peekElemOff p k
go (k-1::Int) (f y s)
go (dim v -1) x
{-# INLINE foldVector #-}
-- the zero-indexed index is passed to the folding function
foldVectorWithIndex :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
foldVectorWithIndex f x v = unsafePerformIO $
unsafeWith v $ \p -> do
let go (-1) s = return s
go !k !s = do y <- peekElemOff p k
go (k-1::Int) (f k y s)
go (dim v -1) x
{-# INLINE foldVectorWithIndex #-}
foldLoop f s0 d = go (d - 1) s0
where
go 0 s = f (0::Int) s
go !j !s = go (j - 1) (f j s)
foldVectorG f s0 v = foldLoop g s0 (dim v)
where g !k !s = f k (at' v) s
{-# INLINE g #-} -- Thanks to Ryan Ingram (http://permalink.gmane.org/gmane.comp.lang.haskell.cafe/46479)
{-# INLINE foldVectorG #-}
-------------------------------------------------------------------
-- | monadic map over Vectors
-- the monad @m@ must be strict
mapVectorM :: (Storable a, Storable b, Monad m) => (a -> m b) -> Vector a -> m (Vector b)
mapVectorM f v = do
w <- return $! unsafePerformIO $! createVector (dim v)
mapVectorM' w 0 (dim v -1)
return w
where mapVectorM' w' !k !t
| k == t = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
y <- f x
return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
| otherwise = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
y <- f x
_ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
mapVectorM' w' (k+1) t
{-# INLINE mapVectorM #-}
-- | monadic map over Vectors
mapVectorM_ :: (Storable a, Monad m) => (a -> m ()) -> Vector a -> m ()
mapVectorM_ f v = do
mapVectorM' 0 (dim v -1)
where mapVectorM' !k !t
| k == t = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
f x
| otherwise = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
_ <- f x
mapVectorM' (k+1) t
{-# INLINE mapVectorM_ #-}
-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
-- the monad @m@ must be strict
mapVectorWithIndexM :: (Storable a, Storable b, Monad m) => (Int -> a -> m b) -> Vector a -> m (Vector b)
mapVectorWithIndexM f v = do
w <- return $! unsafePerformIO $! createVector (dim v)
mapVectorM' w 0 (dim v -1)
return w
where mapVectorM' w' !k !t
| k == t = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
y <- f k x
return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
| otherwise = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
y <- f k x
_ <- return $! inlinePerformIO $! unsafeWith w' $! \q -> pokeElemOff q k y
mapVectorM' w' (k+1) t
{-# INLINE mapVectorWithIndexM #-}
-- | monadic map over Vectors with the zero-indexed index passed to the mapping function
mapVectorWithIndexM_ :: (Storable a, Monad m) => (Int -> a -> m ()) -> Vector a -> m ()
mapVectorWithIndexM_ f v = do
mapVectorM' 0 (dim v -1)
where mapVectorM' !k !t
| k == t = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
f k x
| otherwise = do
x <- return $! inlinePerformIO $! unsafeWith v $! \p -> peekElemOff p k
_ <- f k x
mapVectorM' (k+1) t
{-# INLINE mapVectorWithIndexM_ #-}
-------------------------------------------------------------------
-- | Loads a vector from an ASCII file (the number of elements must be known in advance).
fscanfVector :: FilePath -> Int -> IO (Vector Double)
fscanfVector filename n = do
charname <- newCString filename
res <- createVector n
app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf"
free charname
return res
foreign import ccall "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV
-- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file.
fprintfVector :: FilePath -> String -> Vector Double -> IO ()
fprintfVector filename fmt v = do
charname <- newCString filename
charfmt <- newCString fmt
app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf"
free charname
free charfmt
foreign import ccall "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV
-- | Loads a vector from a binary file (the number of elements must be known in advance).
freadVector :: FilePath -> Int -> IO (Vector Double)
freadVector filename n = do
charname <- newCString filename
res <- createVector n
app1 (gsl_vector_fread charname) vec res "gsl_vector_fread"
free charname
return res
foreign import ccall "vector_fread" gsl_vector_fread:: Ptr CChar -> TV
-- | Saves the elements of a vector to a binary file.
fwriteVector :: FilePath -> Vector Double -> IO ()
fwriteVector filename v = do
charname <- newCString filename
app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite"
free charname
foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV
|