1 -- | The Roots.Simple module contains root-finding algorithms. That
2 -- is, procedures to (numerically) find solutions to the equation,
6 -- where f is assumed to be continuous on the interval of interest.
12 import Data.List (find)
16 import qualified Roots.Fast as F
18 -- | Does the (continuous) function @f@ have a root on the interval
19 -- [a,b]? If f(a) <] 0 and f(b) ]> 0, we know that there's a root in
20 -- [a,b] by the intermediate value theorem. Likewise when f(a) >= 0
26 -- >>> has_root f (-1) 1 Nothing
29 -- This fails if we don't specify an @epsilon@, because cos(-2) ==
30 -- cos(2) doesn't imply that there's a root on [-2,2].
32 -- >>> has_root cos (-2) 2 Nothing
34 -- >>> has_root cos (-2) 2 (Just 0.001)
37 has_root :: (Fractional a, Ord a, Ord b, Num b)
38 => (a -> b) -- ^ The function @f@
39 -> a -- ^ The \"left\" endpoint, @a@
40 -> a -- ^ The \"right\" endpoint, @b@
41 -> Maybe a -- ^ The size of the smallest subinterval
42 -- we'll examine, @epsilon@
44 has_root f a b epsilon =
45 F.has_root f a b epsilon Nothing Nothing
50 -- | We are given a function @f@ and an interval [a,b]. The bisection
51 -- method checks finds a root by splitting [a,b] in half repeatedly.
53 -- If one is found within some prescribed tolerance @epsilon@, it is
54 -- returned. Otherwise, the interval [a,b] is split into two
55 -- subintervals [a,c] and [c,b] of equal length which are then both
56 -- checked via the same process.
58 -- Returns 'Just' the value x for which f(x) == 0 if one is found,
59 -- or Nothing if one of the preconditions is violated.
63 -- >>> bisect cos 1 2 0.001
66 -- >>> bisect sin (-1) 1 0.001
69 bisect :: (Fractional a, Ord a, Num b, Ord b)
70 => (a -> b) -- ^ The function @f@ whose root we seek
71 -> a -- ^ The \"left\" endpoint of the interval, @a@
72 -> a -- ^ The \"right\" endpoint of the interval, @b@
73 -> a -- ^ The tolerance, @epsilon@
75 bisect f a b epsilon =
76 F.bisect f a b epsilon Nothing Nothing
80 -- | The sequence x_{n} of values obtained by applying Newton's method
81 -- on the function @f@ and initial guess @x0@.
86 -- >>> let f x = x^6 - x - 1
87 -- >>> let f' x = 6*x^5 - 1
88 -- >>> tail $ take 4 $ newton_iterations f f' 2
89 -- [1.6806282722513088,1.4307389882390624,1.2549709561094362]
91 newton_iterations :: (Fractional a, Ord a)
92 => (a -> a) -- ^ The function @f@ whose root we seek
93 -> (a -> a) -- ^ The derivative of @f@
94 -> a -- ^ Initial guess, x-naught
96 newton_iterations f f' x0 =
100 xn - ( (f xn) / (f' xn) )
104 -- | Use Newton's method to find a root of @f@ near the initial guess
105 -- @x0@. If your guess is bad, this will recurse forever!
111 -- >>> let f x = x^6 - x - 1
112 -- >>> let f' x = 6*x^5 - 1
113 -- >>> let Just root = newtons_method f f' (1/1000000) 2
115 -- 1.1347241385002211
116 -- >>> abs (f root) < 1/100000
119 -- >>> import Data.Number.BigFloat
120 -- >>> let eps = 1/(10^20) :: BigFloat Prec50
121 -- >>> let Just root = newtons_method f f' eps 2
123 -- 1.13472413840151949260544605450647284028100785303643e0
124 -- >>> abs (f root) < eps
127 newtons_method :: (Fractional a, Ord a)
128 => (a -> a) -- ^ The function @f@ whose root we seek
129 -> (a -> a) -- ^ The derivative of @f@
130 -> a -- ^ The tolerance epsilon
131 -> a -- ^ Initial guess, x-naught
133 newtons_method f f' epsilon x0 =
134 find (\x -> abs (f x) < epsilon) x_n
136 x_n = newton_iterations f f' x0
140 -- | Takes a function @f@ of two arguments and repeatedly applies @f@
141 -- to the previous two values. Returns a list containing all
142 -- generated values, f(x0, x1), f(x1, x2), f(x2, x3)...
146 -- >>> let fibs = iterate2 (+) 0 1
148 -- [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]
150 iterate2 :: (a -> a -> a) -- ^ The function @f@
151 -> a -- ^ The initial value @x0@
152 -> a -- ^ The second value, @x1@
153 -> [a] -- ^ The result list, [x0, x1, ...]
158 let next = f prev2 prev1 in
161 -- | The sequence x_{n} of values obtained by applying the secant
162 -- method on the function @f@ and initial guesses @x0@, @x1@.
164 -- The recursion more or less implements a two-parameter 'iterate',
165 -- although one list is passed to the next iteration (as opposed to
166 -- one function argument, with iterate). At each step, we peel the
167 -- first two elements off the list and then compute/append elements
168 -- three, four... onto the end of the list.
173 -- >>> let f x = x^6 - x - 1
174 -- >>> take 4 $ secant_iterations f 2 1
175 -- [2.0,1.0,1.0161290322580645,1.190577768676638]
177 secant_iterations :: (Fractional a, Ord a)
178 => (a -> a) -- ^ The function @f@ whose root we seek
179 -> a -- ^ Initial guess, x-naught
180 -> a -- ^ Second initial guess, x-one
182 secant_iterations f x0 x1 =
186 let x_change = prev1 - prev2
187 y_change = (f prev1) - (f prev2)
189 (prev1 - (f prev1 * (x_change / y_change)))
192 -- | Use the secant method to find a root of @f@ near the initial guesses
193 -- @x0@ and @x1@. If your guesses are bad, this will recurse forever!
198 -- >>> let f x = x^6 - x - 1
199 -- >>> let Just root = secant_method f (1/10^9) 2 1
201 -- 1.1347241384015196
202 -- >>> abs (f root) < (1/10^9)
205 secant_method :: (Fractional a, Ord a)
206 => (a -> a) -- ^ The function @f@ whose root we seek
207 -> a -- ^ The tolerance epsilon
208 -> a -- ^ Initial guess, x-naught
209 -> a -- ^ Second initial guess, x-one
211 secant_method f epsilon x0 x1
212 = find (\x -> abs (f x) < epsilon) x_n
214 x_n = secant_iterations f x0 x1
218 fixed_point_iterations :: (a -> a) -- ^ The function @f@ to iterate.
219 -> a -- ^ The initial value @x0@.
220 -> [a] -- ^ The resulting sequence of x_{n}.
221 fixed_point_iterations f x0 =
225 -- | Find a fixed point of the function @f@ with the search starting
226 -- at x0. This will find the first element in the chain f(x0),
227 -- f(f(x0)),... such that the magnitude of the difference between it
228 -- and the next element is less than epsilon.
230 fixed_point :: (Num a, Vector a, RealFrac b)
231 => (a -> a) -- ^ The function @f@ to iterate.
232 -> b -- ^ The tolerance, @epsilon@.
233 -> a -- ^ The initial value @x0@.
234 -> a -- ^ The fixed point.
235 fixed_point f epsilon x0 =
238 xn = fixed_point_iterations f x0
239 xn_plus_one = tail $ fixed_point_iterations f x0
241 abs_diff v w = norm (v - w)
243 -- The nth entry in this list is the absolute value of x_{n} -
245 differences = zipWith abs_diff xn xn_plus_one
247 -- A list of pairs, (xn, |x_{n} - x_{n+1}|).
248 pairs = zip xn differences
250 -- The pair (xn, |x_{n} - x_{n+1}|) with
251 -- |x_{n} - x_{n+1}| < epsilon. The pattern match on 'Just' is
252 -- "safe" since the list is infinite. We'll succeed or loop
254 Just winning_pair = find (\(_, diff) -> diff < epsilon) pairs