Interval Arithmetic
December 21, 2010
Over at his blog, where he has been solving the exercises in SICP, Bill Cruise reached the section of the book that discusses interval arithmetic; if you want an introduction to the topic, you might want to look at http://www.cs.utep.edu/interval-comp/hayes.pdf.
Given an interval [x,y] with x the lower bound and y the upper bound, the basic operations of interval arithmetic are defined as follows:
- [a,b] + [c,d] = [a+c,b+d]
- [a,b] − [c,d] = [a−d,b−c]
- [a,b] × [c,d] = [min(ac,ad,bc,bd), max(ac,ad,bc,bd)]
- [a,b] ÷ [c,d] = [min(a/c,a/d,b/c,b/d), max(a/c,a/d,b/c,b/d)], where division by an interval containing zero is undefined
Your task is to write a basic library for interval arithmetic; you may do as many of the SICP exercises as you wish. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] Praxis – Interval Arithmetic By Remco Niemeijer In today’s Programming Praxis exercise, our goal is to implement some functions to do interval arithmetic. […]
My Haskell solution (see http://bonsaicode.wordpress.com/2010/12/21/programming-praxis-interval-arithmetic/ for a version with comments):
My Python submission is available on codepad.org.
It requires the fractions module (new in Python 2.6), which adds support for rational number arithmetic.
My work is slightly hackish and could use better variable names, but it’ll do for now.
I guess something like this is good enough:
(defclass interval-class ()
((min :initarg :min)
(max :initarg :max)))
(defmethod print-object ((iv interval-class) stream)
(format stream “[~a,~a]” (slot-value iv ‘min)
(slot-value iv ‘max)))
(defvar it1 (make-instance ‘interval-class :min 20 :max 30))
(defvar it2 (make-instance ‘interval-class :min 10 :max 20))
(defvar it3 (make-instance ‘interval-class :min -10 :max 20))
(defmacro it-op (name min max &optional guard)
`(defmethod ,name ((i1 interval-class)
(i2 interval-class))
(macrolet ((a () `(slot-value i1 ‘min))
(b () `(slot-value i1 ‘max))
(c () `(slot-value i2 ‘min))
(d () `(slot-value i2 ‘max)))
(unless ,guard
(make-instance ‘interval-class :min ,min :max ,max)))))
(it-op it-add (+ (a) (c)) (+ (b) (d)))
(it-op it-sub (- (a) (c)) (- (b) (d)))
(it-op it-mul
(min (* (a) (c)) (* (a) (d))
(* (b) (c)) (* (b) (d)))
(max (* (a) (c)) (* (a) (d))
(* (b) (c)) (* (b) (d))))
(it-op it-div
(min (/ (a) (c)) (/ (a) (d))
(/ (b) (c)) (/ (b) (d)))
(max (/ (a) (c)) (/ (a) (d))
(/ (b) (c)) (* (b) (d)))
(or (and (<= (a) 0) (<= 0 (b)))
(and (<= (c) 0) (<= 0 (d)))))
Next time, I’ll implement a reader to add intervals to the syntax :)
With less bugs :)
Please disregard the above untested code :)
(defclass interval-class ()
((min :reader i-min :initarg :min)
(max :reader i-max :initarg :max)))
(set-macro-character #\[
(lambda (stream char)
(let* ((lst (read-delimited-list #\] stream t)))
(if (= 2 (length lst))
(make-instance ‘interval-class :min (apply #’min lst) :max (apply #’max lst))
(error “Invalid interval”)))))
(set-macro-character #\] (get-macro-character #\)))
(defmethod print-object ((iv interval-class) stream)
(format stream “[~a,~a]” (i-min iv) (i-max iv)))
(defmethod plus ((i1 interval-class) (i2 interval-class))
(let ((a (+ (i-min i1) (i-min i2)))
(b (+ (i-max i1) (i-max i2))))
(make-instance ‘interval-class :min (min a b) :max (max a b))))
(defmethod minus ((i1 interval-class) (i2 interval-class))
(let ((a (- (i-min i1) (i-max i2)))
(b (- (i-max i1) (i-min i2))))
(make-instance ‘interval-class :min (min a b) :max (max a b))))
(defmacro m-d (opname op &optional guard)
`(defmethod ,opname ((i1 interval-class) (i2 interval-class))
(unless ,guard
(let* ((b0 (loop for i in (list #’i-min #’i-max)
append (loop for j in (list #’i-min #’i-max)
collect (,op (funcall i i1) (funcall j i2)))))
(b1 (apply #’min b0))
(b2 (apply #’max b0)))
(make-instance ‘interval-class :min b1 :max b2)))))
(m-d mul *)
(m-d div / (and (<= (i-min i2) 0) (<= 0 (i-max i2))))
(defvar x [1 2])
(defvar y [4 3]) ;; reverse order \o/
(list (plus x y) (minus x y) (mul x y) (div x y))
A ruby version using the Rational class. The to_s is has a bunch of extra code to make it look “pretty” if the denominator is 1. It might be better to monkey patch this in the Rational class itself.
As Chun Kin Lee pointed out on my blog, my original attempt at removing duplication from times and divide doesn’t work on ranges with negative numbers, so you actually have to get the minimum/maximum of the four possible combinations.