; ======================================================================
;
; Structure and Interpretation of Computer Programs
; (trial answer to excercises)
;
; 计算机程序的构造和解释(习题试解)
;
; created: code17 04/25/05
; modified:
; (保持内容完整不变前提下,可以任意转载)
; ======================================================================
;; SICP No.2.11
;; 对于mul-interval 的参数x和y,各存在3种可能的符号搭配,它们是
;; (1)l<=u<=0 (2)l<=0<=u (3)0<=l<=u
;; 因为x和y为互相独立的变量,所以一共有3*3,9种组合
;; 简单分析可知, 除了x和y同时属于第2种情况的组合需要进一步比较确定,
;; 其他组合的计算中,lower-bound和upper-bound由哪些端点计算产生
;; 均可由自变量interval两端的符号直接决定。
;; 比如当x属于(2), y属于(3)时
;; mul-interval x y 的lower-bound显然等于l(x)*u(y)
(define (mul-interval x y)
(let ((l-x (lower-bound x))
(u-x (upper-bound x))
(l-y (lower-bound y))
(u-y (upper-bound y)))
(cond ((> l-x 0) (cond ((> l-y 0) (make-interval (* l-x l-y) (* u-x u-y)))
((< u-y 0) (make-interval (* u-x l-y) (* l-x u-y)))
(else (make-interval (* u-x l-y) (* u-x u-y)))))
((< u-x 0) (cond ((> l-y 0) (make-interval (* l-x u-y) (* u-x l-y)))
((< u-y 0) (make-interval (* u-x u-y) (* l-x l-y)))
(else (make-interval (* l-x u-y) (* l-x l-y)))))
(else (cond ((> l-y 0) (make-interval (* l-x u-y) (* u-x u-y)))
((< u-y 0) (make-interval (* u-x l-y) (* l-x l-y)))
(else (make-interval
(min (* l-x u-y) (* l-y u-x))
(max (* l-x l-y) (* u-x u-y)))))))))
;; Test-it:
;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
;; ;; .............
;; > (mul-interval (make-interval 1 2) (make-interval 3 5))
;; (3 . 10)
;; > (mul-interval (make-interval 1 2) (make-interval -5 -3))
;; (-10 . -3)
;; > (mul-interval (make-interval 1 2) (make-interval -3 5))
;; (-6 . 10)
;; > (mul-interval (make-interval -2 -1) (make-interval 3 5))
;; (-10 . -3)
;; > (mul-interval (make-interval -2 -1) (make-interval -5 -3))
;; (3 . 10)
;; > (mul-interval (make-interval -2 -1) (make-interval -5 3))
;; (-6 . 10)
;; > (mul-interval (make-interval -2 1) (make-interval 3 5))
;; (-10 . 5)
;; > (mul-interval (make-interval -2 1) (make-interval -5 -3))
;; (-5 . 10)
;; > (mul-interval (make-interval -2 1) (make-interval -5 3))
;; (-6 . 10)
本文地址:http://com.8s8s.com/it/it22099.htm