1 {-# LANGUAGE RebindableSyntax #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
4 -- | The Roots.Simple module contains root-finding algorithms. That
5 -- is, procedures to (numerically) find solutions to the equation,
9 -- where f is assumed to be continuous on the interval of interest.
15 fixed_point_error_ratios,
16 fixed_point_iteration_count,
23 import Data.List (find)
24 import NumericPrelude hiding ( abs )
25 import Algebra.Absolute ( abs )
26 import qualified Algebra.Additive as Additive ( C )
27 import qualified Algebra.Algebraic as Algebraic ( C )
28 import qualified Algebra.Field as Field ( C )
29 import qualified Algebra.RealField as RealField ( C )
30 import qualified Algebra.RealRing as RealRing ( C )
32 import Normed ( Normed(..) )
33 import qualified Roots.Fast as F (
35 fixed_point_iterations,
36 fixed_point_with_iterations,
41 -- | Does the (continuous) function @f@ have a root on the interval
42 -- [a,b]? If f(a) <] 0 and f(b) ]> 0, we know that there's a root in
43 -- [a,b] by the intermediate value theorem. Likewise when f(a) >= 0
49 -- >>> has_root f (-1) 1 Nothing
52 -- This fails if we don't specify an @epsilon@, because cos(-2) ==
53 -- cos(2) doesn't imply that there's a root on [-2,2].
55 -- >>> has_root cos (-2) 2 Nothing
57 -- >>> has_root cos (-2) 2 (Just 0.001)
60 has_root :: (RealField.C a, RealRing.C b)
61 => (a -> b) -- ^ The function @f@
62 -> a -- ^ The \"left\" endpoint, @a@
63 -> a -- ^ The \"right\" endpoint, @b@
64 -> Maybe a -- ^ The size of the smallest subinterval
65 -- we'll examine, @epsilon@
67 has_root f a b epsilon =
68 F.has_root f a b epsilon Nothing Nothing
71 -- | We are given a function @f@ and an interval [a,b]. The bisection
72 -- method finds a root by splitting [a,b] in half repeatedly.
74 -- If one is found within some prescribed tolerance @epsilon@, it is
75 -- returned. Otherwise, the interval [a,b] is split into two
76 -- subintervals [a,c] and [c,b] of equal length which are then both
77 -- checked via the same process.
79 -- Returns 'Just' the value x for which f(x) == 0 if one is found,
80 -- or Nothing if one of the preconditions is violated.
84 -- >>> let actual = 1.5707963267948966
85 -- >>> let Just root = bisect cos 1 2 0.001
88 -- >>> abs (root - actual) < 0.001
91 -- >>> bisect sin (-1) 1 0.001
94 bisect :: (RealField.C a, RealRing.C b)
95 => (a -> b) -- ^ The function @f@ whose root we seek
96 -> a -- ^ The \"left\" endpoint of the interval, @a@
97 -> a -- ^ The \"right\" endpoint of the interval, @b@
98 -> a -- ^ The tolerance, @epsilon@
100 bisect f a b epsilon =
101 F.bisect f a b epsilon Nothing Nothing
104 -- | We are given a function @f@ and an interval [a,b]. The trisection
105 -- method finds a root by splitting [a,b] into three
106 -- subintervals repeatedly.
108 -- If one is found within some prescribed tolerance @epsilon@, it is
109 -- returned. Otherwise, the interval [a,b] is split into two
110 -- subintervals [a,c] and [c,b] of equal length which are then both
111 -- checked via the same process.
113 -- Returns 'Just' the value x for which f(x) == 0 if one is found,
114 -- or Nothing if one of the preconditions is violated.
118 -- >>> let actual = 1.5707963267948966
119 -- >>> let Just root = trisect cos 1 2 0.001
121 -- 1.5713305898491083
122 -- >>> abs (root - actual) < 0.001
125 -- >>> trisect sin (-1) 1 0.001
128 trisect :: (RealField.C a, RealRing.C b)
129 => (a -> b) -- ^ The function @f@ whose root we seek
130 -> a -- ^ The \"left\" endpoint of the interval, @a@
131 -> a -- ^ The \"right\" endpoint of the interval, @b@
132 -> a -- ^ The tolerance, @epsilon@
134 trisect f a b epsilon =
135 F.trisect f a b epsilon Nothing Nothing
138 -- | Find a fixed point of the function @f@ with the search starting
139 -- at x0. We delegate to the version that returns the number of
140 -- iterations and simply discard the number of iterations.
142 fixed_point :: (Normed a, Additive.C a, Algebraic.C b, RealField.C b)
143 => (a -> a) -- ^ The function @f@ to iterate.
144 -> b -- ^ The tolerance, @epsilon@.
145 -> a -- ^ The initial value @x0@.
146 -> a -- ^ The fixed point.
147 fixed_point f epsilon x0 =
148 snd $ F.fixed_point_with_iterations f epsilon x0
151 -- | Return the number of iterations required to find a fixed point of
152 -- the function @f@ with the search starting at x0 and tolerance
153 -- @epsilon@. We delegate to the version that returns the number of
154 -- iterations and simply discard the fixed point.
155 fixed_point_iteration_count :: (Normed a,
159 => (a -> a) -- ^ The function @f@ to iterate.
160 -> b -- ^ The tolerance, @epsilon@.
161 -> a -- ^ The initial value @x0@.
162 -> Int -- ^ The fixed point.
163 fixed_point_iteration_count f epsilon x0 =
164 fst $ F.fixed_point_with_iterations f epsilon x0
167 -- | Returns a list of ratios,
169 -- ||x^{*} - x_{n+1}|| / ||x^{*} - x_{n}||^{p}
171 -- of fixed point iterations for the function @f@ with initial guess
172 -- @x0@ and @p@ some positive power.
174 -- This is used to determine the rate of convergence.
176 fixed_point_error_ratios :: forall a b. (Normed a,
180 => (a -> a) -- ^ The function @f@ to iterate.
181 -> a -- ^ The initial value @x0@.
182 -> a -- ^ The true solution, @x_star@.
183 -> Integer -- ^ The power @p@.
184 -> [b] -- ^ The resulting sequence of x_{n}.
185 fixed_point_error_ratios f x0 x_star p =
186 zipWith (/) en_plus_one en_exp
188 xn = F.fixed_point_iterations f x0
189 en = map (\x -> norm (x_star - x)) xn :: [b]
190 en_plus_one = tail en
195 -- | The sequence x_{n} of values obtained by applying Newton's method
196 -- on the function @f@ and initial guess @x0@.
202 -- >>> let f x = x^6 - x - 1
203 -- >>> let f' x = 6*x^5 - 1
204 -- >>> tail $ take 4 $ newton_iterations f f' 2
205 -- [1.6806282722513088,1.4307389882390624,1.2549709561094362]
207 newton_iterations :: (Field.C a)
208 => (a -> a) -- ^ The function @f@ whose root we seek
209 -> (a -> a) -- ^ The derivative of @f@
210 -> a -- ^ Initial guess, x-naught
212 newton_iterations f f' =
216 xn - ( (f xn) / (f' xn) )
219 -- | Use Newton's method to find a root of @f@ near the initial guess
220 -- @x0@. If your guess is bad, this will recurse forever!
226 -- >>> let f x = x^6 - x - 1
227 -- >>> let f' x = 6*x^5 - 1
228 -- >>> let Just root = newtons_method f f' (1/1000000) 2
230 -- 1.1347241385002211
231 -- >>> abs (f root) < 1/100000
234 -- >>> import Data.Number.BigFloat
235 -- >>> let eps = 1/(10^20) :: BigFloat Prec50
236 -- >>> let Just root = newtons_method f f' eps 2
238 -- 1.13472413840151949260544605450647284028100785303643e0
239 -- >>> abs (f root) < eps
242 newtons_method :: (RealField.C a)
243 => (a -> a) -- ^ The function @f@ whose root we seek
244 -> (a -> a) -- ^ The derivative of @f@
245 -> a -- ^ The tolerance epsilon
246 -> a -- ^ Initial guess, x-naught
248 newtons_method f f' epsilon x0 =
249 find (\x -> abs (f x) < epsilon) x_n
251 x_n = newton_iterations f f' x0
254 -- | Takes a function @f@ of two arguments and repeatedly applies @f@
255 -- to the previous two values. Returns a list containing all
256 -- generated values, f(x0, x1), f(x1, x2), f(x2, x3)...
260 -- >>> let fibs = iterate2 (+) 0 1
262 -- [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]
264 iterate2 :: (a -> a -> a) -- ^ The function @f@
265 -> a -- ^ The initial value @x0@
266 -> a -- ^ The second value, @x1@
267 -> [a] -- ^ The result list, [x0, x1, ...]
272 let next = f prev2 prev1 in
276 -- | The sequence x_{n} of values obtained by applying the secant
277 -- method on the function @f@ and initial guesses @x0@, @x1@.
279 -- The recursion more or less implements a two-parameter 'iterate',
280 -- although one list is passed to the next iteration (as opposed to
281 -- one function argument, with iterate). At each step, we peel the
282 -- first two elements off the list and then compute/append elements
283 -- three, four... onto the end of the list.
289 -- >>> let f x = x^6 - x - 1
290 -- >>> take 4 $ secant_iterations f 2 1
291 -- [2.0,1.0,1.0161290322580645,1.190577768676638]
293 secant_iterations :: (Field.C a)
294 => (a -> a) -- ^ The function @f@ whose root we seek
295 -> a -- ^ Initial guess, x-naught
296 -> a -- ^ Second initial guess, x-one
298 secant_iterations f =
302 let x_change = prev1 - prev2
303 y_change = (f prev1) - (f prev2)
305 (prev1 - (f prev1 * (x_change / y_change)))
308 -- | Use the secant method to find a root of @f@ near the initial guesses
309 -- @x0@ and @x1@. If your guesses are bad, this will recurse forever!
315 -- >>> let f x = x^6 - x - 1
316 -- >>> let Just root = secant_method f (1/10^9) 2 1
318 -- 1.1347241384015196
319 -- >>> abs (f root) < (1/10^9)
322 secant_method :: (RealField.C a)
323 => (a -> a) -- ^ The function @f@ whose root we seek
324 -> a -- ^ The tolerance epsilon
325 -> a -- ^ Initial guess, x-naught
326 -> a -- ^ Second initial guess, x-one
328 secant_method f epsilon x0 x1
329 = find (\x -> abs (f x) < epsilon) x_n
331 x_n = secant_iterations f x0 x1