summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-20 17:35:40 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-20 17:35:40 +0200
commitd0fc6c7192badfa6f03baf0e02e0cf2a73c3906b (patch)
tree0a2ea2c5e39933db2e131d4e58136967a96144e4 /packages/base/src/Numeric
parentc5c6983a1970592c101e76411c3428a301a6a8e3 (diff)
loadMatrix, saveMatrix
Diffstat (limited to 'packages/base/src/Numeric')
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Data.hs7
-rw-r--r--packages/base/src/Numeric/Vectorized.hs53
2 files changed, 54 insertions, 6 deletions
diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs
index 2754576..45fc00c 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Data.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs
@@ -43,9 +43,10 @@ module Numeric.LinearAlgebra.Data(
43 find, maxIndex, minIndex, maxElement, minElement, atIndex, 43 find, maxIndex, minIndex, maxElement, minElement, atIndex,
44 44
45 -- * IO 45 -- * IO
46 disp, dispf, disps, dispcf, latexFormat, format, readMatrix, 46 disp,
47 47 loadMatrix, saveMatrix,
48 -- | loadMatrix, saveMatrix, fromFile, fileDimensions, fscanfVector, fprintfVector, freadVector, fwriteVector 48 latexFormat,
49 dispf, disps, dispcf, format,
49 50
50-- * Conversion 51-- * Conversion
51 Convert(..), 52 Convert(..),
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