## 2.2 Interpolating

As I have said, a Yorick array often represents the values of a continuous function at a number of discrete points. In order to find the values of the function at other points, you need to know how it varies between (or beyond) the given points. In general, to interpolate (or extrapolate), you need a detailed understanding of how the function was discretized in the first place. However, when the list of values accurately represents the function, linear interpolation between the known points will suffice. A function which is linear between successive points is called "piecewise linear".

The `interp` function is a mechanism for converting a list of
function values at discrete points into a piecewise linear function
which can be evaluated at any point.

theta = span(0, pi, 100); x_circle = cos(theta); y_circle = sin(theta); x = span(-2, 2, 64); y = interp(y_circle, x_circle, x); |

This code fragment produces a `y` array with the same number of
points as `x` (64), with the values of the piecewise linear function
defined by the points `(x_circle, y_circle)`. Outside the range
covered by `x_circle`, the piecewise linear function remains
constant -- the simplest possible extrapolation rule.

Regarded as a function of its third argument, `interp` behaves just
like the `sin` or `cos` function -- its first two arguments are
really parameters specifying which piecewise linear function
`interp` will evaluate.

The `integ` function works just like `interp`, except that it
returns the integral of the piecewise linear function. The integration
constant is chosen so that `integ` returns zero at the first point
of the piecewise linear function. (This point will actually have the
maximum value of x if the x array is decreasing.) Thus, the integral
of the piecewise linear approximation to the semicircle and the exact
integral of the semicircle can be computed by:

yi = integ(y_circle, x_circle, x); yi_exact = 0.5*(acos(max(min(x,1),-1)) - x*sqrt(1-min(x^2,1))); |

Again, the piecewise linear function is assumed to remain constant
beyond the first and last points specified. Hence, `integ` is a
linear function when extrapolating, and piecewise parabolic when
interpolating.

Use `integ` only when you need the indefinite integral of a
piecewise linear function. Yorick has more efficient ways to compute
definite integrals. Again, think of `integ`, like `interp`, as
a continuous function of its third argument; the first two arguments are
parameters specifying which function.

Neither `interp` nor `integ` makes sense unless its second
argument is either increasing or decreasing. There is no way to decide
which branch of a multi-valued function should be returned.

Internally, both `interp` and `integ` need a lookup function --
that is, a function which finds the index of the point in x_circle just
beyond each of the x values. This lookup function can also be called
directly; its name is `digitize`.