summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/LinearAlgebra/Util/CG.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Numeric/LinearAlgebra/Util/CG.hs')
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util/CG.hs15
1 files changed, 7 insertions, 8 deletions
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util/CG.hs b/packages/base/src/Numeric/LinearAlgebra/Util/CG.hs
index f821b57..b82c74f 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Util/CG.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Util/CG.hs
@@ -41,22 +41,21 @@ cg sym at a (CGState p r r2 x _) = CGState p' r' r'2 x' rdx
41 ap1 = a p 41 ap1 = a p
42 ap | sym = ap1 42 ap | sym = ap1
43 | otherwise = at ap1 43 | otherwise = at ap1
44 pap | sym = p ap1 44 pap | sym = p <·> ap1
45 | otherwise = norm2 ap1 ** 2 45 | otherwise = norm2 ap1 ** 2
46 alpha = r2 / pap 46 alpha = r2 / pap
47 dx = scale alpha p 47 dx = scale alpha p
48 x' = x + dx 48 x' = x + dx
49 r' = r - scale alpha ap 49 r' = r - scale alpha ap
50 r'2 = r' r' 50 r'2 = r' <·> r'
51 beta = r'2 / r2 51 beta = r'2 / r2
52 p' = r' + scale beta p 52 p' = r' + scale beta p
53 53
54 rdx = norm2 dx / max 1 (norm2 x) 54 rdx = norm2 dx / max 1 (norm2 x)
55 55
56conjugrad 56conjugrad
57 :: (Transposable m mt, Contraction m V V, Contraction mt V V) 57 :: Bool -> GMatrix -> V -> V -> R -> R -> [CGState]
58 => Bool -> m -> V -> V -> R -> R -> [CGState] 58conjugrad sym a b = solveG (tr a !#>) (a !#>) (cg sym) b
59conjugrad sym a b = solveG (tr a ◇) (a ◇) (cg sym) b
60 59
61solveG 60solveG
62 :: (V -> V) -> (V -> V) 61 :: (V -> V) -> (V -> V)
@@ -72,9 +71,9 @@ solveG mat ma meth rawb x0' ϵb ϵx
72 b = mat rawb 71 b = mat rawb
73 x0 = if x0' == 0 then konst 0 (dim b) else x0' 72 x0 = if x0' == 0 then konst 0 (dim b) else x0'
74 r0 = b - a x0 73 r0 = b - a x0
75 r20 = r0 r0 74 r20 = r0 <·> r0
76 p0 = r0 75 p0 = r0
77 nb2 = b b 76 nb2 = b <·> b
78 ok CGState {..} 77 ok CGState {..}
79 = cgr2 <nb2*ϵb**2 78 = cgr2 <nb2*ϵb**2
80 || cgdx < ϵx 79 || cgdx < ϵx
@@ -115,7 +114,7 @@ instance Testable GMatrix
115 sma = convo2 20 3 114 sma = convo2 20 3
116 x1 = vect [1..20] 115 x1 = vect [1..20]
117 x2 = vect [1..40] 116 x2 = vect [1..40]
118 sm = (mkSparse . mkCSR) sma 117 sm = mkSparse sma
119 dm = toDense sma 118 dm = toDense sma
120 119
121 s1 = sm !#> x1 120 s1 = sm !#> x1