-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefine.c
100 lines (80 loc) · 2.44 KB
/
refine.c
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
#include "include.h"
#define TOLERANCE 1e-10
long double brent_method(long double (*f)(long double), long double a, long double b, long double tol) {
long double fa = f(a);
long double fb = f(b);
if (fa * fb >= 0) {
printf("Function does not change sign over the interval\n");
return -1;
}
long double c = a, fc = fa, s, fs;
long double d = b - a, e = d;
while (f_abs(b - a) > tol) {
if (fa != fc && fb != fc) {
s = a * fb * fc / ((fa - fb) * (fa - fc)) +
b * fa * fc / ((fb - fa) * (fb - fc)) +
c * fa * fb / ((fc - fa) * (fc - fb));
}
else {
s = b - fb * (b - a) / (fb - fa);
}
if (!((s > (3 * a + b) / 4 && s < b) ||
(s < (3 * a + b) / 4 && s > a)) ||
f_abs(s - b) >= f_abs(b - c) / 2 ||
f_abs(b - c) < tol) {
s = (a + b) / 2;
}
fs = f(s);
d = c; c = b; fc = fb;
if (fa * fs < 0) {
b = s;
fb = fs;
} else {
a = s;
fa = fs;
}
if (f_abs(fa) < f_abs(fb)) {
long double temp = a;
a = b;
b = temp;
temp = fa;
fa = fb;
fb = temp;
}
}
return b;
}
long double Z_wrapper(long double t) {
return Z(t, 16);
}
void refine_zeros(Zero *zeros, int zero_count) {
for (int i = 0; i < zero_count; i++) {
long double a = zeros[i].t - 0.001L;
long double b = zeros[i].t + 0.001L;
zeros[i].t = brent_method(Z_wrapper, a, b, 1e-2);
zeros[i].z = fabsl(Z(zeros[i].t, 15));
}
}
void refine_and_group_zeros(Zero *zeros, int zero_count) {
Zero refined_zeros[zero_count];
int refined_count = 0;
int i = 0;
while (i < zero_count) {
long double current_t = zeros[i].t;
long double best_z = zeros[i].z;
long double best_t = current_t;
while (i < zero_count && ((long long)(zeros[i].t * 100.0L)) == ((long long)(current_t * 100.0L))) {
if (zeros[i].z < best_z) {
best_z = zeros[i].z;
best_t = zeros[i].t;
}
i++;
}
refined_zeros[refined_count].t = best_t;
refined_zeros[refined_count].z = best_z;
refined_count++;
}
for (int j = 0; j < refined_count; j++) {
print_zero(refined_zeros[j]);
}
}