/// <summary> /// Calculates the associated legendre polynomial(m,l) with parameter x. /// </summary> /// <param name="l"></param> /// <param name="m"></param> /// <param name="x"></param> /// <returns></returns> public static double AssociatedPolynomial(int l, int m, double x) { Contract.Requires(-1 <= x && x <= 1); if (l < 0) { return(AssociatedPolynomial(-l - 1, m, x)); } if (m < 0) { int sign = Functions.IsOdd(m) ? 1 : -1; return(sign * Functions.GammaRatio(l + m + 1, l + 1 - m) * AssociatedPolynomial(l, -m, x)); } if (m > l) { return(0); } if (m == 0) { return(Polynomial(l, x)); } double p0 = Functions.Factorial(Functions.Factorial(2 * m - 1)); if (Functions.IsOdd(m)) { p0 *= -1; } if (m == l) { return(p0); } double p1 = x * (2 * m + 1) * p0; for (int n = m + 1; n < l; ++n) { var p2 = Next(n, m, x, p0, p1); p0 = p1; p1 = p2; } return(p1); }