diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-23 10:51:16 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-23 10:51:16 +0200 |
commit | 0a9ef8f5b0088c1ac25175bffca4ed95d9e109a5 (patch) | |
tree | 7bd461ebbf140804e470ae03bb3a2e29bdd935e2 /packages/base/src/Numeric/Sparse.hs | |
parent | 109fa7d25779e331356bbe310755c10eddfeb235 (diff) |
relativeError, cgSolve'
Diffstat (limited to 'packages/base/src/Numeric/Sparse.hs')
-rw-r--r-- | packages/base/src/Numeric/Sparse.hs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/packages/base/src/Numeric/Sparse.hs b/packages/base/src/Numeric/Sparse.hs index 3835590..1957d3a 100644 --- a/packages/base/src/Numeric/Sparse.hs +++ b/packages/base/src/Numeric/Sparse.hs | |||
@@ -17,7 +17,8 @@ import Control.Arrow((***)) | |||
17 | import Control.Monad(when) | 17 | import Control.Monad(when) |
18 | import Data.List(groupBy, sort) | 18 | import Data.List(groupBy, sort) |
19 | import Foreign.C.Types(CInt(..)) | 19 | import Foreign.C.Types(CInt(..)) |
20 | import Numeric.LinearAlgebra.Util.CG(CGMat) | 20 | import Numeric.LinearAlgebra.Util.CG(CGMat,cgSolve) |
21 | import Numeric.LinearAlgebra.Algorithms(linearSolveLS, relativeError, NormType(..)) | ||
21 | import Data.Packed.Development | 22 | import Data.Packed.Development |
22 | import System.IO.Unsafe(unsafePerformIO) | 23 | import System.IO.Unsafe(unsafePerformIO) |
23 | import Foreign(Ptr) | 24 | import Foreign(Ptr) |
@@ -150,12 +151,13 @@ instance Testable SMatrix | |||
150 | x1 = vect [1..20] | 151 | x1 = vect [1..20] |
151 | x2 = vect [1..40] | 152 | x2 = vect [1..40] |
152 | sm = mkCSR sma | 153 | sm = mkCSR sma |
154 | dm = toDense sma | ||
153 | 155 | ||
154 | s1 = sm ◇ x1 | 156 | s1 = sm ◇ x1 |
155 | d1 = toDense sma ◇ x1 | 157 | d1 = dm ◇ x1 |
156 | 158 | ||
157 | s2 = tr sm ◇ x2 | 159 | s2 = tr sm ◇ x2 |
158 | d2 = tr (toDense sma) ◇ x2 | 160 | d2 = tr dm ◇ x2 |
159 | 161 | ||
160 | sdia = mkDiagR 40 20 (vect [1..10]) | 162 | sdia = mkDiagR 40 20 (vect [1..10]) |
161 | s3 = sdia ◇ x1 | 163 | s3 = sdia ◇ x1 |
@@ -164,6 +166,10 @@ instance Testable SMatrix | |||
164 | d3 = ddia ◇ x1 | 166 | d3 = ddia ◇ x1 |
165 | d4 = tr ddia ◇ x2 | 167 | d4 = tr ddia ◇ x2 |
166 | 168 | ||
169 | v = testb 40 | ||
170 | s5 = cgSolve False sm v | ||
171 | d5 = denseSolve dm v | ||
172 | |||
167 | info = do | 173 | info = do |
168 | print sm | 174 | print sm |
169 | disp (toDense sma) | 175 | disp (toDense sma) |
@@ -171,11 +177,14 @@ instance Testable SMatrix | |||
171 | print s2; print d2 | 177 | print s2; print d2 |
172 | print s3; print d3 | 178 | print s3; print d3 |
173 | print s4; print d4 | 179 | print s4; print d4 |
180 | print s5; print d5 | ||
181 | print $ relativeError Infinity s5 d5 | ||
174 | 182 | ||
175 | ok = s1==d1 | 183 | ok = s1==d1 |
176 | && s2==d2 | 184 | && s2==d2 |
177 | && s3==d3 | 185 | && s3==d3 |
178 | && s4==d4 | 186 | && s4==d4 |
187 | && relativeError Infinity s5 d5 < 1E-10 | ||
179 | 188 | ||
180 | disp = putStr . dispf 2 | 189 | disp = putStr . dispf 2 |
181 | 190 | ||
@@ -189,4 +198,8 @@ instance Testable SMatrix | |||
189 | where | 198 | where |
190 | m1 = convomat n k | 199 | m1 = convomat n k |
191 | m2 = map (((+n) *** id) *** id) m1 | 200 | m2 = map (((+n) *** id) *** id) m1 |
201 | |||
202 | testb n = vect $ take n $ cycle ([0..10]++[9,8..1]) | ||
203 | |||
204 | denseSolve a = flatten . linearSolveLS a . asColumn | ||
192 | 205 | ||