Source file Root1D.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(* File: Root1D.ml

   Copyright (C) 2007-

     Christophe Troestler
     email: Christophe.Troestler@umons.ac.be
     WWW: http://math.umons.ac.be/anum/software/

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
   AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
   OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
   NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  *)

open Printf

let eps = sqrt epsilon_float

exception Root of float
(* Internal exception used when the exact root is found *)

let max_float (a: float) (b: float) =
  if a >= b then a (* ≠ NaN *)
  else b

(* Assume fa = f(a) < 0 < fb = f(b).  A recursive function is more
   elegant but we do it imperatively to allow the compiler to unbox
   the floats. *)
let do_bisection improve f a b fa fb =
  let a = ref a and b = ref b
  and fa = ref fa and fb = ref fb in
  try
    while improve !a !b do
      let m = !a +. 0.5 *. (!b -. !a) in
      let fm = f m in
      if fm = 0. then raise(Root m)
      else if fm < 0. then (a := m; fa := fm)
      else (* fm > 0. *) (b := m; fb := fm)
    done;
    !a +. 0.5 *. (!b -. !a)
  with Root r -> r

let bisection_improve_default a b =
  abs_float(a -. b) > eps *. max_float (abs_float a) (abs_float b)

let bisection_improve_eps eps a b =
  abs_float(a -. b) > eps *. max_float (abs_float a) (abs_float b)

let bisection ?eps f a b =
  let improve = match eps with
    | None -> bisection_improve_default
    | Some eps ->
       if eps <= 0. then invalid_arg "Root1D.bisection: tol <= 0";
       bisection_improve_eps eps in
  let fa = f a
  and fb = f b in
  if fa = 0. then a
  else if fa < 0. then
    if fb = 0. then b
    else if fb < 0. then
      invalid_arg "Root1D.bisection: f(a) and f(b) are both < 0."
    else (* fb > 0. *)
      do_bisection improve f a b fa fb
  else (* fa > 0. *)
    if fb = 0. then b
    else if fb > 0. then
      invalid_arg "Root1D.bisection: f(a) and f(b) are both > 0."
    else (* fb < 0. *)
      do_bisection improve f b a fb fa

let newton_good x xpre fx = abs_float fx < eps

let newton ?(good_enough=newton_good) f_f' x0 =
  let x = ref x0
  and xpre = ref nan (* FIXME: confusing for good_enough *)
  and fx, f'x = f_f' x0 in
  let fx = ref fx
  and f'x = ref f'x in
  while not(good_enough !x !xpre !fx) do
    if !f'x = 0. then failwith(sprintf "Root1D.newton: f'(%g) = 0" !x);
    x := !x -. !fx /. !f'x;
    let fx_next, f'x_next = f_f' !x in
    fx := fx_next;
    f'x := f'x_next;
  done;
  !x *. 1.


type last_iter = A | B

(* Assume fa = f(a) < 0 < f(b) = fb. *)
let do_illinois improve f a b fa fb =
  let a = ref a
  and b = ref b in
  let fa = ref fa in
  let fb = ref fb in
  try
    (* One could also implement this using x0 and x1, the last 2
       iterations, but this is slightly slower. *)
    let last = ref B in
    while improve !a !b do
      let x = !b -. !fb *. (!b -. !a) /. (!fb -. !fa) in
      let fx = f x in
      if fx = 0. then raise(Root x);
      match !last with
      | A -> if fx > 0. then (b := x;  fb := fx;  last := B)
             else (a := x;  fa := fx;  fb := 0.5 *. !fb)
      | B -> if fx < 0. then (a := x;  fa := fx;  last := A)
             else (b := x;  fb := fx;  fa := 0.5 *. !fa)
    done;
    match !last with A -> !a | B -> !b
  with Root r -> r

let illinois ?eps f a b =
  let improve = match eps with
    | None -> bisection_improve_default
    | Some eps ->
       if eps <= 0. then invalid_arg "Root1D.bisection: tol <= 0";
       bisection_improve_eps eps in
  let fa = f a in
  if fa = 0. then a
  else if fa < 0. then
    let fb = f b in
    if fb = 0. then b
    else if fb < 0. then
      invalid_arg "Root.illinois: f(a) and f(b) are both < 0."
    else (* fb > 0. *) do_illinois improve f a b fa fb
  else (* fa > 0. *)
    let fb = f b in
    if fb = 0. then b
    else if fb > 0. then
      invalid_arg "Root.illinois: f(a) and f(b) are both > 0."
    else (* fb < 0. *) do_illinois improve f b a fb fa


let muller f a b = a



