# Piecewise Curves and Cubic Splines

A piecewise curve is a curve that has a different definition on each of a number of intervals. Extreme Numerics.NET supports piecewise constants, lines, and cubic splines.

## The PiecewiseCurve class

The PiecewiseCurve class is the abstract base class for all classes that implement piecewise curves. It defines a number of properties shared by all piecewise curve classes. PiecewiseCurve is itself derived from Curve.

The Degree property specifies the number of pieces that make up the piecewise curve. The x-values that define the boundaries of the pieces are passed to the constructor. The x-values must be listed in ascending order, or an exception will be thrown. Most commonly, the y-values at the boundaries are also specified at the time of construction. An x-value together with its corresponding y-value are called a data point.

A number of methods allow you to get and set the x and y values of an existing piecewise curve. The GetXValue and GetYValue methods take the zero-based index of the data point and return the specified value. The GetDataPoint method does the same and returns a Point structure.

Corresponding to these are SetXValue, SetYValue and SetDataPoint method, which set the x-value, the y-value of both x and y-value of the specified data point to the given value. Care should be taken that the x values remain in ascending order. To set multiple values at once, you can use the SetXValues, SetYValues or SetDataPoints method. These methods take two arguments: an integer array of indices, and a Double or Point array. These methods are useful when setting a single x value would cause the x values to be no longer in ascending order.

The Parameters collection of a piecewise curve has a special structure. The first n+1 parameters are the boundaries of the intervals, where n is the number of intervals. The next n+1 values are the y-values corresponding to the boundaries of the interval. Any remaining parameters further define the shape of the curve.

Since PiecewiseCurve is an abstract class, it cannot be instantiated directly. Three classes inherit from PiecewiseCurve, and represent piecewise constant and linear curves, and cubic splines.

## Piecewise Constant Curves

The PiecewiseConstantCurve class represents a piecewise constant curve, a curve that is constant over each interval. The curve is left-continuous. This means that on each interval, the function value at the left or lower bound is equal to the constant value over the interval. The function value at the right or upper bound is equal to the value of the curve on the next interval.

The PiecewiseConstantCurve class has three constructors. The first constructor takes two Double arrays as arguments. The first array contains the x-values of the data points. The second array contains the y-values. As mentioned before, the x-values must be provided in ascending order. The second constructor takes two Vector<T> objects, with the same meaning as before. The third constructor has only one argument: an array of Point structures. The following example illustrates the use of these constructors:

C#
``````Vector<double> xValues = Vector.Create(1.0, 2.0, 4.0, 6.0);
Vector<double> yValues = Vector.Create(1.0, 3.0, 4.0, 2.0);
PiecewiseConstantCurve constant1 = new PiecewiseConstantCurve(xValues, yValues);
Point[] dataPoints = new Point[]
{new Point(1, 1), new Point(2, 3), new Point(3, 4),
new Point(4, 3), new Point(5, 4), new Point(6, 2)};
PiecewiseConstantCurve constant2 = new PiecewiseConstantCurve(dataPoints);``````

PiecewiseConstantCurve implements most methods and properties of the Curve class.

The ValueAt method returns the value of the curve at a specified point. If the point is the upper bound of an interval, the value is the constant value of the next interval. If the x value is less than the lower bound of the first interval, the value is that of the first interval. If the x value is greater than the upper bound of the last interval, the value is that at the upper bound.

The SlopeAt method returns the derivative. For most values, it is equal to 0. The only exception is on the boundary of an interval, when the curve is discontinuous. In this case, the result is NaN.

C#
``````Console.WriteLine("constant1.ValueAt(2) = {0}", constant1.ValueAt(2));
Console.WriteLine("constant1.SlopeAt(2) = {0}", constant1.SlopeAt(2));``````

Integral evaluates the definite integral over a specified interval. The integral is calculated exactly. The GetDerivative method is not available.

## Piecewise Linear Curves

The PiecewiseLinearCurve class represents a piecewise linear curve, a curve that interpolates linearly between a set of data points. Linear interpolation of tabulated data is the most common application of piecewise linear curves or functions.

The PiecewiseLinearCurve class has three constructors. The first constructor takes two Double arrays as arguments. The first array contains the x-values of the data points. The second array contains the y-values. As mentioned before, the x-values must be provided in ascending order. The second constructor takes two Vector<T> objects, with the same meaning as before. The third constructor has only one argument: an array of Point structures. The following example illustrates the use of these constructors:

C#
``````Vector<double> xValues = Vector.Create(1.0, 2.0, 4.0, 6.0);
Vector<double> yValues = Vector.Create(1.0, 3.0, 4.0, 2.0);
PiecewiseLinearCurve line1 = new PiecewiseLinearCurve(xValues, yValues);
Point[] dataPoints = new Point[]
{new Point(1, 1), new Point(2, 3), new Point(3, 4),
new Point(4, 3), new Point(5, 4), new Point(6, 2)};
PiecewiseLinearCurve line2 = new PiecewiseLinearCurve(dataPoints);``````

PiecewiseLinearCurve implements most methods and properties of the Curve class.

The ValueAt method returns the value of the curve at a specified point. The line on the first interval is extended to the left. The line on the last interval is extended to the right. If the x value is less than the lower bound of the first interval, the value is that of the linear function on the first interval extended to the left. If the x value is greater than the upper bound of the last interval, the value is that of the linear function on the last interval extended to the right.

The SlopeAt method returns the derivative. For most values, it is equal to the slope on the line segment. The only exception is on the boundary of an interval, when the line segments on either side of the point have different slopes. In this case, the result is NaN.

C#
``````Console.WriteLine("line1.ValueAt(2) = {0}", line1.ValueAt(2));
Console.WriteLine("line1.SlopeAt(2) = {0}", line1.SlopeAt(2));``````

Integral evaluates the definite integral over a specified interval. The integral is calculated exactly. The GetDerivative method is not available.

## Cubic Splines

The CubicSpline class represents a cubic spline, a piecewise curve that is a cubic polynomial on each interval. Cubic splines are described in more detail in the next section.