summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/Vectorized.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Numeric/Vectorized.hs')
-rw-r--r--packages/base/src/Numeric/Vectorized.hs53
1 files changed, 50 insertions, 3 deletions
diff --git a/packages/base/src/Numeric/Vectorized.hs b/packages/base/src/Numeric/Vectorized.hs
index 3814579..a2d7f70 100644
--- a/packages/base/src/Numeric/Vectorized.hs
+++ b/packages/base/src/Numeric/Vectorized.hs
@@ -16,20 +16,29 @@ module Numeric.Vectorized (
16 FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, 16 FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ,
17 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, 17 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ,
18 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, 18 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ,
19 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ 19 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ,
20 vectorScan, saveMatrix
20) where 21) where
21 22
22import Data.Packed.Internal.Common 23import Data.Packed.Internal.Common
23import Data.Packed.Internal.Signatures 24import Data.Packed.Internal.Signatures
24import Data.Packed.Internal.Vector 25import Data.Packed.Internal.Vector
26import Data.Packed.Internal.Matrix
25 27
26import Data.Complex 28import Data.Complex
27import Foreign.Marshal.Alloc(free) 29import Foreign.Marshal.Alloc(free,malloc)
28import Foreign.Marshal.Array(newArray) 30import Foreign.Marshal.Array(newArray,copyArray)
29import Foreign.Ptr(Ptr) 31import Foreign.Ptr(Ptr)
32import Foreign.Storable(peek)
30import Foreign.C.Types 33import Foreign.C.Types
34import Foreign.C.String
31import System.IO.Unsafe(unsafePerformIO) 35import System.IO.Unsafe(unsafePerformIO)
32 36
37import Control.Monad(when)
38import Control.Applicative((<$>))
39
40
41
33fromei x = fromIntegral (fromEnum x) :: CInt 42fromei x = fromIntegral (fromEnum x) :: CInt
34 43
35data FunCodeV = Sin 44data FunCodeV = Sin
@@ -271,3 +280,41 @@ vectorZipQ = vectorZipAux c_vectorZipQ
271 280
272foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV 281foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV
273 282
283--------------------------------------------------------------------------------
284
285foreign import ccall unsafe "vectorScan" c_vectorScan
286 :: CString -> Ptr CInt -> Ptr (Ptr Double) -> IO CInt
287
288vectorScan :: FilePath -> IO (Vector Double)
289vectorScan s = do
290 pp <- malloc
291 pn <- malloc
292 cs <- newCString s
293 ok <- c_vectorScan cs pn pp
294 when (not (ok == 0)) $
295 error ("vectorScan \"" ++ s ++"\"")
296 n <- fromIntegral <$> peek pn
297 p <- peek pp
298 v <- createVector n
299 free pn
300 free cs
301 unsafeWith v $ \pv -> copyArray pv p n
302 free p
303 free pp
304 return v
305
306--------------------------------------------------------------------------------
307
308foreign import ccall unsafe "saveMatrix" c_saveMatrix
309 :: CString -> CString -> TM
310
311saveMatrix :: FilePath -> String -> Matrix Double -> IO ()
312saveMatrix name format m = do
313 cname <- newCString name
314 cformat <- newCString format
315 app1 (c_saveMatrix cname cformat) mat m "saveMatrix"
316 free cname
317 free cformat
318 return ()
319
320