区間演算
(make-interval <n> <n>)
(lower-bound <x>)
(upper-bound <x>)
(print-interval <x>)
(define (intadd x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))
(define (intmul 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 (intdiv x y)   ; 区間 y は 0 をまたがないとする
  (intmul x
          (make-interval
            (/ 1 (upper-bound y))
            (/ 1 (lower-bound y)))))
> (define x1 (make-interval -1 1))   ; -1 <= x1 <= 1
> (define x2 (make-interval 2 3))    ;  2 <= x2 <= 3
> (print-interval (intadd x1 x2))
[1,4]                                ; 1 <= x1+x2 <= 4
done
> (print-interval (intmul x1 x2))
[-3,3]                               ; -3 <= x1*x2 <= 3
done
> (print-interval (intdiv x1 x2))
[-1/2,1/2]                           ; -1/2 <= x1/x2 <= 1/2
done


一覧 前へ 次へ