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
use enums;
fn my_max(x1: f64, x2: f64) -> f64 {
if x1 > x2 {
x1
} else {
x2
}
}
fn central_deriv<T>(f: ::function<T>, param: &mut T, x: f64, h: f64, result: &mut f64, abs_err_round: &mut f64, abs_err_trunc: &mut f64) {
let fm1 = f(x - h, param);
let fp1 = f(x + h, param);
let fmh = f(x - h / 2f64, param);
let fph = f(x + h / 2f64, param);
let r3 = 0.5f64 * (fp1 - fm1);
let r5 = (4f64 / 3f64) * (fph - fmh) - (1f64 / 3f64) * r3;
let e3 = unsafe { (fp1.abs() + fm1.abs()) * ::DBL_EPSILON };
let e5 = unsafe { 2f64 * (fph.abs() + fmh.abs()) * ::DBL_EPSILON + e3 };
let dy = unsafe { my_max((r3 / h).abs(), (r5 / h).abs()) * (x.abs() / h) * ::DBL_EPSILON };
*result = r5 / h;
*abs_err_trunc = unsafe { ((r5 - r3) / h).abs() };
*abs_err_round = unsafe { (e5 / h).abs() + dy };
}
pub fn deriv_central<T>(f: ::function<T>, param: &mut T, x: f64, h: f64, result: &mut f64, abs_err: &mut f64) -> enums::Value {
let mut r_0 = 0f64;
let mut round = 0f64;
let mut trunc = 0f64;
central_deriv(f, param, x, h, &mut r_0, &mut round, &mut trunc);
let mut error = round + trunc;
if round < trunc && (round > 0f64 && trunc > 0f64) {
let mut r_opt = 0f64;
let mut round_opt = 0f64;
let mut trunc_opt = 0f64;
let h_opt = unsafe { h * (round / (2f64 * trunc)).powf(1f64 / 3f64) };
central_deriv(f, param, x, h_opt, &mut r_opt, &mut round_opt, &mut trunc_opt);
let error_opt = round_opt + trunc_opt;
if error_opt < error && unsafe { (r_opt - r_0).abs() } < 4f64 * error {
r_0 = r_opt;
error = error_opt;
}
}
*result = r_0;
*abs_err = error;
::Value::Success
}
fn forward_deriv<T>(f: ::function<T>, param: &mut T, x: f64, h: f64, result: &mut f64, abs_err_round: &mut f64, abs_err_trunc: &mut f64) {
let f1 = f(x + h / 4f64, param);
let f2 = f(x + h / 2f64, param);
let f3 = f(x + (3f64 / 4f64) * h, param);
let f4 = f(x + h, param);
let r2 = 2f64 * (f4 - f2);
let r4 = (22f64 / 3f64) * (f4 - f3) - (62f64 / 3f64) * (f3 - f2) + (52f64 / 3f64) * (f2 - f1);
let e4 = unsafe { 2f64 * 20.67f64 * (f4.abs() + f3.abs() + f2.abs() + f1.abs()) * ::DBL_EPSILON };
let dy = unsafe { my_max((r2 / h).abs(), (r4 / h).abs()) * (x.abs() / h) * ::DBL_EPSILON };
*result = r4 / h;
*abs_err_trunc = unsafe { ((r4 - r2) / h).abs() };
*abs_err_round = unsafe { (e4 / h).abs() + dy };
}
pub fn deriv_forward<T>(f: ::function<T>, param: &mut T, x: f64, h: f64, result: &mut f64, abs_err: &mut f64) -> enums::Value {
let mut r_0 = 0f64;
let mut round = 0f64;
let mut trunc = 0f64;
forward_deriv(f, param, x, h, &mut r_0, &mut round, &mut trunc);
let mut error = round + trunc;
if round < trunc && (round > 0f64 && trunc > 0f64) {
let mut r_opt = 0f64;
let mut round_opt = 0f64;
let mut trunc_opt = 0f64;
let h_opt = unsafe { h * (round / trunc).powf(1f64 / 2f64) };
forward_deriv(f, param, x, h_opt, &mut r_opt, &mut round_opt, &mut trunc_opt);
let error_opt = round_opt + trunc_opt;
if error_opt < error && unsafe { (r_opt - r_0).abs() } < 4f64 * error {
r_0 = r_opt;
error = error_opt;
}
}
*result = r_0;
*abs_err = error;
::Value::Success
}
pub fn deriv_backward<T>(f: ::function<T>, param: &mut T, x: f64, h: f64, result: &mut f64, abs_err: &mut f64) -> enums::Value {
deriv_forward(f, param, x, -h, result, abs_err)
}