Subroutine to compute Legendre polynomial coefficients using Bonnet's recursion formula.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=pfqp), | intent(out) | :: | p(0:n) | |||
integer, | intent(in), | value | :: | n |
subroutine poly_legendre(p, n)
integer, intent(in), value :: n
real(pfqp), intent(out) :: p(0:n)
real(pfqp), dimension(0:n) :: p0, p1, p2
integer :: j, m
if (n == 0) then
p = [ 1.0_pfqp ]
return
end if
if (n == 1) then
p = [ 0.0_pfqp, 1.0_pfqp ]
return
end if
p0 = 0.0_pfqp;
p1 = 0.0_pfqp;
p2 = 0.0_pfqp
p0(0) = 1.0_pfqp
p1(1) = 1.0_pfqp
! (n + 1) P_{n+1} = (2n + 1) x P_{n} - n P_{n-1}
do m = 1, n-1
do j = 1, n
p2(j) = ( (2*m + 1) * p1(j-1) - m * p0(j) ) / (m + 1)
end do
p2(0) = - m * p0(0) / (m + 1)
p0 = p1
p1 = p2
end do
p = p2
end subroutine poly_legendre