Skip to content

Commit 2db247e

Browse files
committed
lower bound formula updates
1 parent 373dcb5 commit 2db247e

File tree

2 files changed

+67
-62
lines changed

2 files changed

+67
-62
lines changed

eval/adjust.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
(for ([x (in-list tail-registers)]
106106
[bound (in-list ampl-bounds)]
107107
#:when (>= x varc)) ; when tail register is not a variable
108-
(match-define (list up-bound lo-bound) bound)
108+
(match-define (cons up-bound lo-bound) bound)
109109

110110
; Upper precision bound propogation
111111
(vector-set! vprecs-max

eval/tricks.rkt

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
[4 4096]
2121
[5 8192]))
2222

23+
(define (prev-iter)
24+
(- (*sampling-iteration*) 1))
25+
2326
(define (crosses-zero? x)
2427
(not (equal? (mpfr-sign (ival-lo x)) (mpfr-sign (ival-hi x)))))
2528

@@ -93,32 +96,32 @@
9396
; k = 2: logspan(x)
9497
(define x (first srcs))
9598
(define y (second srcs))
96-
(list (list (logspan y) 0) ; bounds per x
97-
(list (logspan x) 0))] ; bounds per y
99+
(list (cons (logspan y) 0) ; bounds per x
100+
(cons (logspan x) 0))] ; bounds per y
98101

99102
[(ival-div)
100103
; k = 1: logspan(y)
101104
; k = 2: logspan(x) + 2 * logspan(y)
102105
(define x (first srcs))
103106
(define y (second srcs))
104-
(list (list (logspan y) 0) ; bounds per x
105-
(list (+ (logspan x) (* 2 (logspan y))) 0))] ; bounds per y
107+
(list (cons (logspan y) 0) ; bounds per x
108+
(cons (+ (logspan x) (* 2 (logspan y))) 0))] ; bounds per y
106109

107110
[(ival-sqrt ival-cbrt)
108111
; sqrt: logspan(x)/2 - 1
109112
; cbrt: logspan(x)*2/3 - 1
110113
(define x (first srcs))
111-
(list (list (quotient (logspan x) 2) 0))]
114+
(list (cons (quotient (logspan x) 2) 0))]
112115

