summaryrefslogtreecommitdiff
path: root/packages/base/src/Internal/CG.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Internal/CG.hs')
-rw-r--r--packages/base/src/Internal/CG.hs20
1 files changed, 11 insertions, 9 deletions
diff --git a/packages/base/src/Internal/CG.hs b/packages/base/src/Internal/CG.hs
index f0142cd..cc10ad8 100644
--- a/packages/base/src/Internal/CG.hs
+++ b/packages/base/src/Internal/CG.hs
@@ -32,11 +32,11 @@ v /// b = debugMat b 2 asRow v
32type V = Vector R 32type V = Vector R
33 33
34data CGState = CGState 34data CGState = CGState
35 { cgp :: V -- ^ conjugate gradient 35 { cgp :: Vector R -- ^ conjugate gradient
36 , cgr :: V -- ^ residual 36 , cgr :: Vector R -- ^ residual
37 , cgr2 :: R -- ^ squared norm of residual 37 , cgr2 :: R -- ^ squared norm of residual
38 , cgx :: V -- ^ current solution 38 , cgx :: Vector R -- ^ current solution
39 , cgdx :: R -- ^ normalized size of correction 39 , cgdx :: R -- ^ normalized size of correction
40 } 40 }
41 41
42cg :: Bool -> (V -> V) -> (V -> V) -> CGState -> CGState 42cg :: Bool -> (V -> V) -> (V -> V) -> CGState -> CGState
@@ -89,23 +89,25 @@ takeUntil q xs = a++ take 1 b
89 where 89 where
90 (a,b) = break q xs 90 (a,b) = break q xs
91 91
92-- | Solve a sparse linear system using the conjugate gradient method with default parameters.
92cgSolve 93cgSolve
93 :: Bool -- ^ is symmetric 94 :: Bool -- ^ is symmetric
94 -> GMatrix -- ^ coefficient matrix 95 -> GMatrix -- ^ coefficient matrix
95 -> Vector Double -- ^ right-hand side 96 -> Vector R -- ^ right-hand side
96 -> Vector Double -- ^ solution 97 -> Vector R -- ^ solution
97cgSolve sym a b = cgx $ last $ cgSolve' sym 1E-4 1E-3 n a b 0 98cgSolve sym a b = cgx $ last $ cgSolve' sym 1E-4 1E-3 n a b 0
98 where 99 where
99 n = max 10 (round $ sqrt (fromIntegral (dim b) :: Double)) 100 n = max 10 (round $ sqrt (fromIntegral (dim b) :: Double))
100 101
102-- | Solve a sparse linear system using the conjugate gradient method with default parameters.
101cgSolve' 103cgSolve'
102 :: Bool -- ^ symmetric 104 :: Bool -- ^ symmetric
103 -> R -- ^ relative tolerance for the residual (e.g. 1E-4) 105 -> R -- ^ relative tolerance for the residual (e.g. 1E-4)
104 -> R -- ^ relative tolerance for δx (e.g. 1E-3) 106 -> R -- ^ relative tolerance for δx (e.g. 1E-3)
105 -> Int -- ^ maximum number of iterations 107 -> Int -- ^ maximum number of iterations
106 -> GMatrix -- ^ coefficient matrix 108 -> GMatrix -- ^ coefficient matrix
107 -> V -- ^ initial solution 109 -> Vector R -- ^ initial solution
108 -> V -- ^ right-hand side 110 -> Vector R -- ^ right-hand side
109 -> [CGState] -- ^ solution 111 -> [CGState] -- ^ solution
110cgSolve' sym er es n a b x = take n $ conjugrad sym a b x er es 112cgSolve' sym er es n a b x = take n $ conjugrad sym a b x er es
111 113