diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-29 10:45:19 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-29 10:45:19 +0000 |
commit | 59e449d624d5313660848dd0e58fe95dc482f9ca (patch) | |
tree | 9d7581f6504ffbe1bf28fe45ac89a3e5a6c71a4d /lib | |
parent | 3815bc25f62124063e02af83fe3c907336dc86f5 (diff) |
LAPACK cholesky
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Internal/Common.hs | 1 | ||||
-rw-r--r-- | lib/LAPACK.hs | 25 | ||||
-rw-r--r-- | lib/LAPACK/lapack-aux.c | 42 | ||||
-rw-r--r-- | lib/LAPACK/lapack-aux.h | 4 | ||||
-rw-r--r-- | lib/LinearAlgebra/Algorithms.hs | 6 |
5 files changed, 75 insertions, 3 deletions
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs index 218fb6b..75a5b1e 100644 --- a/lib/Data/Packed/Internal/Common.hs +++ b/lib/Data/Packed/Internal/Common.hs | |||
@@ -69,6 +69,7 @@ errorCode 2002 = "memory problem" | |||
69 | errorCode 2003 = "bad file" | 69 | errorCode 2003 = "bad file" |
70 | errorCode 2004 = "singular" | 70 | errorCode 2004 = "singular" |
71 | errorCode 2005 = "didn't converge" | 71 | errorCode 2005 = "didn't converge" |
72 | errorCode 2006 = "the input matrix is not positive definite" | ||
72 | errorCode n = "code "++show n | 73 | errorCode n = "code "++show n |
73 | 74 | ||
74 | {- | conversion of Haskell functions into function pointers that can be used in the C side | 75 | {- | conversion of Haskell functions into function pointers that can be used in the C side |
diff --git a/lib/LAPACK.hs b/lib/LAPACK.hs index 54eea8a..e84647b 100644 --- a/lib/LAPACK.hs +++ b/lib/LAPACK.hs | |||
@@ -19,6 +19,7 @@ module LAPACK ( | |||
19 | linearSolveR, linearSolveC, | 19 | linearSolveR, linearSolveC, |
20 | linearSolveLSR, linearSolveLSC, | 20 | linearSolveLSR, linearSolveLSC, |
21 | linearSolveSVDR, linearSolveSVDC, | 21 | linearSolveSVDR, linearSolveSVDC, |
22 | cholS, cholH | ||
22 | ) where | 23 | ) where |
23 | 24 | ||
24 | import Data.Packed.Internal | 25 | import Data.Packed.Internal |
@@ -279,3 +280,27 @@ linearSolveSVDC_l rcond a b = unsafePerformIO $ do | |||
279 | n = cols a | 280 | n = cols a |
280 | nrhs = cols b | 281 | nrhs = cols b |
281 | 282 | ||
283 | ----------------------------------------------------------------------------------- | ||
284 | foreign import ccall "LAPACK/lapack-aux.h chol_l_H" zpotrf :: TCMCM | ||
285 | |||
286 | -- | Wrapper for LAPACK's /zpotrf/,which computes the Cholesky factorization of a | ||
287 | -- complex Hermitian positive definite matrix. | ||
288 | cholH :: Matrix (Complex Double) -> Matrix (Complex Double) | ||
289 | cholH a = unsafePerformIO $ do | ||
290 | r <- createMatrix ColumnMajor n n | ||
291 | zpotrf // mat fdat a // mat dat r // check "cholH" [fdat a] | ||
292 | return r | ||
293 | where n = rows a | ||
294 | |||
295 | ----------------------------------------------------------------------------------- | ||
296 | foreign import ccall "LAPACK/lapack-aux.h chol_l_S" dpotrf :: TMM | ||
297 | |||
298 | -- | Wrapper for LAPACK's /dpotrf/,which computes the Cholesky factorization of a | ||
299 | -- real symmetric positive definite matrix. | ||
300 | cholS :: Matrix Double -> Matrix Double | ||
301 | cholS a = unsafePerformIO $ do | ||
302 | r <- createMatrix ColumnMajor n n | ||
303 | dpotrf // mat fdat a // mat dat r // check "cholS" [fdat a] | ||
304 | return r | ||
305 | where n = rows a | ||
306 | |||
diff --git a/lib/LAPACK/lapack-aux.c b/lib/LAPACK/lapack-aux.c index c7d6d86..5781cd1 100644 --- a/lib/LAPACK/lapack-aux.c +++ b/lib/LAPACK/lapack-aux.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #define BAD_FILE 2003 | 28 | #define BAD_FILE 2003 |
29 | #define SINGULAR 2004 | 29 | #define SINGULAR 2004 |
30 | #define NOCONVER 2005 | 30 | #define NOCONVER 2005 |
31 | #define NODEFPOS 2006 | ||
31 | 32 | ||
32 | //////////////////// real svd //////////////////////////////////// | 33 | //////////////////// real svd //////////////////////////////////// |
33 | 34 | ||
@@ -577,3 +578,44 @@ int linearSolveSVDC_l(double rcond, KCMAT(a),KCMAT(b),CMAT(x)) { | |||
577 | free(AC); | 578 | free(AC); |
578 | OK | 579 | OK |
579 | } | 580 | } |
581 | |||
582 | //////////////////// Cholesky factorization ///////////////////////// | ||
583 | |||
584 | int chol_l_H(KCMAT(a),CMAT(l)) { | ||
585 | integer n = ar; | ||
586 | REQUIRES(n>=1 && ac == n && lr==n && lc==n,BAD_SIZE); | ||
587 | DEBUGMSG("chol_l_H"); | ||
588 | memcpy(lp,ap,n*n*sizeof(doublecomplex)); | ||
589 | char uplo = 'U'; | ||
590 | integer res; | ||
591 | zpotrf_ (&uplo,&n,(doublecomplex*)lp,&n,&res); | ||
592 | CHECK(res>0,NODEFPOS); | ||
593 | CHECK(res,res); | ||
594 | doublecomplex zero = {0.,0.}; | ||
595 | int r,c; | ||
596 | for (r=0; r<lr-1; r++) { | ||
597 | for(c=r+1; c<lc; c++) { | ||
598 | ((doublecomplex*)lp)[r*lc+c] = zero; | ||
599 | } | ||
600 | } | ||
601 | OK | ||
602 | } | ||
603 | |||
604 | int chol_l_S(KDMAT(a),DMAT(l)) { | ||
605 | integer n = ar; | ||
606 | REQUIRES(n>=1 && ac == n && lr==n && lc==n,BAD_SIZE); | ||
607 | DEBUGMSG("chol_l_S"); | ||
608 | memcpy(lp,ap,n*n*sizeof(double)); | ||
609 | char uplo = 'U'; | ||
610 | integer res; | ||
611 | dpotrf_ (&uplo,&n,lp,&n,&res); | ||
612 | CHECK(res>0,NODEFPOS); | ||
613 | CHECK(res,res); | ||
614 | int r,c; | ||
615 | for (r=0; r<lr-1; r++) { | ||
616 | for(c=r+1; c<lc; c++) { | ||
617 | lp[r*lc+c] = 0.; | ||
618 | } | ||
619 | } | ||
620 | OK | ||
621 | } | ||
diff --git a/lib/LAPACK/lapack-aux.h b/lib/LAPACK/lapack-aux.h index 869eabb..c5bf3cc 100644 --- a/lib/LAPACK/lapack-aux.h +++ b/lib/LAPACK/lapack-aux.h | |||
@@ -36,3 +36,7 @@ int linearSolveLSC_l(KCMAT(a),KCMAT(b),CMAT(x)); | |||
36 | int linearSolveSVDR_l(double,KDMAT(a),KDMAT(b),DMAT(x)); | 36 | int linearSolveSVDR_l(double,KDMAT(a),KDMAT(b),DMAT(x)); |
37 | 37 | ||
38 | int linearSolveSVDC_l(double,KCMAT(a),KCMAT(b),CMAT(x)); | 38 | int linearSolveSVDC_l(double,KCMAT(a),KCMAT(b),CMAT(x)); |
39 | |||
40 | int chol_l_H(KCMAT(a),CMAT(r)); | ||
41 | |||
42 | int chol_l_S(KDMAT(a),DMAT(r)); | ||
diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index a67f822..007067f 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs | |||
@@ -44,7 +44,7 @@ module LinearAlgebra.Algorithms ( | |||
44 | 44 | ||
45 | import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj) | 45 | import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj) |
46 | import Data.Packed | 46 | import Data.Packed |
47 | import GSL.Matrix | 47 | import GSL.Matrix(luR,luC,qr) |
48 | import GSL.Vector | 48 | import GSL.Vector |
49 | import LAPACK | 49 | import LAPACK |
50 | import Complex | 50 | import Complex |
@@ -70,7 +70,7 @@ instance GenMat Double where | |||
70 | ctrans = trans | 70 | ctrans = trans |
71 | eig = eigR | 71 | eig = eigR |
72 | eigSH = LAPACK.eigS | 72 | eigSH = LAPACK.eigS |
73 | chol = cholR | 73 | chol = cholS |
74 | 74 | ||
75 | instance GenMat (Complex Double) where | 75 | instance GenMat (Complex Double) where |
76 | svd = svdC | 76 | svd = svdC |
@@ -80,7 +80,7 @@ instance GenMat (Complex Double) where | |||
80 | ctrans = conjTrans | 80 | ctrans = conjTrans |
81 | eig = eigC | 81 | eig = eigC |
82 | eigSH = LAPACK.eigH | 82 | eigSH = LAPACK.eigH |
83 | chol = error "cholC not yet implemented" -- waiting for GSL-1.10 | 83 | chol = cholH |
84 | 84 | ||
85 | -- | eigensystem of a symmetric matrix | 85 | -- | eigensystem of a symmetric matrix |
86 | eigS :: Matrix Double -> (Vector Double, Matrix Double) | 86 | eigS :: Matrix Double -> (Vector Double, Matrix Double) |