-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-11.scm
83 lines (77 loc) · 2.65 KB
/
2-11.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
#lang planet neil/sicp
(define (make-interval a b) (cons a b))
(define (lower-bound z) (car z))
(define (upper-bound z) (cdr z))
(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 ((lbx (lower-bound x))
(ubx (upper-bound x))
(lby (lower-bound y))
(uby (upper-bound y)))
(cond ((and (>= lbx 0)
(>= ubx 0)
(>= lby 0)
(>= uby 0))
; [ +, + ] x [ +, + ]
(make-interval (* lbx lby) (* ubx uby)))
((and (>= lbx 0)
(>= ubx 0)
(<= lby 0)
(>= uby 0))
; [ +, + ] x [ -, + ]
(make-interval (* ubx lby) (* ubx uby)))
((and (>= lbx 0)
(>= ubx 0)
(<= lby 0)
(<= uby 0))
; [ +, + ] x [ -, - ]
(make-interval (* ubx lby) (* lbx uby)))
((and (<= lbx 0)
(>= ubx 0)
(>= lby 0)
(>= uby 0))
; [ -, + ] x [ +, + ]
(make-interval (* lbx uby) (* ubx uby)))
((and (<= lbx 0)
(>= ubx 0)
(<= lby 0)
(>= uby 0))
; [ -, + ] x [ -, + ]
(make-interval (min (* lbx uby) (* ubx lby))
(max (* ubx uby) (* lbx lby))))
((and (<= lbx 0)
(>= ubx 0)
(<= lby 0)
(<= uby 0))
; [ -, + ] x [ -, - ]
(make-interval (* ubx lby) (* lbx lby)))
((and (<= lbx 0)
(<= ubx 0)
(>= lby 0)
(>= uby 0))
; [ -, - ] x [ +, + ]
(make-interval (* lbx uby) (* ubx lby)))
((and (<= lbx 0)
(<= ubx 0)
(<= lby 0)
(>= uby 0))
; [ -, - ] x [ -, + ]
(make-interval (* lbx uby) (* lbx lby)))
((and (<= lbx 0)
(<= ubx 0)
(<= lby 0)
(<= uby 0))
; [ -, - ] x [ -, - ]
(make-interval (* ubx uby) (* lbx lby))))))
(define (div-interval x y)
(if (or (= 0 (lower-bound y)) (= 0 (upper-bound y)))
(error "Upper or lower bound of y is 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) (lower-bound y))
(- (upper-bound x) (upper-bound y))))
(display (mul-interval (make-interval -1 5) (make-interval 3 4)))