Update from HH
[Flyspeck/.git] / port_interval / univariate.hl
1 (* port of univariate.cc
2    a univariate represents a function u:real->real.
3    its first derivative du:real->real
4    and its second derivative ddu;real->real.
5
6    For example, if the function is x |-> x,
7    its derivative is x |-> 1,
8    and second derivative is x |-> 0,
9    which is implemented as ux1.
10
11    We give a few other examples, sqrt,  1/x, atan.
12
13  *)
14
15 module Univariate = struct
16
17 open Interval;;
18
19 let eval uni x  = function 
20   | 0 -> uni.u x
21   | 1 -> uni.du x
22   | _ -> uni.ddu x;;
23
24 let mk_univariate (u1,du1,ddu1) = { u = u1; du = du1; ddu = ddu1; };;
25
26 let raise_zero x = bounded_from_zero x or  raise Unstable ;;
27
28 (* here are a couple of examples *)
29
30 let ux1 =
31   mk_univariate(
32     (fun x -> x),
33     (fun x -> one),
34     (fun x-> zero)
35   );;
36
37 let usqrt = 
38   let ( / ) = idiv in
39   let ( * ) = imul in
40     mk_univariate(
41       isqrt,
42
43       (fun x -> 
44         let _ = raise_zero x in
45           one / (two * isqrt x)),
46
47       (fun x ->
48          let _ = raise_zero x in
49            ineg (one / (four * x *  isqrt x)))
50 );;
51
52 let uinv = 
53   let ( / ) = idiv in
54   let ( * ) = imul in
55     mk_univariate(
56       (fun x ->
57          let _ = raise_zero x in
58            one / x),
59
60       (fun x ->
61          let _ = raise_zero x in
62            ineg (one / ( x * x))),
63
64       (fun x ->
65          let _ = raise_zero x in
66             two / ( x * x * x))
67     );;
68
69 let uatan = 
70   let ( / ) = idiv in
71   let ( * ) = imul in
72   let ( + ) = iadd in
73     mk_univariate(
74       iatan,
75
76       (fun x ->
77          one / (one + x * x)),
78
79       (fun x ->
80          let t = one + x * x in
81            ineg (two * x/ (t * t)))
82     );;
83
84 end;;