Blame view

stim/math/legendre.h 836 Bytes
f1402849   dmayerich   renewed commit
1
2
3
  #ifndef RTS_LEGENDRE_H
  #define RTS_LEGENDRE_H
  
0288346a   David Mayerich   added support for...
4
  #include "../cuda/cudatools/callable.h"
f1402849   dmayerich   renewed commit
5
  
8a86bd56   David Mayerich   changed rts names...
6
  namespace stim{
f1402849   dmayerich   renewed commit
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  
  template <typename T>
  CUDA_CALLABLE void init_legendre(T x, T& P0, T& P1)
  {
  	//compute the first two Legendre polynomials
  	P0 = 1;
  	P1 = x;
  }
  
  template <typename T>
  CUDA_CALLABLE void shift_legendre(int n, T x, T& P0, T& P1)
  {
  	//compute the next (order n) Legendre polynomial
  	T Pnew = ( (2 * n - 1) * x * P1 - (n-1) * P0 ) / n;
  
  	//shift and add the new value to the array
  	P0 = P1;
  	P1 = Pnew;
  }
  
0288346a   David Mayerich   added support for...
27
  /// Iteratively evaluates the Legendre polynomials for orders l = [0 n]
f1402849   dmayerich   renewed commit
28
29
30
  template <typename T>
  CUDA_CALLABLE void legendre(int n, T x, T* P)
  {
0288346a   David Mayerich   added support for...
31
  	if(n < 0) return;
f1402849   dmayerich   renewed commit
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      P[0] = 1;
  
      if(n >= 1)
          P[1] = x;
  
      for(int i=2; i<=n; i++)
      {
          P[i] = ( (2 * i - 1) * x * P[i-1] - (i-1) * P[i-2] ) / i;
      }
  
  }
  
  }
  
  
  #endif