diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-10-02 18:59:50 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-10-02 18:59:50 +0000 |
commit | 42bec1ac9911131b552f66779203eb599a86563d (patch) | |
tree | c4aefaedb21730644fcd4d2f85d830fe4d4daf07 /lib/Numeric/LinearAlgebra/Algorithms.hs | |
parent | d925bada507562250a75587c409bdb35bbbc6ed8 (diff) |
lapack real and complex unpacked QR
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Algorithms.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 3513b18..5492e07 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -38,17 +38,18 @@ module Numeric.LinearAlgebra.Algorithms ( | |||
38 | eps, i, | 38 | eps, i, |
39 | ctrans, | 39 | ctrans, |
40 | Normed(..), NormType(..), | 40 | Normed(..), NormType(..), |
41 | GenMat(linearSolveSVD,lu,eigSH') | 41 | GenMat(linearSolveSVD,lu,eigSH'), unpackQR |
42 | ) where | 42 | ) where |
43 | 43 | ||
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 Numeric.GSL.Matrix(luR,luC,qr) | 47 | import qualified Numeric.GSL.Matrix as GSL |
48 | import Numeric.GSL.Vector | 48 | import Numeric.GSL.Vector |
49 | import Numeric.LinearAlgebra.LAPACK as LAPACK | 49 | import Numeric.LinearAlgebra.LAPACK as LAPACK |
50 | import Complex | 50 | import Complex |
51 | import Numeric.LinearAlgebra.Linear | 51 | import Numeric.LinearAlgebra.Linear |
52 | import Data.List(foldl1') | ||
52 | 53 | ||
53 | -- | Auxiliary typeclass used to define generic computations for both real and complex matrices. | 54 | -- | Auxiliary typeclass used to define generic computations for both real and complex matrices. |
54 | class (Linear Matrix t) => GenMat t where | 55 | class (Linear Matrix t) => GenMat t where |
@@ -59,28 +60,31 @@ class (Linear Matrix t) => GenMat t where | |||
59 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) | 60 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) |
60 | eigSH' :: Matrix t -> (Vector Double, Matrix t) | 61 | eigSH' :: Matrix t -> (Vector Double, Matrix t) |
61 | cholSH :: Matrix t -> Matrix t | 62 | cholSH :: Matrix t -> Matrix t |
63 | qr :: Matrix t -> (Matrix t, Matrix t) | ||
62 | -- | conjugate transpose | 64 | -- | conjugate transpose |
63 | ctrans :: Matrix t -> Matrix t | 65 | ctrans :: Matrix t -> Matrix t |
64 | 66 | ||
65 | instance GenMat Double where | 67 | instance GenMat Double where |
66 | svd = svdR | 68 | svd = svdR |
67 | lu = luR | 69 | lu = GSL.luR |
68 | linearSolve = linearSolveR | 70 | linearSolve = linearSolveR |
69 | linearSolveSVD = linearSolveSVDR Nothing | 71 | linearSolveSVD = linearSolveSVDR Nothing |
70 | ctrans = trans | 72 | ctrans = trans |
71 | eig = eigR | 73 | eig = eigR |
72 | eigSH' = eigS | 74 | eigSH' = eigS |
73 | cholSH = cholS | 75 | cholSH = cholS |
76 | qr = GSL.unpackQR . qrR | ||
74 | 77 | ||
75 | instance GenMat (Complex Double) where | 78 | instance GenMat (Complex Double) where |
76 | svd = svdC | 79 | svd = svdC |
77 | lu = luC | 80 | lu = GSL.luC |
78 | linearSolve = linearSolveC | 81 | linearSolve = linearSolveC |
79 | linearSolveSVD = linearSolveSVDC Nothing | 82 | linearSolveSVD = linearSolveSVDC Nothing |
80 | ctrans = conjTrans | 83 | ctrans = conjTrans |
81 | eig = eigC | 84 | eig = eigC |
82 | eigSH' = eigH | 85 | eigSH' = eigH |
83 | cholSH = cholH | 86 | cholSH = cholH |
87 | qr = unpackQR . qrC | ||
84 | 88 | ||
85 | -- | eigensystem of complex hermitian or real symmetric matrix | 89 | -- | eigensystem of complex hermitian or real symmetric matrix |
86 | eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t) | 90 | eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t) |
@@ -92,9 +96,6 @@ chol :: GenMat t => Matrix t -> Matrix t | |||
92 | chol m | m `equal` ctrans m = cholSH m | 96 | chol m | m `equal` ctrans m = cholSH m |
93 | | otherwise = error "chol requires positive definite complex hermitian or real symmetric matrix" | 97 | | otherwise = error "chol requires positive definite complex hermitian or real symmetric matrix" |
94 | 98 | ||
95 | qr :: Matrix Double -> (Matrix Double, Matrix Double) | ||
96 | qr = Numeric.GSL.Matrix.qr | ||
97 | |||
98 | square m = rows m == cols m | 99 | square m = rows m == cols m |
99 | 100 | ||
100 | det :: GenMat t => Matrix t -> t | 101 | det :: GenMat t => Matrix t -> t |
@@ -257,3 +258,26 @@ pinvTol t m = v' `mXm` diag s' `mXm` trans u' where | |||
257 | d = dim s | 258 | d = dim s |
258 | u' = takeColumns d u | 259 | u' = takeColumns d u |
259 | v' = takeColumns d v | 260 | v' = takeColumns d v |
261 | |||
262 | --------------------------------------------------------------------- | ||
263 | |||
264 | -- many thanks, quickcheck! | ||
265 | |||
266 | haussholder tau v = ident (dim v) `sub` (tau `scale` (w `mXm` ctrans w)) | ||
267 | where w = asColumn v | ||
268 | |||
269 | unpackQR (pq, tau) = (q,r) | ||
270 | where cs = toColumns pq | ||
271 | m = rows pq | ||
272 | n = cols pq | ||
273 | mn = min m n | ||
274 | r = fromColumns $ zipWith zt ([m-1, m-2 .. 1] ++ repeat 0) cs | ||
275 | vs = zipWith zh [1..mn] cs | ||
276 | hs = zipWith haussholder (toList tau) vs | ||
277 | q = foldl1' mXm hs | ||
278 | |||
279 | zh k v = fromList $ replicate (k-1) 0 ++ (1:drop k xs) | ||
280 | where xs = toList v | ||
281 | |||
282 | zt 0 v = v | ||
283 | zt k v = join [subVector 0 (dim v - k) v, constant 0 k] | ||