Numerical integration/Gauss-Legendre Quadrature: Difference between revisions

→‎{{header|Python}}: added new code using numpy polynomial library
m (Added in C# version, suprised it wasnt already in as C# and Java are quite close.)
(→‎{{header|Python}}: added new code using numpy polynomial library)
Line 2,156:
Integral : 20.0355777184
</pre>
===With library routine===
One can also use the already invented wheel in NumPy:
<lang python>import numpy as np
 
# func is a function that takes a list-like input values
def gauss_legendre_integrate(func, domain, deg):
x, w = np.polynomial.legendre.leggauss(deg)
s = (domain[1] - domain[0])/2
a = (domain[1] + domain[0])/2
return np.sum(s*w*func(s*x + a))
 
for d in range(3, 10):
print(d, gauss_legendre_integrate(np.exp, [-3, 3], d))</lang>
{{out}}
<pre>3 19.853691996805587
4 20.028688395290693
5 20.035577718385575
6 20.035746975092323
7 20.03574981972664
8 20.035749854494522
9 20.03574985481744</pre>
 
=={{header|Racket}}==