File: math.rkt

package info (click to toggle)
racket 7.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,432 kB
  • sloc: ansic: 258,980; pascal: 59,975; sh: 33,650; asm: 13,558; lisp: 7,124; makefile: 3,329; cpp: 2,889; exp: 499; python: 274; xml: 11
file content (641 lines) | stat: -rw-r--r-- 20,734 bytes parent folder | download | duplicates (6)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#lang typed/racket/base

(require racket/match racket/list racket/vector math/base math/flonum
         "type-doc.rkt")

(provide (all-defined-out))

;; ===================================================================================================
;; Flonums

(:: flblend (-> Flonum Flonum Flonum Flonum))
(define (flblend x y α)
  (+ (* α x) (* (- 1.0 α) y)))

(:: flsumof (All (A) (-> (-> A Flonum) (Listof A) Flonum)))
(define (flsumof f xs)
  (flsum (map f xs)))

(:: fldistance (-> Flonum * Flonum))
(define fldistance
  (case-lambda
    [()   0.0]
    [(x)  (abs x)]
    [(x y)  (flsqrt (+ (* x x) (* y y)))]
    [(x y z)  (flsqrt (+ (* x x) (* y y) (* z z)))]
    [xs  (flsqrt (flsumof (λ ([x : Flonum]) (* x x)) xs))]))

;; ===================================================================================================
;; Reals

