diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/LinearAlgebra/Algorithms.hs | 36 | ||||
-rw-r--r-- | lib/LinearAlgebra/LAPACK.hs | 4 | ||||
-rw-r--r-- | lib/LinearAlgebra/Linear.hs | 7 |
3 files changed, 28 insertions, 19 deletions
diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index 481dbd6..682b17f 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs | |||
@@ -28,7 +28,7 @@ module LinearAlgebra.Algorithms ( | |||
28 | svd, | 28 | svd, |
29 | full, economy, | 29 | full, economy, |
30 | -- ** Eigensystems | 30 | -- ** Eigensystems |
31 | eig, LinearAlgebra.Algorithms.eigS, LinearAlgebra.Algorithms.eigH, | 31 | eig, eigSH, |
32 | -- ** Other | 32 | -- ** Other |
33 | LinearAlgebra.Algorithms.qr, chol, | 33 | LinearAlgebra.Algorithms.qr, chol, |
34 | -- * Nullspace | 34 | -- * Nullspace |
@@ -38,7 +38,7 @@ module 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') |
42 | ) where | 42 | ) where |
43 | 43 | ||
44 | 44 | ||
@@ -50,15 +50,15 @@ import LinearAlgebra.LAPACK as LAPACK | |||
50 | import Complex | 50 | import Complex |
51 | import LinearAlgebra.Linear | 51 | import LinearAlgebra.Linear |
52 | 52 | ||
53 | -- | matrix computations available for both real and complex matrices | 53 | -- | Auxiliary typeclass used to define generic computations for both real and complex matrices. |
54 | class (Linear Matrix t) => GenMat t where | 54 | class (Linear Matrix t) => GenMat t where |
55 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) | 55 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) |
56 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) | 56 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) |
57 | linearSolve :: Matrix t -> Matrix t -> Matrix t | 57 | linearSolve :: Matrix t -> Matrix t -> Matrix t |
58 | linearSolveSVD :: Matrix t -> Matrix t -> Matrix t | 58 | linearSolveSVD :: Matrix t -> Matrix t -> Matrix t |
59 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) | 59 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) |
60 | eigSH :: Matrix t -> (Vector Double, Matrix t) | 60 | eigSH' :: Matrix t -> (Vector Double, Matrix t) |
61 | chol :: Matrix t -> Matrix t | 61 | cholSH :: Matrix t -> Matrix t |
62 | -- | conjugate transpose | 62 | -- | conjugate transpose |
63 | ctrans :: Matrix t -> Matrix t | 63 | ctrans :: Matrix t -> Matrix t |
64 | 64 | ||
@@ -69,8 +69,8 @@ instance GenMat Double where | |||
69 | linearSolveSVD = linearSolveSVDR Nothing | 69 | linearSolveSVD = linearSolveSVDR Nothing |
70 | ctrans = trans | 70 | ctrans = trans |
71 | eig = eigR | 71 | eig = eigR |
72 | eigSH = LAPACK.eigS | 72 | eigSH' = eigS |
73 | chol = cholS | 73 | cholSH = cholS |
74 | 74 | ||
75 | instance GenMat (Complex Double) where | 75 | instance GenMat (Complex Double) where |
76 | svd = svdC | 76 | svd = svdC |
@@ -79,16 +79,18 @@ instance GenMat (Complex Double) where | |||
79 | linearSolveSVD = linearSolveSVDC Nothing | 79 | linearSolveSVD = linearSolveSVDC Nothing |
80 | ctrans = conjTrans | 80 | ctrans = conjTrans |
81 | eig = eigC | 81 | eig = eigC |
82 | eigSH = LAPACK.eigH | 82 | eigSH' = eigH |
83 | chol = cholH | 83 | cholSH = cholH |
84 | 84 | ||
85 | -- | eigensystem of a symmetric matrix | 85 | -- | eigensystem of complex hermitian or real symmetric matrix |
86 | eigS :: Matrix Double -> (Vector Double, Matrix Double) | 86 | eigSH :: GenMat t => Matrix t -> (Vector Double, Matrix t) |
87 | eigS = LAPACK.eigS | 87 | eigSH m | m `equal` ctrans m = eigSH' m |
88 | 88 | | otherwise = error "eigSH requires complex hermitian or real symmetric matrix" | |
89 | -- | eigensystem of a hermitian matrix | 89 | |
90 | eigH :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) | 90 | -- | Cholesky factorization of a positive definite hermitian or symmetric matrix |
91 | eigH = LAPACK.eigH | 91 | chol :: GenMat t => Matrix t -> Matrix t |
92 | chol m | m `equal` ctrans m = cholSH m | ||
93 | | otherwise = error "chol requires positive definite complex hermitian or real symmetric matrix" | ||
92 | 94 | ||
93 | qr :: Matrix Double -> (Matrix Double, Matrix Double) | 95 | qr :: Matrix Double -> (Matrix Double, Matrix Double) |
94 | qr = GSL.Matrix.qr | 96 | qr = GSL.Matrix.qr |
diff --git a/lib/LinearAlgebra/LAPACK.hs b/lib/LinearAlgebra/LAPACK.hs index 2861061..c6908e3 100644 --- a/lib/LinearAlgebra/LAPACK.hs +++ b/lib/LinearAlgebra/LAPACK.hs | |||
@@ -15,7 +15,7 @@ | |||
15 | 15 | ||
16 | module LinearAlgebra.LAPACK ( | 16 | module LinearAlgebra.LAPACK ( |
17 | svdR, svdRdd, svdC, | 17 | svdR, svdRdd, svdC, |
18 | eigC, eigR, eigS, eigH, | 18 | eigC, eigR, eigS, eigH, eigS', eigH', |
19 | linearSolveR, linearSolveC, | 19 | linearSolveR, linearSolveC, |
20 | linearSolveLSR, linearSolveLSC, | 20 | linearSolveLSR, linearSolveLSC, |
21 | linearSolveSVDR, linearSolveSVDC, | 21 | linearSolveSVDR, linearSolveSVDC, |
@@ -166,7 +166,7 @@ foreign import ccall "LAPACK/lapack-aux.h eig_l_H" zheev :: TCMVCM | |||
166 | -- if @(l,v)=eigH m@ then @m \<\> s v = v \<\> diag l@. | 166 | -- if @(l,v)=eigH m@ then @m \<\> s v = v \<\> diag l@. |
167 | -- | 167 | -- |
168 | -- The eigenvectors are the columns of v. | 168 | -- The eigenvectors are the columns of v. |
169 | -- The eigenvalues are sorted in descending order. | 169 | -- The eigenvalues are sorted in descending order (use eigH' for ascending order). |
170 | eigH :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) | 170 | eigH :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) |
171 | eigH m = (s', fliprl v) | 171 | eigH m = (s', fliprl v) |
172 | where (s,v) = eigH' m | 172 | where (s,v) = eigH' m |
diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs index 3e6c55d..7cd1a52 100644 --- a/lib/LinearAlgebra/Linear.hs +++ b/lib/LinearAlgebra/Linear.hs | |||
@@ -37,6 +37,8 @@ class (Container c e) => Linear c e where | |||
37 | divide :: c e -> c e -> c e | 37 | divide :: c e -> c e -> c e |
38 | -- | scale the element by element reciprocal of the object: @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ | 38 | -- | scale the element by element reciprocal of the object: @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ |
39 | scaleRecip :: e -> c e -> c e | 39 | scaleRecip :: e -> c e -> c e |
40 | equal :: c e -> c e -> Bool | ||
41 | -- numequal :: Double -> c e -> c e -> Bool | ||
40 | 42 | ||
41 | instance Linear Vector Double where | 43 | instance Linear Vector Double where |
42 | scale = vectorMapValR Scale | 44 | scale = vectorMapValR Scale |
@@ -46,6 +48,7 @@ instance Linear Vector Double where | |||
46 | sub = vectorZipR Sub | 48 | sub = vectorZipR Sub |
47 | mul = vectorZipR Mul | 49 | mul = vectorZipR Mul |
48 | divide = vectorZipR Div | 50 | divide = vectorZipR Div |
51 | equal u v = dim u == dim v && vectorMax (vectorMapR Abs (sub u v)) == 0.0 | ||
49 | 52 | ||
50 | instance Linear Vector (Complex Double) where | 53 | instance Linear Vector (Complex Double) where |
51 | scale = vectorMapValC Scale | 54 | scale = vectorMapValC Scale |
@@ -55,6 +58,7 @@ instance Linear Vector (Complex Double) where | |||
55 | sub = vectorZipC Sub | 58 | sub = vectorZipC Sub |
56 | mul = vectorZipC Mul | 59 | mul = vectorZipC Mul |
57 | divide = vectorZipC Div | 60 | divide = vectorZipC Div |
61 | equal u v = dim u == dim v && vectorMax (liftVector magnitude (sub u v)) == 0.0 | ||
58 | 62 | ||
59 | instance Linear Matrix Double where | 63 | instance Linear Matrix Double where |
60 | scale x = liftMatrix (scale x) | 64 | scale x = liftMatrix (scale x) |
@@ -64,6 +68,8 @@ instance Linear Matrix Double where | |||
64 | sub = liftMatrix2 sub | 68 | sub = liftMatrix2 sub |
65 | mul = liftMatrix2 mul | 69 | mul = liftMatrix2 mul |
66 | divide = liftMatrix2 divide | 70 | divide = liftMatrix2 divide |
71 | equal a b = cols a == cols b && cdat a `equal` cdat b | ||
72 | |||
67 | 73 | ||
68 | instance Linear Matrix (Complex Double) where | 74 | instance Linear Matrix (Complex Double) where |
69 | scale x = liftMatrix (scale x) | 75 | scale x = liftMatrix (scale x) |
@@ -73,6 +79,7 @@ instance Linear Matrix (Complex Double) where | |||
73 | sub = liftMatrix2 sub | 79 | sub = liftMatrix2 sub |
74 | mul = liftMatrix2 mul | 80 | mul = liftMatrix2 mul |
75 | divide = liftMatrix2 divide | 81 | divide = liftMatrix2 divide |
82 | equal a b = cols a == cols b && cdat a `equal` cdat b | ||
76 | 83 | ||
77 | -------------------------------------------------- | 84 | -------------------------------------------------- |
78 | 85 | ||