{|border=1 cellspacing=0 cellpadding=3
|In a general [[wp:Gaussian quadrature|Gaussian quadrature]] rule, an definite integral of <math>f(x)</math> is first approximated over the interval <math>[-1,1]</math> by a polynomial approximable function <math>g(x)</math> and a known weighting function <math>W(x)</math>.
|<math>\int_{-1}^1 f(x) \, dx = \int_{-1}^1 W(x) g(x) \, dx</math>
|-
|Those are then approximated by a sum of function values at specified points <math>x_i</math> multiplied by some weights <math>w_i</math>:
|<math>\int_{-1}^1 W(x) g(x) \, dx \approx \sum_{i=1}^n w_i g(x_i)</math>
|-
|In the case of Gauss-Legendre quadrature, the weighting function <math>W(x) = 1</math>, so we can approximate an integral of <math>f(x)</math> with:
|<math>\int_{-1}^1 f(x)\,dx \approx \sum_{i=1}^n w_i f(x_i)</math>
|}


For this, we first need to calculate the nodes and the weights, but after we have them, we can reuse them for numerious integral evaluations, which greatly speeds up the calculation compared to more [[Numerical Integration|simple numerical integration methods]].

{|border=1 cellspacing=0 cellpadding=3
|The <math>n</math> evaluation points <math>x_i</math> for a n-point rule, also called "nodes", are roots of n-th order [[wp:Legendre Polynomials|Legendre Polynomials]] <math>P_n(x)</math>. Legendre polynomials are defined by the following recursive rule:
|<math>P_0(x) = 1</math><br/>
<math>P_1(x) = x</math><br/>
<math>nP_{n}(x) = (2n-1)xP_{n-1}(x)-(n-1)P_{n-2}(x)</math>
|-
|There is also a recursive equation for their derivative:
|<math>P_{n}'(x) = \frac{n}{x^2-1} \left( x P_n(x) - P_{n-1}(x) \right)</math>
|-
|The roots of those polynomials are in general not analytically solvable, so they have to be approximated numerically, for example by [[wp:Newton's method|Newton-Raphson iteration]]:
|<math>x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}</math>
|-
|The first guess <math>x_0</math> for the <math>i</math>-th root of a <math>n</math>-order polynomial <math>P_n</math> can be given by
|<math>x_0 = \cos \left( \pi \, \frac{i - \frac{1}{4}}{n+\frac{1}{2}} \right)</math>
|-
|After we get the nodes <math>x_i</math>, we compute the appropriate weights by:
|<math>w_i = \frac{2}{\left( 1-x_i^2 \right) [P'_n(x_i)]^2}</math>
|-
|After we have the nodes and the weights for a n-point quadrature rule, we can approximate an integral over any interval <math>[a,b]</math> by
|<math>\int_a^b f(x)\,dx \approx \frac{b-a}{2} \sum_{i=1}^n w_i f\left(\frac{b-a}{2}x_i + \frac{a+b}{2}\right)</math>
|}


'''Task description'''

Similar to the task [[Numerical Integration]], the task here is to calculate the definite integral of a function <math>f(x)</math>, but by applying an n-point Gauss-Legendre quadrature rule, as described [[wp:Gaussian Quadrature|here]], for example. The input values should be an function f to integrate, the bounds of the integration interval a and b, and the number of gaussian evaluation points n. An reference implementation in Common Lisp is provided for comparison.

To demonstrate the calculation, compute the weights and nodes for an 5-point quadrature rule and then use them to compute:
          <big><big><math>\int_{-3}^{3} \exp(x) \, dx \approx \sum_{i=1}^5 w_i \; \exp(x_i) \approx 20.036</math></big></big>
<br><br>