(: rational?* (-> Any Boolean : #:+ Real))
(define (rational?* x)
  (and (real? x) (rational? x)))

(:: maybe-inexact->exact (-> (U #f Real) (U #f Exact-Rational)))
(define (maybe-inexact->exact x)
  (cond [x  (unless (rational? x)
              (raise-argument-error 'maybe-inexact->exact "rational or #f" x))
            (inexact->exact x)]
        [else  #f]))

(:: equal?* (-> Any * Boolean))
(define equal?*
  (case-lambda
    [()   #t]
    [(x)  #t]
    [xs   (and (equal? (car xs) (cadr xs))
               (apply equal?* (cdr xs)))]))

(define-syntax-rule (min2* x y)
  (cond [(x . < . y)  x]
        [(y . < . x)  y]
        [(exact? x)   x]
        [else  y]))

(define-syntax-rule (max2* x y)
  (cond [(x . > . y)  x]
        [(y . > . x)  y]
        [(exact? x)   x]
        [else  y]))

(:: min* (-> Real * Real))
(define min*
  (case-lambda
    [()     +inf.0]
    [(x)    x]
    [(x y)  (min2* x y)]
    [xs  (for/fold ([m : Real  +inf.0]) ([y  (in-list xs)])
           (min2* m y))]))

(:: max* (-> Real * Real))
(define max*
  (case-lambda
    [()     -inf.0]
    [(x)    x]
    [(x y)  (max2* x y)]
    [xs  (for/fold ([m : Real -inf.0]) ([y  (in-list xs)])
           (max2* m y))]))

(:: blend (-> Real Real Real Real))
(define (blend x y α)
  (+ (* α x) (* (- 1 α) y)))

(:: atan2 (-> Real Real Real))
(define (atan2 y x) real?
  (if (and (zero? y) (zero? x)) 0 (atan y x)))

(:: sumof (All (A) (-> (-> A Real) (Listof A) Real)))
(define (sumof f xs)
  (sum (map f xs)))

(:: real-modulo (-> Real Real Real))
(define (real-modulo x y)
  (- x (* y (floor (/ x y)))))

(:: distance (-> Real * Real))
(define distance
  (case-lambda
    [()  0]
    [(x)  (abs x)]
    [(x y)  (real-part (sqrt (+ (* x x) (* y y))))]
    [(x y z)  (real-part (sqrt (+ (* x x) (* y y) (* z z))))]
    [xs  (real-part (sqrt (sumof sqr xs)))]))

(:: exact∘log (-> Positive-Real Exact-Rational))
(define (exact∘log x)
  (define y (log x))
  (cond [(infinite? y)  (- (inexact->exact (log (numerator x)))
                           (inexact->exact (log (denominator x))))]
        [else  (inexact->exact y)]))

(:: floor-log/base (-> Positive-Integer Positive-Real Integer))
(define (floor-log/base b x)
  (cond [(not (b . >= . 2))
         (raise-argument-error 'floor-log/base "exact integer >= 2" 0 b x)]
        [else
         (define q (inexact->exact x))
         (define m (floor (/ (exact∘log q) (inexact->exact (log b)))))
         (let loop ([m m] [p  (expt b m)])
           (cond [(q . < . p)  (loop (sub1 m) (/ p b))]
                 [else  (define u (* p b))
                        (cond [(q . >= . u)  (loop (add1 m) u)]
                              [else  m])]))]))

(:: ceiling-log/base (-> Positive-Integer Positive-Real Integer))
(define (ceiling-log/base b x) exact-integer?
  (- (floor-log/base b (/ (inexact->exact x)))))

(:: polar->cartesian (-> Real Real (Vector Real Real)))
(define (polar->cartesian θ r)
  (let ([θ  (fl θ)]
        [r  (fl r)])
    (vector (* r (flcos θ))
            (* r (flsin θ)))))

(:: 3d-polar->3d-cartesian (-> Real Real Real (Vector Real Real Real)))
(define (3d-polar->3d-cartesian θ ρ r)
  (let ([θ  (fl θ)]
        [ρ  (fl ρ)]
        [r  (fl r)])
    (define cos-ρ (flcos ρ))
    (vector (* r (* (flcos θ) cos-ρ))
            (* r (* (flsin θ) cos-ρ))
            (* r (flsin ρ)))))

;; ===================================================================================================
;; Vectors

;; The final case was dead code and has been deleted. There are no instances of
;; vector-andmap thattake more than two vectors as arguments.
(:: vector-andmap
   (All (A B C ...)
        (case-> (-> (-> A Boolean) (Vectorof A) Boolean)
                (-> (-> A B Boolean) (Vectorof A) (Vectorof B) Boolean))))
(define vector-andmap
  (case-lambda
    [([f : (-> A Boolean)] [as : (Vectorof A)])
     (for/and ([a  (in-vector as)])
       (f a))]
    [([f : (-> A B Boolean)] [as : (Vectorof A)] [bs : (Vectorof B)])
     (define n (vector-length as))
     (unless (= (vector-length bs) n)
       (raise-argument-error 'vector-andmap (format "vector of length ~a" n) 2 f as bs))
     (for/and ([a  (in-vector as)]
               [b  (in-vector bs)])
       (f a b))]))


;; As with vector-andmap, dead code has been deleted.
(:: vector-ormap
   (All (A B C ...)
        (case-> (-> (-> A Boolean) (Vectorof A) Boolean)
                (-> (-> A B Boolean) (Vectorof A) (Vectorof B) Boolean))))
(define vector-ormap
  (case-lambda
    [([f : (-> A Boolean)] [as : (Vectorof A)])
     (for/or ([a  (in-vector as)])
       (f a))]
    [([f : (-> A B Boolean)] [as : (Vectorof A)] [bs : (Vectorof B)])
     (define n (vector-length as))
     (unless (= (vector-length bs) n)
       (raise-argument-error 'vector-ormap (format "vector of length ~a" n) 2 f as bs))
     (for/or ([a  (in-vector as)]
              [b  (in-vector bs)])
       (f a b))]))

(:: vcross (-> (Vectorof Real) (Vectorof Real) (Vector Real Real Real)))
(define (vcross v1 v2)
  (match v1
    [(vector x1 y1 z1)
     (match v2
       [(vector x2 y2 z2)
        (vector (- (* y1 z2) (* z1 y2))
                (- (* z1 x2) (* x1 z2))
                (- (* x1 y2) (* y1 x2)))]
       [_  (raise-argument-error 'vcross "vector of length 3" 1 v1 v2)])]
    [_  (raise-argument-error 'vcross "vector of length 3" 0 v1 v2)]))

(:: vcross2 (-> (Vectorof Real) (Vectorof Real) Real))
(define (vcross2 v1 v2)
  (match v1
    [(vector x1 y1)
     (match v2
       [(vector x2 y2)  (- (* x1 y2) (* y1 x2))]
       [_  (raise-argument-error 'vcross2 "vector of length 2" 1 v1 v2)])]
    [_  (raise-argument-error 'vcross2 "vector of length 2" 0 v1 v2)]))

(define-syntax-rule (vmap name f v)
  (let ()
    (define n (vector-length v))
    (for/vector #:length n ([x  (in-vector v)]) : Real
      (f x))))

(define-syntax-rule (unrolled-vmap name f v)
  (let ()
    (match v
      [(vector x y)    (vector (f x) (f y))]
      [(vector x y z)  (vector (f x) (f y) (f z))]
      [_  (vmap name f v)])))

(define-syntax-rule (vmap2 name f v1 v2)
  (let ()
    (define n (vector-length v1))
    (unless (= n (vector-length v2))
      (raise-argument-error name (format "vector of length ~a" n) 1 v1 v2))
    (for/vector #:length n ([x  (in-vector v1)]
                            [y  (in-vector v2)]) : Real
      (f x y))))

(define-syntax-rule (unrolled-vmap2 name f v1 v2)
  (match v1
    [(vector x1 y1)
     (match v2
       [(vector x2 y2)  (vector (f x1 x2) (f y1 y2))]
       [_  (raise-argument-error name "vector of length 2" 1 v1 v2)])]
    [(vector x1 y1 z1)
     (match v2
       [(vector x2 y2 z2)  (vector (f x1 x2) (f y1 y2) (f z1 z2))]
       [_  (raise-argument-error name "vector of length 3" 1 v1 v2)])]
    [_  (vmap2 name f v1 v2)]))

(:: v+ (-> (Vectorof Real) (Vectorof Real) (Vectorof Real)))
(define (v+ v1 v2)
  (unrolled-vmap2 'v+ + v1 v2))

(:: v- (-> (Vectorof Real) (Vectorof Real) (Vectorof Real)))
(define (v- v1 v2)
  (unrolled-vmap2 'v- - v1 v2))

(:: vneg (-> (Vectorof Real) (Vectorof Real)))
(define (vneg v)
  (unrolled-vmap 'vneg - v))

(:: v* (-> (Vectorof Real) Real (Vectorof Real)))
(define (v* v c)
  (define-syntax-rule (f x) (* x c))
  (unrolled-vmap 'v* f v))

(:: v/ (-> (Vectorof Real) Real (Vectorof Real)))
(define (v/ v c)
  (define-syntax-rule (f x) (/ x c))
  (unrolled-vmap 'v/ f v))

(:: vmag^2 (-> (Vectorof Real) Nonnegative-Real))
(define (vmag^2 v)
  (match v
    [(vector x y)    (+ (sqr x) (sqr y))]
    [(vector x y z)  (+ (sqr x) (sqr y) (sqr z))]
    [_  (for/fold ([mag : Nonnegative-Real  0]) ([x  (in-vector v)])
          (+ mag (sqr x)))]))

(:: vmag (-> (Vectorof Real) Nonnegative-Real))
(define (vmag v)
  (sqrt (vmag^2 v)))

(:: vnormalize (-> (Vectorof Real) (Vectorof Real)))
(define (vnormalize v)
  (define m (vmag v))
  (if (= 0 m) v (v/ v m)))

(:: vdot (-> (Vectorof Real) (Vectorof Real) Real))
(define (vdot v1 v2)
  (match v1
    [(vector x1 y1)
     (match v2
       [(vector x2 y2)  (+ (* x1 x2) (* y1 y2))]
       [_  (raise-argument-error 'vdot "vector of length 2" 1 v1 v2)])]
    [(vector x1 y1 z1)
     (match v2
       [(vector x2 y2 z2)  (+ (* x1 x2) (* y1 y2) (* z1 z2))]
       [_  (raise-argument-error 'vdot "vector of length 3" 1 v1 v2)])]
    [_
     (unless (= (vector-length v1) (vector-length v2))
       (raise-argument-error 'vdot (format "vector of ~a real numbers" (vector-length v1)) 1 v1 v2))
     (for/fold ([dot : Real  0]) ([x1  (in-vector v1)] [x2  (in-vector v2)])
       (+ dot (* x1 x2)))]))

(:: vcos-angle (-> (Vectorof Real) (Vectorof Real) Real))
(define (vcos-angle v1 v2)
  (define d (vdot v1 v2))
  (if (= d 0) d (/ d (vmag v1) (vmag v2))))

(:: vrational? (-> (Vectorof Real) Boolean))
(define (vrational? v)
  (match v
    [(vector (? rational? x) (? rational? y))  #t]
    [(vector (? rational? x) (? rational? y) (? rational? z))  #t]
    [_  (vector-andmap rational? v)]))

(:: v= (-> (Vectorof Real) (Vectorof Real) Boolean))
(define (v= v1 v2)
  (match v1
    [(vector x1 y1)
     (match v2
       [(vector x2 y2)  (and (= x1 x2) (= y1 y2))]
       [_  (raise-argument-error 'v= "vector of length 2" 1 v1 v2)])]
    [(vector x1 y1 z1)
     (match v2
       [(vector x2 y2 z2)  (and (= x1 x2) (= y1 y2) (= z1 z2))]
       [_  (raise-argument-error 'v= "vector of length 3" 1 v1 v2)])]
    [_
     (define n (vector-length v1))
     (unless (= (vector-length v2) n)
       (raise-argument-error 'v= (format "vector of length ~a" n) 1 v1 v2))
     (for/and ([x1  (in-vector v1)] [x2  (in-vector v2)])
       (= x1 x2))]))

(:: vcenter (-> (Listof (Vectorof Real)) (Vectorof Real)))
(define (vcenter vs)
  (match vs
    [(list (vector #{xs : (Listof Real)} #{ys : (Listof Real)}) ...)
     (define x-min (apply min* xs))
     (define x-max (apply max* xs))
     (define y-min (apply min* ys))
     (define y-max (apply max* ys))
     (vector (* 1/2 (+ x-min x-max)) (* 1/2 (+ y-min y-max)))]
    [(list (vector #{xs : (Listof Real)} #{ys : (Listof Real)} #{zs : (Listof Real)}) ...)
     (define x-min (apply min* xs))
     (define x-max (apply max* xs))
     (define y-min (apply min* ys))
     (define y-max (apply max* ys))
     (define z-min (apply min* zs))
     (define z-max (apply max* zs))
     (vector (* 1/2 (+ x-min x-max)) (* 1/2 (+ y-min y-max)) (* 1/2 (+ z-min z-max)))]
    [_
     (define-values (mins maxs)
       (for/fold ([mins : (Vectorof Real)  (first vs)]
                  [maxs : (Vectorof Real)  (first vs)])
                 ([v  (in-list (rest vs))])
         (values (vector-map min mins v)
                 (vector-map max maxs v))))
     (unrolled-vmap2 'vcenter (λ (x1 x2) (* 1/2 (+ x1 x2))) mins maxs)]))

(:: vrational-sublists (-> (Listof (Vectorof Real)) (Listof (Listof (Vectorof Real)))))
(define (vrational-sublists vs)
  (: res (Listof (Listof (Vectorof Real))))
  (define res
    (let loop ([vs vs])
      (cond [(null? vs)  (list null)]
            [(vrational? (car vs))  (define rst (loop (cdr vs)))
                                    (cons (cons (car vs) (car rst)) (cdr rst))]
            [else  (cons null (loop (cdr vs)))])))
  (cond [(and (not (null? res)) (null? (car res)))  (cdr res)]
        [else  res]))

(:: remove-degenerate-edges (-> (Listof (Vectorof Real)) (Listof (Vectorof Real))))
(define (remove-degenerate-edges vs)
  (cond
    [(empty? vs)  empty]
    [else
     (let*-values ([(last vs)
                    (for/fold ([last : (Vectorof Real)  (first vs)]
                               [vs : (Listof (Vectorof Real))  (list (first vs))])
                      ([v  (in-list (rest vs))])
                      (cond [(v= last v)  (values v vs)]
                            [else         (values v (cons v vs))]))]
                   [(vs)  (reverse vs)])
       (cond [(v= last (first vs))  (rest vs)]
             [else  vs]))]))

(:: default-normal (Vectorof Real))
(define default-normal (vector 0.0 -1.0 0.0))

(:: vnormal (-> (Listof (Vectorof Real)) (Vectorof Real)))
(define (vnormal vs)
  (let ([vs  (remove-degenerate-edges vs)])
    (cond
      [((length vs) . < . 3)  default-normal]
      [else
       (let ([vs  (append vs (take vs 2))])
         (define norm
           (for/fold ([norm : (Vectorof Real)  (vector 0.0 0.0 0.0)])
                     ([v1  (in-list vs)]
                      [v2  (in-list (rest vs))]
                      [v3  (in-list (rest (rest vs)))])
             (v+ norm (vcross (v- v3 v2) (v- v1 v2)))))
         (define m (vmag norm))
         (if (m . > . 0) (v/ norm m) default-normal))])))

;; ===================================================================================================
;; Intervals

(define-syntax-rule (maybe-min x y)
  (if x (if y (min* x y) x)
      (if y y #f)))

(define-syntax-rule (maybe-max x y)
  (if x (if y (max* x y) x)
      (if y y #f)))

(:: ivl-guard (-> (U Real #f) (U Real #f) Symbol (Values (U Real #f) (U Real #f))))
(define (ivl-guard a b _)
  (cond [(or (and a (nan? a)) (and b (nan? b)))  (values +nan.0 +nan.0)]
        [(and a b)  (values (min* a b) (max* a b))]
        [else  (values a b)]))

(struct ivl ([min : (U Real #f)] [max : (U Real #f)]) #:transparent #:guard ivl-guard)

(defthing empty-ivl ivl (ivl +nan.0 +nan.0))
(defthing unknown-ivl ivl (ivl #f #f))

(:: ivl-empty? (-> ivl Boolean))
(define (ivl-empty? i)
  (define a (ivl-min i))
  (and a (nan? a)))

(:: ivl-known? (-> ivl Boolean))
(define (ivl-known? i)
  (match-define (ivl a b) i)
  (and a b #t))

(:: ivl-rational? (-> ivl Boolean))
(define (ivl-rational? i)
  (match-define (ivl a b) i)
  (and (rational? a) (rational? b)))

#|
(:: rational-ivl? (-> Any Boolean))
(define (rational-ivl? i)
  (and (ivl? i) (ivl-rational? i)))
|#

(define (rational-ivl? i)
  (and (ivl? i) (ivl-rational? i)))

(:: ivl-singular? (-> ivl Boolean))
(define (ivl-singular? i)
  (match-define (ivl a b) i)
  (and a b (= a b)))

(:: ivl-length (-> ivl (U Real #f)))
(define (ivl-length i)
  (match-define (ivl a b) i)
  (if (and a b) (- b a) #f))

(:: ivl-center (-> ivl (U Real #f)))
(define (ivl-center i)
  (match-define (ivl a b) i)
  (if (and a b) (* 1/2 (+ a b)) #f))

(:: ivl-zero-length? (-> ivl Boolean))
(define (ivl-zero-length? i)
  (or (ivl-empty? i) (ivl-singular? i)))

(:: ivl-inexact->exact (-> ivl ivl))
(define (ivl-inexact->exact i)
  (match-define (ivl a b) i)
  (ivl (and a (if (nan? a) a (inexact->exact a)))
       (and b (if (nan? b) b (inexact->exact b)))))

(:: ivl-contains? (-> ivl Real Boolean))
(define (ivl-contains? i x)
  (match-define (ivl a b) i)
  (and a b (x . >= . a) (x . <= . b)))

(: ivl-meet2 (-> ivl ivl ivl))
(define (ivl-meet2 i1 i2)
  (cond [(or (ivl-empty? i1) (ivl-empty? i2))  empty-ivl]
        [else
         (match-define (ivl a1 b1) i1)
         (match-define (ivl a2 b2) i2)
         (define a (maybe-max a1 a2))
         (define b (maybe-min b1 b2))
         (if (and a b (a . > . b)) empty-ivl (ivl a b))]))

(:: ivl-meet (-> ivl * ivl))
(define (ivl-meet . is)
  (for/fold ([res  unknown-ivl]) ([i  (in-list is)])
    (ivl-meet2 res i)))

(: ivl-join2 (-> ivl ivl ivl))
(define (ivl-join2 i1 i2)
  (cond [(ivl-empty? i1)  i2]
        [(ivl-empty? i2)  i1]
        [else
         (match-define (ivl a1 b1) i1)
         (match-define (ivl a2 b2) i2)
         (ivl (maybe-min a1 a2) (maybe-max b1 b2))]))

(:: ivl-join (-> ivl * ivl))
(define (ivl-join . is)
  (for/fold ([res  empty-ivl]) ([i  (in-list is)])
    (ivl-join2 res i)))

(:: ivl-translate (-> ivl Real ivl))
(define (ivl-translate i d)
  (match-define (ivl a b) i)
  (ivl (and a (+ a d)) (and b (+ b d))))

(:: bounds->intervals (-> (Listof Real) (Listof ivl)))
(define (bounds->intervals xs)
  (cond [((length xs) . < . 2)  (raise-argument-error 'bounds->intervals "list with length >= 2" xs)]
        [else
         (for/list ([x1  (in-list xs)]
                    [x2  (in-list (rest xs))])
           (ivl x1 x2))]))

(:: clamp-real (-> Real ivl Real))
(define (clamp-real x i)
  (match-define (ivl a b) i)
  (let* ([x  (if b (min x b) x)]
         [x  (if a (max x a) x)])
    x))

;; Return a Real drawn randomly from the interval [(- val jitter) (+ val jitter)].
;; If #:ivl is given, result will lie within the given interval.
(: apply-jitter (->* [Real Nonnegative-Real] [#:ivl ivl] Real))
(define (apply-jitter val jitter #:ivl [interval unknown-ivl])
  (let ([offset (* (random) jitter)])
    (if (zero? (random 2))
        (let ([val- (- val offset)])
          (max val- (or (ivl-min interval) val-)))
        (let ([val+ (+ val offset)])
          (min val+ (or (ivl-max interval) val+))))))

;; Precondition: all vectors in the arguments are the same length
(:: points-apply-jitters (->* [(Listof (Vectorof Real)) (Vectorof Nonnegative-Real)] [#:ivls (U (Vectorof ivl) #f)] Void))
(define (points-apply-jitters vals jitters #:ivls [ivls #f])
  (for ([v (in-list vals)])
    (for ([i (in-range (vector-length jitters))])
      (let ([v_i (vector-ref v i)]
            [iv  (if ivls (vector-ref ivls i) unknown-ivl)]
            [jt  (vector-ref jitters i)])
        (vector-set! v i (apply-jitter v_i jt #:ivl iv))))))

;; ===================================================================================================
;; Rectangles

(deftype Rect (Vectorof ivl))

(:: empty-rect (-> Natural Rect))
(define (empty-rect n)
  (make-vector n empty-ivl))

(:: unknown-rect (-> Natural Rect))
(define (unknown-rect n)
  (make-vector n unknown-ivl))

(:: bounding-rect (-> (Listof (Vectorof Real)) Rect))
(define (bounding-rect vs)
  (define-values (mins maxs)
    (for/fold ([mins : (Vectorof Real)  (first vs)]
               [maxs : (Vectorof Real)  (first vs)])
              ([v  (in-list (rest vs))])
      (values (vector-map min mins v)
              (vector-map max maxs v))))
  (vector-map ivl mins maxs))

(:: rect-empty? (-> Rect Boolean))
(define (rect-empty? r)
  (vector-ormap ivl-empty? r))

(:: rect-known? (-> Rect Boolean))
(define (rect-known? r)
  (vector-andmap ivl-known? r))

(:: rect-rational? (-> Rect Boolean))
(define (rect-rational? r)
  (vector-andmap ivl-rational? r))

#|
(:: rational-rect? (-> Any Boolean))
(define (rational-rect? r)
  (and (vector? r) (vector-andmap rational-ivl? r)))
|#

(:: rect-area (-> Rect (U Real #f)))
(define (rect-area r)
  (let/ec break : (U Real #f)
    (for/fold ([area : Real  1]) ([i  (in-vector r)])
      (define len (ivl-length i))
      (cond [(or (not len) (zero? len))  (break len)]
            [else  (* area len)]))))

(:: rect-center (-> Rect (U #f (Vectorof Real))))
(define (rect-center r)
  (define n (vector-length r))
  (let/ec return : (U #f (Vectorof Real))
    (for/vector #:length n ([i  (in-vector r)]) : Real
      (define x (ivl-center i))
      (if x x (return #f)))))

(:: rect-zero-area? (-> Rect Boolean))
(define (rect-zero-area? r)
  (vector-ormap ivl-zero-length? r))

(:: rect-singular? (-> Rect Boolean))
(define (rect-singular? r)
  (vector-andmap ivl-singular? r))

(:: rect-inexact->exact (-> Rect Rect))
(define (rect-inexact->exact r)
  (vector-map ivl-inexact->exact r))

(:: rect-contains? (-> Rect (Vectorof Real) Boolean))
(define (rect-contains? r v)
  (vector-andmap ivl-contains? r v))

(: rect-meet2 (-> Rect Rect Rect))
(define (rect-meet2 r1 r2)
  (vector-map ivl-meet2 r1 r2))

(:: rect-meet (-> Rect * Rect))
(define (rect-meet . rs)
  (define n (apply max (map vector-length rs)))
  (for/fold ([res  (unknown-rect n)]) ([r  (in-list rs)])
    (rect-meet2 res r)))

(: rect-join2 (-> Rect Rect Rect))
(define (rect-join2 r1 r2)
  (vector-map ivl-join2 r1 r2))

(:: rect-join (-> Rect * Rect))
(define (rect-join . rs)
  (define n (apply max (map vector-length rs)))
  (for/fold ([res  (empty-rect n)]) ([r  (in-list rs)])
    (rect-join2 res r)))

(:: rect-translate (-> Rect (Vectorof Real) Rect))
(define (rect-translate r v)
  (vector-map ivl-translate r v))