File Coverage

inverse_interpolate.c
Criterion Covered Total %
statement 95 103 92.2
branch 135 206 65.5
condition n/a
subroutine n/a
pod n/a
total 230 309 74.4


line stmt bran cond sub pod time code
1             #include
2             #include
3             #include
4              
5             #include "ptypes.h"
6             #include "inverse_interpolate.h"
7             #include "util.h"
8             #include "mathl.h"
9              
10             static const int _dbgprint = 0;
11              
12             /* TODO: Consider Brent's method. */
13              
14              
15             /* Return x with v(x)=func(x,k) s.t. either of:
16             * 1. v(x) == n and v(x-1-threshold) < n
17             * 2. v(x) < n and v(x+1) > n
18             */
19              
20             #define LINEAR_INTERP(n, lo, hi, rlo, rhi) \
21             (lo + (UV) (((double)(n-rlo) * (double)(hi-lo) / (double)(rhi-rlo))+0.5))
22              
23             #define MPU_CALLBACK(n) ((funck) ? funck(n,k) : func(n))
24              
25             #if 0 /* Debugging return, checking the conditions above. */
26             #define RETURNI(x) \
27             { \
28             UV v = x; \
29             UV rv = MPU_CALLBACK(v); \
30             /* printf("v %lu rv %lu n %lu\n",v,rv,n); */\
31             MPUassert( rv <= n, "BAD INTERP v > n" ); \
32             if (rv == n) { \
33             if (v > threshold) { \
34             /* printf("threshold %lu v %lu func(%lu) = %lu\n", threshold, v, v-1-threshold, MPU_CALLBACK(v-1-threshold)); */\
35             MPUassert( MPU_CALLBACK(v-1-threshold) < n, "BAD INTERP v-1-thresh >= n" ); \
36             } \
37             } else { \
38             MPUassert( MPU_CALLBACK(v+1) > n, "BAD INTERP v+1 <= n" ); \
39             } \
40             return v; \
41             }
42             #else
43             #define RETURNI(x) { return x; }
44             #endif
45              
46 3101           static UV _inverse_interpolate(UV lo, UV hi, UV n,
47             UV k, UV (*funck)(UV mid, UV k),
48             UV (*func)(UV mid),
49             UV threshold) {
50             UV mid, rlo, rhi, rmid, iloopc;
51              
52 3101 100         if (hi != 0) {
53             /* Given both lo and hi, halve the range on start. */
54 2920           mid = lo + ((hi-lo)>>1);
55 2920 100         rmid = MPU_CALLBACK(mid);
56 2920 50         if(_dbgprint)printf(" 01 lo %lu mid %lu hi %lu\n", lo, mid, hi);
57 2920 100         if (rmid >= n) {
58 2373           hi = mid; rhi = rmid;
59 2373 100         rlo = MPU_CALLBACK(lo);
60 2373 50         if (rlo == n) RETURNI(lo); /* Possible bad limit */
61             } else {
62 547           lo = mid; rlo = rmid;
63 547 100         rhi = MPU_CALLBACK(hi);
64             }
65             } else {
66             /* They don't know what hi might be, so estimate something. */
67 181 50         rlo = MPU_CALLBACK(lo);
68 181 50         if (rlo == n) RETURNI(lo); /* Possible bad limit */
69 181           rhi = UV_MAX; /* this should always be replaced below */
70 425 100         while (hi == 0) {
71 244           double estf = (double)n/(double)rlo - 0.004;
72 244 100         if (estf <= 1.004) estf = 1.004;
73 232 100         else if (estf > 8.0) estf = 8.0;
74 488           mid = ((double)UV_MAX/(double)lo <= estf) ? UV_MAX
75 244 50         : (UV) (estf * (double)lo + 1);
76 244 50         if(_dbgprint)printf(" 0s lo %lu mid %lu hi %lu\n", lo, mid, hi);
77 244 50         rmid = MPU_CALLBACK(mid);
78 244 100         if (rmid >= n) { hi = mid; rhi = rmid; }
79 63           else { lo = mid; rlo = rmid; }
80 244 50         if (lo == UV_MAX) break; /* Overflow */
81             }
82             }
83              
84 3101 50         MPUassert(rlo <= n && rhi >= n, "interpolation: bad initial limits");
    50          
85 3101 100         if ((hi-lo) <= 1) RETURNI( (rlo == n || (rlo < n && rhi > n)) ? lo : hi );
    50          
    50          
    50          
86              
87             /* Step 1. Linear interpolation until rhi is correct. */
88 3100 50         if(_dbgprint)printf(" 1 lo %lu hi %lu\n", lo, hi);
89              
90 3100 100         mid = (n == rhi) ? hi-1 : LINEAR_INTERP(n,lo,hi,rlo,rhi);
91 3100 100         if (mid == lo) mid++; else if (mid == hi) mid--;
    100          
92              
93 4119 100         for (iloopc = 1; (hi-lo) > 1 && rhi > n; iloopc++) {
    100          
94 3884 50         MPUassert(lo < mid && mid < hi, "interpolation: assume 3 unique points");
    50          
95 3884 100         rmid = MPU_CALLBACK(mid);
96 3884 100         if (rmid >= n) { hi = mid; rhi = rmid; }
97 605           else { lo = mid; rlo = rmid; }
98 3884 100         if (rhi == n) break;
99 1019           mid += (IV)(((double)n-(double)rmid)*(double)(hi-lo) / (double)(rhi-rlo));
100             /* Sometimes we get stuck getting closer and closer but not bracketing.
101             * We could do Ridder's method of alternating bisection, or using a
102             * multiplier on mid on alternate iterations to reflect about n.
103             * What we're going to do instead is, every few loops, check if we're
104             * very close to one of the edges and try to pull in the other edge.
105             */
106 1019 100         if ((iloopc % 6) == 0) {
107 12           UV close = .003*(hi-lo) + 1.0;
108 12 50         if (lo+close > mid) mid = lo+close;
109 12 50         else if (hi-close < mid) mid = hi-close;
110             }
111             /* Alternately:
112             if (mid == lo) { mid = lo + .01*(hi-lo); }
113             else if (mid == hi) { mid = hi - .01*(hi-lo); }
114             */
115 1019 100         if (mid <= lo) mid=lo+1; else if (mid >= hi) mid=hi-1;
    100          
116 1019 50         MPUassert(lo <= mid && mid <= hi, "interpolation: range error");
    50          
117 1019 50         if(_dbgprint)printf(" 1s lo %lu mid %lu hi %lu (%lu)\n", lo, mid, hi, rhi-n);
118             }
119              
120 3100 50         if (rlo == n) RETURNI(lo);
121 3100 100         if ((hi-lo) <= 1) RETURNI((rlo == n || (rlo < n && rhi > n)) ? lo : hi);
    50          
    50          
    100          
122              
123 2948 50         MPUassert(rlo < n && rhi == n, "interpolation: bad step 1 interpolation");
    50          
124              
125             /* Step 2. Ridder's method until we're very close. */
126              
127 2948 50         MPUassert(rlo < n && rhi >= n, "interpolation: Ridder initial assumption");
    50          
128 2948 50         if(_dbgprint)printf(" 2 lo %lu mid %lu hi %lu\n", lo, mid, hi);
129              
130 5736 100         while ((hi-lo) > 8 && ((hi-lo) > threshold || rhi > n)) {
    100          
    50          
131 2788           UV x0 = lo, x1 = lo + ((hi-lo)>>1); /* x2 = hi */
132 2788 100         UV rx1 = MPU_CALLBACK(x1);
133 2788           IV fx0 = rlo-n, fx1 = rx1-n, fx2=rhi-n+1;
134              
135 2788           double pos = ((double)(x1-x0) * (double)fx1)
136 2788           / sqrtl((double)fx1 * (double)fx1 - (double)fx0 * (double)fx2);
137 2788           UV x3 = x1 - (IV)(pos+0.5);
138              
139 2788 50         if(_dbgprint)printf(" 2s lo %lu mid %lu hi %lu (%lu)\n", lo, x1, hi, (rx1>n) ? rx1-n : n-rx1);
    0          
140              
141 2788 50         if (x3 >= hi || x3 <= lo || x3 == x1) {
    50          
    100          
142             /* We got nothing from the new point. Just use the bisection. */
143 133 50         if (rx1 >= n) { hi = x1; rhi = rx1; }
144 0           else { lo = x1; rlo = rx1; }
145             } else {
146 2655 100         UV rx3 = MPU_CALLBACK(x3);
147 2655 50         if(_dbgprint)printf(" 2S lo %lu mid %lu hi %lu (%lu)\n", lo, x3, hi, (rx3>n) ? rx3-n : n-rx3);
    0          
148             /* Swap if needed to have: [lo x1 x3 hi] */
149 2655 50         if (rx1 > rx3) { UV t=x1; x1=x3; x3=t; t=rx1; fx1=rx3; rx3=t; }
150 2655 50         if (rx1 >= n) { hi = x1; rhi = rx1; }
151 2655 100         else if (rx3 >= n) { lo = x1; rlo = rx1; hi = x3; rhi = rx3; }
152 2464           else { lo = x3; rlo = rx3; }
153             }
154 2788 50         MPUassert(rlo < n && rhi >= n, "interpolation: Ridder step error");
    50          
155             }
156              
157             /* Step 3. Binary search. */
158              
159             /* Binary search until within threshold */
160 9317 100         while ((hi-lo) > 1 && ((hi-lo) > threshold || rhi > n)) {
    100          
    50          
161 6369           mid = lo + ((hi-lo)>>1);
162 6369 100         if (MPU_CALLBACK(mid) < n) lo = mid; /* Keeps invariant f(lo) < n */
    100          
163 2272           else hi = mid;
164             }
165 2948 50         if(_dbgprint)printf("final %lu - %lu threshold %lu\n", lo, hi, threshold);
166 2948           RETURNI(hi);
167             }
168              
169              
170 2876           UV inverse_interpolate(UV lo, UV hi, UV n, UV (*func)(UV mid), UV threshold) {
171 2876           return _inverse_interpolate(lo,hi,n,0,0,func,threshold);
172             }
173              
174 225           UV inverse_interpolate_k(UV lo, UV hi, UV n, UV k, UV (*funck)(UV mid, UV k), UV threshold) {
175 225           return _inverse_interpolate(lo,hi,n,k,funck,0,threshold);
176             }
177              
178              
179             /******************************************************************************/
180              
181              
182 125           UV interpolate_with_approx(UV n,
183             UV *gcount,
184             UV tol,
185             UV (*fnth)(UV n),
186             UV (*fcnt)(UV n),
187             bool (*fis)(UV n) /* optional */
188             ) {
189 125           UV approx_nth_n, guess, gn, count, ming = 0, maxg = UV_MAX;
190              
191 125           approx_nth_n = guess = fnth(n);
192 125 50         for (gn = 2; gn < 20; gn++) {
193             IV adjust;
194 125 50         MPUverbose(2, " interp %"UVuf"-th is around %"UVuf" ... ", n, guess);
195 125           count = fcnt(guess);
196 125 50         MPUverbose(2, "(%"IVdf")\n", (IV)(n-count));
197             /* Stop guessing if within our tolerance */
198 125 100         if (n==count || (n>count && n-count < tol) || (n
    100          
    50          
    50          
    50          
199             /* Determine how far off we think we are */
200 0           adjust = (IV) (approx_nth_n - fnth(count));
201             /* When computing new guess, ensure we don't overshoot. Rarely used. */
202 0 0         if (count <= n && guess > ming) ming = guess; /* Previous guesses */
    0          
203 0 0         if (count >= n && guess < maxg) maxg = guess;
    0          
204 0           guess += adjust;
205 0 0         if (guess <= ming || guess >= maxg) MPUverbose(2, " fix min/max for %"UVuf"\n",n);
    0          
    0          
206 0 0         if (guess <= ming) guess = ming + tol - 1;
207 0 0         if (guess >= maxg) guess = maxg - tol + 1;
208             /* TODO: if min/max dist is small, split the difference. */
209             }
210 125 50         if (gn == 20) count = fcnt(guess);
211              
212 125 100         if (fis) {
213 64 100         if (count < n) {
214              
215             /* Increase count one at a time if needed */
216 533 100         for ( ; count < n; count++)
217 9856 100         while (!fis(++guess))
218             ;
219              
220 58 50         } else if (count >= n) {
221              
222             /* Make sure this is the least value at this count */
223 65 100         while (!fis(guess)) guess--;
224             /* Reduce count one at a time if needed */
225 209 100         for ( ; count > n; count--)
226 2665 100         while (!fis(--guess))
227             ;
228              
229             }
230             }
231              
232 125 100         if (gcount) *gcount = count;
233 125           return guess;
234             }