summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/Vector.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Vector.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Vector.hs133
1 files changed, 124 insertions, 9 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/Vector.hs b/packages/hmatrix/src/Numeric/GSL/Vector.hs
index 6204b8e..29c8bb7 100644
--- a/packages/hmatrix/src/Numeric/GSL/Vector.hs
+++ b/packages/hmatrix/src/Numeric/GSL/Vector.hs
@@ -2,11 +2,9 @@
2-- | 2-- |
3-- Module : Numeric.GSL.Vector 3-- Module : Numeric.GSL.Vector
4-- Copyright : (c) Alberto Ruiz 2007 4-- Copyright : (c) Alberto Ruiz 2007
5-- License : GPL-style 5-- License : GPL
6-- 6-- Maintainer : Alberto Ruiz
7-- Maintainer : Alberto Ruiz <aruiz@um.es>
8-- Stability : provisional 7-- Stability : provisional
9-- Portability : portable (uses FFI)
10-- 8--
11-- Low level interface to vector operations. 9-- Low level interface to vector operations.
12-- 10--
@@ -20,18 +18,20 @@ module Numeric.GSL.Vector (
20 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ, 18 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ,
21 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ, 19 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ,
22 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ, 20 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ,
23 RandDist(..), randomVector 21 RandDist(..), randomVector,
22 saveMatrix,
23 fwriteVector, freadVector, fprintfVector, fscanfVector
24) where 24) where
25 25
26import Data.Packed.Internal.Common 26import Data.Packed
27import Data.Packed.Internal.Signatures 27import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM)
28import Data.Packed.Internal.Vector
29 28
30import Data.Complex 29import Data.Complex
31import Foreign.Marshal.Alloc(free) 30import Foreign.Marshal.Alloc(free)
32import Foreign.Marshal.Array(newArray) 31import Foreign.Marshal.Array(newArray)
33import Foreign.Ptr(Ptr) 32import Foreign.Ptr(Ptr)
34import Foreign.C.Types 33import Foreign.C.Types
34import Foreign.C.String(newCString)
35import System.IO.Unsafe(unsafePerformIO) 35import System.IO.Unsafe(unsafePerformIO)
36import Control.Monad(when) 36import Control.Monad(when)
37 37
@@ -186,7 +186,7 @@ foreign import ccall unsafe "gsl-aux.h dotC" c_dotC :: TCVCVCV
186toScalarAux fun code v = unsafePerformIO $ do 186toScalarAux fun code v = unsafePerformIO $ do
187 r <- createVector 1 187 r <- createVector 1
188 app2 (fun (fromei code)) vec v vec r "toScalarAux" 188 app2 (fun (fromei code)) vec v vec r "toScalarAux"
189 return (r `at` 0) 189 return (r @> 0)
190 190
191vectorMapAux fun code v = unsafePerformIO $ do 191vectorMapAux fun code v = unsafePerformIO $ do
192 r <- createVector (dim v) 192 r <- createVector (dim v)
@@ -326,3 +326,118 @@ randomVector seed dist n = unsafePerformIO $ do
326 return r 326 return r
327 327
328foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV 328foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV
329
330--------------------------------------------------------------------------------
331
332-- | Saves a matrix as 2D ASCII table.
333saveMatrix :: FilePath
334 -> String -- ^ format (%f, %g, %e)
335 -> Matrix Double
336 -> IO ()
337saveMatrix filename fmt m = do
338 charname <- newCString filename
339 charfmt <- newCString fmt
340 let o = if orderOf m == RowMajor then 1 else 0
341 app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf"
342 free charname
343 free charfmt
344
345foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM
346
347--------------------------------------------------------------------------------
348
349-- | Loads a vector from an ASCII file (the number of elements must be known in advance).
350fscanfVector :: FilePath -> Int -> IO (Vector Double)
351fscanfVector filename n = do
352 charname <- newCString filename
353 res <- createVector n
354 app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf"
355 free charname
356 return res
357
358foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV
359
360-- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file.
361fprintfVector :: FilePath -> String -> Vector Double -> IO ()
362fprintfVector filename fmt v = do
363 charname <- newCString filename
364 charfmt <- newCString fmt
365 app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf"
366 free charname
367 free charfmt
368
369foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV
370
371-- | Loads a vector from a binary file (the number of elements must be known in advance).
372freadVector :: FilePath -> Int -> IO (Vector Double)
373freadVector filename n = do
374 charname <- newCString filename
375 res <- createVector n
376 app1 (gsl_vector_fread charname) vec res "gsl_vector_fread"
377 free charname
378 return res
379
380foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV
381
382-- | Saves the elements of a vector to a binary file.
383fwriteVector :: FilePath -> Vector Double -> IO ()
384fwriteVector filename v = do
385 charname <- newCString filename
386 app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite"
387 free charname
388
389foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV
390
391type PF = Ptr Float --
392type PD = Ptr Double --
393type PQ = Ptr (Complex Float) --
394type PC = Ptr (Complex Double) --
395type TF = CInt -> PF -> IO CInt --
396type TFF = CInt -> PF -> TF --
397type TFV = CInt -> PF -> TV --
398type TVF = CInt -> PD -> TF --
399type TFFF = CInt -> PF -> TFF --
400type TV = CInt -> PD -> IO CInt --
401type TVV = CInt -> PD -> TV --
402type TVVV = CInt -> PD -> TVV --
403type TFM = CInt -> CInt -> PF -> IO CInt --
404type TFMFM = CInt -> CInt -> PF -> TFM --
405type TFMFMFM = CInt -> CInt -> PF -> TFMFM --
406type TM = CInt -> CInt -> PD -> IO CInt --
407type TMM = CInt -> CInt -> PD -> TM --
408type TVMM = CInt -> PD -> TMM --
409type TMVMM = CInt -> CInt -> PD -> TVMM --
410type TMMM = CInt -> CInt -> PD -> TMM --
411type TVM = CInt -> PD -> TM --
412type TVVM = CInt -> PD -> TVM --
413type TMV = CInt -> CInt -> PD -> TV --
414type TMMV = CInt -> CInt -> PD -> TMV --
415type TMVM = CInt -> CInt -> PD -> TVM --
416type TMMVM = CInt -> CInt -> PD -> TMVM --
417type TCM = CInt -> CInt -> PC -> IO CInt --
418type TCVCM = CInt -> PC -> TCM --
419type TCMCVCM = CInt -> CInt -> PC -> TCVCM --
420type TMCMCVCM = CInt -> CInt -> PD -> TCMCVCM --
421type TCMCMCVCM = CInt -> CInt -> PC -> TCMCVCM --
422type TCMCM = CInt -> CInt -> PC -> TCM --
423type TVCM = CInt -> PD -> TCM --
424type TCMVCM = CInt -> CInt -> PC -> TVCM --
425type TCMCMVCM = CInt -> CInt -> PC -> TCMVCM --
426type TCMCMCM = CInt -> CInt -> PC -> TCMCM --
427type TCV = CInt -> PC -> IO CInt --
428type TCVCV = CInt -> PC -> TCV --
429type TCVCVCV = CInt -> PC -> TCVCV --
430type TCVV = CInt -> PC -> TV --
431type TQV = CInt -> PQ -> IO CInt --
432type TQVQV = CInt -> PQ -> TQV --
433type TQVQVQV = CInt -> PQ -> TQVQV --
434type TQVF = CInt -> PQ -> TF --
435type TQM = CInt -> CInt -> PQ -> IO CInt --
436type TQMQM = CInt -> CInt -> PQ -> TQM --
437type TQMQMQM = CInt -> CInt -> PQ -> TQMQM --
438type TCMCV = CInt -> CInt -> PC -> TCV --
439type TVCV = CInt -> PD -> TCV --
440type TCVM = CInt -> PC -> TM --
441type TMCVM = CInt -> CInt -> PD -> TCVM --
442type TMMCVM = CInt -> CInt -> PD -> TMCVM --
443