Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ALGLIB-2.5.0/ALGLIB-2.5.0/legendre.cs @ 5229

Last change on this file since 5229 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.3 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
19
20namespace alglib {
21  public class legendre {
22    /*************************************************************************
23    Calculation of the value of the Legendre polynomial Pn.
24
25    Parameters:
26        n   -   degree, n>=0
27        x   -   argument
28
29    Result:
30        the value of the Legendre polynomial Pn at x
31    *************************************************************************/
32    public static double legendrecalculate(int n,
33        double x) {
34      double result = 0;
35      double a = 0;
36      double b = 0;
37      int i = 0;
38
39      result = 1;
40      a = 1;
41      b = x;
42      if (n == 0) {
43        result = a;
44        return result;
45      }
46      if (n == 1) {
47        result = b;
48        return result;
49      }
50      for (i = 2; i <= n; i++) {
51        result = ((2 * i - 1) * x * b - (i - 1) * a) / i;
52        a = b;
53        b = result;
54      }
55      return result;
56    }
57
58
59    /*************************************************************************
60    Summation of Legendre polynomials using Clenshaw’s recurrence formula.
61
62    This routine calculates
63        c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
64
65    Parameters:
66        n   -   degree, n>=0
67        x   -   argument
68
69    Result:
70        the value of the Legendre polynomial at x
71    *************************************************************************/
72    public static double legendresum(ref double[] c,
73        int n,
74        double x) {
75      double result = 0;
76      double b1 = 0;
77      double b2 = 0;
78      int i = 0;
79
80      b1 = 0;
81      b2 = 0;
82      for (i = n; i >= 0; i--) {
83        result = (2 * i + 1) * x * b1 / (i + 1) - (i + 1) * b2 / (i + 2) + c[i];
84        b2 = b1;
85        b1 = result;
86      }
87      return result;
88    }
89
90
91    /*************************************************************************
92    Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
93
94    Input parameters:
95        N   -   polynomial degree, n>=0
96
97    Output parameters:
98        C   -   coefficients
99    *************************************************************************/
100    public static void legendrecoefficients(int n,
101        ref double[] c) {
102      int i = 0;
103
104      c = new double[n + 1];
105      for (i = 0; i <= n; i++) {
106        c[i] = 0;
107      }
108      c[n] = 1;
109      for (i = 1; i <= n; i++) {
110        c[n] = c[n] * (n + i) / 2 / i;
111      }
112      for (i = 0; i <= n / 2 - 1; i++) {
113        c[n - 2 * (i + 1)] = -(c[n - 2 * i] * (n - 2 * i) * (n - 2 * i - 1) / 2 / (i + 1) / (2 * (n - i) - 1));
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.