-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintervals.scm
100 lines (80 loc) · 2.42 KB
/
intervals.scm
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
(define (make-interval a b) (cons a b))
(define (upper-bound interval) (max (car interval) (cdr interval)))
(define (lower-bound interval) (min (car interval) (cdr interval)))
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))
)
)
(define (mul-interval x y)
(let (
(p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y)))
)
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4)
)
)
)
(define (div-interval x y)
(if (= (lower-bound y) (upper-bound y))
(error "Division by interval that spans zero")
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))
)
)
)
)
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))
)
)
(define (make-center-width c w)
(make-interval (- c w) (+ c w))
)
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2)
)
(define (width i)
(/ (- (upper-bound i) (lower-bound u)) 2)
)
(define (make-center-percent c p)
(make-interval (- c (* c p)) (+ c (* c p)))
)
; center selector is unchanged
(define (percent i)
(/ (width i) (center i))
)
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)
)
)
(define (par2 r1 r2)
(let (
(one (make-interval 1 1))
)
(div-interval one
(add-interval (div-interval one r1)
(div-interval one r2)
)
)
)
)
(define r1 (make-center-percent 100 0.1))
(define r2 (make-center-percent 200 0.1))
(par1 r1 r2)
(par2 r1 r2)
(div-interval r1 r1)
(div-interval r1 r2)
; The problem is that by dividing A/A we get as a result an interval and not 1.
; The method does not know that (div-interval A A) represents the same number and
; that it should return one, instead it assumes the first argument is some number
; from range of A and the second argument is some other number from range of A
; and computes the interval accordingly.
; Moreover, a lot of algebraic laws that are true for numbers do not hold in
; this interval arithmetic system.