Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/ALGLIB/hermite.cs @ 2597

Last change on this file since 2597 was 2563, checked in by gkronber, 15 years ago

Updated ALGLIB to latest version. #751 (Plugin for for data-modeling with ANN (integrated into CEDMA))

File size: 3.9 KB
Line 
1/*************************************************************************
2>>> SOURCE LICENSE >>>
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation (www.fsf.org); either version 2 of the
6License, or (at your option) any later version.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11GNU General Public License for more details.
12
13A copy of the GNU General Public License is available at
14http://www.fsf.org/licensing/licenses
15
16>>> END OF LICENSE >>>
17*************************************************************************/
18
19using System;
20
21namespace alglib
22{
23    public class hermite
24    {
25        /*************************************************************************
26        Calculation of the value of the Hermite polynomial.
27
28        Parameters:
29            n   -   degree, n>=0
30            x   -   argument
31
32        Result:
33            the value of the Hermite polynomial Hn at x
34        *************************************************************************/
35        public static double hermitecalculate(int n,
36            double x)
37        {
38            double result = 0;
39            int i = 0;
40            double a = 0;
41            double b = 0;
42
43           
44            //
45            // Prepare A and B
46            //
47            a = 1;
48            b = 2*x;
49           
50            //
51            // Special cases: N=0 or N=1
52            //
53            if( n==0 )
54            {
55                result = a;
56                return result;
57            }
58            if( n==1 )
59            {
60                result = b;
61                return result;
62            }
63           
64            //
65            // General case: N>=2
66            //
67            for(i=2; i<=n; i++)
68            {
69                result = 2*x*b-2*(i-1)*a;
70                a = b;
71                b = result;
72            }
73            return result;
74        }
75
76
77        /*************************************************************************
78        Summation of Hermite polynomials using Clenshaw’s recurrence formula.
79
80        This routine calculates
81            c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
82
83        Parameters:
84            n   -   degree, n>=0
85            x   -   argument
86
87        Result:
88            the value of the Hermite polynomial at x
89        *************************************************************************/
90        public static double hermitesum(ref double[] c,
91            int n,
92            double x)
93        {
94            double result = 0;
95            double b1 = 0;
96            double b2 = 0;
97            int i = 0;
98
99            b1 = 0;
100            b2 = 0;
101            for(i=n; i>=0; i--)
102            {
103                result = 2*(x*b1-(i+1)*b2)+c[i];
104                b2 = b1;
105                b1 = result;
106            }
107            return result;
108        }
109
110
111        /*************************************************************************
112        Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
113
114        Input parameters:
115            N   -   polynomial degree, n>=0
116
117        Output parameters:
118            C   -   coefficients
119        *************************************************************************/
120        public static void hermitecoefficients(int n,
121            ref double[] c)
122        {
123            int i = 0;
124
125            c = new double[n+1];
126            for(i=0; i<=n; i++)
127            {
128                c[i] = 0;
129            }
130            c[n] = Math.Exp(n*Math.Log(2));
131            for(i=0; i<=n/2-1; i++)
132            {
133                c[n-2*(i+1)] = -(c[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1));
134            }
135        }
136    }
137}
Note: See TracBrowser for help on using the repository browser.