-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol-2.11.tex
54 lines (53 loc) · 2.16 KB
/
sol-2.11.tex
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
An interval can have either both extrema $<0$, or both extrema $\geq 0$, or one extrema $<0$ and the other $\geq 0$. Since we multiply two intervals, in total there are $3^2$ cases. Eight of these cases require two multiplications only, while the remaining one requires four multiplications.
\begtt\scm
(define (mul-interval x y)
(let ((a (lower-bound x))
(b (upper-bound x))
(c (lower-bound y))
(d (upper-bound y)))
(cond ((and (>= a 0) (>= c 0))
(make-interval (* a c) (* b d)))
((and (>= a 0) (< c 0) (>= d 0))
(make-interval (* b c) (* b d)))
((and (>= a 0) (< d 0))
(make-interval (* b c) (* a d)))
((and (< a 0) (>= b 0) (>= c 0))
(make-interval (* a d) (* b d)))
((and (< a 0) (>= b 0) (< c 0) (>= d 0))
(make-interval (min (* a d) (* b c))
(max (* b d) (* a c))))
((and (< a 0) (>= b 0) (< d 0))
(make-interval (* b c) (* a c)))
((and (< b 0) (>= c 0))
(make-interval (* a d) (* b c)))
((and (< b 0) (< c 0) (>= d 0))
(make-interval (* a d) (* a c)))
((and (< b 0) (< d 0))
(make-interval (* b d) (* a c))))))
\endtt
We can also avoid redundant checks by rewriting the procedure in the following way:
\begtt\scm
(define (mul-interval x y)
(let ((a (lower-bound x))
(b (upper-bound x))
(c (lower-bound y))
(d (upper-bound y)))
(if (>= a 0)
(if (>= c 0)
(make-interval (* a c) (* b d))
(if (>= d 0)
(make-interval (* b c) (* b d))
(make-interval (* b c) (* a d))))
(if (>= b 0)
(if (>= c 0)
(make-interval (* a d) (* b d))
(if (>= d 0)
(make-interval (min (* a d) (* b c))
(max (* b d) (* a c)))
(make-interval (* b c) (* a c))))
(if (>= c 0)
(make-interval (* a d) (* b c))
(if (>= d 0)
(make-interval (* a d) (* a c))
(make-interval (* b d) (* a c))))))))
\endtt