C++ Functions to Evaluate Chebyshev Polynomials

Here’s some C++ functions which evaluate Chebyshev polynomials:

T0(x):
double T0(double x) ;

T1(x):
double T1(double x) ;

T2(x):
double T2(double x) ;

Tn(x):
double Tn(unsigned int n, double x) ;

These are inline functions defined in the header file, chebyshev.h:

— start —

//==================================================================
/**
 *  chebyshev.h -- C++ functions to evaluate Chebyshev polynomials
 *
 *  Copyright (C) 2007 by James A. Chappell (rlrrlrll@gmail.com)
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or
 *  sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  condition:
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */
//=================================================================
/*
 * chebyshev.h:  Version 0.01
 * Created by James A. Chappell
 * Created 29 September 2005
 *
 * History:
 * 28-jul-2007  created
 */
//==============

#ifndef __CHEBYSHEV_H__
#define __CHEBYSHEV_H__
/*
 *	Function calculates Chebyshev Polynomials Tn(x)
 */

namespace Chebyshev
{
  // n = 0
  inline double T0(double x)
  {
    return 1.0 ;
  }

  // n = 1
  inline double T1(double x)
  {
    return x ;
  }

  // n = 2
  inline double T2(double x)
  {
    return (2.0 * x*x) - 1.0 ;
  }

/*
 *	Tn(x)
 */
  inline double Tn(unsigned int n, double x)
  {
    if (n == 0)
    {
      return T0(x) ;
    }
    else if (n == 1)
    {
      return T1(x) ;
    }
    else if (n == 2)
    {
      return T2(x) ;
    }

/* We could simply do this:
    return (2.0 * x * Tn(n - 1, x)) - Tn(n - 2, x) ;
   but it could be slow for large n */
 
    double tnm1(T2(x)) ;
    double tnm2(T1(x)) ;
    double tn(tnm1) ;

    for (unsigned int l = 3 ; l <= n ; l++)
    { 
      tn = (2.0 * x * tnm1) - tnm2 ;
      tnm2 = tnm1;
      tnm1 = tn;
    }

    return tn ;
  }
}
#endif

— end —

Here’s a sample program:

— start —

#include <iostream>
#include "chebyshev.h"

using namespace std ;
using namespace Chebyshev ;

int main()
{
  double tn ;

  cout.precision(5) ;
  for (unsigned int n = 0 ; n <= 5 ; n++)
  {
    for (double x = -1.0 ; x <= 1.0 ; x = x + 0.1)
    { 
      tn = Tn(n, x) ;
      cout << "T" << n << "(" << x << ") = " << tn << endl ;
    }
    cout << endl ;
  }

  return 0 ;
}

— end —

UPDATE: 2014-10-01:>
This project can now be found on GitHub:
Chebyshev-polynomials
– HTTPS Clone URL: https://github.com/jachappell/Chebyshev-polynomials.git
– Download ZIP

4 thoughts on “C++ Functions to Evaluate Chebyshev Polynomials”

  1. I don’t understand your program, however, i have a question for you. “How do you use a struct data type to create a function that evaluates polynomial addition, subtraction and synthetic division?” I would appreciate any help from you. Thanx.

  2. I dont get your program,i have a problem with using struct data to create,input,display,adding,subtracting,dividing ,multiplying,deleting a polynomial of exponents in an array.Please I highly need your help.That will be my pleasure if i get your assistance

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.