|
10 | 10 |
|
11 | 11 | (provide get-bounds |
12 | 12 | get-slack |
13 | | - minlog+maxlog+logspan) |
| 13 | + ival+minlog+maxlog+logspan) |
14 | 14 |
|
15 | 15 | (define (get-slack [iter (*sampling-iteration*)]) |
16 | 16 | (match iter |
|
70 | 70 | [else (min (mpfr-exp lo) (mpfr-exp hi))])])) |
71 | 71 |
|
72 | 72 | ; Returns (list (minlog x) (maxlog x) (logspan x)) |
73 | | -(define (minlog+maxlog+logspan x slack) |
| 73 | +(define (ival+minlog+maxlog+logspan x slack) |
74 | 74 | (define lo (ival-lo x)) |
75 | 75 | (define hi (ival-hi x)) |
76 | 76 | (match (and (boolean? lo) (boolean? hi)) |
77 | | - [#t (list 0 0 0)] |
| 77 | + [#t (list x 0 0 0)] |
78 | 78 | [#f |
79 | 79 | (define lo-exp (mpfr-exp lo)) |
80 | 80 | (define hi-exp (mpfr-exp hi)) |
81 | | - (define logspan |
| 81 | + ; logspan |
| 82 | + (define lg |
82 | 83 | (match (*bumps-activated*) |
83 | 84 | [#t |
84 | 85 | (if (or (bfzero? lo) (bfinfinite? lo) (bfzero? hi) (bfinfinite? hi)) |
85 | 86 | slack |
86 | 87 | (+ (abs (- lo-exp hi-exp)) 1))] |
87 | 88 | [#f 0])) |
88 | | - |
89 | | - (cond |
90 | | - ; x = [0.bf, 0.bf] |
91 | | - [(and (bfzero? lo) (bfzero? hi)) (list (- slack) (- slack) logspan)] |
92 | | - [(bfzero? lo) |
93 | | - (if (bfinfinite? hi) |
94 | | - (list (- slack) slack logspan) ; x = [0.bf, +inf] |
95 | | - (list (- (min hi-exp 0) slack) (+ hi-exp 1) logspan))] ; x = [0.bf, +...] |
96 | | - [(bfzero? hi) |
97 | | - (if (bfinfinite? lo) |
98 | | - (list (- slack) slack logspan) ; x = [-inf, 0.bf] |
99 | | - (list (- (min lo-exp 0) slack) (+ lo-exp 1) logspan))] ; x = [-..., 0.bf] |
100 | | - [(crosses-zero? x) ; x = [-..., +...] |
101 | | - (cond |
102 | | - [(and (bfinfinite? hi) (bfinfinite? lo)) (list (- slack) slack logspan)] ; [-inf, +inf] |
103 | | - [(bfinfinite? hi) |
104 | | - (list (- (min lo-exp 0) slack) (+ (max lo-exp 0) slack) logspan)] ; [-..., +inf] |
105 | | - [(bfinfinite? lo) |
106 | | - (list (- (min hi-exp 0) slack) (+ (max hi-exp 0) slack) logspan)] ; [-inf, +...] |
107 | | - [else |
108 | | - (list (- (min lo-exp hi-exp 0) slack) |
109 | | - (+ (max lo-exp hi-exp) 1) |
110 | | - logspan)])] ; x = [-..., +...] |
111 | | - [else |
112 | | - (cond |
113 | | - ; Can't both be inf, since: |
114 | | - ; - [inf, inf] not a valid interval |
115 | | - ; - [-inf, inf] crosses zero |
116 | | - [(bfinfinite? lo) (list hi-exp (+ (max hi-exp 0) slack) logspan)] ; [-inf, -...] |
117 | | - [(bfinfinite? hi) (list lo-exp (+ (max lo-exp 0) slack) logspan)] ; [+..., +inf] |
118 | | - [else (list (min lo-exp hi-exp) (+ (max lo-exp hi-exp) 1) logspan)])])])) |
| 89 | + ; minlog+maxlog |
| 90 | + (match-define (list mn mx) |
| 91 | + (cond |
| 92 | + ; x = [0.bf, 0.bf] |
| 93 | + [(and (bfzero? lo) (bfzero? hi)) (list (- slack) (- slack))] |
| 94 | + [(bfzero? lo) |
| 95 | + (if (bfinfinite? hi) |
| 96 | + (list (- slack) slack) ; x = [0.bf, +inf] |
| 97 | + (list (- (min hi-exp 0) slack) (+ hi-exp 1)))] ; x = [0.bf, +...] |
| 98 | + [(bfzero? hi) |
| 99 | + (if (bfinfinite? lo) |
| 100 | + (list (- slack) slack) ; x = [-inf, 0.bf] |
| 101 | + (list (- (min lo-exp 0) slack) (+ lo-exp 1)))] ; x = [-..., 0.bf] |
| 102 | + [(crosses-zero? x) ; x = [-..., +...] |
| 103 | + (cond |
| 104 | + [(and (bfinfinite? hi) (bfinfinite? lo)) (list (- slack) slack)] ; [-inf, +inf] |
| 105 | + [(bfinfinite? hi) (list (- (min lo-exp 0) slack) (+ (max lo-exp 0) slack))] ; [-..., +inf] |
| 106 | + [(bfinfinite? lo) (list (- (min hi-exp 0) slack) (+ (max hi-exp 0) slack))] ; [-inf, +...] |
| 107 | + [else |
| 108 | + (list (- (min lo-exp hi-exp 0) slack) (+ (max lo-exp hi-exp) 1))])] ; x = [-..., +...] |
| 109 | + [else |
| 110 | + (cond |
| 111 | + ; Can't both be inf, since: |
| 112 | + ; - [inf, inf] not a valid interval |
| 113 | + ; - [-inf, inf] crosses zero |
| 114 | + [(bfinfinite? lo) (list hi-exp (+ (max hi-exp 0) slack))] ; [-inf, -...] |
| 115 | + [(bfinfinite? hi) (list lo-exp (+ (max lo-exp 0) slack))] ; [+..., +inf] |
| 116 | + [else (list (min lo-exp hi-exp) (+ (max lo-exp hi-exp) 1))])])) |
| 117 | + ; output |
| 118 | + (list x mn mx lg)])) |
119 | 119 |
|
120 | 120 | (define (logspan x) |
121 | 121 | (match (*bumps-activated*) |
|
142 | 142 | ; Γ[*]'y = 1 |
143 | 143 | ; ↑ampl[*]'y = logspan(x) |
144 | 144 | ; ↓ampl[*]'y = 0 |
145 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
146 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
| 145 | + (match-define (list _ _ _ lg-x) (first srcs)) |
| 146 | + (match-define (list _ _ _ lg-y) (second srcs)) |
147 | 147 | (list (cons lg-x 0) ; bounds per x |
148 | 148 | (cons lg-y 0))] ; bounds per y |
149 | 149 |
|
|
155 | 155 | ; Γ[/]'y = 1 |
156 | 156 | ; ↑ampl[/]'y = logspan(x) + 2 * logspan(y) |
157 | 157 | ; ↓ampl[/]'y = 0 |
158 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
159 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
| 158 | + (match-define (list _ _ _ lg-x) (first srcs)) |
| 159 | + (match-define (list _ _ _ lg-y) (second srcs)) |
160 | 160 | (list (cons lg-y 0) ; bounds per x |
161 | 161 | (cons (+ lg-x (* 2 lg-y)) 0))] ; bounds per y |
162 | 162 |
|
|
168 | 168 | ; Γ[cbrt]'x = 1/3 |
169 | 169 | ; ↑ampl[cbrt]'x = logspan(x)*2/3 - 1 |
170 | 170 | ; ↓ampl[cbrt]'x = 0 |
171 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
| 171 | + (match-define (list _ _ _ lg-x) (first srcs)) |
172 | 172 | (list (cons (quotient lg-x 2) 0))] |
173 | 173 |
|
174 | 174 | [(ival-add ival-sub ival-add! ival-sub!) |
|
179 | 179 | ; Γ[+ & -]'y = |y/(x+y)| & |-y/(x-y)| |
180 | 180 | ; ↑ampl[+ & -]'y = maxlog(y) - minlog(z) |
181 | 181 | ; ↓ampl[+ & -]'y = minlog(y) - maxlog(z) |
182 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
183 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
184 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 182 | + (match-define (list x _ mx-x _) (first srcs)) |
| 183 | + (match-define (list y _ mx-y _) (second srcs)) |
| 184 | + (match-define (list z mn-z _ _) output) |
185 | 185 |
|
186 | 186 | (if (*lower-bound-early-stopping*) |
187 | 187 | (list (cons (- mx-x mn-z) |
|
199 | 199 | ; Γ[pow]'y = |y*ln(x)| |
200 | 200 | ; ↑ampl[pow]'y = maxlog(y) + max(|minlog(x)|,|maxlog(x)|) + logspan(z) |
201 | 201 | ; ↓ampl[pow]'y = minlog(y) |
202 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
203 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
204 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 202 | + (match-define (list x mn-x mx-x lg-x) (first srcs)) |
| 203 | + (match-define (list y _ mx-y _) (second srcs)) |
| 204 | + (match-define (list z _ _ lg-z) output) |
205 | 205 |
|
206 | 206 | ; when output crosses zero and x is negative - means that y was fractional and not fixed (specific of Rival) |
207 | 207 | ; solution - add more slack for y to converge |
|
231 | 231 | ; Γ[exp & exp2]'x = |x| & |x*ln(2)| |
232 | 232 | ; ↑ampl[exp & exp2]'x = maxlog(x) + logspan(z) |
233 | 233 | ; ↓ampl[exp & exp2]'x = minlog(x) |
234 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
235 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 234 | + (match-define (list x _ mx-x _) (first srcs)) |
| 235 | + (match-define (list _ _ _ lg-z) output) |
236 | 236 |
|
237 | 237 | (if (*lower-bound-early-stopping*) |
238 | 238 | (list (cons (+ mx-x lg-z) (minlog x #:less-slack #t))) |
|
242 | 242 | ; Γ[tan]'x = |x / (cos(x) * sin(x))| |
243 | 243 | ; ↑ampl[tan]'x = maxlog(x) + max(|minlog(z)|,|maxlog(z)|) + logspan(z) + 1 |
244 | 244 | ; ↓ampl[tan]'x = minlog(x) + min(|minlog(z)|,|maxlog(z)|) - 1 |
245 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
246 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 245 | + (match-define (list x _ mx-x _) (first srcs)) |
| 246 | + (match-define (list z mn-z mx-z lg-z) output) |
247 | 247 |
|
248 | 248 | (if (*lower-bound-early-stopping*) |
249 | 249 | (list (cons (+ mx-x (max (abs mx-z) (abs mn-z)) lg-z 1) |
|
257 | 257 | ; ↑ampl[sin]'x = maxlog(x) - minlog(z) |
258 | 258 | ; ↓ampl[sin]'x = | - maxlog(z) - 1, if maxlog(x) > 1 |
259 | 259 | ; | 0 else |
260 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
261 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 260 | + (match-define (list _ _ mx-x _) (first srcs)) |
| 261 | + (match-define (list z mn-z _ _) output) |
262 | 262 |
|
263 | 263 | (if (*lower-bound-early-stopping*) |
264 | 264 | (list (cons (- mx-x mn-z) |
|
271 | 271 | ; Γ[cos]'x = |x * sin(x) / cos(x)| |
272 | 272 | ; ↑ampl[cos]'x = maxlog(x) - minlog(z) + min(maxlog(x), 0) |
273 | 273 | ; ↓ampl[cos]'x = - maxlog(x) - 2 |
274 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
275 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 274 | + (match-define (list _ _ mx-x _) (first srcs)) |
| 275 | + (match-define (list z mn-z _ _) output) |
276 | 276 |
|
277 | 277 | (if (*lower-bound-early-stopping*) |
278 | 278 | (list (cons (+ (- mx-x mn-z) (min mx-x 0)) (- (- 2) (maxlog z #:less-slack #t)))) |
|
282 | 282 | ; Γ[sinh]'x = |x * cosh(x) / sinh(x)| |
283 | 283 | ; ↑ampl[sinh]'x = maxlog(x) + logspan(z) - min(minlog(x), 0) |
284 | 284 | ; ↓ampl[sinh]'x = max(0, minlog(x)) |
285 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
286 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 285 | + (match-define (list x mn-x mx-x _) (first srcs)) |
| 286 | + (match-define (list _ _ _ lg-z) output) |
287 | 287 |
|
288 | 288 | (if (*lower-bound-early-stopping*) |
289 | 289 | (list (cons (- (+ mx-x lg-z) (min mn-x 0)) (max 0 (minlog x #:less-slack #t)))) |
|
293 | 293 | ; Γ[cosh]'x = |x * sinh(x) / cosh(x)| |
294 | 294 | ; ↑ampl[cosh]'x = maxlog(x) + logspan(z) + min(maxlog(x), 0) |
295 | 295 | ; ↓ampl[cosh]'x = max(0, minlog(x) - 1) |
296 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
297 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 296 | + (match-define (list x _ mx-x lg-x) (first srcs)) |
| 297 | + (match-define (list _ _ _ lg-z) output) |
298 | 298 |
|
299 | 299 | (if (*lower-bound-early-stopping*) |
300 | 300 | (list (cons (+ mx-x lg-z (min mx-x 0)) (max 0 (- (minlog x #:less-slack #t) 1)))) |
|
304 | 304 | ; Γ[log & log2 & log10]'x = |1 / ln(x)| & |ln(2) / ln(x)| & |ln(10) / ln(x)| |
305 | 305 | ; ↑ampl[log & log2 & log10]'x = logspan(x) - minlog(z) + 1 |
306 | 306 | ; ↓ampl[log & log2 & log10]'x = - maxlog(z) |
307 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
308 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 307 | + (match-define (list _ _ _ lg-x) (first srcs)) |
| 308 | + (match-define (list z mn-z _ _) output) |
309 | 309 |
|
310 | 310 | (if (*lower-bound-early-stopping*) |
311 | 311 | (list (cons (+ (- lg-x mn-z) 1) (- (maxlog z #:less-slack #t)))) |
|
316 | 316 | ; ↑ampl[asin]'x = | slack, if maxlog(z) > 1 |
317 | 317 | ; | 1 else |
318 | 318 | ; ↓ampl[asin]'x = 0 |
319 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 319 | + (match-define (list _ _ mx-z _) output) |
320 | 320 |
|
321 | 321 | (list (if (>= mx-z 1) |
322 | 322 | (cons (get-slack) 0) ; assumes that log[1-x^2]/2 is equal to slack |
|
327 | 327 | ; ↑ampl[acos]'x = | slack, if maxlog(x) >= 0 |
328 | 328 | ; | 0 else |
329 | 329 | ; ↓ampl[acos]'x = 0 |
330 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
| 330 | + (match-define (list _ _ mx-x _) (first srcs)) |
331 | 331 |
|
332 | 332 | (list (if (>= mx-x 0) |
333 | 333 | (cons (get-slack) 0) ; assumes that log[1-x^2]/2 is equal to slack |
|
337 | 337 | ; Γ[atan]'x = | x / ((1+x^2) * arctan(x))| |
338 | 338 | ; ↑ampl[atan]'x = - min(|minlog(x)|, |maxlog(x)|) - minlog(z) + logspan(x) |
339 | 339 | ; ↓ampl[atan]'x = - max(|minlog(x)|, |maxlog(x)|) - maxlog(z) - 2 |
340 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
341 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 340 | + (match-define (list x mn-x mx-x lg-x) (first srcs)) |
| 341 | + (match-define (list z mn-z _ _) output) |
342 | 342 |
|
343 | 343 | (if (*lower-bound-early-stopping*) |
344 | 344 | (list (cons (- lg-x (min (abs mn-x) (abs mx-x)) mn-z) |
|
356 | 356 | ; Γ[mod]'y ` |y/mod(x,y)| |
357 | 357 | ; ↑ampl[mod]'y = maxlog(y) - minlog(z) |
358 | 358 | ; ↓ampl[mod]'y = minlog(y) - maxlog(z) or just 0 |
359 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
360 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
361 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 359 | + (match-define (list _ _ mx-x _) (first srcs)) |
| 360 | + (match-define (list y _ _ _) (second srcs)) |
| 361 | + (match-define (list _ mn-z _ _) output) |
362 | 362 |
|
363 | 363 | (define slack |
364 | 364 | (if (crosses-zero? y) |
|
374 | 374 | ; ↑ampl[log1p]'x = | maxlog(x) - minlog(z) + slack, if x is negative |
375 | 375 | ; | maxlog(x) - minlog(z), else |
376 | 376 | ; ↓ampl[log1p]'x = 0 |
377 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
378 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 377 | + (match-define (list x _ mx-x _) (first srcs)) |
| 378 | + (match-define (list _ mn-z _ _) output) |
379 | 379 |
|
380 | 380 | (define xhi (ival-hi x)) |
381 | 381 | (define xlo (ival-lo x)) |
|
389 | 389 | ; Γ[expm1]'x = |x * e^x / expm1| |
390 | 390 | ; ↑ampl[expm1]'x = max(1 + maxlog(x), 1 + maxlog(x) - minlog(z)) |
391 | 391 | ; ↓ampl[expm1]'x = 0 |
392 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
393 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 392 | + (match-define (list _ _ mx-x _) (first srcs)) |
| 393 | + (match-define (list _ mn-z _ _) output) |
394 | 394 |
|
395 | 395 | (list (cons (max (+ 1 mx-x) (+ 1 (- mx-x mn-z))) 0))] |
396 | 396 |
|
397 | 397 | [(ival-atan2) |
398 | 398 | ; Γ[atan2]'x = Γ[atan2]'y = |xy / ((x^2 + y^2)*arctan(y/x))| |
399 | 399 | ; ↑ampl[expm1]'x = maxlog(x) + maxlog(y) - 2*min(minlog(x), minlog(y)) - minlog(z) |
400 | 400 | ; ↓ampl[expm1]'x = minlog(x) + minlog(y) - 2*max(maxlog(x), maxlog(y)) - maxlog(z) |
401 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
402 | | - (match-define (cons y (list mn-y mx-y lg-y)) (second srcs)) |
403 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 401 | + (match-define (list x mn-x mx-x _) (first srcs)) |
| 402 | + (match-define (list y mn-y mx-y _) (second srcs)) |
| 403 | + (match-define (list z mn-z _ _) output) |
404 | 404 |
|
405 | 405 | (if (*lower-bound-early-stopping*) |
406 | 406 | (make-list 2 |
|
414 | 414 | ; Γ[tanh]'x = |x / (sinh(x) * cosh(x))| |
415 | 415 | ; ↑ampl[tanh]'x = logspan(z) + logspan(x) |
416 | 416 | ; ↓ampl[tanh]'x = 0 |
417 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
418 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 417 | + (match-define (list _ _ _ lg-x) (first srcs)) |
| 418 | + (match-define (list _ _ _ lg-z) output) |
419 | 419 |
|
420 | 420 | (list (cons (+ lg-z lg-x) 0))] |
421 | 421 |
|
|
424 | 424 | ; ↑ampl[atanh]'x = | 1, if x < 0.5 |
425 | 425 | ; | slack |
426 | 426 | ; ↓ampl[atanh]'x = 0 |
427 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
| 427 | + (match-define (list _ _ mx-x _) (first srcs)) |
428 | 428 | (list (if (>= mx-x 1) |
429 | 429 | (cons (get-slack) 0) |
430 | 430 | (cons 1 0)))] |
|
434 | 434 | ; ↑ampl[acosh]'x = | -minlog(z) + slack, if minlog(z) < 2 |
435 | 435 | ; | 0 |
436 | 436 | ; ↓ampl[acosh]'x = 0 |
437 | | - (match-define (cons z (list mn-z mx-z lg-z)) output) |
| 437 | + (match-define (list _ mn-z _ _) output) |
438 | 438 | (list (if (< mn-z 2) ; when acosh(x) < 1 |
439 | 439 | (cons (- (get-slack) mn-z) 0) |
440 | 440 | (cons 0 0)))] |
|
443 | 443 | ; Γ[acosh]'x = |2 x x* / x^2| |
444 | 444 | ; ↑ampl[pow2]'x = logspan(x) + 1 |
445 | 445 | ; ↓ampl[pow2]'x = 0 |
446 | | - (match-define (cons x (list mn-x mx-x lg-x)) (first srcs)) |
| 446 | + (match-define (list _ _ _ lg-x) (first srcs)) |
447 | 447 | (list (cons (+ lg-x 1) 0))] |
448 | 448 |
|
449 | 449 | ; TODO |
|
0 commit comments