-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadrature.py
43 lines (39 loc) · 1.58 KB
/
quadrature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import itertools
# Generate quadrature points and weights with Legendre-Gauss('LL') or Chebyshev-Gauss('CC')
def getQuad(quad='LL', npoints=3, ndim=2):
if (quad=='LL'):
q, w = np.polynomial.legendre.leggauss(npoints)
elif (quad=='CC'):
q, wtemp = np.polynomial.chebyshev.chebgauss(npoints)
w = wtemp * np.sqrt(1 - (q[:])**2) ## Since weight function has f(x) = 1/sqrt{1 - x^2}
else:
print("No points and weights gerenated since quadrature not specified!")
return np.array([]), np.array([])
points = np.array(list(itertools.product(q, repeat=ndim)))
weights = np.prod(np.array(list(itertools.product(w, repeat=ndim))), axis=1)
return points, weights
# Transformation
def transform(points, weights, limits):
if (points.shape[0] > 0):
transfromPoints = np.zeros(points.shape)
transfromWeights = weights
for counter, limit in enumerate(limits):
transfromPoints[:,counter] = (limit[1] - limit[0])/2 * points[:,counter] + (limit[0] + limit[1])/2
transfromWeights = transfromWeights * (limit[1] - limit[0])/2
return transfromPoints, transfromWeights
else:
print("No transformed points and weights gerenated!")
return np.array([]), np.array([])
# Integrate
def solveQuad(fn, limits, quad, npoints):
ndim = len(limits)
if (ndim > 0):
points, weights = getQuad(quad, npoints, ndim)
q, w = transform(points, weights, limits)
if (q.shape[0] > 0):
return np.sum(fn(q) * w[:])
else:
print("No solution gerenated!")
else:
print("No solution gerenated since limits and dimensions are less than 1!")