10月 02

Legendre 多項式...

 P_{n}(x) = \left\{ \begin{array}{c l} \frac{1}{2^{n}n!}\left(\frac{d}{dx}\right)(x^{2}-1)^{n} & (n \geq 1) \\ 1 & (n = 0) \\ \end{array} \right.

これを計算機で計算させるために...

 P_{0}(x) = 1,
 P_{1}(x) = x,
 P_{n}(x) = \frac{2n-1}{n} x P_{n-1}(x) - \frac{n-1}{n}P_{n-2}(x) (n \geq 2)

このような漸化式を用いて次のような感じで実装.

// Legendre Function
double legendreFunction(int n, double x){
  if(n==0)
    return 1;
  else if(n==1)
    return x;
  else if(n>1){
    return (
	    (2*n-1)/(double)n*x*legendreFunction(n-1,x)
	    - (n-1)/(double)n*legendreFunction(n-2,x)
	    );
  }
  else
    return 0;
}

簡単に使えることを優先して再帰的な処理にしているので,次数の増加に伴い計算量が爆発的に増大します.計算時間がネックになる場合は,バッファを用意したり GPGPU などの並列処理計算を考えた方がよいと思います.そちらも少し考えようと思います.
 

One Response to “Legendre 多項式 の関数をナイーブに実装”

  1. [...] Legendre 多項式 の関数をナイーブに実装 [...]

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
preload preload preload