summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Algorithms.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Algorithms.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs38
1 files changed, 32 insertions, 6 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index 84c399a..1345975 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -33,6 +33,8 @@ module Numeric.LinearAlgebra.Algorithms (
33 qr, 33 qr,
34-- ** Cholesky 34-- ** Cholesky
35 chol, 35 chol,
36-- ** Hessenberg
37 hess,
36-- * Nullspace 38-- * Nullspace
37 nullspacePrec, 39 nullspacePrec,
38 nullVector, 40 nullVector,
@@ -42,7 +44,9 @@ module Numeric.LinearAlgebra.Algorithms (
42 ctrans, 44 ctrans,
43 eps, i, 45 eps, i,
44-- * Util 46-- * Util
45 GenMat(linearSolveSVD,lu,eigSH',cholSH), unpackQR, haussholder 47 GenMat(linearSolveSVD,lu,eigSH',cholSH),
48 haussholder,
49 unpackQR, unpackHess
46) where 50) where
47 51
48 52
@@ -64,7 +68,8 @@ class (Linear Matrix t) => GenMat t where
64 -- See also other versions of linearSolve in "Numeric.LinearAlgebra.LAPACK". 68 -- See also other versions of linearSolve in "Numeric.LinearAlgebra.LAPACK".
65 linearSolve :: Matrix t -> Matrix t -> Matrix t 69 linearSolve :: Matrix t -> Matrix t -> Matrix t
66 linearSolveSVD :: Matrix t -> Matrix t -> Matrix t 70 linearSolveSVD :: Matrix t -> Matrix t -> Matrix t
67 -- | Eigenvalues and eigenvectors of a general square matrix using lapack's dgeev or zgeev. If @(s,v) = eig m@ then @m \<> v == v \<> diag s@ 71 -- | Eigenvalues and eigenvectors of a general square matrix using lapack's dgeev or zgeev.
72 -- If @(s,v) = eig m@ then @m \<> v == v \<> diag s@
68 eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) 73 eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double))
69 -- | Similar to eigSH without checking that the input matrix is hermitian or symmetric. 74 -- | Similar to eigSH without checking that the input matrix is hermitian or symmetric.
70 eigSH' :: Matrix t -> (Vector Double, Matrix t) 75 eigSH' :: Matrix t -> (Vector Double, Matrix t)
@@ -72,6 +77,8 @@ class (Linear Matrix t) => GenMat t where
72 cholSH :: Matrix t -> Matrix t 77 cholSH :: Matrix t -> Matrix t
73 -- | QR factorization using lapack's dgeqr2 or zgeqr2. 78 -- | QR factorization using lapack's dgeqr2 or zgeqr2.
74 qr :: Matrix t -> (Matrix t, Matrix t) 79 qr :: Matrix t -> (Matrix t, Matrix t)
80 -- | Hessenberg factorization using lapack's dgehrd or zgehrd.
81 hess :: Matrix t -> (Matrix t, Matrix t)
75 -- | Conjugate transpose. 82 -- | Conjugate transpose.
76 ctrans :: Matrix t -> Matrix t 83 ctrans :: Matrix t -> Matrix t
77 84
@@ -85,6 +92,7 @@ instance GenMat Double where
85 eigSH' = eigS 92 eigSH' = eigS
86 cholSH = cholS 93 cholSH = cholS
87 qr = GSL.unpackQR . qrR 94 qr = GSL.unpackQR . qrR
95 hess = unpackHess hessR
88 96
89instance GenMat (Complex Double) where 97instance GenMat (Complex Double) where
90 svd = svdC 98 svd = svdC
@@ -96,6 +104,7 @@ instance GenMat (Complex Double) where
96 eigSH' = eigH 104 eigSH' = eigH
97 cholSH = cholH 105 cholSH = cholH
98 qr = unpackQR . qrC 106 qr = unpackQR . qrC
107 hess = unpackHess hessC
99 108
100-- | Eigenvalues and Eigenvectors of a complex hermitian or real symmetric matrix using lapack's dsyev or zheev. If @(s,v) = eigSH m@ then @m == v \<> diag s \<> ctrans v@ 109-- | Eigenvalues and Eigenvectors of a complex hermitian or real symmetric matrix using lapack's dsyev or zheev. If @(s,v) = eigSH m@ then @m == v \<> diag s \<> ctrans v@
101eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t) 110eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t)
@@ -277,6 +286,14 @@ haussholder :: (GenMat a) => a -> Vector a -> Matrix a
277haussholder tau v = ident (dim v) `sub` (tau `scale` (w `mXm` ctrans w)) 286haussholder tau v = ident (dim v) `sub` (tau `scale` (w `mXm` ctrans w))
278 where w = asColumn v 287 where w = asColumn v
279 288
289
290zh k v = fromList $ replicate (k-1) 0 ++ (1:drop k xs)
291 where xs = toList v
292
293zt 0 v = v
294zt k v = join [subVector 0 (dim v - k) v, constant 0 k]
295
296
280unpackQR :: (GenMat t) => (Matrix t, Vector t) -> (Matrix t, Matrix t) 297unpackQR :: (GenMat t) => (Matrix t, Vector t) -> (Matrix t, Matrix t)
281unpackQR (pq, tau) = (q,r) 298unpackQR (pq, tau) = (q,r)
282 where cs = toColumns pq 299 where cs = toColumns pq
@@ -288,8 +305,17 @@ unpackQR (pq, tau) = (q,r)
288 hs = zipWith haussholder (toList tau) vs 305 hs = zipWith haussholder (toList tau) vs
289 q = foldl1' mXm hs 306 q = foldl1' mXm hs
290 307
291 zh k v = fromList $ replicate (k-1) 0 ++ (1:drop k xs) 308unpackHess :: (GenMat t) => (Matrix t -> (Matrix t,Vector t)) -> Matrix t -> (Matrix t, Matrix t)
292 where xs = toList v 309unpackHess hf m
310 | rows m == 1 = ((1><1)[1],m)
311 | otherwise = (uH . hf) m
293 312
294 zt 0 v = v 313uH (pq, tau) = (p,h)
295 zt k v = join [subVector 0 (dim v - k) v, constant 0 k] 314 where cs = toColumns pq
315 m = rows pq
316 n = cols pq
317 mn = min m n
318 h = fromColumns $ zipWith zt ([m-2, m-3 .. 1] ++ repeat 0) cs
319 vs = zipWith zh [2..mn] cs
320 hs = zipWith haussholder (toList tau) vs
321 p = foldl1' mXm hs