summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Algorithms.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-10-02 18:59:50 +0000
committerAlberto Ruiz <aruiz@um.es>2007-10-02 18:59:50 +0000
commit42bec1ac9911131b552f66779203eb599a86563d (patch)
treec4aefaedb21730644fcd4d2f85d830fe4d4daf07 /lib/Numeric/LinearAlgebra/Algorithms.hs
parentd925bada507562250a75587c409bdb35bbbc6ed8 (diff)
lapack real and complex unpacked QR
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Algorithms.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs38
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
45import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj) 45import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj)
46import Data.Packed 46import Data.Packed
47import Numeric.GSL.Matrix(luR,luC,qr) 47import qualified Numeric.GSL.Matrix as GSL
48import Numeric.GSL.Vector 48import Numeric.GSL.Vector
49import Numeric.LinearAlgebra.LAPACK as LAPACK 49import Numeric.LinearAlgebra.LAPACK as LAPACK
50import Complex 50import Complex
51import Numeric.LinearAlgebra.Linear 51import Numeric.LinearAlgebra.Linear
52import 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.
54class (Linear Matrix t) => GenMat t where 55class (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
65instance GenMat Double where 67instance 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
75instance GenMat (Complex Double) where 78instance 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
86eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t) 90eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t)
@@ -92,9 +96,6 @@ chol :: GenMat t => Matrix t -> Matrix t
92chol m | m `equal` ctrans m = cholSH m 96chol 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
95qr :: Matrix Double -> (Matrix Double, Matrix Double)
96qr = Numeric.GSL.Matrix.qr
97
98square m = rows m == cols m 99square m = rows m == cols m
99 100
100det :: GenMat t => Matrix t -> t 101det :: 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
266haussholder tau v = ident (dim v) `sub` (tau `scale` (w `mXm` ctrans w))
267 where w = asColumn v
268
269unpackQR (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]