Chebyshev Series in IronPython QuickStart Sample
Illustrates the basic use of the ChebyshevSeries class in IronPython.
This sample is also available in: C#, Visual Basic, F#.
Overview
This QuickStart sample demonstrates how to work with Chebyshev series for polynomial approximation in Numerics.NET. Chebyshev polynomials provide a robust alternative basis for representing and manipulating polynomials with superior numerical stability.
The sample covers:
- Creating Chebyshev series with explicit coefficients over specified intervals
- Generating Chebyshev approximations of arbitrary functions
- Examining the coefficients and error characteristics of Chebyshev approximations
- Performing least squares fitting using Chebyshev basis functions
The example specifically shows approximating the cosine function and demonstrates the high accuracy achievable with relatively low-degree Chebyshev expansions. It includes error analysis at the zeros of higher-degree Chebyshev polynomials and illustrates how to work with both interpolation and least squares fitting approaches.
This approach is particularly valuable when high numerical precision is required or when working with higher-degree polynomial approximations where traditional polynomial bases may become unstable.
The code
import numerics
from math import *
from System import Array
# The ChebyshevSeries class resides in the Extreme.Mathematics.Curves
# namespace.
from Extreme.Mathematics.Curves import *
# The Func<double, double> delegate resides in the
# Extreme.Mathematics namespace.
from Extreme.Mathematics import *
# Illustrates the use of the ChebyshevSeries class
# in the Extreme.Mathematics.Curve namespace of Extreme Numerics.NET.
# Chebyshev polynomials form an alternative basis
# for polynomials. A Chebyshev expansion is a
# polynomial expressed as a sum of Chebyshev
# polynomials.
#
# Using the ChebyshevSeries class instead of
# Polynomial can have two major advantages:
# 1. They are numerically more stable. Higher
# accuracy is maintained even for large problems.
# 2. When approximating other functions with
# polynomials, the coefficients in the
# Chebyshev expansion will tend to decrease
# in size, where those of the normal polynomial
# approximation will tend to oscillate wildly.
#
# Constructing Chebyshev expansions
#
# Chebyshev expansions are defined over an interval.
# The first constructor requires you to specify the
# boundaries of the interval, and the coefficients
# of the expansion.
coefficients = Array[float]([ 1, 0.5, -0.3, 0.1 ])
chebyshev1 = ChebyshevSeries(0, 2, coefficients)
# If you omit the boundaries, they are assumed to be
# -1 and +1:
chebyshev2 = ChebyshevSeries(coefficients)
#
# Chebyshev approximations
#
# A third constructor creates a Chebyshev
# approximation to an arbitrary function. For more
# about the Func<double, double> delegate, see the
# FunctionDelegates QuickStart Sample.
#
# Chebyshev expansions allow us to obtain an
# excellent approximation at minimal cost.
#
# The following creates a Chebyshev approximation
# of degree 7 to Cos(x) over the interval [0, 2]:
approximation1 = ChebyshevSeries.GetInterpolatingPolynomial(cos, 0, 2, 7)
# The coefficients of the expansion are available through
# the indexer property of the ChebyshevSeries object:
print "Chebyshev approximation of cos(x):"
for index in range(0, 8):
print " c{0} = {1}".format(index, approximation1[index])
# The largest errors are approximately at the
# zeroes of the Chebyshev polynomial of degree 8:
for index in range(0,9):
zero = 1 + cos(index * Constants.Pi / 8)
error = approximation1.ValueAt(zero) - cos(zero)
print " Error {0} = {1}".format(index, error)
#
# Least squares approximations
#
# We will now calculate the least squares polynomial
# of degree 7 through 33 points.
# First, calculate the points:
xValues = Vector([ 1 + cos(index * Constants.Pi / 32) for index in range(0,33) ])
yValues = Vector([ cos(x) for x in xValues ])
# Next, define a ChebyshevBasis object for the
# approximation we want: interval [0,2] and degree
# is 7.
basis = ChebyshevBasis(0, 2, 7)
# Now we can calculate the least squares fit:
approximation2 = basis.LeastSquaresFit(xValues, yValues)
# We can see it is close to the original
# approximation we found earlier:
for index in range(0,7):
print " c{0} = {1}".format(index, approximation2[index])