Debian Codegraph @

School05/Tuesday/cheby.C

raw
1 #include "headcpp.h"
2 #include "math.h"
3
4 #include "tbl.h"
5 #include "matrice.h"
6 #include "cheby.h"
7
8 // Chebyshev polynomial
9 double cheby (int n, double x) {
10
11 if (n==0)
12 return 1 ;
13 else if (n==1)
14 return x ;
15 else {
16 double tm2 = 1 ;
17 double tm1 = x ;
18 double tm ;
19 for (int i=2 ; i<=n ; i++) {
20 tm = 2*x*tm1 - tm2 ;
21 tm2 = tm1 ;
22 tm1 = tm ;
23 }
24 return tm ;
25 }
26 }
27
28 // Norme
29 double norme_cheb (int i) {
30 if (i==0)
31 return M_PI ;
32 else
33 return M_PI/2 ;
34 }
35
36 // Colocation point
37 Tbl coloc_cheb (int n) {
38 Tbl res (n) ;
39 res.set_etat_qcq() ;
40 for (int i=0 ; i<n ; i++)
41 res.set(i) = -cos(M_PI*i/(n-1)) ;
42 return res ;
43 }
44
45 // Weight functions
46 Tbl weight_cheb (int n) {
47 Tbl res (n) ;
48 res.set_etat_qcq() ;
49 for (int i=0 ; i<n ; i++)
50 res.set(i) = ((i==0) || (i==n-1)) ? M_PI/2./n : M_PI/n ;
51 return res ;
52 }
53
54 // Normalisation factor :
55 Tbl gamma_cheb (int n) {
56 Tbl res (n) ;
57 res.annule_hard() ;
58 for (int i=0 ; i<n ; i++)
59 res.set(i) = M_PI/2*(n-1)/n ;
60 res.set(0) *= 2 ;
61 res.set(n-1) *= 2 ;
62 return res ;
63 }
64
65 // Coefficients :
66 Tbl coef_cheb (const Tbl& so) {
67
68 // Verification of size :
69 assert (so.get_ndim() == 1) ;
70 int n = so.get_dim(0) ;
71
72 // Auxiliary quantities :
73 Tbl colocation (coloc_cheb(n)) ;
74 Tbl poids (weight_cheb(n)) ;
75 Tbl norme (gamma_cheb(n)) ;
76
77 // The result is set to zero
78 Tbl res (n) ;
79 res.annule_hard() ;
80
81 for (int i=0 ; i<n ; i++)
82 for (int j=0 ; j<n ; j++)
83 res.set(i) += so(j)*cheby(i, colocation(j))*poids(j)/norme(i) ;
84 return res ;
85 }