## Interval Arithmetic

### December 21, 2010

We represent an interval as a pair and state the operations as given:

`(define (plus x y)`

(cons (+ (car x) (car y)) (+ (cdr x) (cdr y))))

`(define (minus x y)`

(cons (- (car x) (cdr y)) (- (cdr x) (car y))))

`(define (times x y)`

(cons (min (* (car x) (car y)) (* (car x) (cdr y))

(* (cdr x) (car y)) (* (cdr x) (cdr y)))

(max (* (car x) (car y)) (* (car x) (cdr y))

(* (cdr x) (car y)) (* (cdr x) (cdr y)))))

`(define (divide x y)`

(if (< (car y) 0 (cdr y))

(error 'divide "divide by zero")

(cons (min (/ (car x) (car y)) (/ (car x) (cdr y))

(/ (cdr x) (car y)) (/ (cdr x) (cdr y)))

(max (/ (car x) (car y)) (/ (car x) (cdr y))

(/ (cdr x) (car y)) (/ (cdr x) (cdr y))))))

To represent the interval as a pair containing its central point and a tolerance (width), we provide two conversions back-and-forth between the two representations:

`(define (ends->center x)`

(cons (/ (+ (car x) (cdr x)) 2)

(/ (- (cdr x) (car x)) 2)))

`(define (center->ends x)`

(cons (- (car x) (cdr x)) (+ (car x) (cdr x))))

Here are some examples, which are reproduced at http://programmingpraxis.codepad.org/JZQwpzdb.

`> (define x (cons 1 2))`

> (define y (cons 3 4))

> (plus x y)

(4 . 6)

> (minus x y)

(-3 . -1)

> (times x y)

(3 . 8)

> (divide x y)

(1/4 . 2/3)

> (ends->center x)

(3/2 1/2)

> (center->ends (cons 3/2 1/2))

(1 . 2)

> (divide x x)

(1/2 . 2)

For the other three exercises, note that the problem is that normal algebra doesn’t necessarily work on intervals. You can see the basic problem by dividing an interval by itself: if *x* is the interval [1, 2], *x*/*x* is not 1, as we are accustomed to in algebra, but the interval [1/2, 2] that includes 1 as it’s geometric mean. Eva is correct in pointing out that `par2`

is better than `par1`

because it calculates each interval once, instead of applying one interval to another. The last exercise is the subject of several books and academic papers, which we won’t even try to summarize here; if you’re interested, a good place to start is Vladik Kreinovich’s web site.

Pages: 1 2

[...] 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.