Solutions: You can find the file with solutions for all questions here.

## Required Questions

### Intervals (data abstraction)

Acknowledgements. This interval arithmetic example is based on a classic problem from Structure and Interpretation of Computer Programs, Section 2.1.4.

Introduction. Alyssa P. Hacker is designing a system to help people solve engineering problems. One feature she wants to provide in her system is the ability to manipulate inexact quantities (such as measured parameters of physical devices) with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision.

Alyssa's idea is to implement interval arithmetic as a set of arithmetic operations for combining "intervals" (objects that represent the range of possible values of an inexact quantity). The result of adding, subracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result.

Alyssa postulates the existence of an abstract object called an "interval" that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor interval. Using the constructor and selectors, she defines the following operations:

``````def str_interval(x):
"""Return a string representation of interval x.

>>> str_interval(interval(-1, 2))
'-1 to 2'
"""
return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
"""Return an interval that contains the sum of any value in interval x and
any value in interval y.

>>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
'3 to 10'
"""
lower = lower_bound(x) + lower_bound(y)
upper = upper_bound(x) + upper_bound(y)
return interval(lower, upper)

def mul_interval(x, y):
"""Return the interval that contains the product of any value in x and any
value in y.

>>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
'-8 to 16'
"""
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)
return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))``````

A constructor is something that creates whatever you want to make, and a selector gets the elements from the thing you made. For example, your constructor `interval` will take in two numbers a and b. It will construct a two element list of them. Then your `lower_bound` selector will return the smaller item in the list and the `upper_bound` selector will return the bigger element in the list.

### Question 1

Alyssa's program is incomplete because she has not specified the implementation of the interval abstraction. Define the constructor and selectors in terms of two-element lists:

``````def interval(a, b):
"""Construct an interval from a to b."""
return [a, b]

def lower_bound(x):
"""Return the lower bound of interval x."""
return x

def upper_bound(x):
"""Return the upper bound of interval x."""
return x``````

Use OK to test your code:

``````python3 ok -q str_interval
python3 ok -q add_interval
python3 ok -q mul_interval``````

### Question 2

Alyssa implements division below, by multiplying by the reciprocal of `y`. Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Return `False` if the interval being divided by contains zero.

``````def div_interval(x, y):
"""Return the interval that contains the quotient of any value in x divided by any value in y.

Division is implemented as the multiplication of x by the reciprocal of y.

>>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
'-0.25 to 0.5'
>>> div_interval(interval(4, 8), interval(-1, 2))
False
"""
if not (lower_bound(y) > 0 or upper_bound(y) < 0):
return False
reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
return mul_interval(x, reciprocal_y)``````

Use OK to test your code:

``python3 ok -q div_interval``

### Question 3

Using reasoning analogous to Alyssa's, define a subtraction function for intervals:

``````def sub_interval(x, y):
"""Return the interval that contains the difference between any value in x
and any value in y.

>>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
'-9 to -2'
"""
negative_y = interval(-upper_bound(y), -lower_bound(y))
return add_interval(x, negative_y)``````

Use OK to test your code:

``python3 ok -q sub_interval``

### Question 4

After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as `3.5 +/- 0.15` rather than `3.35` to `3.65`. Alyssa returns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors in terms of the existing ones:

``````def make_center_width(c, w):
"""Construct an interval from center and width."""
return interval(c - w, c + w)

def center(x):
"""Return the center of interval x."""
return (upper_bound(x) + lower_bound(x)) / 2

def width(x):
"""Return the width of interval x."""
return (upper_bound(x) - lower_bound(x)) / 2``````

Unfortunately, most of Alyssa's users are engineers. Real engineering situations usually involve measurements with only a small uncertainty, measured as the ratio of the width of the interval to the midpoint of the interval. Engineers usually specify percentage tolerances on the parameters of devices.

Define a constructor `make_center_percent` that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above:

``````def make_center_percent(c, p):
"""Construct an interval from center and percentage tolerance.

>>> str_interval(make_center_percent(2, 50))
'1.0 to 3.0'
"""
return make_center_width(c, c*p/100)

