diff options
Diffstat (limited to 'lib/LAPACK')
-rw-r--r-- | lib/LAPACK/Internal.hs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/LAPACK/Internal.hs b/lib/LAPACK/Internal.hs index 4c755bc..2569215 100644 --- a/lib/LAPACK/Internal.hs +++ b/lib/LAPACK/Internal.hs | |||
@@ -27,6 +27,24 @@ import Foreign.C.String | |||
27 | foreign import ccall "lapack-aux.h svd_l_R" | 27 | foreign import ccall "lapack-aux.h svd_l_R" |
28 | dgesvd :: Double ::> Double ::> (Double :> Double ::> IO Int) | 28 | dgesvd :: Double ::> Double ::> (Double :> Double ::> IO Int) |
29 | 29 | ||
30 | -- | Wrapper for LAPACK's /dgesvd/, which computes the full svd decomposition of a real matrix. | ||
31 | -- | ||
32 | -- @(u,s,v)=svdR m@ so that @m=u \<\> s \<\> 'trans' v@. | ||
33 | svdR :: Matrix Double -> (Matrix Double, Matrix Double , Matrix Double) | ||
34 | svdR x@M {rows = r, cols = c} = (u, s, v) | ||
35 | where (u,s',v) = svdR' x | ||
36 | s | r == c = diag s' | ||
37 | | r < c = joinHoriz [diag s' , zeros (r,c-r)] | ||
38 | | otherwise = joinVert [diag s' , zeros (r-c,c)] | ||
39 | zeros (r,c) = reshape c $ constant (r*c) 0 | ||
40 | |||
41 | svdR' x@M {rows = r, cols = c} = unsafePerformIO $ do | ||
42 | u <- createMatrix ColumnMajor r r | ||
43 | s <- createVector (min r c) | ||
44 | v <- createMatrix ColumnMajor c c | ||
45 | dgesvd // mat fdat x // mat dat u // vec s // mat dat v // check "svdR" [fdat x] | ||
46 | return (u,s,trans v) | ||
47 | |||
30 | ----------------------------------------------------------------------------- | 48 | ----------------------------------------------------------------------------- |
31 | -- dgesdd | 49 | -- dgesdd |
32 | foreign import ccall "lapack-aux.h svd_l_Rdd" | 50 | foreign import ccall "lapack-aux.h svd_l_Rdd" |
@@ -62,11 +80,27 @@ foreign import ccall "lapack-aux.h eig_l_R" | |||
62 | dgeev :: Double ::> Double ::> ((Complex Double) :> Double ::> IO Int) | 80 | dgeev :: Double ::> Double ::> ((Complex Double) :> Double ::> IO Int) |
63 | 81 | ||
64 | ----------------------------------------------------------------------------- | 82 | ----------------------------------------------------------------------------- |
65 | |||
66 | -- dsyev | 83 | -- dsyev |
67 | foreign import ccall "lapack-aux.h eig_l_S" | 84 | foreign import ccall "lapack-aux.h eig_l_S" |
68 | dsyev :: Double ::> (Double :> Double ::> IO Int) | 85 | dsyev :: Double ::> (Double :> Double ::> IO Int) |
69 | 86 | ||
87 | -- | Wrapper for LAPACK's /dsyev/, which computes the eigenvalues and right eigenvectors of a symmetric real matrix: | ||
88 | -- | ||
89 | -- if @(l,v)=eigSl m@ then @m \<\> v = v \<\> diag l@. | ||
90 | -- | ||
91 | -- The eigenvectors are the columns of v. | ||
92 | -- The eigenvalues are sorted in descending order (use eigS' for ascending order). | ||
93 | eigS :: Matrix Double -> (Vector Double, Matrix Double) | ||
94 | eigS m = (s', fliprl v) | ||
95 | where (s,v) = eigS' m | ||
96 | s' = fromList . reverse . toList $ s | ||
97 | |||
98 | eigS' (m@M {rows = r}) = unsafePerformIO $ do | ||
99 | l <- createVector r | ||
100 | v <- createMatrix ColumnMajor r r | ||
101 | dsyev // mat fdat m // vec l // mat dat v // check "eigS" [fdat m] | ||
102 | return (l,v) | ||
103 | |||
70 | ----------------------------------------------------------------------------- | 104 | ----------------------------------------------------------------------------- |
71 | -- zheev | 105 | -- zheev |
72 | foreign import ccall "lapack-aux.h eig_l_H" | 106 | foreign import ccall "lapack-aux.h eig_l_H" |