From f60862267a551f469283b0465862baa31b0d9bea Mon Sep 17 00:00:00 2001 From: ludoro Date: Mon, 17 Dec 2018 22:02:33 +0100 Subject: [PATCH 1/2] First try at implementing evaluation of legendrePolynomial --- src/legendrePolynomial.jl | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/legendrePolynomial.jl diff --git a/src/legendrePolynomial.jl b/src/legendrePolynomial.jl new file mode 100644 index 00000000..a84b2b7a --- /dev/null +++ b/src/legendrePolynomial.jl @@ -0,0 +1,9 @@ +function legendrePolynomial(x,n) + #Evaluating the legendre polynomial of degree n in the point x. + sum = 0 + for k = 0:n + sum += (factorial(n)/(factorial(n-k)*factorial(k)))^2*(x-1)^(n-k)*(x+1)^k + end + evaluation = 1/(2^n)*sum + return evaluation +end From 88ead3e304f41536c00f19b2ed00c636cc582364 Mon Sep 17 00:00:00 2001 From: ludoro Date: Thu, 27 Dec 2018 13:46:09 +0100 Subject: [PATCH 2/2] Worked on recursion algorithm for legendre polynomial coefficients --- src/legendrePolynomial.jl | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/legendrePolynomial.jl b/src/legendrePolynomial.jl index a84b2b7a..97c3bac6 100644 --- a/src/legendrePolynomial.jl +++ b/src/legendrePolynomial.jl @@ -1,9 +1,23 @@ -function legendrePolynomial(x,n) - #Evaluating the legendre polynomial of degree n in the point x. - sum = 0 - for k = 0:n - sum += (factorial(n)/(factorial(n-k)*factorial(k)))^2*(x-1)^(n-k)*(x+1)^k +function legendrePolynomial(n) + #Storing the coefficients of the Legendre Polynomial up + # to n degree in a n+1 x n+1 matrix. We need to store all the coefficients + # because the loops are expression of the following recursive relation (Bonnet): + #(n+1)*P_{n+1}(x) = (2n+1)*x*P_{n}(x) - n*P_{n-1}(x) + c = zeros((n+1, n+1)) + c[1,1] = 1.0 + + if n <= 0 + return c end - evaluation = 1/(2^n)*sum - return evaluation + c[2,2] = 1.0 + + for i = 2:n + for j=0:i-1 + c[i+1,j+1] = (- i + 1) * c[i-1,j+1] / i + end + for j=1:i + c[i+1,j+1] = c[i+1,j+1] + (i+i - 1) * c[i,j] / i + end + end + return c end