Chebyshev coefficients: Difference between revisions
m (→{{header|REXX}}: added optimization.) |
m (→{{header|REXX}}: added another optimization.) |
||
Line 69: | Line 69: | ||
fac=2/n |
fac=2/n |
||
do j=0 for n; $=0 |
do j=0 for n; $=0 |
||
piJ=pi*j |
piJ@n=pi*j/n |
||
do m=0 for n |
do m=0 for n |
||
$=$+!.m*cos(piJ*(m+.5) |
$=$+!.m*cos(piJ@n*(m+.5)) |
||
end /*m*/ |
end /*m*/ |
||
cheby.j=fac*$ |
cheby.j=fac*$ |
Revision as of 21:46, 2 August 2015
Chebyshev coefficients are the basis of polynomial approximations of functions. Write a program to generate Chebyshev coefficients.
Calculate coefficients: cosine function, 10 coefficients, interval 0 1
C
<lang C> // Program to calculate Chebyshev coefficients // Code taken from Numerical Recipes in C 1/e
- include <math.h>
- define PI 3.141592653589793
void chebft(float a, float b, float c[], int n, float (*func)()) {
int k,j; float fac,bpa,bma,f[300]; bma = 0.5 * (b-a) bpa = 0.5 * (b+a) for(k = 0;k<n;k++) { float y = cos(PI*(k+0.5)/n); f[k] = (*func)(y*bma+bpa); } fac = 2.0/n; for (j = 0;j<n;j++) { double sum = 0.0; for(k = 0;k<n;k++) sum += f[k] * cos(PI*j*(k+0.5)/n); c[j] = fac*sum; }
} </lang>
J
From 'J for C Programmers: Calculating Chebyshev Coefficients [[1]] <lang J> chebft =: adverb define
f =. u 0.5 * (+/y) - (-/y) * 2 o. o. (0.5 + i. x) % x
(2 % x) * +/ f * 2 o. o. (0.5 + i. x) *"0 1 (i. x) % x
) </lang> Calculate coefficients: <lang J>
10 (2&o.) chebft 0 1
1.64717 _0.232299 _0.0537151 0.00245824 0.000282119 _7.72223e_6 _5.89856e_7 1.15214e_8 6.59629e_10 _1.00227e_11 </lang>
REXX
<lang rexx>/*REXX program calculates ten Chebyshev coefficients for the range 0 ──► 1.*/ /*Pafnuty Lvovich Chebysheff: Chebysheff [English transliteration] */ /*─────────────────────────── Chebyshov [ " " */ /*─────────────────────────── Tchebychev [French " */ /*─────────────────────────── Tchebysheff [ " " */ /*─────────────────────────── Tschebyschow [German " */ /*─────────────────────────── Tschebyschev [ " " */ /*─────────────────────────── Tschebyschef [ " " */ /*─────────────────────────── Tschebyscheff [ " " */ numeric digits length(pi()) /*DIGITS default is 9, but use more. */ parse arg a b n . /*obtain optional arguments from the CL.*/ if a== | a==',' then a=0 /*A not specified? Then use the default*/ if b== | b==',' then b=1 /*B " " " " " " */ if n== | n==',' then n=10 /*N " " " " " " */ bma=(b-a)/2 /*calculate one-half of the difference. */ bpa=(b+a)/2 /* " " " " sum. */
do k=0 for n y=cos(pi*(k+.5)/n) !.k=y*bma+bpa end /*k*/
fac=2/n
do j=0 for n; $=0 piJ@n=pi*j/n do m=0 for n $=$+!.m*cos(piJ@n*(m+.5)) end /*m*/ cheby.j=fac*$ end /*j*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────subroutines───────────────────────────────────────────────*/ cos: procedure; parse arg x; x=r2r(x); a=abs(x); numeric fuzz 5
if a=pi then return -1; if a=pi*.5 | a=pi*2 then return 0 pi3=pi/3; if a=pi3 then return .5; if a=2*pi3 then return -.5 z=1; _=1; q=x*x do k=2 by 2 until p=z; p=z; _=-_*q/(k*(k-1)); z=z+_; end /*k*/ return z
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862; return pi
r2r: return arg(1) // (pi()*2) /*normalize radians ──► a unit circle*/</lang>