diff options
author | Alberto Ruiz <aruiz@um.es> | 2009-12-28 15:47:26 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2009-12-28 15:47:26 +0000 |
commit | b2715e91d7aef5cee1b64b641b8f173167a7145a (patch) | |
tree | f97b82cfa435441f52153ccdfad5e1fa119f14dc /lib/Numeric/LinearAlgebra/LAPACK.hs | |
parent | 107478b2288b0904159599be94089230c7cd3edf (diff) |
additional svd functions
Diffstat (limited to 'lib/Numeric/LinearAlgebra/LAPACK.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK.hs | 225 |
1 files changed, 157 insertions, 68 deletions
diff --git a/lib/Numeric/LinearAlgebra/LAPACK.hs b/lib/Numeric/LinearAlgebra/LAPACK.hs index 30a3d3b..2eae9b7 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK.hs +++ b/lib/Numeric/LinearAlgebra/LAPACK.hs | |||
@@ -1,4 +1,3 @@ | |||
1 | {-# OPTIONS_GHC #-} | ||
2 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
3 | -- | | 2 | -- | |
4 | -- Module : Numeric.LinearAlgebra.LAPACK | 3 | -- Module : Numeric.LinearAlgebra.LAPACK |
@@ -9,21 +8,34 @@ | |||
9 | -- Stability : provisional | 8 | -- Stability : provisional |
10 | -- Portability : portable (uses FFI) | 9 | -- Portability : portable (uses FFI) |
11 | -- | 10 | -- |
12 | -- Wrappers for a few LAPACK functions (<http://www.netlib.org/lapack>). | 11 | -- Functional interface to selected LAPACK functions (<http://www.netlib.org/lapack>). |
13 | -- | 12 | -- |
14 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
15 | 14 | ||
16 | module Numeric.LinearAlgebra.LAPACK ( | 15 | module Numeric.LinearAlgebra.LAPACK ( |
16 | -- * Matrix product | ||
17 | multiplyR, multiplyC, | 17 | multiplyR, multiplyC, |
18 | svdR, svdRdd, svdC, | 18 | -- * Linear systems |
19 | eigC, eigR, eigS, eigH, eigS', eigH', | ||
20 | linearSolveR, linearSolveC, | 19 | linearSolveR, linearSolveC, |
20 | lusR, lusC, | ||
21 | linearSolveLSR, linearSolveLSC, | 21 | linearSolveLSR, linearSolveLSC, |
22 | linearSolveSVDR, linearSolveSVDC, | 22 | linearSolveSVDR, linearSolveSVDC, |
23 | luR, luC, lusR, lusC, | 23 | -- * SVD |
24 | svR, svRd, svC, svCd, | ||
25 | svdR, svdRd, svdC, svdCd, | ||
26 | thinSVDR, thinSVDRd, thinSVDC, thinSVDCd, | ||
27 | rightSVR, rightSVC, leftSVR, leftSVC, | ||
28 | -- * Eigensystems | ||
29 | eigR, eigC, eigS, eigS', eigH, eigH', | ||
30 | -- * LU | ||
31 | luR, luC, | ||
32 | -- * Cholesky | ||
24 | cholS, cholH, | 33 | cholS, cholH, |
34 | -- * QR | ||
25 | qrR, qrC, | 35 | qrR, qrC, |
36 | -- * Hessenberg | ||
26 | hessR, hessC, | 37 | hessR, hessC, |
38 | -- * Schur | ||
27 | schurR, schurC | 39 | schurR, schurC |
28 | ) where | 40 | ) where |
29 | 41 | ||
@@ -61,28 +73,27 @@ multiplyC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Compl | |||
61 | multiplyC a b = multiplyAux zgemmc "zgemmc" a b | 73 | multiplyC a b = multiplyAux zgemmc "zgemmc" a b |
62 | 74 | ||
63 | ----------------------------------------------------------------------------- | 75 | ----------------------------------------------------------------------------- |
64 | foreign import ccall "LAPACK/lapack-aux.h svd_l_R" dgesvd :: TMMVM | 76 | foreign import ccall "svd_l_R" dgesvd :: TMMVM |
65 | foreign import ccall "LAPACK/lapack-aux.h svd_l_C" zgesvd :: TCMCMVCM | 77 | foreign import ccall "svd_l_C" zgesvd :: TCMCMVCM |
66 | foreign import ccall "LAPACK/lapack-aux.h svd_l_Rdd" dgesdd :: TMMVM | 78 | foreign import ccall "svd_l_Rdd" dgesdd :: TMMVM |
79 | foreign import ccall "svd_l_Cdd" zgesdd :: TCMCMVCM | ||
67 | 80 | ||
68 | -- | Wrapper for LAPACK's /dgesvd/, which computes the full svd decomposition of a real matrix. | 81 | -- | Full SVD of a real matrix using LAPACK's /dgesvd/. |
69 | -- | ||
70 | -- @(u,s,v)=full svdR m@ so that @m=u \<\> s \<\> 'trans' v@. | ||
71 | svdR :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) | 82 | svdR :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) |
72 | svdR = svdAux dgesvd "svdR" . fmat | 83 | svdR = svdAux dgesvd "svdR" . fmat |
73 | 84 | ||
74 | -- | Wrapper for LAPACK's /dgesvd/, which computes the full svd decomposition of a real matrix. | 85 | -- | Full SVD of a real matrix using LAPACK's /dgesdd/. |
75 | -- | 86 | svdRd :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) |
76 | -- @(u,s,v)=full svdRdd m@ so that @m=u \<\> s \<\> 'trans' v@. | 87 | svdRd = svdAux dgesdd "svdRdd" . fmat |
77 | svdRdd :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) | ||
78 | svdRdd = svdAux dgesdd "svdRdd" . fmat | ||
79 | 88 | ||
80 | -- | Wrapper for LAPACK's /zgesvd/, which computes the full svd decomposition of a complex matrix. | 89 | -- | Full SVD of a complex matrix using LAPACK's /zgesvd/. |
81 | -- | ||
82 | -- @(u,s,v)=full svdC m@ so that @m=u \<\> comp s \<\> 'trans' v@. | ||
83 | svdC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double, Matrix (Complex Double)) | 90 | svdC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double, Matrix (Complex Double)) |
84 | svdC = svdAux zgesvd "svdC" . fmat | 91 | svdC = svdAux zgesvd "svdC" . fmat |
85 | 92 | ||
93 | -- | Full SVD of a complex matrix using LAPACK's /zgesdd/. | ||
94 | svdCd :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double, Matrix (Complex Double)) | ||
95 | svdCd = svdAux zgesdd "svdCdd" . fmat | ||
96 | |||
86 | svdAux f st x = unsafePerformIO $ do | 97 | svdAux f st x = unsafePerformIO $ do |
87 | u <- createMatrix ColumnMajor r r | 98 | u <- createMatrix ColumnMajor r r |
88 | s <- createVector (min r c) | 99 | s <- createVector (min r c) |
@@ -92,7 +103,104 @@ svdAux f st x = unsafePerformIO $ do | |||
92 | where r = rows x | 103 | where r = rows x |
93 | c = cols x | 104 | c = cols x |
94 | 105 | ||
106 | |||
107 | -- | Thin SVD of a real matrix, using LAPACK's /dgesvd/ with jobu == jobvt == \'S\'. | ||
108 | thinSVDR :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) | ||
109 | thinSVDR = thinSVDAux dgesvd "thinSVDR" . fmat | ||
110 | |||
111 | -- | Thin SVD of a complex matrix, using LAPACK's /zgesvd/ with jobu == jobvt == \'S\'. | ||
112 | thinSVDC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double, Matrix (Complex Double)) | ||
113 | thinSVDC = thinSVDAux zgesvd "thinSVDC" . fmat | ||
114 | |||
115 | -- | Thin SVD of a real matrix, using LAPACK's /dgesdd/ with jobz == \'S\'. | ||
116 | thinSVDRd :: Matrix Double -> (Matrix Double, Vector Double, Matrix Double) | ||
117 | thinSVDRd = thinSVDAux dgesdd "thinSVDRdd" . fmat | ||
118 | |||
119 | -- | Thin SVD of a complex matrix, using LAPACK's /zgesdd/ with jobz == \'S\'. | ||
120 | thinSVDCd :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double, Matrix (Complex Double)) | ||
121 | thinSVDCd = thinSVDAux zgesdd "thinSVDCdd" . fmat | ||
122 | |||
123 | thinSVDAux f st x = unsafePerformIO $ do | ||
124 | u <- createMatrix ColumnMajor r q | ||
125 | s <- createVector q | ||
126 | v <- createMatrix ColumnMajor q c | ||
127 | app4 f mat x mat u vec s mat v st | ||
128 | return (u,s,trans v) | ||
129 | where r = rows x | ||
130 | c = cols x | ||
131 | q = min r c | ||
132 | |||
133 | |||
134 | -- | Singular values of a real matrix, using LAPACK's /dgesvd/ with jobu == jobvt == \'N\'. | ||
135 | svR :: Matrix Double -> Vector Double | ||
136 | svR = svAux dgesvd "svR" . fmat | ||
137 | |||
138 | -- | Singular values of a complex matrix, using LAPACK's /zgesvd/ with jobu == jobvt == \'N\'. | ||
139 | svC :: Matrix (Complex Double) -> Vector Double | ||
140 | svC = svAux zgesvd "svC" . fmat | ||
141 | |||
142 | -- | Singular values of a real matrix, using LAPACK's /dgesdd/ with jobz == \'N\'. | ||
143 | svRd :: Matrix Double -> Vector Double | ||
144 | svRd = svAux dgesdd "svRd" . fmat | ||
145 | |||
146 | -- | Singular values of a complex matrix, using LAPACK's /zgesdd/ with jobz == \'N\'. | ||
147 | svCd :: Matrix (Complex Double) -> Vector Double | ||
148 | svCd = svAux zgesdd "svCd" . fmat | ||
149 | |||
150 | svAux f st x = unsafePerformIO $ do | ||
151 | s <- createVector q | ||
152 | app2 g mat x vec s st | ||
153 | return s | ||
154 | where r = rows x | ||
155 | c = cols x | ||
156 | q = min r c | ||
157 | g ra ca pa nb pb = f ra ca pa 0 0 nullPtr nb pb 0 0 nullPtr | ||
158 | |||
159 | |||
160 | -- | Singular values and all right singular vectors of a real matrix, using LAPACK's /dgesvd/ with jobu == \'N\' and jobvt == \'A\'. | ||
161 | rightSVR :: Matrix Double -> (Vector Double, Matrix Double) | ||
162 | rightSVR = rightSVAux dgesvd "rightSVR" . fmat | ||
163 | |||
164 | -- | Singular values and all right singular vectors of a complex matrix, using LAPACK's /zgesvd/ with jobu == \'N\' and jobvt == \'A\'. | ||
165 | rightSVC :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) | ||
166 | rightSVC = rightSVAux zgesvd "rightSVC" . fmat | ||
167 | |||
168 | rightSVAux f st x = unsafePerformIO $ do | ||
169 | s <- createVector q | ||
170 | v <- createMatrix ColumnMajor c c | ||
171 | app3 g mat x vec s mat v st | ||
172 | return (s,trans v) | ||
173 | where r = rows x | ||
174 | c = cols x | ||
175 | q = min r c | ||
176 | g ra ca pa = f ra ca pa 0 0 nullPtr | ||
177 | |||
178 | |||
179 | -- | Singular values and all left singular vectors of a real matrix, using LAPACK's /dgesvd/ with jobu == \'A\' and jobvt == \'N\'. | ||
180 | leftSVR :: Matrix Double -> (Matrix Double, Vector Double) | ||
181 | leftSVR = leftSVAux dgesvd "leftSVR" . fmat | ||
182 | |||
183 | -- | Singular values and all left singular vectors of a complex matrix, using LAPACK's /zgesvd/ with jobu == \'A\' and jobvt == \'N\'. | ||
184 | leftSVC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector Double) | ||
185 | leftSVC = leftSVAux zgesvd "leftSVC" . fmat | ||
186 | |||
187 | leftSVAux f st x = unsafePerformIO $ do | ||
188 | u <- createMatrix ColumnMajor r r | ||
189 | s <- createVector q | ||
190 | app3 g mat x mat u vec s st | ||
191 | return (u,s) | ||
192 | where r = rows x | ||
193 | c = cols x | ||
194 | q = min r c | ||
195 | g ra ca pa ru cu pu nb pb = f ra ca pa ru cu pu nb pb 0 0 nullPtr | ||
196 | |||
95 | ----------------------------------------------------------------------------- | 197 | ----------------------------------------------------------------------------- |
198 | |||
199 | foreign import ccall "LAPACK/lapack-aux.h eig_l_R" dgeev :: TMMCVM | ||
200 | foreign import ccall "LAPACK/lapack-aux.h eig_l_C" zgeev :: TCMCMCVCM | ||
201 | foreign import ccall "LAPACK/lapack-aux.h eig_l_S" dsyev :: TMVM | ||
202 | foreign import ccall "LAPACK/lapack-aux.h eig_l_H" zheev :: TCMVCM | ||
203 | |||
96 | eigAux f st m | 204 | eigAux f st m |
97 | | r == 1 = (fromList [flatten m `at` 0], singleton 1) | 205 | | r == 1 = (fromList [flatten m `at` 0], singleton 1) |
98 | | otherwise = unsafePerformIO $ do | 206 | | otherwise = unsafePerformIO $ do |
@@ -104,28 +212,13 @@ eigAux f st m | |||
104 | where r = rows m | 212 | where r = rows m |
105 | 213 | ||
106 | 214 | ||
107 | foreign import ccall "LAPACK/lapack-aux.h eig_l_C" zgeev :: TCMCMCVCM | 215 | -- | Eigenvalues and right eigenvectors of a general complex matrix, using LAPACK's /zgeev/. |
108 | foreign import ccall "LAPACK/lapack-aux.h eig_l_R" dgeev :: TMMCVM | 216 | -- The eigenvectors are the columns of v. The eigenvalues are not sorted. |
109 | foreign import ccall "LAPACK/lapack-aux.h eig_l_S" dsyev :: TMVM | ||
110 | foreign import ccall "LAPACK/lapack-aux.h eig_l_H" zheev :: TCMVCM | ||
111 | |||
112 | -- | Wrapper for LAPACK's /zgeev/, which computes the eigenvalues and right eigenvectors of a general complex matrix: | ||
113 | -- | ||
114 | -- if @(l,v)=eigC m@ then @m \<\> v = v \<\> diag l@. | ||
115 | -- | ||
116 | -- The eigenvectors are the columns of v. | ||
117 | -- The eigenvalues are not sorted. | ||
118 | eigC :: Matrix (Complex Double) -> (Vector (Complex Double), Matrix (Complex Double)) | 217 | eigC :: Matrix (Complex Double) -> (Vector (Complex Double), Matrix (Complex Double)) |
119 | eigC = eigAux zgeev "eigC" . fmat | 218 | eigC = eigAux zgeev "eigC" . fmat |
120 | 219 | ||
121 | ----------------------------------------------------------------------------- | 220 | -- | Eigenvalues and right eigenvectors of a general real matrix, using LAPACK's /dgeev/. |
122 | 221 | -- The eigenvectors are the columns of v. The eigenvalues are not sorted. | |
123 | -- | Wrapper for LAPACK's /dgeev/, which computes the eigenvalues and right eigenvectors of a general real matrix: | ||
124 | -- | ||
125 | -- if @(l,v)=eigR m@ then @m \<\> v = v \<\> diag l@. | ||
126 | -- | ||
127 | -- The eigenvectors are the columns of v. | ||
128 | -- The eigenvalues are not sorted. | ||
129 | eigR :: Matrix Double -> (Vector (Complex Double), Matrix (Complex Double)) | 222 | eigR :: Matrix Double -> (Vector (Complex Double), Matrix (Complex Double)) |
130 | eigR m = (s', v'') | 223 | eigR m = (s', v'') |
131 | where (s,v) = eigRaux (fmat m) | 224 | where (s,v) = eigRaux (fmat m) |
@@ -155,17 +248,16 @@ fixeig _ _ = error "fixeig with impossible inputs" | |||
155 | 248 | ||
156 | ----------------------------------------------------------------------------- | 249 | ----------------------------------------------------------------------------- |
157 | 250 | ||
158 | -- | Wrapper for LAPACK's /dsyev/, which computes the eigenvalues and right eigenvectors of a symmetric real matrix: | 251 | -- | Eigenvalues and right eigenvectors of a symmetric real matrix, using LAPACK's /dsyev/. |
159 | -- | ||
160 | -- if @(l,v)=eigSl m@ then @m \<\> v = v \<\> diag l@. | ||
161 | -- | ||
162 | -- The eigenvectors are the columns of v. | 252 | -- The eigenvectors are the columns of v. |
163 | -- The eigenvalues are sorted in descending order (use eigS' for ascending order). | 253 | -- The eigenvalues are sorted in descending order (use 'eigS'' for ascending order). |
164 | eigS :: Matrix Double -> (Vector Double, Matrix Double) | 254 | eigS :: Matrix Double -> (Vector Double, Matrix Double) |
165 | eigS m = (s', fliprl v) | 255 | eigS m = (s', fliprl v) |
166 | where (s,v) = eigS' (fmat m) | 256 | where (s,v) = eigS' (fmat m) |
167 | s' = fromList . reverse . toList $ s | 257 | s' = fromList . reverse . toList $ s |
168 | 258 | ||
259 | -- | 'eigS' in ascending order | ||
260 | eigS' :: Matrix Double -> (Vector Double, Matrix Double) | ||
169 | eigS' m | 261 | eigS' m |
170 | | r == 1 = (fromList [flatten m `at` 0], singleton 1) | 262 | | r == 1 = (fromList [flatten m `at` 0], singleton 1) |
171 | | otherwise = unsafePerformIO $ do | 263 | | otherwise = unsafePerformIO $ do |
@@ -177,17 +269,16 @@ eigS' m | |||
177 | 269 | ||
178 | ----------------------------------------------------------------------------- | 270 | ----------------------------------------------------------------------------- |
179 | 271 | ||
180 | -- | Wrapper for LAPACK's /zheev/, which computes the eigenvalues and right eigenvectors of a hermitian complex matrix: | 272 | -- | Eigenvalues and right eigenvectors of a hermitian complex matrix, using LAPACK's /zheev/. |
181 | -- | ||
182 | -- if @(l,v)=eigH m@ then @m \<\> s v = v \<\> diag l@. | ||
183 | -- | ||
184 | -- The eigenvectors are the columns of v. | 273 | -- The eigenvectors are the columns of v. |
185 | -- The eigenvalues are sorted in descending order (use eigH' for ascending order). | 274 | -- The eigenvalues are sorted in descending order (use 'eigH'' for ascending order). |
186 | eigH :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) | 275 | eigH :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) |
187 | eigH m = (s', fliprl v) | 276 | eigH m = (s', fliprl v) |
188 | where (s,v) = eigH' (fmat m) | 277 | where (s,v) = eigH' (fmat m) |
189 | s' = fromList . reverse . toList $ s | 278 | s' = fromList . reverse . toList $ s |
190 | 279 | ||
280 | -- | 'eigH' in ascending order | ||
281 | eigH' :: Matrix (Complex Double) -> (Vector Double, Matrix (Complex Double)) | ||
191 | eigH' m | 282 | eigH' m |
192 | | r == 1 = (fromList [realPart (flatten m `at` 0)], singleton 1) | 283 | | r == 1 = (fromList [realPart (flatten m `at` 0)], singleton 1) |
193 | | otherwise = unsafePerformIO $ do | 284 | | otherwise = unsafePerformIO $ do |
@@ -212,11 +303,11 @@ linearSolveSQAux f st a b | |||
212 | r = rows b | 303 | r = rows b |
213 | c = cols b | 304 | c = cols b |
214 | 305 | ||
215 | -- | Wrapper for LAPACK's /dgesv/, which solves a general real linear system (for several right-hand sides) internally using the lu decomposition. | 306 | -- | Solve a real linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /dgesv/. For underconstrained or overconstrained systems use 'linearSolveLSR' or 'linearSolveSVDR'. See also 'lusR'. |
216 | linearSolveR :: Matrix Double -> Matrix Double -> Matrix Double | 307 | linearSolveR :: Matrix Double -> Matrix Double -> Matrix Double |
217 | linearSolveR a b = linearSolveSQAux dgesv "linearSolveR" (fmat a) (fmat b) | 308 | linearSolveR a b = linearSolveSQAux dgesv "linearSolveR" (fmat a) (fmat b) |
218 | 309 | ||
219 | -- | Wrapper for LAPACK's /zgesv/, which solves a general complex linear system (for several right-hand sides) internally using the lu decomposition. | 310 | -- | Solve a complex linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, based on LAPACK's /zgesv/. For underconstrained or overconstrained systems use 'linearSolveLSC' or 'linearSolveSVDC'. See also 'lusC'. |
220 | linearSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) | 311 | linearSolveC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) |
221 | linearSolveC a b = linearSolveSQAux zgesv "linearSolveC" (fmat a) (fmat b) | 312 | linearSolveC a b = linearSolveSQAux zgesv "linearSolveC" (fmat a) (fmat b) |
222 | 313 | ||
@@ -234,17 +325,17 @@ linearSolveAux f st a b = unsafePerformIO $ do | |||
234 | n = cols a | 325 | n = cols a |
235 | nrhs = cols b | 326 | nrhs = cols b |
236 | 327 | ||
237 | -- | Wrapper for LAPACK's /dgels/, which obtains the least squared error solution of an overconstrained real linear system or the minimum norm solution of an underdetermined system, for several right-hand sides. For rank deficient systems use 'linearSolveSVDR'. | 328 | -- | Least squared error solution of an overconstrained real linear system, or the minimum norm solution of an underconstrained system, using LAPACK's /dgels/. For rank-deficient systems use 'linearSolveSVDR'. |
238 | linearSolveLSR :: Matrix Double -> Matrix Double -> Matrix Double | 329 | linearSolveLSR :: Matrix Double -> Matrix Double -> Matrix Double |
239 | linearSolveLSR a b = subMatrix (0,0) (cols a, cols b) $ | 330 | linearSolveLSR a b = subMatrix (0,0) (cols a, cols b) $ |
240 | linearSolveAux dgels "linearSolverLSR" (fmat a) (fmat b) | 331 | linearSolveAux dgels "linearSolverLSR" (fmat a) (fmat b) |
241 | 332 | ||
242 | -- | Wrapper for LAPACK's /zgels/, which obtains the least squared error solution of an overconstrained complex linear system or the minimum norm solution of an underdetermined system, for several right-hand sides. For rank deficient systems use 'linearSolveSVDC'. | 333 | -- | Least squared error solution of an overconstrained complex linear system, or the minimum norm solution of an underconstrained system, using LAPACK's /zgels/. For rank-deficient systems use 'linearSolveSVDC'. |
243 | linearSolveLSC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) | 334 | linearSolveLSC :: Matrix (Complex Double) -> Matrix (Complex Double) -> Matrix (Complex Double) |
244 | linearSolveLSC a b = subMatrix (0,0) (cols a, cols b) $ | 335 | linearSolveLSC a b = subMatrix (0,0) (cols a, cols b) $ |
245 | linearSolveAux zgels "linearSolveLSC" (fmat a) (fmat b) | 336 | linearSolveAux zgels "linearSolveLSC" (fmat a) (fmat b) |
246 | 337 | ||
247 | -- | Wrapper for LAPACK's /dgelss/, which obtains the minimum norm solution to a real linear least squares problem Ax=B using the svd, for several right-hand sides. Admits rank deficient systems but it is slower than 'linearSolveLSR'. The effective rank of A is determined by treating as zero those singular valures which are less than rcond times the largest singular value. If rcond == Nothing machine precision is used. | 338 | -- | Minimum norm solution of a general real linear least squares problem Ax=B using the SVD, based on LAPACK's /dgelss/. Admits rank-deficient systems but it is slower than 'linearSolveLSR'. The effective rank of A is determined by treating as zero those singular valures which are less than rcond times the largest singular value. If rcond == Nothing machine precision is used. |
248 | linearSolveSVDR :: Maybe Double -- ^ rcond | 339 | linearSolveSVDR :: Maybe Double -- ^ rcond |
249 | -> Matrix Double -- ^ coefficient matrix | 340 | -> Matrix Double -- ^ coefficient matrix |
250 | -> Matrix Double -- ^ right hand sides (as columns) | 341 | -> Matrix Double -- ^ right hand sides (as columns) |
@@ -253,7 +344,7 @@ linearSolveSVDR (Just rcond) a b = subMatrix (0,0) (cols a, cols b) $ | |||
253 | linearSolveAux (dgelss rcond) "linearSolveSVDR" (fmat a) (fmat b) | 344 | linearSolveAux (dgelss rcond) "linearSolveSVDR" (fmat a) (fmat b) |
254 | linearSolveSVDR Nothing a b = linearSolveSVDR (Just (-1)) (fmat a) (fmat b) | 345 | linearSolveSVDR Nothing a b = linearSolveSVDR (Just (-1)) (fmat a) (fmat b) |
255 | 346 | ||
256 | -- | Wrapper for LAPACK's /zgelss/, which obtains the minimum norm solution to a complex linear least squares problem Ax=B using the svd, for several right-hand sides. Admits rank deficient systems but it is slower than 'linearSolveLSC'. The effective rank of A is determined by treating as zero those singular valures which are less than rcond times the largest singular value. If rcond == Nothing machine precision is used. | 347 | -- | Minimum norm solution of a general complex linear least squares problem Ax=B using the SVD, based on LAPACK's /zgelss/. Admits rank-deficient systems but it is slower than 'linearSolveLSC'. The effective rank of A is determined by treating as zero those singular valures which are less than rcond times the largest singular value. If rcond == Nothing machine precision is used. |
257 | linearSolveSVDC :: Maybe Double -- ^ rcond | 348 | linearSolveSVDC :: Maybe Double -- ^ rcond |
258 | -> Matrix (Complex Double) -- ^ coefficient matrix | 349 | -> Matrix (Complex Double) -- ^ coefficient matrix |
259 | -> Matrix (Complex Double) -- ^ right hand sides (as columns) | 350 | -> Matrix (Complex Double) -- ^ right hand sides (as columns) |
@@ -266,13 +357,11 @@ linearSolveSVDC Nothing a b = linearSolveSVDC (Just (-1)) (fmat a) (fmat b) | |||
266 | foreign import ccall "LAPACK/lapack-aux.h chol_l_H" zpotrf :: TCMCM | 357 | foreign import ccall "LAPACK/lapack-aux.h chol_l_H" zpotrf :: TCMCM |
267 | foreign import ccall "LAPACK/lapack-aux.h chol_l_S" dpotrf :: TMM | 358 | foreign import ccall "LAPACK/lapack-aux.h chol_l_S" dpotrf :: TMM |
268 | 359 | ||
269 | -- | Wrapper for LAPACK's /zpotrf/, which computes the Cholesky factorization of a | 360 | -- | Cholesky factorization of a complex Hermitian positive definite matrix, using LAPACK's /zpotrf/. |
270 | -- complex Hermitian positive definite matrix. | ||
271 | cholH :: Matrix (Complex Double) -> Matrix (Complex Double) | 361 | cholH :: Matrix (Complex Double) -> Matrix (Complex Double) |
272 | cholH = cholAux zpotrf "cholH" . fmat | 362 | cholH = cholAux zpotrf "cholH" . fmat |
273 | 363 | ||
274 | -- | Wrapper for LAPACK's /dpotrf/, which computes the Cholesky factorization of a | 364 | -- | Cholesky factorization of a real symmetric positive definite matrix, using LAPACK's /dpotrf/. |
275 | -- real symmetric positive definite matrix. | ||
276 | cholS :: Matrix Double -> Matrix Double | 365 | cholS :: Matrix Double -> Matrix Double |
277 | cholS = cholAux dpotrf "cholS" . fmat | 366 | cholS = cholAux dpotrf "cholS" . fmat |
278 | 367 | ||
@@ -286,11 +375,11 @@ cholAux f st a = unsafePerformIO $ do | |||
286 | foreign import ccall "LAPACK/lapack-aux.h qr_l_R" dgeqr2 :: TMVM | 375 | foreign import ccall "LAPACK/lapack-aux.h qr_l_R" dgeqr2 :: TMVM |
287 | foreign import ccall "LAPACK/lapack-aux.h qr_l_C" zgeqr2 :: TCMCVCM | 376 | foreign import ccall "LAPACK/lapack-aux.h qr_l_C" zgeqr2 :: TCMCVCM |
288 | 377 | ||
289 | -- | Wrapper for LAPACK's /dgeqr2/, which computes a QR factorization of a real matrix. | 378 | -- | QR factorization of a real matrix, using LAPACK's /dgeqr2/. |
290 | qrR :: Matrix Double -> (Matrix Double, Vector Double) | 379 | qrR :: Matrix Double -> (Matrix Double, Vector Double) |
291 | qrR = qrAux dgeqr2 "qrR" . fmat | 380 | qrR = qrAux dgeqr2 "qrR" . fmat |
292 | 381 | ||
293 | -- | Wrapper for LAPACK's /zgeqr2/, which computes a QR factorization of a complex matrix. | 382 | -- | QR factorization of a complex matrix, using LAPACK's /zgeqr2/. |
294 | qrC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector (Complex Double)) | 383 | qrC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector (Complex Double)) |
295 | qrC = qrAux zgeqr2 "qrC" . fmat | 384 | qrC = qrAux zgeqr2 "qrC" . fmat |
296 | 385 | ||
@@ -307,11 +396,11 @@ qrAux f st a = unsafePerformIO $ do | |||
307 | foreign import ccall "LAPACK/lapack-aux.h hess_l_R" dgehrd :: TMVM | 396 | foreign import ccall "LAPACK/lapack-aux.h hess_l_R" dgehrd :: TMVM |
308 | foreign import ccall "LAPACK/lapack-aux.h hess_l_C" zgehrd :: TCMCVCM | 397 | foreign import ccall "LAPACK/lapack-aux.h hess_l_C" zgehrd :: TCMCVCM |
309 | 398 | ||
310 | -- | Wrapper for LAPACK's /dgehrd/, which computes a Hessenberg factorization of a square real matrix. | 399 | -- | Hessenberg factorization of a square real matrix, using LAPACK's /dgehrd/. |
311 | hessR :: Matrix Double -> (Matrix Double, Vector Double) | 400 | hessR :: Matrix Double -> (Matrix Double, Vector Double) |
312 | hessR = hessAux dgehrd "hessR" . fmat | 401 | hessR = hessAux dgehrd "hessR" . fmat |
313 | 402 | ||
314 | -- | Wrapper for LAPACK's /zgehrd/, which computes a Hessenberg factorization of a square complex matrix. | 403 | -- | Hessenberg factorization of a square complex matrix, using LAPACK's /zgehrd/. |
315 | hessC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector (Complex Double)) | 404 | hessC :: Matrix (Complex Double) -> (Matrix (Complex Double), Vector (Complex Double)) |
316 | hessC = hessAux zgehrd "hessC" . fmat | 405 | hessC = hessAux zgehrd "hessC" . fmat |
317 | 406 | ||
@@ -328,11 +417,11 @@ hessAux f st a = unsafePerformIO $ do | |||
328 | foreign import ccall "LAPACK/lapack-aux.h schur_l_R" dgees :: TMMM | 417 | foreign import ccall "LAPACK/lapack-aux.h schur_l_R" dgees :: TMMM |
329 | foreign import ccall "LAPACK/lapack-aux.h schur_l_C" zgees :: TCMCMCM | 418 | foreign import ccall "LAPACK/lapack-aux.h schur_l_C" zgees :: TCMCMCM |
330 | 419 | ||
331 | -- | Wrapper for LAPACK's /dgees/, which computes a Schur factorization of a square real matrix. | 420 | -- | Schur factorization of a square real matrix, using LAPACK's /dgees/. |
332 | schurR :: Matrix Double -> (Matrix Double, Matrix Double) | 421 | schurR :: Matrix Double -> (Matrix Double, Matrix Double) |
333 | schurR = schurAux dgees "schurR" . fmat | 422 | schurR = schurAux dgees "schurR" . fmat |
334 | 423 | ||
335 | -- | Wrapper for LAPACK's /zgees/, which computes a Schur factorization of a square complex matrix. | 424 | -- | Schur factorization of a square complex matrix, using LAPACK's /zgees/. |
336 | schurC :: Matrix (Complex Double) -> (Matrix (Complex Double), Matrix (Complex Double)) | 425 | schurC :: Matrix (Complex Double) -> (Matrix (Complex Double), Matrix (Complex Double)) |
337 | schurC = schurAux zgees "schurC" . fmat | 426 | schurC = schurAux zgees "schurC" . fmat |
338 | 427 | ||
@@ -347,11 +436,11 @@ schurAux f st a = unsafePerformIO $ do | |||
347 | foreign import ccall "LAPACK/lapack-aux.h lu_l_R" dgetrf :: TMVM | 436 | foreign import ccall "LAPACK/lapack-aux.h lu_l_R" dgetrf :: TMVM |
348 | foreign import ccall "LAPACK/lapack-aux.h lu_l_C" zgetrf :: TCMVCM | 437 | foreign import ccall "LAPACK/lapack-aux.h lu_l_C" zgetrf :: TCMVCM |
349 | 438 | ||
350 | -- | Wrapper for LAPACK's /dgetrf/, which computes a LU factorization of a general real matrix. | 439 | -- | LU factorization of a general real matrix, using LAPACK's /dgetrf/. |
351 | luR :: Matrix Double -> (Matrix Double, [Int]) | 440 | luR :: Matrix Double -> (Matrix Double, [Int]) |
352 | luR = luAux dgetrf "luR" . fmat | 441 | luR = luAux dgetrf "luR" . fmat |
353 | 442 | ||
354 | -- | Wrapper for LAPACK's /zgees/, which computes a Schur factorization of a square complex matrix. | 443 | -- | LU factorization of a general complex matrix, using LAPACK's /zgetrf/. |
355 | luC :: Matrix (Complex Double) -> (Matrix (Complex Double), [Int]) | 444 | luC :: Matrix (Complex Double) -> (Matrix (Complex Double), [Int]) |
356 | luC = luAux zgetrf "luC" . fmat | 445 | luC = luAux zgetrf "luC" . fmat |
357 | 446 | ||
@@ -370,11 +459,11 @@ type TQ a = CInt -> CInt -> PC -> a | |||
370 | foreign import ccall "LAPACK/lapack-aux.h luS_l_R" dgetrs :: TMVMM | 459 | foreign import ccall "LAPACK/lapack-aux.h luS_l_R" dgetrs :: TMVMM |
371 | foreign import ccall "LAPACK/lapack-aux.h luS_l_C" zgetrs :: TQ (TW (TQ (TQ (IO CInt)))) | 460 | foreign import ccall "LAPACK/lapack-aux.h luS_l_C" zgetrs :: TQ (TW (TQ (TQ (IO CInt)))) |
372 | 461 | ||
373 | -- | Wrapper for LAPACK's /dgetrs/, which solves a general real linear system (for several right-hand sides) from a precomputed LU decomposition. | 462 | -- | Solve a real linear system from a precomputed LU decomposition ('luR'), using LAPACK's /dgetrs/. |
374 | lusR :: Matrix Double -> [Int] -> Matrix Double -> Matrix Double | 463 | lusR :: Matrix Double -> [Int] -> Matrix Double -> Matrix Double |
375 | lusR a piv b = lusAux dgetrs "lusR" (fmat a) piv (fmat b) | 464 | lusR a piv b = lusAux dgetrs "lusR" (fmat a) piv (fmat b) |
376 | 465 | ||
377 | -- | Wrapper for LAPACK's /zgetrs/, which solves a general real linear system (for several right-hand sides) from a precomputed LU decomposition. | 466 | -- | Solve a real linear system from a precomputed LU decomposition ('luC'), using LAPACK's /zgetrs/. |
378 | lusC :: Matrix (Complex Double) -> [Int] -> Matrix (Complex Double) -> Matrix (Complex Double) | 467 | lusC :: Matrix (Complex Double) -> [Int] -> Matrix (Complex Double) -> Matrix (Complex Double) |
379 | lusC a piv b = lusAux zgetrs "lusC" (fmat a) piv (fmat b) | 468 | lusC a piv b = lusAux zgetrs "lusC" (fmat a) piv (fmat b) |
380 | 469 | ||