diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-08 13:43:07 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-08 13:43:07 +0200 |
commit | 551cf7498c33bc0948bb4cb8444ae6f8af7278ea (patch) | |
tree | ec86ff73151746f5e13b83549ea5c60ed442764d /packages/hmatrix/src/Numeric/GSL/Vector.hs | |
parent | 561a6c0e21bb77c21114ccbbd86d3af5ddb5a3f1 (diff) |
separation ok
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Vector.hs')
-rw-r--r-- | packages/hmatrix/src/Numeric/GSL/Vector.hs | 133 |
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 | ||
26 | import Data.Packed.Internal.Common | 26 | import Data.Packed |
27 | import Data.Packed.Internal.Signatures | 27 | import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM) |
28 | import Data.Packed.Internal.Vector | ||
29 | 28 | ||
30 | import Data.Complex | 29 | import Data.Complex |
31 | import Foreign.Marshal.Alloc(free) | 30 | import Foreign.Marshal.Alloc(free) |
32 | import Foreign.Marshal.Array(newArray) | 31 | import Foreign.Marshal.Array(newArray) |
33 | import Foreign.Ptr(Ptr) | 32 | import Foreign.Ptr(Ptr) |
34 | import Foreign.C.Types | 33 | import Foreign.C.Types |
34 | import Foreign.C.String(newCString) | ||
35 | import System.IO.Unsafe(unsafePerformIO) | 35 | import System.IO.Unsafe(unsafePerformIO) |
36 | import Control.Monad(when) | 36 | import Control.Monad(when) |
37 | 37 | ||
@@ -186,7 +186,7 @@ foreign import ccall unsafe "gsl-aux.h dotC" c_dotC :: TCVCVCV | |||
186 | toScalarAux fun code v = unsafePerformIO $ do | 186 | toScalarAux 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 | ||
191 | vectorMapAux fun code v = unsafePerformIO $ do | 191 | vectorMapAux 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 | ||
328 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV | 328 | foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV |
329 | |||
330 | -------------------------------------------------------------------------------- | ||
331 | |||
332 | -- | Saves a matrix as 2D ASCII table. | ||
333 | saveMatrix :: FilePath | ||
334 | -> String -- ^ format (%f, %g, %e) | ||
335 | -> Matrix Double | ||
336 | -> IO () | ||
337 | saveMatrix 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 | |||
345 | foreign 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). | ||
350 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | ||
351 | fscanfVector 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 | |||
358 | foreign 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. | ||
361 | fprintfVector :: FilePath -> String -> Vector Double -> IO () | ||
362 | fprintfVector 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 | |||
369 | foreign 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). | ||
372 | freadVector :: FilePath -> Int -> IO (Vector Double) | ||
373 | freadVector 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 | |||
380 | foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV | ||
381 | |||
382 | -- | Saves the elements of a vector to a binary file. | ||
383 | fwriteVector :: FilePath -> Vector Double -> IO () | ||
384 | fwriteVector filename v = do | ||
385 | charname <- newCString filename | ||
386 | app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite" | ||
387 | free charname | ||
388 | |||
389 | foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | ||
390 | |||
391 | type PF = Ptr Float -- | ||
392 | type PD = Ptr Double -- | ||
393 | type PQ = Ptr (Complex Float) -- | ||
394 | type PC = Ptr (Complex Double) -- | ||
395 | type TF = CInt -> PF -> IO CInt -- | ||
396 | type TFF = CInt -> PF -> TF -- | ||
397 | type TFV = CInt -> PF -> TV -- | ||
398 | type TVF = CInt -> PD -> TF -- | ||
399 | type TFFF = CInt -> PF -> TFF -- | ||
400 | type TV = CInt -> PD -> IO CInt -- | ||
401 | type TVV = CInt -> PD -> TV -- | ||
402 | type TVVV = CInt -> PD -> TVV -- | ||
403 | type TFM = CInt -> CInt -> PF -> IO CInt -- | ||
404 | type TFMFM = CInt -> CInt -> PF -> TFM -- | ||
405 | type TFMFMFM = CInt -> CInt -> PF -> TFMFM -- | ||
406 | type TM = CInt -> CInt -> PD -> IO CInt -- | ||
407 | type TMM = CInt -> CInt -> PD -> TM -- | ||
408 | type TVMM = CInt -> PD -> TMM -- | ||
409 | type TMVMM = CInt -> CInt -> PD -> TVMM -- | ||
410 | type TMMM = CInt -> CInt -> PD -> TMM -- | ||
411 | type TVM = CInt -> PD -> TM -- | ||
412 | type TVVM = CInt -> PD -> TVM -- | ||
413 | type TMV = CInt -> CInt -> PD -> TV -- | ||
414 | type TMMV = CInt -> CInt -> PD -> TMV -- | ||
415 | type TMVM = CInt -> CInt -> PD -> TVM -- | ||
416 | type TMMVM = CInt -> CInt -> PD -> TMVM -- | ||
417 | type TCM = CInt -> CInt -> PC -> IO CInt -- | ||
418 | type TCVCM = CInt -> PC -> TCM -- | ||
419 | type TCMCVCM = CInt -> CInt -> PC -> TCVCM -- | ||
420 | type TMCMCVCM = CInt -> CInt -> PD -> TCMCVCM -- | ||
421 | type TCMCMCVCM = CInt -> CInt -> PC -> TCMCVCM -- | ||
422 | type TCMCM = CInt -> CInt -> PC -> TCM -- | ||
423 | type TVCM = CInt -> PD -> TCM -- | ||
424 | type TCMVCM = CInt -> CInt -> PC -> TVCM -- | ||
425 | type TCMCMVCM = CInt -> CInt -> PC -> TCMVCM -- | ||
426 | type TCMCMCM = CInt -> CInt -> PC -> TCMCM -- | ||
427 | type TCV = CInt -> PC -> IO CInt -- | ||
428 | type TCVCV = CInt -> PC -> TCV -- | ||
429 | type TCVCVCV = CInt -> PC -> TCVCV -- | ||
430 | type TCVV = CInt -> PC -> TV -- | ||
431 | type TQV = CInt -> PQ -> IO CInt -- | ||
432 | type TQVQV = CInt -> PQ -> TQV -- | ||
433 | type TQVQVQV = CInt -> PQ -> TQVQV -- | ||
434 | type TQVF = CInt -> PQ -> TF -- | ||
435 | type TQM = CInt -> CInt -> PQ -> IO CInt -- | ||
436 | type TQMQM = CInt -> CInt -> PQ -> TQM -- | ||
437 | type TQMQMQM = CInt -> CInt -> PQ -> TQMQM -- | ||
438 | type TCMCV = CInt -> CInt -> PC -> TCV -- | ||
439 | type TVCV = CInt -> PD -> TCV -- | ||
440 | type TCVM = CInt -> PC -> TM -- | ||
441 | type TMCVM = CInt -> CInt -> PD -> TCVM -- | ||
442 | type TMMCVM = CInt -> CInt -> PD -> TMCVM -- | ||
443 | |||