summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/GSL.hs6
-rw-r--r--lib/GSL/Matrix.hs33
-rw-r--r--lib/GSL/Special.hs6
-rw-r--r--lib/GSL/gsl-aux.c21
-rw-r--r--lib/GSL/gsl-aux.h4
-rw-r--r--lib/LinearAlgebra.hs8
-rw-r--r--lib/LinearAlgebra/Algorithms.hs58
-rw-r--r--lib/LinearAlgebra/Interface.hs2
-rw-r--r--lib/LinearAlgebra/Linear.hs3
9 files changed, 94 insertions, 47 deletions
diff --git a/lib/GSL.hs b/lib/GSL.hs
index d65f8ff..f04cf26 100644
--- a/lib/GSL.hs
+++ b/lib/GSL.hs
@@ -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
25import GSL.Integration 26import GSL.Integration
@@ -30,3 +31,8 @@ import GSL.Polynomials
30import GSL.Minimization 31import GSL.Minimization
31import Complex 32import Complex
32import GSL.Special 33import 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.
38foreign 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)
1592.236 0. 159 [ 1.0, 0.0
1601.789 1.342 160 , 2.0, 2.23606797749979 ]@
161\
162\> c \<\> 'trans' c
1635.000 4.000
1644.000 5.000@
165 161
166-} 162-}
167chol :: Matrix Double -> Matrix Double 163cholR :: Matrix Double -> Matrix Double
168chol x = unsafePerformIO $ do 164cholR 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
173foreign import ccall "gsl-aux.h chol" c_chol :: TMM 169foreign import ccall "gsl-aux.h cholR" c_cholR :: TMM
170
171cholC :: Matrix (Complex Double) -> Matrix (Complex Double)
172cholC 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
177foreign 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)
46where 45where
47 46
@@ -73,8 +72,3 @@ import GSL.Special.Psi
73import GSL.Special.Synchrotron 72import GSL.Special.Synchrotron
74import GSL.Special.Trig 73import GSL.Special.Trig
75import GSL.Special.Zeta 74import 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.
80foreign 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
474int chol(KRMAT(x),RMAT(l)) { 474int 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
491int 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
492int fft(int code, KCVEC(X), CVEC(R)) { 509int 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
39int QR(KRMAT(x),RMAT(q),RMAT(r)); 39int QR(KRMAT(x),RMAT(q),RMAT(r));
40 40
41int chol(KRMAT(x),RMAT(l)); 41int cholR(KRMAT(x),RMAT(l));
42
43int cholC(KCMAT(x),CMAT(l));
42 44
43int fft(int code, KCVEC(a), CVEC(b)); 45int 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)
8Stability : provisional 8Stability : provisional
9Portability : uses ffi 9Portability : uses ffi
10 10
11Some linear algebra algorithms, implemented by means of BLAS, LAPACK or GSL. 11Basic matrix computations implemented by BLAS, LAPACK and GSL.
12 12
13-} 13-}
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15module LinearAlgebra ( 15module 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
27import LinearAlgebra.Linear
28import LinearAlgebra.Instances 24import LinearAlgebra.Instances
29import LinearAlgebra.Interface 25import LinearAlgebra.Interface
30import LinearAlgebra.Algorithms 26import LinearAlgebra.Algorithms
31import LAPACK
32import GSL.Matrix
33import Data.Packed.Matrix 27import Data.Packed.Matrix
34import Data.Packed.Vector 28import Data.Packed.Vector
35import Complex \ No newline at end of file 29import 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)
9Stability : provisional 9Stability : provisional
10Portability : uses ffi 10Portability : uses ffi
11 11
12A simple interface to the available matrix computations. We have defined some generic functions
13on both real and complex matrices in such a way that higher level algorithms and
14testing properties are valid for both base types.
15
16In any case the specific functions for particular base types can also be explicitly
17imported from the LAPACK and GSL.Matrix modules.
12 18
13-} 19-}
14----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
15 21
16module LinearAlgebra.Algorithms ( 22module 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
28import Data.Packed.Internal 52import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj)
29import Data.Packed.Matrix 53import Data.Packed.Matrix
30import GSL.Matrix 54import GSL.Matrix
31import GSL.Vector 55import GSL.Vector
@@ -33,7 +57,8 @@ import LAPACK
33import Complex 57import Complex
34import LinearAlgebra.Linear 58import LinearAlgebra.Linear
35 59
36class (Linear Matrix t) => GMatrix t where 60-- | Base types for which some optimized matrix computations are available
61class (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
45instance GMatrix Double where 71instance 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
54instance GMatrix (Complex Double) where 81instance 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
63square m = rows m == cols m 91square m = rows m == cols m
64 92
65det :: GMatrix t => Matrix t -> t 93det :: Optimized t => Matrix t -> t
66det m | square m = s * (product $ toList $ takeDiag $ u) 94det 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
70inv :: GMatrix t => Matrix t -> Matrix t 98inv :: Optimized t => Matrix t -> Matrix t
71inv m | square m = m `linearSolve` ident (rows m) 99inv m | square m = m `linearSolve` ident (rows m)
72 | otherwise = error "inv of nonsquare matrix" 100 | otherwise = error "inv of nonsquare matrix"
73 101
74pinv :: GMatrix t => Matrix t -> Matrix t 102pinv :: Optimized t => Matrix t -> Matrix t
75pinv m = linearSolveSVD m (ident (rows m)) 103pinv 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
122mXm :: (Num t, Field t) => Matrix t -> Matrix t -> Matrix t 150mXm :: (Num t, Optimized t) => Matrix t -> Matrix t -> Matrix t
123mXm = multiply 151mXm = multiply
124 152
125-- | matrix - vector product 153-- | matrix - vector product
126mXv :: (Num t, Field t) => Matrix t -> Vector t -> Vector t 154mXv :: (Num t, Optimized t) => Matrix t -> Vector t -> Vector t
127mXv m v = flatten $ m `mXm` (asColumn v) 155mXv m v = flatten $ m `mXm` (asColumn v)
128 156
129-- | vector - matrix product 157-- | vector - matrix product
130vXm :: (Num t, Field t) => Vector t -> Matrix t -> Vector t 158vXm :: (Num t, Optimized t) => Vector t -> Matrix t -> Vector t
131vXm v m = flatten $ (asRow v) `mXm` m 159vXm 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
177class Normed t where 205class 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.
197nullspacePrec :: GMatrix t 225nullspacePrec :: 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@).
210nullVector :: GMatrix t => Matrix t -> Vector t 238nullVector :: Optimized t => Matrix t -> Vector t
211nullVector = last . nullspacePrec 1 239nullVector = 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
16module LinearAlgebra.Linear ( 17module LinearAlgebra.Linear (
17 Linear(..), 18 Linear(..),
@@ -24,7 +25,7 @@ import Data.Packed.Matrix
24import GSL.Vector 25import GSL.Vector
25import Complex 26import Complex
26 27
27 28-- | basic optimized operations
28class (Field e) => Linear c e where 29class (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