(* Based on http://www.netlib.org/c/brent.shar zeroin  and
   http://www.netlib.org/go/zeroin.f

   Algorithm

   G.Forsythe, M.Malcolm, C.Moler, Computer methods for mathematical
   computations. M., Mir, 1980, p.180 of the Russian edition

   The function makes use of the bissection procedure combined with
   the linear or quadric inverse interpolation.
   At every step program operates on three abscissae - a, b, and c.
   b - the last and the best approximation to the root
   a - the last but one best approximation
   c - the last but one or even earlier approximation than a that
       1) |f(b)| <= |f(c)|
       2) f(b) and f(c) have opposite signs, i.e. b and c confine
     	  the root
   At every step Zeroin selects one of the two new approximations, the
   former being obtained by the bissection procedure and the latter
   resulting in the interpolation (if a,b, and c are all different
   the quadric interpolation is utilized, otherwise the linear one).
   If the latter (i.e. obtained by the interpolation) point is
   reasonable (i.e. lies within the current interval [b,c] not being
   too close to the boundaries) it is accepted. The bissection result
   is used in the other case. Therefore, the range of uncertainty is
   ensured to be reduced at least by the factor 1.6

   See also www.physics.mcgill.ca/~patscott/teaching/numeric/Lec%203.pdf *)
