1 {-# LANGUAGE NoImplicitPrelude #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# LANGUAGE TypeFamilies #-}
5 -- | Classical iterative methods to solve the system Ax = b.
7 module Linear.Iteration (
8 gauss_seidel_iteration,
9 gauss_seidel_iterations,
20 import Data.List ( find )
21 import Data.Maybe ( fromJust )
22 import Data.Vector.Fixed ( Arity, N1, S )
23 import NumericPrelude hiding ( (*) )
24 import qualified Algebra.Algebraic as Algebraic ( C )
25 import qualified Algebra.Field as Field ( C )
26 import qualified Algebra.RealField as RealField ( C )
27 import qualified Algebra.ToRational as ToRational ( C )
29 import Linear.Matrix (
38 import Linear.System ( forward_substitute )
39 import Normed ( Normed(..) )
42 -- | A generalized implementation for Jacobi, Gauss-Seidel, etc. All
43 -- that we really need to know is how to construct the matrix M, so we
44 -- take a function that does it as an argument.
45 classical_iteration :: (Eq a, Field.C a, m ~ S n, Arity n)
46 => (Mat m m a -> Mat m m a)
51 classical_iteration m_function matrix b x_current =
54 big_m = m_function matrix
55 big_n = big_m - matrix
56 rhs = big_n*x_current + b
57 -- TODO: Should be solve below! M might not be lower-triangular.
58 x_next = forward_substitute big_m rhs
61 -- | Perform one iteration of successive over-relaxation.
63 sor_iteration :: forall m n a.
64 (Eq a, Field.C a, m ~ S n, Arity n)
66 -> Mat m m a -- ^ Matrix A
67 -> Mat m N1 a -- ^ Vector b
68 -> Mat m N1 a -- ^ Vector x_current
69 -> Mat m N1 a -- ^ Output vector x_next
71 classical_iteration m_function
73 m_function :: Mat m m a -> Mat m m a
75 let diag = (recip omega) *> (diagonal_part matrix)
76 lt = lt_part_strict matrix
81 -- | Compute an infinite list of SOR iterations starting with the
83 sor_iterations :: (Eq a, Field.C a, m ~ S n, Arity n)
89 sor_iterations omega matrix b =
90 iterate (sor_iteration omega matrix b)
93 -- | Perform one iteration of Gauss-Seidel.
94 gauss_seidel_iteration :: (Eq a, Field.C a, m ~ S n, Arity n)
99 gauss_seidel_iteration = sor_iteration one
102 -- | Compute an infinite list of Gauss-Seidel iterations starting with
104 gauss_seidel_iterations :: (Eq a, Field.C a, m ~ S n, Arity n)
109 gauss_seidel_iterations matrix b =
110 iterate (gauss_seidel_iteration matrix b)
113 -- | Perform one Jacobi iteration,
115 -- x1 = M^(-1) * (N*x0 + b)
119 -- >>> import Linear.Matrix ( Mat2, fromList, vec2d )
121 -- >>> let m = fromList [[4,2],[2,2]] :: Mat2 Double
122 -- >>> let x0 = vec2d (0, 0::Double)
123 -- >>> let b = vec2d (1, 1::Double)
124 -- >>> jacobi_iteration m b x0
126 -- >>> let x1 = jacobi_iteration m b x0
127 -- >>> jacobi_iteration m b x1
130 jacobi_iteration :: (Eq a, Field.C a, m ~ S n, Arity n)
136 classical_iteration diagonal_part
139 -- | Compute an infinite list of Jacobi iterations starting with the
141 jacobi_iterations :: (Eq a, Field.C a, m ~ S n, Arity n)
146 jacobi_iterations matrix b =
147 iterate (jacobi_iteration matrix b)
150 -- | Solve the system Ax = b using the Jacobi method. This will run
151 -- forever if the iterations do not converge.
155 -- >>> import Linear.Matrix ( Mat2, fromList, vec2d )
157 -- >>> let m = fromList [[4,2],[2,2]] :: Mat2 Double
158 -- >>> let x0 = vec2d (0, 0::Double)
159 -- >>> let b = vec2d (1, 1::Double)
160 -- >>> let epsilon = 10**(-6)
161 -- >>> jacobi_method m b x0 epsilon
162 -- ((0.0),(0.4999995231628418))
164 jacobi_method :: (RealField.C a,
165 Algebraic.C a, -- Normed instance
166 ToRational.C a, -- Normed instance
170 Arity n, -- Normed instance
178 classical_method jacobi_iterations
181 -- | Solve the system Ax = b using the Gauss-Seidel method. This will
182 -- run forever if the iterations do not converge.
186 -- >>> import Linear.Matrix ( Mat2, fromList, vec2d )
188 -- >>> let m = fromList [[4,2],[2,2]] :: Mat2 Double
189 -- >>> let x0 = vec2d (0, 0::Double)
190 -- >>> let b = vec2d (1, 1::Double)
191 -- >>> let epsilon = 10**(-12)
192 -- >>> gauss_seidel_method m b x0 epsilon
193 -- ((4.547473508864641e-13),(0.49999999999954525))
195 gauss_seidel_method :: (RealField.C a,
196 Algebraic.C a, -- Normed instance
197 ToRational.C a, -- Normed instance
201 Arity n, -- Normed instance
208 gauss_seidel_method =
209 classical_method gauss_seidel_iterations
212 -- | Solve the system Ax = b using the Successive Over-Relaxation
213 -- (SOR) method. This will run forever if the iterations do not
218 -- >>> import Linear.Matrix ( Mat2, fromList, vec2d )
220 -- >>> let m = fromList [[4,2],[2,2]] :: Mat2 Double
221 -- >>> let x0 = vec2d (0, 0::Double)
222 -- >>> let b = vec2d (1, 1::Double)
223 -- >>> let epsilon = 10**(-12)
224 -- >>> sor_method 1.5 m b x0 epsilon
225 -- ((6.567246746413957e-13),(0.4999999999993727))
227 sor_method :: (RealField.C a,
228 Algebraic.C a, -- Normed instance
229 ToRational.C a, -- Normed instance
233 Arity n, -- Normed instance
242 classical_method (sor_iterations omega)
245 -- | General implementation for all classical iteration methods. For
246 -- its first argument, it takes a function which generates the
247 -- sequence of iterates when supplied with the remaining arguments
248 -- (except for the tolerance).
250 classical_method :: forall m n a b.
252 Algebraic.C a, -- Normed instance
253 ToRational.C a, -- Normed instance
257 Arity n, -- Normed instance
259 => (Mat m m a -> Mat m N1 a -> Mat m N1 a -> [Mat m N1 a])
265 classical_method iterations_function matrix b x0 epsilon =
266 -- fromJust is "safe," because the list is infinite. If the
267 -- algorithm doesn't converge, 'find' will search forever and never
269 fst' $ fromJust $ find error_small_enough diff_pairs
271 x_n = iterations_function matrix b x0
273 pairs :: [(Mat m N1 a, Mat m N1 a)]
274 pairs = zip (tail x_n) x_n
276 append_diff :: (Mat m N1 a, Mat m N1 a)
277 -> (Mat m N1 a, Mat m N1 a, b)
278 append_diff (cur,prev) =
281 diff = norm (cur - prev)
283 diff_pairs :: [(Mat m N1 a, Mat m N1 a, b)]
284 diff_pairs = map append_diff pairs
286 fst' :: (c, d, e) -> c
289 error_small_enough :: (Mat m N1 a, Mat m N1 a, b)-> Bool
290 error_small_enough (_,_,err) = err < epsilon
294 -- | Compute the Rayleigh quotient of @matrix@ and @vector@.
298 -- >>> import Linear.Matrix ( Mat2, fromList, vec2d )
300 -- >>> let m = fromList [[3,1],[1,2]] :: Mat2 Rational
301 -- >>> let v = vec2d (1, 1::Rational)
302 -- >>> rayleigh_quotient m v
305 rayleigh_quotient :: (RealField.C a,
312 rayleigh_quotient matrix vector =
313 (vector `dot` (matrix * vector)) / (norm_squared vector)
315 -- We don't use the norm function here to avoid the algebraic
316 -- requirement on our field.
317 norm_squared v = ((transpose v) * v) !!! (0,0)