def percent(x):
"""Return the percentage tolerance of interval x.

>>> percent(interval(1, 3))
50.0
"""
return 100 * width(x) / center(x)``````

Use OK to test your code:

``python3 ok -q make_center_percent``

### Question 5

Write a function `quadratic` that returns the interval of all values `f(t)` such that `t` is in the argument interval `x` and `f(t)` is a quadratic function:

``f(t) = a*t*t + b*t + c``

Make sure that your implementation returns the smallest such interval, one that does not suffer from the multiple references problem.

Hint: the derivative `f'(t) = 2*a*t + b`, and so the extreme point of the quadratic is `-b/(2*a)`:

``````def quadratic(x, a, b, c):
"""Return the interval that is the range of the quadratic defined by
coefficients a, b, and c, for domain interval x.

>>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
'-3 to 0.125'
>>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
'0 to 10'
"""
extremum = -b / (2*a)
f = lambda x: a * x * x + b * x + c
l, u, e = map(f, (lower_bound(x), upper_bound(x), extremum))
if extremum >= lower_bound(x) and extremum <= upper_bound(x):
return interval(min(l, u, e), max(l, u, e))
else:
return interval(min(l, u), max(l, u))``````

Use OK to test your code:

``python3 ok -q quadratic``

## Extra Questions

Extra questions are not worth extra credit and are entirely optional. They are designed to challenge you to think creatively!

### Question 6

Write a function polynomial that takes an interval `x` and a list of coefficients `c`, and returns the interval containing all values of `f(t)` for `t` in interval `x`, where:

``f(t) = c[k-1] * pow(t, k-1) + c[k-2] * pow(t, k-2) + ... + c * 1``

Like quadratic, your polynomial function should return the smallest such interval, one that does not suffer from the multiple references problem.

Hint: You can approximate this result. Try using Newton's method.

``````def polynomial(x, c):
"""Return the interval that is the range of the polynomial defined by
coefficients c, for domain interval x.

>>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
'-3 to 0.125'
>>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
'0 to 10'
>>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
'18.0 to 23.0'
"""
def add_fn(coeff, k, f):
return lambda x: coeff * pow(x, k) + f(x)

def add_dfn(coeff, k, df):
return lambda x: k * coeff * pow(x, k-1) + df(x)

def add_ddfn(coeff, k, ddf):
return lambda x: k * (k-1) * coeff * pow(x, k-2) + ddf(x)

# Define the polynomial and its first and second derivatives.
f = lambda x: 0
df = lambda x: 0
ddf = lambda x: 0
for k, coeff in enumerate(c):
f = add_fn(coeff, k, f)
if k > 0:
df = add_dfn(coeff, k, df)
if k > 1:
ddf = add_ddfn(coeff, k, ddf)

# Find as many extreme points as we can using Newton's method
lower, upper = lower_bound(x), upper_bound(x)
num_steps = 20
step = (upper - lower) / num_steps
starts = [lower + k * step for k in range(num_steps)]
extremums = [find_zero(df, ddf, n) for n in starts]

# Filter for the interval x and return
ns = [n for n in extremums if n > lower and n < upper] + [lower, upper]
values = [f(n) for n in ns]
return interval(min(values), max(values))

# Newton's method from lecture

def improve(update, close, guess=1, max_updates=100):
"""Iteratively improve guess with update until close(guess) is true or
max_updates have been applied."""
k = 0
while not close(guess) and k < max_updates:
guess = update(guess)
k = k + 1
return guess

def approx_eq(x, y, tolerance=1e-15):
return abs(x - y) < tolerance

def find_zero(f, df, guess=1):
"""Return a zero of the function f with derivative df."""
def near_zero(x):
return approx_eq(f(x), 0)
return improve(newton_update(f, df), near_zero, guess)

def newton_update(f, df):
"""Return an update function for f with derivative df,
using Newton's method."""
def update(x):
return x - f(x) / df(x)
return update``````

Use OK to test your code:

``python3 ok -q polynomial``