113116
[(ival-add ival-sub)
114117
; k = 1: maxlog(x) - minlog(z)
115118
; k = 2: maxlog(y) - minlog(z)
116119
(define x (first srcs))
117120
(define y (second srcs))
118121

119-
(list (list (- (maxlog x) (minlog z))
122+
(list (cons (- (maxlog x) (minlog z))
120123
(- (minlog x #:no-slack #t) (maxlog z #:no-slack #t))) ; bounds per x
121-
(list (- (maxlog y) (minlog z))
124+
(cons (- (maxlog y) (minlog z))
122125
(- (minlog y #:no-slack #t) (maxlog z #:no-slack #t))))] ; bounds per y
123126

124127
[(ival-pow)
@@ -140,79 +143,83 @@
140143
(get-slack)
141144
0))
142145

143-
(list (list (max (+ (maxlog y) (logspan x) (logspan z) x-slack) x-slack)
146+
(list (cons (max (+ (maxlog y) (logspan x) (logspan z) x-slack) x-slack)
144147
(minlog y #:no-slack #t)) ; bounds per x
145-
(list (max (+ (maxlog y) (max (abs (maxlog x)) (abs (minlog x))) (logspan z) y-slack)
148+
(cons (max (+ (maxlog y) (max (abs (maxlog x)) (abs (minlog x))) (logspan z) y-slack)
146149
y-slack)
147150
(minlog y #:no-slack #t)))] ; bounds per y
148151

149152
[(ival-exp ival-exp2)
150153
; maxlog(x) + logspan(z)
151154
(define x (car srcs))
152-
(list (list (+ (maxlog x) (logspan z)) (minlog x #:no-slack #t)))]
155+
(list (cons (+ (maxlog x) (logspan z)) (minlog x #:no-slack #t)))]
153156

154157
[(ival-tan)
155158
; maxlog(x) + max(|minlog(z)|,|maxlog(z)|) + logspan(z) + 1
156159
(define x (first srcs))
157-
(list (list (+ (maxlog x) (max (abs (maxlog z)) (abs (minlog z))) (logspan z) 1)
160+
(list (cons (+ (maxlog x) (max (abs (maxlog z)) (abs (minlog z))) (logspan z) 1)
158161
(+ (minlog x #:no-slack #t)
159162
(min (abs (maxlog z #:no-slack #t)) (abs (minlog z #:no-slack #t))))))]
160163

161164
[(ival-sin)
162165
; maxlog(x) - minlog(z)
163166
(define x (first srcs))
164-
(list (list (- (maxlog x) (minlog z)) (- (maxlog z #:no-slack #t))))]
167+
(list (cons (- (maxlog x) (minlog z)) (- (maxlog z #:no-slack #t))))]
165168

166169
[(ival-cos)
167170
; maxlog(x) - minlog(z) + min(maxlog(x), 0)
168171
(define x (first srcs))
169-
(list (list (+ (- (maxlog x) (minlog z)) (min (maxlog x) 0)) (- (maxlog z #:no-slack #t))))]
172+
(list (cons (+ (- (maxlog x) (minlog z)) (min (maxlog x) 0)) (- (maxlog z #:no-slack #t))))]
170173

171174
[(ival-sinh)
172175
; maxlog(x) + logspan(z) - min(minlog(x), 0)
173176
(define x (first srcs))
174-
(list (list (- (+ (maxlog x) (logspan z)) (min (minlog x) 0)) (max 0 (minlog x #:no-slack #t))))]
177+
(list (cons (- (+ (maxlog x) (logspan z)) (min (minlog x) 0)) (max 0 (minlog x #:no-slack #t))))]
175178

176179
[(ival-cosh)
177180
; maxlog(x) + logspan(z) + min(maxlog(x), 0)
178181
(define x (first srcs))
179-
(list (list (+ (maxlog x) (logspan z) (min (maxlog x) 0)) (max 0 (minlog x #:no-slack #t))))]
182+
(list (cons (+ (maxlog x) (logspan z) (min (maxlog x) 0)) (max 0 (minlog x #:no-slack #t))))]
180183

181184
[(ival-log ival-log2 ival-log10)
182185
; log: logspan(x) - minlog(z)
183186
; log2: logspan(x) - minlog(z) + 1
184187
; log10: logspan(x) - minlog(z) - 1
185188
(define x (first srcs))
186-
(list (list (+ (- (logspan x) (minlog z)) 1) (- (maxlog z #:no-slack #t))))]
189+
(list (cons (+ (- (logspan x) (minlog z)) 1) (- (maxlog z #:no-slack #t))))]
187190

188191
[(ival-asin)
189-
; maxlog(x) - log[1-x^2]/2 - minlog(z)
190-
; ^^^^^^^^^^^^
191-
; condition of uncertainty
192-
(define x (first srcs))
193-
(define slack
194-
(if (>= (maxlog z) 1) ; Condition of uncertainty
195-
(get-slack) ; assumes that log[1-x^2]/2 is equal to slack
196-
1))
192+
; Γ[asin] = | x / (sqrt(1-x^2) * arcsin(x))|
193+
; ampl[asin] = maxlog(x) - log[1-x^2]/2 - minlog(z)
194+
; ^^^^^^^^^^^^
195+
; unknown part
197196

198-
(list (list slack 0))]
197+
(define x (first srcs))
198+
(if (>= (maxlog z) 1)
199+
(list (cons (get-slack)
200+
(get-slack (prev-iter)))) ; assumes that log[1-x^2]/2 is equal to slack
201+
(list (cons 1 0)))]
199202

200203
[(ival-acos)
201-
; maxlog(x) - log[1-x^2]/2 - minlog(z)
202-
; ^^^^^^^^^^^^
203-
; condition of uncertainty
204-
(define x (first srcs))
205-
(define slack
206-
(if (>= (maxlog x) 0) ; Condition of uncertainty
207-
(get-slack) ; assumes that log[1-x^2]/2 is equal to slack
208-
0))
204+
; Γ[acos] = |- x / (sqrt(1-x^2) * arccos(x))|
205+
; ampl[acos] = maxlog(x) - log[1-x^2]/2 - minlog(z)
206+
; ^^^^^^^^^^^^
207+
; unknown part
209208

210-
(list (list slack 0))]
209+
(define x (first srcs))
210+
(if (>= (maxlog x) 0)
211+
(list (cons (get-slack)
212+
(get-slack (prev-iter)))) ; assumes that log[1-x^2]/2 is equal to slack
213+
(list (cons 0 0)))]
211214

212215
[(ival-atan)
213-
; logspan(x) - min(|minlog(x)|, |maxlog(x)|) - minlog(z)
216+
; Γ[atan] = | x / ((1+x^2) * arctan(x))|
217+
; ampl[atan] = logspan(x) - min(|minlog(x)|, |maxlog(x)|) - minlog(z)
214218
(define x (first srcs))
215-
(list (list (- (logspan x) (min (abs (minlog x)) (abs (maxlog x))) (minlog z)) 0))]
219+
(list (cons (- (logspan x) (min (abs (minlog x)) (abs (maxlog x))) (minlog z))
220+
(- (- (max (abs (minlog x #:no-slack #t)) (abs (maxlog x #:no-slack #t))))
221+
(maxlog z #:no-slack #t)
222+
2)))]
216223

217224
[(ival-fmod ival-remainder)
218225
; x mod y = x - y*q, where q is rnd_down(x/y)
@@ -228,8 +235,8 @@
228235
(get-slack) ; y crosses zero
229236
0))
230237

231-
(list (list (- (maxlog x) (minlog z)) 0) ; bounds per x
232-
(list (max (+ (- (maxlog x) (minlog z)) slack) slack) 0))] ; bounds per y
238+
(list (cons (- (maxlog x) (minlog z)) 0) ; bounds per x
239+
(cons (max (+ (- (maxlog x) (minlog z)) slack) slack) 0))] ; bounds per y
233240

234241
; Currently log1p has a very poor approximation
235242
[(ival-log1p)
@@ -240,60 +247,58 @@
240247
(define xhi (ival-hi x))
241248
(define xlo (ival-lo x))
242249

243-
(define slack
244-
(if (or (equal? (mpfr-sign xlo) -1) (equal? (mpfr-sign xhi) -1))
245-
(get-slack) ; if x in negative
246-
0))
247-
248-
(list (list (max (+ (- (maxlog x) (minlog z)) slack) slack) 0))]
250+
(list (if (or (equal? (mpfr-sign xlo) -1) (equal? (mpfr-sign xhi) -1))
251+
(cons (max (+ (- (maxlog x) (minlog z)) (get-slack)) (get-slack)) 0) ; if x in negative
252+
(cons (- (maxlog x) (minlog z)) 0)))]
249253

250254
; Currently expm1 has a very poor solution for negative values
251255
[(ival-expm1)
252256
; log[Гexpm1] = log[x * e^x / expm1] <= max(1 + maxlog(x), 1 + maxlog(x) - minlog(z))
253257
(define x (first srcs))
254-
(list (list (max (+ 1 (maxlog x)) (+ 1 (- (maxlog x) (minlog z)))) 0))]
258+
(list (cons (max (+ 1 (maxlog x)) (+ 1 (- (maxlog x) (minlog z)))) 0))]
255259

256260
[(ival-atan2)
257261
; maxlog(x) + maxlog(y) - 2*max(minlog(x), minlog(y)) - minlog(z)
258262
(define x (first srcs))
259263
(define y (second srcs))
260264

261265
(make-list 2
262-
(list (- (+ (maxlog x) (maxlog y)) (* 2 (max (minlog x) (minlog y))) (minlog z)) 0))]
266+
(cons (- (+ (maxlog x) (maxlog y)) (* 2 (max (minlog x) (minlog y))) (minlog z))
267+
(- (+ (minlog x #:no-slack #t) (minlog y #:no-slack #t))
268+
(* 2 (min (maxlog x #:no-slack #t) (maxlog y #:no-slack #t)))
269+
(maxlog z #:no-slack #t))))]
263270

264271
[(ival-tanh)
265272
; logspan(z) + logspan(x)
266273
(define x (first srcs))
267-
(list (list (+ (logspan z) (logspan x)) 0))]
274+
(list (cons (+ (logspan z) (logspan x)) 0))]
268275

269276
[(ival-atanh)
270277
; log[Гarctanh] = maxlog(x) - log[(1-x^2)] - minlog(z) = 1 if x < 0.5, otherwise slack
271278
; ^^^^^^^
272279
; a possible uncertainty
273280
(define x (first srcs))
274-
(list (list (if (>= (maxlog x) 1)
275-
(get-slack)
276-
1)
277-
0))]
281+
(list (if (>= (maxlog x) 1)
282+
(cons (get-slack) (get-slack (prev-iter)))
283+
(cons 1 0)))]
278284

279285
[(ival-acosh)
280286
; log[Гacosh] = log[x / (sqrt(x-1) * sqrt(x+1) * acosh)] <= -minlog(z) + slack
281287
(define z-exp (minlog z))
282-
(define slack
283-
(if (< z-exp 2) ; when acosh(x) < 1
284-
(get-slack)
285-
0))
286-
287-
(list (list (max (- slack z-exp) slack) 0))]
288+
(list (if (< z-exp 2) ; when acosh(x) < 1
289+
(cons (max (- (get-slack) z-exp) (get-slack)) 0)
290+
(cons 0 0)))]
288291

289292
[(ival-pow2)
290293
; same as multiplication
291294
(define x (first srcs))
292-
(list (list (+ (logspan x) 1) 0))]
295+
(list (cons (+ (logspan x) 1) 0))]
293296

294297
; TODO
295-
[(ival-erfc ival-erf ival-lgamma ival-tgamma ival-asinh ival-logb) (list (list (get-slack) 0))]
298+
[(ival-erfc ival-erf ival-lgamma ival-tgamma ival-asinh ival-logb)
299+
(list (cons (get-slack) (get-slack (prev-iter))))]
296300
; TODO
297-
[(ival-ceil ival-floor ival-rint ival-round ival-trunc) (list (list (get-slack) 0))]
301+
[(ival-ceil ival-floor ival-rint ival-round ival-trunc)
302+
(list (cons (get-slack) (get-slack (prev-iter))))]
298303

299-
[else (map (λ (_) (list 0 0)) srcs)])) ; exponents for arguments
304+
[else (map (λ (_) (cons 0 0)) srcs)])) ; exponents for arguments

0 commit comments

Comments
 (0)