let brent ?(tol=eps) f a0 b0 =
  let a = ref a0
  and b = ref b0
  and c = ref a0 in
  let fa = ref(f !a)
  and fb = ref(f !b) in
  let fc = ref(!fa) in
  if !fa = 0. then !a
  else if !fb = 0. then !b
  else if !fa *. !fb > 0. then
    invalid_arg "Root1D.brent: f(a) and f(b) must have opposite signs"
  else (
    let continue = ref true in
    while !continue do
      let prev_step = !b -. !a in
      (* Swap b and c for b to be the best approximation *)
      if abs_float !fc < abs_float !fb then (
        a := !b;   b := !c;   c := !a;
        fa := !fb; fb := !fc; fc := !fa;
      );
      let tol_act = 2. *. epsilon_float *. abs_float(!b) +. 0.5 *. tol in
      let c_b = !c -. !b in
      if 0.5 *. abs_float c_b <= tol_act || !fb = 0. then
        continue := false (* the root is in [b] *)
      else (
        let new_step =
          if abs_float prev_step >= tol_act
             && abs_float !fa > abs_float !fb then
            (* prev_step was large enough and was in true direction,
             Interpolatiom may be tried *)
            let p, q =
              if !a = !c then
                (* linear interpolation *)
                let s = !fb /. !fa in (c_b *. s, 1. -. s)
              else
                (* Quadric inverse interpolation *)
                let t = !fa /. !fc and r = !fb /. !fc and s = !fb /. !fa in
                (s *. (c_b *. t *. (t -. r) -. (!b -. !a) *. (r -. 1.)),
                 (t -. 1.) *. (r -. 1.) *. (s -. 1.)) in
            let p, q = if p > 0. then p, -. q else -. p, q in
            (* If b+p/q falls in [b,c] and isn't too large, it is accepted *)
            if p < 0.75 *. c_b *. q -. 0.5 *. abs_float(tol_act *. q)
               && p < abs_float(0.5 *. prev_step *. q) then p /. q
            else 0.5 *. c_b
          else 0.5 *. c_b in
        a := !b;  fa := !fb; (* Save the previous approx. *)
        if abs_float new_step > tol_act then
          b := !b +. new_step
        else
          (* Adjust the step to be not less than tolerance *)
          b := !b +. copysign tol_act c_b;
        fb := f(!b);
        (* Adjust c for it to have a sign opposite to that of b *)
        if !fb *. !fc > 0. then (
          c := !a;  fc := !fa;
          assert(!fb *. !fc <= 0.);
        )
      )
    done;
    !b
  )

let twice_epsilon_float = 2. *. epsilon_float

let rec brent_loop half_tol f a fa b fb c fc d e =
  (* [b]: best guess for the root, |f(b)| ≤ |f(a)|, |f(c)|.
     [c]: opposite side of x axis to [b], so [b] and [c] bracket the root.
     [a]: previous best guess.
   *)
  let tol_act = twice_epsilon_float *. abs_float(b) +. half_tol in
  let m = 0.5 *. (c -. b) in
  if abs_float m <= tol_act || fb = 0. then b
  else (
    let step, e' =
      if abs_float e < tol_act || abs_float fa <= abs_float fb then
        m, m (* bisection *)
      else
        (* prev_step was large enough and was in true direction,
           Interpolatiom may be tried *)
        let s = fb /. fa in
        let p, q =
          if a = c then (* Linear interpolation *)
            (2. *. m *. s, 1. -. s)
          else
            (* Inverse quadratic interpolation *)
            let q = fa /. fc and r = fb /. fc in
            (s *. (2. *. m *. q *. (q -. r) -. (b -. a) *. (r -. 1.)),
             (q -. 1.) *. (r -. 1.) *. (s -. 1.)) in
        let p, q = if p > 0. then p, -. q else -. p, q in
        (* If b+p/q falls in [b,c] and isn't too large, it is accepted *)
        if 2. *. p < 3. *. m *. q -. abs_float(tol_act *. q)
           && p < abs_float(0.5 *. e *. q)
        then p /. q, d
        else m, m (* bisection *)
    in
    (* Adjust the step to be not less than tolerance *)
    let b' = b +. (if abs_float step > tol_act then step
                   else if m > 0. then tol_act else -. tol_act) in
    let fb' = f b' in
    (* Adjust c for it to have a sign opposite to that of b *)
    if (fb' > 0.) = (fc > 0.) then (* => fb' * fb <= 0 *)
      let d = b' -. b in
      if abs_float fb < abs_float fb' then
        brent_loop half_tol f b' fb' b fb b' fb' d d
      else
        brent_loop half_tol f b fb b' fb' b fb d d
    else (* => fb' * fc <= 0 *)
      if abs_float fc < abs_float fb' then
        brent_loop half_tol f b' fb' c fc b' fb' step e'
      else
        brent_loop half_tol f b fb b' fb' c fc step e'
  )
;;

let brent1 ?(tol=eps) f a b =
  if tol < 0. then invalid_arg "Root1D.brent: tol < 0.";
  let fa = f a and fb = f b in
  if fa = 0. then a
  else if fb = 0. then b
  else if (fa < 0. && fb < 0.) || (fa > 0. && fb > 0.) then
    invalid_arg "Root1D.brent: f(a) and f(b) must have opposite signs"
  else
    let d = b -. a in
    if abs_float fa < abs_float fb then
      brent_loop (0.5 *. tol) f b fb a fa b fb d d
    else
      brent_loop (0.5 *. tol) f a fa b fb a fa d d


let rec brent2_loop half_tol f  a fa ea  b fb eb  c fc ec  d e =
  let tol_act = twice_epsilon_float *. abs_float(b) +. half_tol in
  let m = 0.5 *. (c -. b) in
  if abs_float m <= tol_act || fb = 0. then b
  else (
    let step, e' =
      if abs_float e < tol_act
         || (ea <= eb && ldexp (abs_float fa) (ea - eb) <= abs_float fb)
         || (ea > eb && ldexp (abs_float fb) (eb - ea) >= abs_float fa) then
        m, m
      else
        let s = ldexp fb (eb - ea) /. fa in
        let p, q =
          if a = c then (* Linear interpolation *)
            (2. *. m *. s, 1. -. s)
          else
            (* Inverse quadratic interpolation *)
            let q = ldexp fa (ea - ec) /. fc
            and r = ldexp fb (eb - ec) /. fc in
            (s *. (2. *. m *. q *. (q -. r) -. (b -. a) *. (r -. 1.)),
             (q -. 1.) *. (r -. 1.) *. (s -. 1.)) in
        let p, q = if p > 0. then p, -. q else -. p, q in
        (* If b+p/q falls in [b,c] and isn't too large, it is accepted *)
        if 2. *. p < 3. *. m *. q -. abs_float(tol_act *. q)
           && p < abs_float(0.5 *. e *. q)
        then p /. q, d
        else m, m
    in
    (* Adjust the step to be not less than tolerance *)
    let b' = b +. (if abs_float step > tol_act then step
                   else if m > 0. then tol_act else -. tol_act) in
    let fb', eb' = f b' in
    (* Adjust c for it to have a sign opposite to that of b *)
    if (fb' > 0.) = (fc > 0.) then (* => fb' * fb <= 0 *)
      let d = b' -. b in
      if (eb <= eb' && ldexp (abs_float fb) (eb - eb') < abs_float fb')
         || (eb > eb' && ldexp (abs_float fb') (eb' - eb) >= abs_float fb) then
        brent2_loop half_tol f b' fb' eb' b fb eb b' fb' eb' d d
      else
        brent2_loop half_tol f b fb eb b' fb' eb' b fb eb d d
    else (* => fb' * fc <= 0 *)
      if (ec <= eb' && ldexp (abs_float fc) (ec - eb') < abs_float fb')
         || (ec > eb' && ldexp (abs_float fb') (eb' - ec) >= abs_float fc) then
        brent2_loop half_tol f b' fb' eb' c fc ec b' fb' eb' step e'
      else
        brent2_loop half_tol f b fb eb b' fb' eb' c fc ec step e'
  )

let brent2 ?(tol=eps) f a b =
  if tol < 0. then invalid_arg "Root1D.brent2: tol < 0.";
  let fa, ea = f a and fb, eb = f b in
  if fa = 0. then a
  else if fb = 0. then b
  else if (fa < 0. && fb < 0.) || (fa > 0. && fb > 0.) then
    invalid_arg "Root1D.brent: f(a) and f(b) must have opposite signs"
  else
    let d = b -. a in
    if (ea <= eb && ldexp (abs_float fa) (ea - eb) < abs_float fb)
       || (ea > eb && ldexp (abs_float fb) (eb - ea) >= abs_float fa) then
      brent2_loop (0.5 *. tol) f  b fb eb  a fa ea  b fb eb  d d
    else
      brent2_loop (0.5 *. tol) f  a fa ea  b fb eb  a fa ea  d d


(* Local Variables: *)
(* compile-command: "make -k -C .." *)
(* End: *)