# Chebyshev Series

Chebyshev polynomials form a special class of polynomials
whose properties make them especially suited for approximating
other functions. As such, they form an essential part of
any numerical library.
**Numerics.NET** supports Chebyshev series through the
ChebyshevSeries
class.

## The ChebyshevSeries class

A Chebyshev series is a linear combination of Chebyshev polynomials. The Chebyshev polynomials are never formed explicitly. All calculations can be performed using only the coefficients.

The Chebyshev polynomials provide an alternate basis for representing general polynomials. Two characteristics make Chebyshev polynomials especially attractive. They are mutually orthogonal, and there exists a simple recurrence relation between three consecutive polynomials. The first characteristic makes it so that most algorithms involving Chebyshev polynomials are numerically stable. The second characteristic makes it easy to evaluate series of Chebyshev polynomials.

Chebyshev polynomials are defined over the interval [-1, 1]. Using Chebyshev series outside of this interval is usually not meaningful and is to be avoided. To allow series over any finite interval, transformations are applied wherever necessary.

The ChebyshevSeries class inherits from PolynomialBase This class defines a number of properties shared by all polynomial classes. The PolynomialBase is itself derived from LinearCombination.

The Parameters of a Chebyshev series are the coefficients of the polynomial.

The Degree of a Chebyshev series is the highest degree of a Chebyshev polynomial that appears in the sum. The number of parameters of the series equals the degree plus one.

## Constructing Chebyshev Series

The ChebyshevSeries has four constructors. The first two variants let you specify the degree of the highest order Chebyshev polynomial in the series. You can also specify the lower and upper bound of the interval. If the lower and upper bound are omitted, the standard interval [-1,1] is assumed:

```
// Chebyshev expansion of degree 5:
ChebyshevSeries c1 = new ChebyshevSeries(5);
// Chebyshev expansion of degree 5 over the interval [0,2]:
ChebyshevSeries c2 = new ChebyshevSeries(5, 0.0, 2.0);
```

The third and fourth variants let you specify the coefficients
of the series, and optionally the lower and upper bound of the interval.
The *k*th element of the coefficient array should be
the coefficient of the Chebyshev polynomial of degree *k*.

```
double[] coefficients = new Double[] { 1, 0.5, 0.25, 0.125, 0.0625 };
// Chebyshev expansion of degree 4 with the above coefficients:
ChebyshevSeries c3 = new ChebyshevSeries(coefficients);
// Chebyshev expansion of degree 4 over the interval [0, 2]:
ChebyshevSeries c4 = new ChebyshevSeries(coefficients, 0.0, 2.0);
```

In addition, you can also create a Chebyshev series using one of several static methods.

The LeastSquaresFit
method constructs a ChebyshevSeries
that is the least squares fit of a specified degree
through a given set of points. If the boundaries
of the approximation interval are not specified,
the smallest and highest *x*-coordinates are used
as the lower and upper bound. The result is mathematically equivalent to
the polynomial obtained from the
LeastSquaresFit
method of the Polynomial
class. The calculation is much more stable, however,
leading to good accuracy even for high degree approximations.

The GetInterpolatingPolynomial
method interpolates a function.
The function is specified as its first parameter in the form of a
Func<T, TResult> delegate.
The second argument is an integer indicating the degree of the polynomial.
This method calculates the interpolating polynomial
(in the form of a Chebyshev series) through the so-called
*Chebyshev points*. This technique ensures that
the approximation deviates as little as possible from the actual function.

The following examples illustrate these methods. We will approximate the function sin(*x*) over the
interval [-pi/2,pi/2] in two ways. First, we will find a least squares fit through 7 points on this curve. We will
then calculate the Chebyshev interpolating polynomial of degree 4.

```
// x and y values contain the data points:
var xValues = Vector.CreateFromFunction(13, i => (i - 6) * Constants.Pi / 12);
var yValues = Vector.Sin(xValues);
// Now we can find the least squares approximation:
ChebyshevSeries lsqApproximation =
ChebyshevSeries.LeastSquaresFit(xValues, yValues,
-Constants.Pi / 2, Constants.Pi / 2, 4);
// Note that, as expected, the odd coefficients are close to zero.
Console.WriteLine("Least squares fit:");
for (int i = 0; i <= 4; i++)
Console.WriteLine("c({0}) = {0}",
i, lsqApproximation[i]);
// Now let's calculate the interpolating Chebyshev expansion:
ChebyshevSeries interpolant =
ChebyshevSeries.GetInterpolatingPolynomial(Math.Sin,
-Constants.Pi / 2, Constants.Pi / 2, 4);
// This expansion is close to the previous one:
Console.WriteLine("Interpolating polynomial:");
for (int i = 0; i <= 4; i++)
Console.WriteLine("c({0}) = {0}",
i, interpolant[i]);
```

Note that, even though the interpolating polynomial was calculated using only a third of the data, the resulting approximation is still very close to the least squares solution.

## Working with Chebyshev series

The ChebyshevSeries class implements all methods and properties of the Curve class.

The ValueAt method returns the value of the polynomial at a specified point. SlopeAt returns the derivative. For series over an interval different from the standard [-1,1], appropriate transformations are applied automatically.

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

The GetDerivative method returns the Chebyshev series that is the derivative of a Chebyshev series.

```
Curve derivative = c1.GetDerivative();
Console.WriteLine("Slope at 2 (derivative) = {0}",
derivative.ValueAt(2));
```

Integral evaluates the definite integral over a specified interval. This value is calculated directly using the coefficients of the series. No numerical approximation is used.