summaryrefslogtreecommitdiff
path: root/lib/LinearAlgebra/Algorithms.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-28 07:37:49 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-28 07:37:49 +0000
commit74e7d42263b196c22d1f5da3d51beec69071600d (patch)
tree04dd5cd4ef4e22dfd114a6739c9ed39bdaf6f26b /lib/LinearAlgebra/Algorithms.hs
parent0198366bba7a5f2d67338633f9eb90889ffc31b2 (diff)
save work
Diffstat (limited to 'lib/LinearAlgebra/Algorithms.hs')
-rw-r--r--lib/LinearAlgebra/Algorithms.hs58
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)
9Stability : provisional 9Stability : provisional
10Portability : uses ffi 10Portability : uses ffi
11 11
12A simple interface to the available matrix computations. We have defined some generic functions
13on both real and complex matrices in such a way that higher level algorithms and
14testing properties are valid for both base types.
15
16In any case the specific functions for particular base types can also be explicitly
17imported from the LAPACK and GSL.Matrix modules.
12 18
13-} 19-}
14----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
15 21
16module LinearAlgebra.Algorithms ( 22module 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
28import Data.Packed.Internal 52import Data.Packed.Internal hiding (fromComplex, toComplex, comp, conj)
29import Data.Packed.Matrix 53import Data.Packed.Matrix
30import GSL.Matrix 54import GSL.Matrix
31import GSL.Vector 55import GSL.Vector
@@ -33,7 +57,8 @@ import LAPACK
33import Complex 57import Complex
34import LinearAlgebra.Linear 58import LinearAlgebra.Linear
35 59
36class (Linear Matrix t) => GMatrix t where 60-- | Base types for which some optimized matrix computations are available
61class (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
45instance GMatrix Double where 71instance 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
54instance GMatrix (Complex Double) where 81instance 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
63square m = rows m == cols m 91square m = rows m == cols m
64 92
65det :: GMatrix t => Matrix t -> t 93det :: Optimized t => Matrix t -> t
66det m | square m = s * (product $ toList $ takeDiag $ u) 94det 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
70inv :: GMatrix t => Matrix t -> Matrix t 98inv :: Optimized t => Matrix t -> Matrix t
71inv m | square m = m `linearSolve` ident (rows m) 99inv m | square m = m `linearSolve` ident (rows m)
72 | otherwise = error "inv of nonsquare matrix" 100 | otherwise = error "inv of nonsquare matrix"
73 101
74pinv :: GMatrix t => Matrix t -> Matrix t 102pinv :: Optimized t => Matrix t -> Matrix t
75pinv m = linearSolveSVD m (ident (rows m)) 103pinv 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
122mXm :: (Num t, Field t) => Matrix t -> Matrix t -> Matrix t 150mXm :: (Num t, Optimized t) => Matrix t -> Matrix t -> Matrix t
123mXm = multiply 151mXm = multiply
124 152
125-- | matrix - vector product 153-- | matrix - vector product
126mXv :: (Num t, Field t) => Matrix t -> Vector t -> Vector t 154mXv :: (Num t, Optimized t) => Matrix t -> Vector t -> Vector t
127mXv m v = flatten $ m `mXm` (asColumn v) 155mXv m v = flatten $ m `mXm` (asColumn v)
128 156
129-- | vector - matrix product 157-- | vector - matrix product
130vXm :: (Num t, Field t) => Vector t -> Matrix t -> Vector t 158vXm :: (Num t, Optimized t) => Vector t -> Matrix t -> Vector t
131vXm v m = flatten $ (asRow v) `mXm` m 159vXm 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
177class Normed t where 205class 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.
197nullspacePrec :: GMatrix t 225nullspacePrec :: 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@).
210nullVector :: GMatrix t => Matrix t -> Vector t 238nullVector :: Optimized t => Matrix t -> Vector t
211nullVector = last . nullspacePrec 1 239nullVector = last . nullspacePrec 1
212 240
213------------------------------------------------------------------------ 241------------------------------------------------------------------------