summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs160
1 files changed, 160 insertions, 0 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
new file mode 100644
index 0000000..43e46c5
--- /dev/null
+++ b/packages/hmatrix/src/Numeric/GSL/LinearAlgebra.hs
@@ -0,0 +1,160 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Numeric.GSL.LinearAlgebra
4-- Copyright : (c) Alberto Ruiz 2007-14
5-- License : GPL
6-- Maintainer : Alberto Ruiz
7-- Stability : provisional
8--
9-----------------------------------------------------------------------------
10
11module Numeric.GSL.LinearAlgebra (
12 RandDist(..), randomVector,
13 saveMatrix,
14 fwriteVector, freadVector, fprintfVector, fscanfVector
15) where
16
17import Data.Packed
18import Numeric.GSL.Internal hiding (TV,TM,TCV,TCM)
19
20import Data.Complex
21import Foreign.Marshal.Alloc(free)
22import Foreign.Ptr(Ptr)
23import Foreign.C.Types
24import Foreign.C.String(newCString)
25import System.IO.Unsafe(unsafePerformIO)
26
27fromei x = fromIntegral (fromEnum x) :: CInt
28
29-----------------------------------------------------------------------
30
31data RandDist = Uniform -- ^ uniform distribution in [0,1)
32 | Gaussian -- ^ normal distribution with mean zero and standard deviation one
33 deriving Enum
34
35-- | Obtains a vector of pseudorandom elements from the the mt19937 generator in GSL, with a given seed. Use randomIO to get a random seed.
36randomVector :: Int -- ^ seed
37 -> RandDist -- ^ distribution
38 -> Int -- ^ vector size
39 -> Vector Double
40randomVector seed dist n = unsafePerformIO $ do
41 r <- createVector n
42 app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector"
43 return r
44
45foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV
46
47--------------------------------------------------------------------------------
48
49-- | Saves a matrix as 2D ASCII table.
50saveMatrix :: FilePath
51 -> String -- ^ format (%f, %g, %e)
52 -> Matrix Double
53 -> IO ()
54saveMatrix filename fmt m = do
55 charname <- newCString filename
56 charfmt <- newCString fmt
57 let o = if orderOf m == RowMajor then 1 else 0
58 app1 (matrix_fprintf charname charfmt o) mat m "matrix_fprintf"
59 free charname
60 free charfmt
61
62foreign import ccall unsafe "matrix_fprintf" matrix_fprintf :: Ptr CChar -> Ptr CChar -> CInt -> TM
63
64--------------------------------------------------------------------------------
65
66-- | Loads a vector from an ASCII file (the number of elements must be known in advance).
67fscanfVector :: FilePath -> Int -> IO (Vector Double)
68fscanfVector filename n = do
69 charname <- newCString filename
70 res <- createVector n
71 app1 (gsl_vector_fscanf charname) vec res "gsl_vector_fscanf"
72 free charname
73 return res
74
75foreign import ccall unsafe "vector_fscanf" gsl_vector_fscanf:: Ptr CChar -> TV
76
77-- | Saves the elements of a vector, with a given format (%f, %e, %g), to an ASCII file.
78fprintfVector :: FilePath -> String -> Vector Double -> IO ()
79fprintfVector filename fmt v = do
80 charname <- newCString filename
81 charfmt <- newCString fmt
82 app1 (gsl_vector_fprintf charname charfmt) vec v "gsl_vector_fprintf"
83 free charname
84 free charfmt
85
86foreign import ccall unsafe "vector_fprintf" gsl_vector_fprintf :: Ptr CChar -> Ptr CChar -> TV
87
88-- | Loads a vector from a binary file (the number of elements must be known in advance).
89freadVector :: FilePath -> Int -> IO (Vector Double)
90freadVector filename n = do
91 charname <- newCString filename
92 res <- createVector n
93 app1 (gsl_vector_fread charname) vec res "gsl_vector_fread"
94 free charname
95 return res
96
97foreign import ccall unsafe "vector_fread" gsl_vector_fread:: Ptr CChar -> TV
98
99-- | Saves the elements of a vector to a binary file.
100fwriteVector :: FilePath -> Vector Double -> IO ()
101fwriteVector filename v = do
102 charname <- newCString filename
103 app1 (gsl_vector_fwrite charname) vec v "gsl_vector_fwrite"
104 free charname
105
106foreign import ccall unsafe "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV
107
108type PF = Ptr Float --
109type PD = Ptr Double --
110type PQ = Ptr (Complex Float) --
111type PC = Ptr (Complex Double) --
112type TF = CInt -> PF -> IO CInt --
113type TFF = CInt -> PF -> TF --
114type TFV = CInt -> PF -> TV --
115type TVF = CInt -> PD -> TF --
116type TFFF = CInt -> PF -> TFF --
117type TV = CInt -> PD -> IO CInt --
118type TVV = CInt -> PD -> TV --
119type TVVV = CInt -> PD -> TVV --
120type TFM = CInt -> CInt -> PF -> IO CInt --
121type TFMFM = CInt -> CInt -> PF -> TFM --
122type TFMFMFM = CInt -> CInt -> PF -> TFMFM --
123type TM = CInt -> CInt -> PD -> IO CInt --
124type TMM = CInt -> CInt -> PD -> TM --
125type TVMM = CInt -> PD -> TMM --
126type TMVMM = CInt -> CInt -> PD -> TVMM --
127type TMMM = CInt -> CInt -> PD -> TMM --
128type TVM = CInt -> PD -> TM --
129type TVVM = CInt -> PD -> TVM --
130type TMV = CInt -> CInt -> PD -> TV --
131type TMMV = CInt -> CInt -> PD -> TMV --
132type TMVM = CInt -> CInt -> PD -> TVM --
133type TMMVM = CInt -> CInt -> PD -> TMVM --
134type TCM = CInt -> CInt -> PC -> IO CInt --
135type TCVCM = CInt -> PC -> TCM --
136type TCMCVCM = CInt -> CInt -> PC -> TCVCM --
137type TMCMCVCM = CInt -> CInt -> PD -> TCMCVCM --
138type TCMCMCVCM = CInt -> CInt -> PC -> TCMCVCM --
139type TCMCM = CInt -> CInt -> PC -> TCM --
140type TVCM = CInt -> PD -> TCM --
141type TCMVCM = CInt -> CInt -> PC -> TVCM --
142type TCMCMVCM = CInt -> CInt -> PC -> TCMVCM --
143type TCMCMCM = CInt -> CInt -> PC -> TCMCM --
144type TCV = CInt -> PC -> IO CInt --
145type TCVCV = CInt -> PC -> TCV --
146type TCVCVCV = CInt -> PC -> TCVCV --
147type TCVV = CInt -> PC -> TV --
148type TQV = CInt -> PQ -> IO CInt --
149type TQVQV = CInt -> PQ -> TQV --
150type TQVQVQV = CInt -> PQ -> TQVQV --
151type TQVF = CInt -> PQ -> TF --
152type TQM = CInt -> CInt -> PQ -> IO CInt --
153type TQMQM = CInt -> CInt -> PQ -> TQM --
154type TQMQMQM = CInt -> CInt -> PQ -> TQMQM --
155type TCMCV = CInt -> CInt -> PC -> TCV --
156type TVCV = CInt -> PD -> TCV --
157type TCVM = CInt -> PC -> TM --
158type TMCVM = CInt -> CInt -> PD -> TCVM --
159type TMMCVM = CInt -> CInt -> PD -> TMCVM --
160