]>
If Cut/Copy and Paste fails, then click here for download.
#include <math.h>
extern double F (double);
void approxc2 (unsigned int n, double *c, double *tmpx, double *tmpy)
{
double pi, h, ai, hi, ya, yb, ci, t0, t1, x2, t2;
unsigned int i, k;
pi = 4.0 * atan (1.0);
h = pi / (double) (n != 1 ? n - 1 : 1);
ai = 0.0;
for (i = 0; i < (n + 1) >> 1; i++) {
hi = h * ai; ai += 1.0; tmpx[i] = cos (hi);
}
for (i = (n + 1) >> 1; i < n; i++)
tmpx[i] = -tmpx[n - (i + 1)];
for (i = 0; i < n; i++)
tmpy[i] = F (tmpx[i]);
h = 2.0 / (double) (n != 1 ? n - 1 : 1);
if (n > 0) {
ya = tmpy[0]; yb = tmpy[n - 1];
}
else
ya = yb = 0.0;
for (i = 0; i < n; i++) {
ci = 0.5 * (ya + ((i & (unsigned int) 0x1) == 0 ? yb : -yb));
t0 = 1.0; t1 = tmpx[i]; x2 = 2.0 * t1;
for (k = 2; k < n; k++) {
t2 = t1; t1 = t0; t0 = t1 * x2 - t2;
ci += t0 * tmpy[k - 1];
}
c[i] = ci * h;
}
if (n > 0)
c[0] *= 0.5;
if (n > 1)
c[n - 1] *= 0.5;
return;
}