From d0fc6c7192badfa6f03baf0e02e0cf2a73c3906b Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 20 May 2014 17:35:40 +0200 Subject: loadMatrix, saveMatrix --- packages/base/src/Numeric/Vectorized.hs | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'packages/base/src/Numeric/Vectorized.hs') 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 ( FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, - FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ + FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ, + vectorScan, saveMatrix ) where import Data.Packed.Internal.Common import Data.Packed.Internal.Signatures import Data.Packed.Internal.Vector +import Data.Packed.Internal.Matrix import Data.Complex -import Foreign.Marshal.Alloc(free) -import Foreign.Marshal.Array(newArray) +import Foreign.Marshal.Alloc(free,malloc) +import Foreign.Marshal.Array(newArray,copyArray) import Foreign.Ptr(Ptr) +import Foreign.Storable(peek) import Foreign.C.Types +import Foreign.C.String import System.IO.Unsafe(unsafePerformIO) +import Control.Monad(when) +import Control.Applicative((<$>)) + + + fromei x = fromIntegral (fromEnum x) :: CInt data FunCodeV = Sin @@ -271,3 +280,41 @@ vectorZipQ = vectorZipAux c_vectorZipQ foreign import ccall unsafe "zipQ" c_vectorZipQ :: CInt -> TQVQVQV +-------------------------------------------------------------------------------- + +foreign import ccall unsafe "vectorScan" c_vectorScan + :: CString -> Ptr CInt -> Ptr (Ptr Double) -> IO CInt + +vectorScan :: FilePath -> IO (Vector Double) +vectorScan s = do + pp <- malloc + pn <- malloc + cs <- newCString s + ok <- c_vectorScan cs pn pp + when (not (ok == 0)) $ + error ("vectorScan \"" ++ s ++"\"") + n <- fromIntegral <$> peek pn + p <- peek pp + v <- createVector n + free pn + free cs + unsafeWith v $ \pv -> copyArray pv p n + free p + free pp + return v + +-------------------------------------------------------------------------------- + +foreign import ccall unsafe "saveMatrix" c_saveMatrix + :: CString -> CString -> TM + +saveMatrix :: FilePath -> String -> Matrix Double -> IO () +saveMatrix name format m = do + cname <- newCString name + cformat <- newCString format + app1 (c_saveMatrix cname cformat) mat m "saveMatrix" + free cname + free cformat + return () + + -- cgit v1.2.3