diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-28 07:37:49 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-28 07:37:49 +0000 |
commit | 74e7d42263b196c22d1f5da3d51beec69071600d (patch) | |
tree | 04dd5cd4ef4e22dfd114a6739c9ed39bdaf6f26b /lib/LinearAlgebra/Algorithms.hs | |
parent | 0198366bba7a5f2d67338633f9eb90889ffc31b2 (diff) |
save work
Diffstat (limited to 'lib/LinearAlgebra/Algorithms.hs')
-rw-r--r-- | lib/LinearAlgebra/Algorithms.hs | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index 7953386..162426f 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs | |||
@@ -9,23 +9,47 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) | |||
9 | Stability : provisional | 9 | Stability : provisional |
10 | Portability : uses ffi | 10 | Portability : uses ffi |
11 | 11 | ||
12 | A simple interface to the available matrix computations. We have defined some generic functions | ||
13 | on both real and complex matrices in such a way that higher level algorithms and | ||
14 | testing properties are valid for both base types. | ||
15 | |||
16 | In any case the specific functions for particular base types can also be explicitly | ||
17 | imported from the LAPACK and GSL.Matrix modules. | ||
12 | 18 | ||
13 | -} | 19 | -} |
14 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
15 | 21 | ||
16 | module LinearAlgebra.Algorithms ( | 22 | module LinearAlgebra.Algorithms ( |
17 | GMatrix(..), | 23 | -- * Basic Linear Algebra |
24 | scale, | ||
25 | add, | ||
26 | multiply, dot, outer, | ||
27 | linearSolve, linearSolveSVD, | ||
28 | -- * Matrix factorizations | ||
29 | svd, lu, eig, eigSH, | ||
30 | qr, chol, | ||
31 | -- * Utilities | ||
18 | Normed(..), NormType(..), | 32 | Normed(..), NormType(..), |
19 | det,inv,pinv,full,economy, | 33 | det,inv,pinv,full,economy, |
20 | pinvTol, | 34 | pinvTol, |
21 | -- pinvTolg, | 35 | -- pinvTolg, |
22 | nullspacePrec, | 36 | nullspacePrec, |
23 | nullVector, | 37 | nullVector, |
24 | eps, i | 38 | -- * Conversions |
39 | toComplex, fromComplex, | ||
40 | comp, real, complex, | ||
41 | conj, ctrans, | ||
42 | -- * Misc | ||
43 | eps, i, | ||
44 | scaleRecip, | ||
45 | addConstant, | ||
46 | sub, | ||
47 | mul, | ||
48 | divide, | ||
25 | ) where | 49 | ) where |
26 | 50 | ||
27 | 51 | ||
28 | import Data.Packed.Internal | 52 | import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj) |
29 | import Data.Packed.Matrix | 53 | import Data.Packed.Matrix |
30 | import GSL.Matrix | 54 | import GSL.Matrix |
31 | import GSL.Vector | 55 | import GSL.Vector |
@@ -33,7 +57,8 @@ import LAPACK | |||
33 | import Complex | 57 | import Complex |
34 | import LinearAlgebra.Linear | 58 | import LinearAlgebra.Linear |
35 | 59 | ||
36 | class (Linear Matrix t) => GMatrix t where | 60 | -- | Base types for which some optimized matrix computations are available |
61 | class (Linear Matrix t) => Optimized t where | ||
37 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) | 62 | svd :: Matrix t -> (Matrix t, Vector Double, Matrix t) |
38 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) | 63 | lu :: Matrix t -> (Matrix t, Matrix t, [Int], t) |
39 | linearSolve :: Matrix t -> Matrix t -> Matrix t | 64 | linearSolve :: Matrix t -> Matrix t -> Matrix t |
@@ -41,8 +66,9 @@ class (Linear Matrix t) => GMatrix t where | |||
41 | ctrans :: Matrix t -> Matrix t | 66 | ctrans :: Matrix t -> Matrix t |
42 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) | 67 | eig :: Matrix t -> (Vector (Complex Double), Matrix (Complex Double)) |
43 | eigSH :: Matrix t -> (Vector Double, Matrix t) | 68 | eigSH :: Matrix t -> (Vector Double, Matrix t) |
69 | chol :: Matrix t -> Matrix t | ||
44 | 70 | ||
45 | instance GMatrix Double where | 71 | instance Optimized Double where |
46 | svd = svdR | 72 | svd = svdR |
47 | lu = luR | 73 | lu = luR |
48 | linearSolve = linearSolveR | 74 | linearSolve = linearSolveR |
@@ -50,8 +76,9 @@ instance GMatrix Double where | |||
50 | ctrans = trans | 76 | ctrans = trans |
51 | eig = eigR | 77 | eig = eigR |
52 | eigSH = eigS | 78 | eigSH = eigS |
79 | chol = cholR | ||
53 | 80 | ||
54 | instance GMatrix (Complex Double) where | 81 | instance Optimized (Complex Double) where |
55 | svd = svdC | 82 | svd = svdC |
56 | lu = luC | 83 | lu = luC |
57 | linearSolve = linearSolveC | 84 | linearSolve = linearSolveC |
@@ -59,19 +86,20 @@ instance GMatrix (Complex Double) where | |||
59 | ctrans = conjTrans | 86 | ctrans = conjTrans |
60 | eig = eigC | 87 | eig = eigC |
61 | eigSH = eigH | 88 | eigSH = eigH |
89 | chol = error "cholC not yet implemented" -- waiting for GSL-1.10 | ||
62 | 90 | ||
63 | square m = rows m == cols m | 91 | square m = rows m == cols m |
64 | 92 | ||
65 | det :: GMatrix t => Matrix t -> t | 93 | det :: Optimized t => Matrix t -> t |
66 | det m | square m = s * (product $ toList $ takeDiag $ u) | 94 | det m | square m = s * (product $ toList $ takeDiag $ u) |
67 | | otherwise = error "det of nonsquare matrix" | 95 | | otherwise = error "det of nonsquare matrix" |
68 | where (_,u,_,s) = lu m | 96 | where (_,u,_,s) = lu m |
69 | 97 | ||
70 | inv :: GMatrix t => Matrix t -> Matrix t | 98 | inv :: Optimized t => Matrix t -> Matrix t |
71 | inv m | square m = m `linearSolve` ident (rows m) | 99 | inv m | square m = m `linearSolve` ident (rows m) |
72 | | otherwise = error "inv of nonsquare matrix" | 100 | | otherwise = error "inv of nonsquare matrix" |
73 | 101 | ||
74 | pinv :: GMatrix t => Matrix t -> Matrix t | 102 | pinv :: Optimized t => Matrix t -> Matrix t |
75 | pinv m = linearSolveSVD m (ident (rows m)) | 103 | pinv m = linearSolveSVD m (ident (rows m)) |
76 | 104 | ||
77 | 105 | ||
@@ -119,15 +147,15 @@ i = 0:+1 | |||
119 | 147 | ||
120 | 148 | ||
121 | -- | matrix product | 149 | -- | matrix product |
122 | mXm :: (Num t, Field t) => Matrix t -> Matrix t -> Matrix t | 150 | mXm :: (Num t, Optimized t) => Matrix t -> Matrix t -> Matrix t |
123 | mXm = multiply | 151 | mXm = multiply |
124 | 152 | ||
125 | -- | matrix - vector product | 153 | -- | matrix - vector product |
126 | mXv :: (Num t, Field t) => Matrix t -> Vector t -> Vector t | 154 | mXv :: (Num t, Optimized t) => Matrix t -> Vector t -> Vector t |
127 | mXv m v = flatten $ m `mXm` (asColumn v) | 155 | mXv m v = flatten $ m `mXm` (asColumn v) |
128 | 156 | ||
129 | -- | vector - matrix product | 157 | -- | vector - matrix product |
130 | vXm :: (Num t, Field t) => Vector t -> Matrix t -> Vector t | 158 | vXm :: (Num t, Optimized t) => Vector t -> Matrix t -> Vector t |
131 | vXm v m = flatten $ (asRow v) `mXm` m | 159 | vXm v m = flatten $ (asRow v) `mXm` m |
132 | 160 | ||
133 | 161 | ||
@@ -171,7 +199,7 @@ pnormCM Infinity m = vectorMax $ liftMatrix (liftVector magnitude) m `mXv` const | |||
171 | --pnormCM _ _ = error "p norm not yet defined" | 199 | --pnormCM _ _ = error "p norm not yet defined" |
172 | 200 | ||
173 | -- -- | computes the p-norm of a matrix or vector (with the same definitions as GNU-octave). pnorm 0 denotes \\inf-norm. See also 'norm'. | 201 | -- -- | computes the p-norm of a matrix or vector (with the same definitions as GNU-octave). pnorm 0 denotes \\inf-norm. See also 'norm'. |
174 | --pnorm :: (Container t, Field a) => Int -> t a -> Double | 202 | --pnorm :: (Container t, Optimized a) => Int -> t a -> Double |
175 | --pnorm = pnormG | 203 | --pnorm = pnormG |
176 | 204 | ||
177 | class Normed t where | 205 | class Normed t where |
@@ -194,7 +222,7 @@ instance Normed (Matrix (Complex Double)) where | |||
194 | ----------------------------------------------------------------------- | 222 | ----------------------------------------------------------------------- |
195 | 223 | ||
196 | -- | The nullspace of a matrix from its SVD decomposition. | 224 | -- | The nullspace of a matrix from its SVD decomposition. |
197 | nullspacePrec :: GMatrix t | 225 | nullspacePrec :: Optimized t |
198 | => Double -- ^ relative tolerance in 'eps' units | 226 | => Double -- ^ relative tolerance in 'eps' units |
199 | -> Matrix t -- ^ input matrix | 227 | -> Matrix t -- ^ input matrix |
200 | -> [Vector t] -- ^ list of unitary vectors spanning the nullspace | 228 | -> [Vector t] -- ^ list of unitary vectors spanning the nullspace |
@@ -207,7 +235,7 @@ nullspacePrec t m = ns where | |||
207 | ns = drop rank $ toRows $ ctrans v | 235 | ns = drop rank $ toRows $ ctrans v |
208 | 236 | ||
209 | -- | The nullspace of a matrix, assumed to be one-dimensional, with default tolerance (shortcut for @last . nullspacePrec 1@). | 237 | -- | The nullspace of a matrix, assumed to be one-dimensional, with default tolerance (shortcut for @last . nullspacePrec 1@). |
210 | nullVector :: GMatrix t => Matrix t -> Vector t | 238 | nullVector :: Optimized t => Matrix t -> Vector t |
211 | nullVector = last . nullspacePrec 1 | 239 | nullVector = last . nullspacePrec 1 |
212 | 240 | ||
213 | ------------------------------------------------------------------------ | 241 | ------------------------------------------------------------------------ |