[3839] | 1 | /*************************************************************************
|
---|
| 2 | >>> SOURCE LICENSE >>>
|
---|
| 3 | This program is free software; you can redistribute it and/or modify
|
---|
| 4 | it under the terms of the GNU General Public License as published by
|
---|
| 5 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
| 6 | License, or (at your option) any later version.
|
---|
| 7 |
|
---|
| 8 | This program is distributed in the hope that it will be useful,
|
---|
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 11 | GNU General Public License for more details.
|
---|
| 12 |
|
---|
| 13 | A copy of the GNU General Public License is available at
|
---|
| 14 | http://www.fsf.org/licensing/licenses
|
---|
| 15 |
|
---|
| 16 | >>> END OF LICENSE >>>
|
---|
| 17 | *************************************************************************/
|
---|
| 18 |
|
---|
| 19 | using System;
|
---|
| 20 |
|
---|
| 21 | namespace alglib
|
---|
| 22 | {
|
---|
| 23 | public class chebyshev
|
---|
| 24 | {
|
---|
| 25 | /*************************************************************************
|
---|
| 26 | Calculation of the value of the Chebyshev polynomials of the
|
---|
| 27 | first and second kinds.
|
---|
| 28 |
|
---|
| 29 | Parameters:
|
---|
| 30 | r - polynomial kind, either 1 or 2.
|
---|
| 31 | n - degree, n>=0
|
---|
| 32 | x - argument, -1 <= x <= 1
|
---|
| 33 |
|
---|
| 34 | Result:
|
---|
| 35 | the value of the Chebyshev polynomial at x
|
---|
| 36 | *************************************************************************/
|
---|
| 37 | public static double chebyshevcalculate(int r,
|
---|
| 38 | int n,
|
---|
| 39 | double x)
|
---|
| 40 | {
|
---|
| 41 | double result = 0;
|
---|
| 42 | int i = 0;
|
---|
| 43 | double a = 0;
|
---|
| 44 | double b = 0;
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | //
|
---|
| 48 | // Prepare A and B
|
---|
| 49 | //
|
---|
| 50 | if( r==1 )
|
---|
| 51 | {
|
---|
| 52 | a = 1;
|
---|
| 53 | b = x;
|
---|
| 54 | }
|
---|
| 55 | else
|
---|
| 56 | {
|
---|
| 57 | a = 1;
|
---|
| 58 | b = 2*x;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | //
|
---|
| 62 | // Special cases: N=0 or N=1
|
---|
| 63 | //
|
---|
| 64 | if( n==0 )
|
---|
| 65 | {
|
---|
| 66 | result = a;
|
---|
| 67 | return result;
|
---|
| 68 | }
|
---|
| 69 | if( n==1 )
|
---|
| 70 | {
|
---|
| 71 | result = b;
|
---|
| 72 | return result;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //
|
---|
| 76 | // General case: N>=2
|
---|
| 77 | //
|
---|
| 78 | for(i=2; i<=n; i++)
|
---|
| 79 | {
|
---|
| 80 | result = 2*x*b-a;
|
---|
| 81 | a = b;
|
---|
| 82 | b = result;
|
---|
| 83 | }
|
---|
| 84 | return result;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | /*************************************************************************
|
---|
| 89 | Summation of Chebyshev polynomials using Clenshaws recurrence formula.
|
---|
| 90 |
|
---|
| 91 | This routine calculates
|
---|
| 92 | c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
|
---|
| 93 | or
|
---|
| 94 | c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
|
---|
| 95 | depending on the R.
|
---|
| 96 |
|
---|
| 97 | Parameters:
|
---|
| 98 | r - polynomial kind, either 1 or 2.
|
---|
| 99 | n - degree, n>=0
|
---|
| 100 | x - argument
|
---|
| 101 |
|
---|
| 102 | Result:
|
---|
| 103 | the value of the Chebyshev polynomial at x
|
---|
| 104 | *************************************************************************/
|
---|
| 105 | public static double chebyshevsum(ref double[] c,
|
---|
| 106 | int r,
|
---|
| 107 | int n,
|
---|
| 108 | double x)
|
---|
| 109 | {
|
---|
| 110 | double result = 0;
|
---|
| 111 | double b1 = 0;
|
---|
| 112 | double b2 = 0;
|
---|
| 113 | int i = 0;
|
---|
| 114 |
|
---|
| 115 | b1 = 0;
|
---|
| 116 | b2 = 0;
|
---|
| 117 | for(i=n; i>=1; i--)
|
---|
| 118 | {
|
---|
| 119 | result = 2*x*b1-b2+c[i];
|
---|
| 120 | b2 = b1;
|
---|
| 121 | b1 = result;
|
---|
| 122 | }
|
---|
| 123 | if( r==1 )
|
---|
| 124 | {
|
---|
| 125 | result = -b2+x*b1+c[0];
|
---|
| 126 | }
|
---|
| 127 | else
|
---|
| 128 | {
|
---|
| 129 | result = -b2+2*x*b1+c[0];
|
---|
| 130 | }
|
---|
| 131 | return result;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /*************************************************************************
|
---|
| 136 | Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
|
---|
| 137 |
|
---|
| 138 | Input parameters:
|
---|
| 139 | N - polynomial degree, n>=0
|
---|
| 140 |
|
---|
| 141 | Output parameters:
|
---|
| 142 | C - coefficients
|
---|
| 143 | *************************************************************************/
|
---|
| 144 | public static void chebyshevcoefficients(int n,
|
---|
| 145 | ref double[] c)
|
---|
| 146 | {
|
---|
| 147 | int i = 0;
|
---|
| 148 |
|
---|
| 149 | c = new double[n+1];
|
---|
| 150 | for(i=0; i<=n; i++)
|
---|
| 151 | {
|
---|
| 152 | c[i] = 0;
|
---|
| 153 | }
|
---|
| 154 | if( n==0 | n==1 )
|
---|
| 155 | {
|
---|
| 156 | c[n] = 1;
|
---|
| 157 | }
|
---|
| 158 | else
|
---|
| 159 | {
|
---|
| 160 | c[n] = Math.Exp((n-1)*Math.Log(2));
|
---|
| 161 | for(i=0; i<=n/2-1; i++)
|
---|
| 162 | {
|
---|
| 163 | c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1)/(n-i-1));
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | /*************************************************************************
|
---|
| 170 | Conversion of a series of Chebyshev polynomials to a power series.
|
---|
| 171 |
|
---|
| 172 | Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
|
---|
| 173 | B[0] + B[1]*X + ... + B[N]*X^N.
|
---|
| 174 |
|
---|
| 175 | Input parameters:
|
---|
| 176 | A - Chebyshev series coefficients
|
---|
| 177 | N - degree, N>=0
|
---|
| 178 |
|
---|
| 179 | Output parameters
|
---|
| 180 | B - power series coefficients
|
---|
| 181 | *************************************************************************/
|
---|
| 182 | public static void fromchebyshev(ref double[] a,
|
---|
| 183 | int n,
|
---|
| 184 | ref double[] b)
|
---|
| 185 | {
|
---|
| 186 | int i = 0;
|
---|
| 187 | int k = 0;
|
---|
| 188 | double e = 0;
|
---|
| 189 | double d = 0;
|
---|
| 190 |
|
---|
| 191 | b = new double[n+1];
|
---|
| 192 | for(i=0; i<=n; i++)
|
---|
| 193 | {
|
---|
| 194 | b[i] = 0;
|
---|
| 195 | }
|
---|
| 196 | d = 0;
|
---|
| 197 | i = 0;
|
---|
| 198 | do
|
---|
| 199 | {
|
---|
| 200 | k = i;
|
---|
| 201 | do
|
---|
| 202 | {
|
---|
| 203 | e = b[k];
|
---|
| 204 | b[k] = 0;
|
---|
| 205 | if( i<=1 & k==i )
|
---|
| 206 | {
|
---|
| 207 | b[k] = 1;
|
---|
| 208 | }
|
---|
| 209 | else
|
---|
| 210 | {
|
---|
| 211 | if( i!=0 )
|
---|
| 212 | {
|
---|
| 213 | b[k] = 2*d;
|
---|
| 214 | }
|
---|
| 215 | if( k>i+1 )
|
---|
| 216 | {
|
---|
| 217 | b[k] = b[k]-b[k-2];
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | d = e;
|
---|
| 221 | k = k+1;
|
---|
| 222 | }
|
---|
| 223 | while( k<=n );
|
---|
| 224 | d = b[i];
|
---|
| 225 | e = 0;
|
---|
| 226 | k = i;
|
---|
| 227 | while( k<=n )
|
---|
| 228 | {
|
---|
| 229 | e = e+b[k]*a[k];
|
---|
| 230 | k = k+2;
|
---|
| 231 | }
|
---|
| 232 | b[i] = e;
|
---|
| 233 | i = i+1;
|
---|
| 234 | }
|
---|
| 235 | while( i<=n );
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|