-
Notifications
You must be signed in to change notification settings - Fork 5
/
2.11.scm
executable file
·79 lines (73 loc) · 2.94 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
#!/usr/bin/env chicken-scheme
(use test)
(include "interval.scm")
(define (positive-interval? x)
(and (positive? (lower-bound x))
(positive? (upper-bound x))))
(define (negative-interval? x)
(and (negative? (lower-bound x))
(negative? (upper-bound x))))
(define (mixed-interval? x)
(xor (positive? (lower-bound x))
(positive? (upper-bound x))))
(define (mul-interval-cases x y)
(let ((x-negative? (negative-interval? x))
(y-negative? (negative-interval? y))
(x-positive? (positive-interval? x))
(y-positive? (positive-interval? y))
(x-mixed? (mixed-interval? x))
(y-mixed? (mixed-interval? y))
(x-lower (lower-bound x))
(x-upper (upper-bound x))
(y-lower (lower-bound y))
(y-upper (upper-bound y)))
(cond ((and x-negative? y-negative?)
(make-interval (* x-upper y-upper)
(* x-lower y-lower)))
((and x-negative? y-positive?)
(make-interval (* x-lower y-upper)
(* x-upper y-lower)))
((and x-negative? y-mixed?)
(make-interval (* x-lower y-upper)
(* x-lower y-lower)))
((and x-positive? y-negative?)
(make-interval (* x-upper y-lower)
(* x-lower y-upper)))
((and x-positive? y-positive?)
(make-interval (* x-lower y-lower)
(* x-upper y-upper)))
((and x-positive? y-mixed?)
(make-interval (* x-upper y-lower)
(* x-upper y-upper)))
((and x-mixed? y-negative?)
(make-interval (* x-upper y-lower)
(* x-lower y-lower)))
((and x-mixed? y-positive?)
(make-interval (* x-lower y-upper)
(* x-upper y-upper)))
((and x-mixed? y-mixed?)
(make-interval (min (* x-lower y-upper)
(* x-upper y-lower))
(max (* x-upper y-upper)
(* x-lower y-lower)))))))
(let ((negative (make-interval -5 -3))
(mixed (make-interval -1 7))
(positive (make-interval 11 13)))
(test (mul-interval negative negative)
(mul-interval-cases negative negative))
(test (mul-interval negative positive)
(mul-interval-cases negative positive))
(test (mul-interval negative mixed)
(mul-interval-cases negative mixed))
(test (mul-interval positive negative)
(mul-interval-cases positive negative))
(test (mul-interval positive positive)
(mul-interval-cases positive positive))
(test (mul-interval positive mixed)
(mul-interval-cases positive mixed))
(test (mul-interval negative negative)
(mul-interval-cases negative negative))
(test (mul-interval mixed positive)
(mul-interval-cases mixed positive))
(test (mul-interval mixed mixed)
(mul-interval-cases mixed mixed)))