Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created December 1, 2010 06:50
Show Gist options
  • Save pervognsen/723088 to your computer and use it in GitHub Desktop.
Save pervognsen/723088 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
template<size_t N>
struct Chebyshev_CB
{
double operator()(double x)
{
return 2.0 * x * Chebyshev_CB<N-1>()(x) - Chebyshev_CB<N-2>()(x);
}
};
template<>
struct Chebyshev_CB<1>
{
double operator()(double x)
{
return x;
}
};
template<>
struct Chebyshev_CB<0>
{
double operator()(double x)
{
return 1.0;
}
};
template<size_t N, int F=1>
struct Chebyshev_Per
{
double operator()(double x)
{
return x * Chebyshev_Per<N-1, 2*F>()(x) - Chebyshev_Per<N-2, F>()(x);
}
};
template<int F>
struct Chebyshev_Per<1, F>
{
double operator()(double x)
{
return double(F) * x;
}
};
template<int F>
struct Chebyshev_Per<0, F>
{
double operator()(double x)
{
return double(F);
}
};
inline double chebyshev_ryg(int n, double x)
{
double a = 1.0, b = x;
while (n--) {
const double t = b;
b = 2.0 * x * b - a;
a = t;
}
return a;
}
void test_per(double x)
{
double per = Chebyshev_Per<10>()(x);
printf("Per: %g\n", per);
}
void test_cb(double x)
{
double cb = Chebyshev_CB<10>()(x);
printf("Per: %g\n", cb);
}
void test_ryg(double x)
{
double cb = chebyshev_ryg(10, x);
printf("Ryg: %g\n", cb);
}
int main()
{
char line[1024];
while (true) {
gets(line);
double x = atof(line);
test_per(x);
test_cb(x);
test_ryg(x);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment