diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/GSL.hs | 6 | ||||
-rw-r--r-- | lib/GSL/Matrix.hs | 33 | ||||
-rw-r--r-- | lib/GSL/Special.hs | 6 | ||||
-rw-r--r-- | lib/GSL/gsl-aux.c | 21 | ||||
-rw-r--r-- | lib/GSL/gsl-aux.h | 4 | ||||
-rw-r--r-- | lib/LinearAlgebra.hs | 8 | ||||
-rw-r--r-- | lib/LinearAlgebra/Algorithms.hs | 58 | ||||
-rw-r--r-- | lib/LinearAlgebra/Interface.hs | 2 | ||||
-rw-r--r-- | lib/LinearAlgebra/Linear.hs | 3 |
9 files changed, 94 insertions, 47 deletions
@@ -20,6 +20,7 @@ module GSL ( | |||
20 | , module GSL.Minimization | 20 | , module GSL.Minimization |
21 | , module GSL.Special | 21 | , module GSL.Special |
22 | , module Complex | 22 | , module Complex |
23 | , setErrorHandlerOff | ||
23 | ) where | 24 | ) where |
24 | 25 | ||
25 | import GSL.Integration | 26 | import GSL.Integration |
@@ -30,3 +31,8 @@ import GSL.Polynomials | |||
30 | import GSL.Minimization | 31 | import GSL.Minimization |
31 | import Complex | 32 | import Complex |
32 | import GSL.Special | 33 | import GSL.Special |
34 | |||
35 | |||
36 | -- | This action removes the GSL default error handler (which aborts the program), so that | ||
37 | -- GSL errors can be handled by Haskell (using Control.Exception) and ghci doesn't abort. | ||
38 | foreign import ccall "GSL/gsl-aux.h no_abort_on_error" setErrorHandlerOff :: IO () | ||
diff --git a/lib/GSL/Matrix.hs b/lib/GSL/Matrix.hs index ec8ceea..c1bce37 100644 --- a/lib/GSL/Matrix.hs +++ b/lib/GSL/Matrix.hs | |||
@@ -16,7 +16,7 @@ module GSL.Matrix( | |||
16 | eigSg, eigHg, | 16 | eigSg, eigHg, |
17 | svdg, | 17 | svdg, |
18 | qr, | 18 | qr, |
19 | chol, | 19 | cholR, -- cholC, |
20 | luSolveR, luSolveC, | 20 | luSolveR, luSolveC, |
21 | luR, luC, | 21 | luR, luC, |
22 | fromFile, extractRows | 22 | fromFile, extractRows |
@@ -153,24 +153,29 @@ foreign import ccall "gsl-aux.h QR" c_qr :: TMMM | |||
153 | 153 | ||
154 | {- | Cholesky decomposition of a symmetric positive definite real matrix using /gsl_linalg_cholesky_decomp/. | 154 | {- | Cholesky decomposition of a symmetric positive definite real matrix using /gsl_linalg_cholesky_decomp/. |
155 | 155 | ||
156 | @\> let c = chol $ 'fromLists' [[5,4],[4,5]] | 156 | @\> chol $ (2><2) [1,2, |
157 | \ | 157 | 2,9::Double] |
158 | \> c | 158 | (2><2) |
159 | 2.236 0. | 159 | [ 1.0, 0.0 |
160 | 1.789 1.342 | 160 | , 2.0, 2.23606797749979 ]@ |
161 | \ | ||
162 | \> c \<\> 'trans' c | ||
163 | 5.000 4.000 | ||
164 | 4.000 5.000@ | ||
165 | 161 | ||
166 | -} | 162 | -} |
167 | chol :: Matrix Double -> Matrix Double | 163 | cholR :: Matrix Double -> Matrix Double |
168 | chol x = unsafePerformIO $ do | 164 | cholR x = unsafePerformIO $ do |
169 | res <- createMatrix RowMajor r r | 165 | res <- createMatrix RowMajor r r |
170 | c_chol // mat cdat x // mat dat res // check "chol" [cdat x] | 166 | c_cholR // mat cdat x // mat dat res // check "cholR" [cdat x] |
171 | return res | 167 | return res |
172 | where r = rows x | 168 | where r = rows x |
173 | foreign import ccall "gsl-aux.h chol" c_chol :: TMM | 169 | foreign import ccall "gsl-aux.h cholR" c_cholR :: TMM |
170 | |||
171 | cholC :: Matrix (Complex Double) -> Matrix (Complex Double) | ||
172 | cholC x = unsafePerformIO $ do | ||
173 | res <- createMatrix RowMajor r r | ||
174 | c_cholC // mat cdat x // mat dat res // check "cholC" [cdat x] | ||
175 | return res | ||
176 | where r = rows x | ||
177 | foreign import ccall "gsl-aux.h cholC" c_cholC :: TCMCM | ||
178 | |||
174 | 179 | ||
175 | -------------------------------------------------------- | 180 | -------------------------------------------------------- |
176 | 181 | ||
diff --git a/lib/GSL/Special.hs b/lib/GSL/Special.hs index bdee1b2..fa002b9 100644 --- a/lib/GSL/Special.hs +++ b/lib/GSL/Special.hs | |||
@@ -41,7 +41,6 @@ module GSL.Special ( | |||
41 | , module GSL.Special.Synchrotron | 41 | , module GSL.Special.Synchrotron |
42 | , module GSL.Special.Trig | 42 | , module GSL.Special.Trig |
43 | , module GSL.Special.Zeta | 43 | , module GSL.Special.Zeta |
44 | , setErrorHandlerOff | ||
45 | ) | 44 | ) |
46 | where | 45 | where |
47 | 46 | ||
@@ -73,8 +72,3 @@ import GSL.Special.Psi | |||
73 | import GSL.Special.Synchrotron | 72 | import GSL.Special.Synchrotron |
74 | import GSL.Special.Trig | 73 | import GSL.Special.Trig |
75 | import GSL.Special.Zeta | 74 | import GSL.Special.Zeta |
76 | |||
77 | |||
78 | -- | This action removes the GSL default error handler which aborts the program, so that | ||
79 | -- GSL errors can be handled by Haskell (using Control.Exception) and ghci doesn't abort. | ||
80 | foreign import ccall "gsl-aux.h no_abort_on_error" setErrorHandlerOff :: IO () | ||
diff --git a/lib/GSL/gsl-aux.c b/lib/GSL/gsl-aux.c index 0e8406c..c602d5e 100644 --- a/lib/GSL/gsl-aux.c +++ b/lib/GSL/gsl-aux.c | |||
@@ -471,9 +471,9 @@ int QR(KRMAT(x),RMAT(q),RMAT(r)) { | |||
471 | OK | 471 | OK |
472 | } | 472 | } |
473 | 473 | ||
474 | int chol(KRMAT(x),RMAT(l)) { | 474 | int cholR(KRMAT(x),RMAT(l)) { |
475 | REQUIRES(xr==xc && lr==xr && lr==lc,BAD_SIZE); | 475 | REQUIRES(xr==xc && lr==xr && lr==lc,BAD_SIZE); |
476 | DEBUGMSG("chol"); | 476 | DEBUGMSG("cholR"); |
477 | KDMVIEW(x); | 477 | KDMVIEW(x); |
478 | DMVIEW(l); | 478 | DMVIEW(l); |
479 | gsl_matrix_memcpy(M(l),M(x)); | 479 | gsl_matrix_memcpy(M(l),M(x)); |
@@ -488,6 +488,23 @@ int chol(KRMAT(x),RMAT(l)) { | |||
488 | OK | 488 | OK |
489 | } | 489 | } |
490 | 490 | ||
491 | int cholC(KCMAT(x),CMAT(l)) { | ||
492 | REQUIRES(xr==xc && lr==xr && lr==lc,BAD_SIZE); | ||
493 | DEBUGMSG("cholC"); | ||
494 | KCMVIEW(x); | ||
495 | CMVIEW(l); | ||
496 | gsl_matrix_complex_memcpy(M(l),M(x)); | ||
497 | int res = 0; // gsl_linalg_complex_cholesky_decomp(M(l)); | ||
498 | CHECK(res,res); | ||
499 | gsl_complex zero = {0.,0.}; | ||
500 | int r,c; | ||
501 | for (r=0; r<xr-1; r++) { | ||
502 | for(c=r+1; c<xc; c++) { | ||
503 | lp[r*lc+c] = zero; | ||
504 | } | ||
505 | } | ||
506 | OK | ||
507 | } | ||
491 | 508 | ||
492 | int fft(int code, KCVEC(X), CVEC(R)) { | 509 | int fft(int code, KCVEC(X), CVEC(R)) { |
493 | REQUIRES(Xn == Rn,BAD_SIZE); | 510 | REQUIRES(Xn == Rn,BAD_SIZE); |
diff --git a/lib/GSL/gsl-aux.h b/lib/GSL/gsl-aux.h index 8f8618a..dd1a247 100644 --- a/lib/GSL/gsl-aux.h +++ b/lib/GSL/gsl-aux.h | |||
@@ -38,7 +38,9 @@ int eigensystemC(KCMAT(x),RVEC(l),CMAT(v)); | |||
38 | 38 | ||
39 | int QR(KRMAT(x),RMAT(q),RMAT(r)); | 39 | int QR(KRMAT(x),RMAT(q),RMAT(r)); |
40 | 40 | ||
41 | int chol(KRMAT(x),RMAT(l)); | 41 | int cholR(KRMAT(x),RMAT(l)); |
42 | |||
43 | int cholC(KCMAT(x),CMAT(l)); | ||
42 | 44 | ||
43 | int fft(int code, KCVEC(a), CVEC(b)); | 45 | int fft(int code, KCVEC(a), CVEC(b)); |
44 | 46 | ||
diff --git a/lib/LinearAlgebra.hs b/lib/LinearAlgebra.hs index a271592..d8f44b4 100644 --- a/lib/LinearAlgebra.hs +++ b/lib/LinearAlgebra.hs | |||
@@ -8,28 +8,22 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) | |||
8 | Stability : provisional | 8 | Stability : provisional |
9 | Portability : uses ffi | 9 | Portability : uses ffi |
10 | 10 | ||
11 | Some linear algebra algorithms, implemented by means of BLAS, LAPACK or GSL. | 11 | Basic matrix computations implemented by BLAS, LAPACK and GSL. |
12 | 12 | ||
13 | -} | 13 | -} |
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | module LinearAlgebra ( | 15 | module LinearAlgebra ( |
16 | module Data.Packed.Vector, | 16 | module Data.Packed.Vector, |
17 | module Data.Packed.Matrix, | 17 | module Data.Packed.Matrix, |
18 | module LinearAlgebra.Linear, | ||
19 | module LinearAlgebra.Instances, | 18 | module LinearAlgebra.Instances, |
20 | module LinearAlgebra.Interface, | 19 | module LinearAlgebra.Interface, |
21 | module LAPACK, | ||
22 | module GSL.Matrix, | ||
23 | module LinearAlgebra.Algorithms, | 20 | module LinearAlgebra.Algorithms, |
24 | module Complex | 21 | module Complex |
25 | ) where | 22 | ) where |
26 | 23 | ||
27 | import LinearAlgebra.Linear | ||
28 | import LinearAlgebra.Instances | 24 | import LinearAlgebra.Instances |
29 | import LinearAlgebra.Interface | 25 | import LinearAlgebra.Interface |
30 | import LinearAlgebra.Algorithms | 26 | import LinearAlgebra.Algorithms |
31 | import LAPACK | ||
32 | import GSL.Matrix | ||
33 | import Data.Packed.Matrix | 27 | import Data.Packed.Matrix |
34 | import Data.Packed.Vector | 28 | import Data.Packed.Vector |
35 | import Complex \ No newline at end of file | 29 | import Complex \ No newline at end of file |
diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index 7953386..162426f 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs | |||
@@ -9,23 +9,47 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) | |||
9 | Stability : provisional | 9 | Stability : provisional |
10 | Portability : uses ffi | 10 | Portability : uses ffi |
11 | 11 | ||
12 | A simple interface to the available matrix computations. We have defined some generic functions | ||
13 | on both real and complex matrices in such a way that higher level algorithms and | ||
14 | testing properties are valid for both base types. | ||
15 | |||
16 | In any case the specific functions for particular base types can also be explicitly | ||
17 | imported from the LAPACK and GSL.Matrix modules. | ||
12 | 18 | ||
13 | -} | 19 | -} |
14 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
15 | 21 | ||
16 | module LinearAlgebra.Algorithms ( | 22 | module LinearAlgebra.Algorithms ( |
17 | GMatrix(..), | 23 | -- * Basic Linear Algebra |
24 | scale, | ||
25 | add, | ||
26 | multiply, dot, outer, | ||
27 | linearSolve, linearSolveSVD, | ||
28 | -- * Matrix factorizations | ||
29 | svd, lu, eig, eigSH, | ||
30 | qr, chol, | ||
31 | -- * Utilities | ||
18 | Normed(..), NormType(..), | 32 | Normed(..), NormType(..), |
19 | det,inv,pinv,full,economy, | 33 | det,inv,pinv,full,economy, |
20 | pinvTol, | 34 | pinvTol, |
21 | -- pinvTolg, | 35 | -- pinvTolg, |
22 | nullspacePrec, | 36 | nullspacePrec, |
23 | nullVector, | 37 | nullVector, |
24 | eps, i | 38 | -- * Conversions |
39 | toComplex, fromComplex, | ||
40 | comp, real, complex, | ||
41 | conj, ctrans, | ||
42 | -- * Misc | ||
43 | eps, i, | ||
44 | scaleRecip, | ||
45 | addConstant, | ||
46 | sub, | ||
47 | mul, | ||
48 | divide, | ||
25 | ) where | 49 | ) where |
26 | 50 | ||
27 | 51 | ||
28 | import Data.Packed.Internal | 52 | import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj) |
29 | import Data.Packed.Matrix | 53 | import Data.Packed.Matrix |
30 | import GSL.Matrix | 54 | import GSL.Matrix |
31 | import GSL.Vector | 55 | import GSL.Vector |
@@ -33,7 +57,8 @@ import LAPACK | |||
33 | import Complex | 57 | import Complex |
34 | import LinearAlgebra.Linear | 58 | import LinearAlgebra.Linear |
35 | 59 | ||
36 | class (Linear Matrix t) => GMatrix t where | 60 | -- | Base types for which some optimized matrix computations are available |
61 | class (Linear Matrix t) => Optimized t where | ||
37 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) | 62 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) |
38 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) | 63 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) |
39 | linearSolve :: Matrix t -> Matrix t -> Matrix t | 64 | linearSolve :: Matrix t -> Matrix t -> Matrix t |
@@ -41,8 +66,9 @@ class (Linear Matrix t) => GMatrix t where | |||
41 | ctrans :: Matrix t -> Matrix t | 66 | ctrans :: Matrix t -> Matrix t |
42 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) | 67 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) |
43 | eigSH :: Matrix t -> (Vector Double, Matrix t) | 68 | eigSH :: Matrix t -> (Vector Double, Matrix t) |
69 | chol :: Matrix t -> Matrix t | ||
44 | 70 | ||
45 | instance GMatrix Double where | 71 | instance Optimized Double where |
46 | svd = svdR | 72 | svd = svdR |
47 | lu = luR | 73 | lu = luR |
48 | linearSolve = linearSolveR | 74 | linearSolve = linearSolveR |
@@ -50,8 +76,9 @@ instance GMatrix Double where | |||
50 | ctrans = trans | 76 | ctrans = trans |
51 | eig = eigR | 77 | eig = eigR |
52 | eigSH = eigS | 78 | eigSH = eigS |
79 | chol = cholR | ||
53 | 80 | ||
54 | instance GMatrix (Complex Double) where | 81 | instance Optimized (Complex Double) where |
55 | svd = svdC | 82 | svd = svdC |
56 | lu = luC | 83 | lu = luC |
57 | linearSolve = linearSolveC | 84 | linearSolve = linearSolveC |
@@ -59,19 +86,20 @@ instance GMatrix (Complex Double) where | |||
59 | ctrans = conjTrans | 86 | ctrans = conjTrans |
60 | eig = eigC | 87 | eig = eigC |
61 | eigSH = eigH | 88 | eigSH = eigH |
89 | chol = error "cholC not yet implemented" -- waiting for GSL-1.10 | ||
62 | 90 | ||
63 | square m = rows m == cols m | 91 | square m = rows m == cols m |
64 | 92 | ||
65 | det :: GMatrix t => Matrix t -> t | 93 | det :: Optimized t => Matrix t -> t |
66 | det m | square m = s * (product $ toList $ takeDiag $ u) | 94 | det m | square m = s * (product $ toList $ takeDiag $ u) |
67 | | otherwise = error "det of nonsquare matrix" | 95 | | otherwise = error "det of nonsquare matrix" |
68 | where (_,u,_,s) = lu m | 96 | where (_,u,_,s) = lu m |
69 | 97 | ||
70 | inv :: GMatrix t => Matrix t -> Matrix t | 98 | inv :: Optimized t => Matrix t -> Matrix t |
71 | inv m | square m = m `linearSolve` ident (rows m) | 99 | inv m | square m = m `linearSolve` ident (rows m) |
72 | | otherwise = error "inv of nonsquare matrix" | 100 | | otherwise = error "inv of nonsquare matrix" |
73 | 101 | ||
74 | pinv :: GMatrix t => Matrix t -> Matrix t | 102 | pinv :: Optimized t => Matrix t -> Matrix t |
75 | pinv m = linearSolveSVD m (ident (rows m)) | 103 | pinv m = linearSolveSVD m (ident (rows m)) |
76 | 104 | ||
77 | 105 | ||
@@ -119,15 +147,15 @@ i = 0:+1 | |||
119 | 147 | ||
120 | 148 | ||
121 | -- | matrix product | 149 | -- | matrix product |
122 | mXm :: (Num t, Field t) => Matrix t -> Matrix t -> Matrix t | 150 | mXm :: (Num t, Optimized t) => Matrix t -> Matrix t -> Matrix t |
123 | mXm = multiply | 151 | mXm = multiply |
124 | 152 | ||
125 | -- | matrix - vector product | 153 | -- | matrix - vector product |
126 | mXv :: (Num t, Field t) => Matrix t -> Vector t -> Vector t | 154 | mXv :: (Num t, Optimized t) => Matrix t -> Vector t -> Vector t |
127 | mXv m v = flatten $ m `mXm` (asColumn v) | 155 | mXv m v = flatten $ m `mXm` (asColumn v) |
128 | 156 | ||
129 | -- | vector - matrix product | 157 | -- | vector - matrix product |
130 | vXm :: (Num t, Field t) => Vector t -> Matrix t -> Vector t | 158 | vXm :: (Num t, Optimized t) => Vector t -> Matrix t -> Vector t |
131 | vXm v m = flatten $ (asRow v) `mXm` m | 159 | vXm v m = flatten $ (asRow v) `mXm` m |
132 | 160 | ||
133 | 161 | ||
@@ -171,7 +199,7 @@ pnormCM Infinity m = vectorMax $ liftMatrix (liftVector magnitude) m `mXv` const | |||
171 | --pnormCM _ _ = error "p norm not yet defined" | 199 | --pnormCM _ _ = error "p norm not yet defined" |
172 | 200 | ||
173 | -- -- | computes the p-norm of a matrix or vector (with the same definitions as GNU-octave). pnorm 0 denotes \\inf-norm. See also 'norm'. | 201 | -- -- | computes the p-norm of a matrix or vector (with the same definitions as GNU-octave). pnorm 0 denotes \\inf-norm. See also 'norm'. |
174 | --pnorm :: (Container t, Field a) => Int -> t a -> Double | 202 | --pnorm :: (Container t, Optimized a) => Int -> t a -> Double |
175 | --pnorm = pnormG | 203 | --pnorm = pnormG |
176 | 204 | ||
177 | class Normed t where | 205 | class Normed t where |
@@ -194,7 +222,7 @@ instance Normed (Matrix (Complex Double)) where | |||
194 | ----------------------------------------------------------------------- | 222 | ----------------------------------------------------------------------- |
195 | 223 | ||
196 | -- | The nullspace of a matrix from its SVD decomposition. | 224 | -- | The nullspace of a matrix from its SVD decomposition. |
197 | nullspacePrec :: GMatrix t | 225 | nullspacePrec :: Optimized t |
198 | => Double -- ^ relative tolerance in 'eps' units | 226 | => Double -- ^ relative tolerance in 'eps' units |
199 | -> Matrix t -- ^ input matrix | 227 | -> Matrix t -- ^ input matrix |
200 | -> [Vector t] -- ^ list of unitary vectors spanning the nullspace | 228 | -> [Vector t] -- ^ list of unitary vectors spanning the nullspace |
@@ -207,7 +235,7 @@ nullspacePrec t m = ns where | |||
207 | ns = drop rank $ toRows $ ctrans v | 235 | ns = drop rank $ toRows $ ctrans v |
208 | 236 | ||
209 | -- | The nullspace of a matrix, assumed to be one-dimensional, with default tolerance (shortcut for @last . nullspacePrec 1@). | 237 | -- | The nullspace of a matrix, assumed to be one-dimensional, with default tolerance (shortcut for @last . nullspacePrec 1@). |
210 | nullVector :: GMatrix t => Matrix t -> Vector t | 238 | nullVector :: Optimized t => Matrix t -> Vector t |
211 | nullVector = last . nullspacePrec 1 | 239 | nullVector = last . nullspacePrec 1 |
212 | 240 | ||
213 | ------------------------------------------------------------------------ | 241 | ------------------------------------------------------------------------ |
diff --git a/lib/LinearAlgebra/Interface.hs b/lib/LinearAlgebra/Interface.hs index 0d2c0a0..3392d54 100644 --- a/lib/LinearAlgebra/Interface.hs +++ b/lib/LinearAlgebra/Interface.hs | |||
@@ -82,7 +82,7 @@ infixl 3 <-> | |||
82 | 82 | ||
83 | {- | Horizontal concatenation of matrices and vectors: | 83 | {- | Horizontal concatenation of matrices and vectors: |
84 | 84 | ||
85 | @> (ident 3 -&- 3 * ident 3) |&| fromList [1..6.0] | 85 | @> (ident 3 \<-\> 3 * ident 3) \<|\> fromList [1..6.0] |
86 | (6><4) | 86 | (6><4) |
87 | [ 1.0, 0.0, 0.0, 1.0 | 87 | [ 1.0, 0.0, 0.0, 1.0 |
88 | , 0.0, 1.0, 0.0, 2.0 | 88 | , 0.0, 1.0, 0.0, 2.0 |
diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs index 2f1bc6f..85daa4a 100644 --- a/lib/LinearAlgebra/Linear.hs +++ b/lib/LinearAlgebra/Linear.hs | |||
@@ -12,6 +12,7 @@ Portability : uses ffi | |||
12 | 12 | ||
13 | -} | 13 | -} |
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | -- #hide | ||
15 | 16 | ||
16 | module LinearAlgebra.Linear ( | 17 | module LinearAlgebra.Linear ( |
17 | Linear(..), | 18 | Linear(..), |
@@ -24,7 +25,7 @@ import Data.Packed.Matrix | |||
24 | import GSL.Vector | 25 | import GSL.Vector |
25 | import Complex | 26 | import Complex |
26 | 27 | ||
27 | 28 | -- | basic optimized operations | |
28 | class (Field e) => Linear c e where | 29 | class (Field e) => Linear c e where |
29 | scale :: e -> c e -> c e | 30 | scale :: e -> c e -> c e |
30 | scaleRecip :: e -> c e -> c e | 31 | scaleRecip :: e -> c e -> c e |