def test_j_roots(): rf = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu) ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x) wf = lambda a, b: lambda x: (1 - x) ** a * (1 + x) ** b vgq = verify_gauss_quad vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 5) vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 25, atol=1e-12) vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1.0, 1.0, 100, atol=1e-11) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 5) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 25, atol=1.5e-13) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1.0, 1.0, 100, atol=1e-12) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 5, atol=2e-13) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 25, atol=2e-13) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1.0, 1.0, 100, atol=1e-12) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 5) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 25, atol=1e-13) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1.0, 1.0, 100, atol=2e-13) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 5) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 25) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1.0, 1.0, 100, atol=1e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 5, atol=1e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 25, atol=2e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1.0, 1.0, 100, atol=1e-11) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 5) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 25, atol=1e-13) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1.0, 1.0, 100, atol=1e-13) # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x) xj, wj = orth.j_roots(6, 0.0, 0.0) xl, wl = orth.p_roots(6) assert_allclose(xj, xl, 1e-14, 1e-14) assert_allclose(wj, wl, 1e-14, 1e-14) # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x) xj, wj = orth.j_roots(6, 4.0, 4.0) xc, wc = orth.cg_roots(6, 4.5) assert_allclose(xj, xc, 1e-14, 1e-14) assert_allclose(wj, wc, 1e-14, 1e-14) x, w = orth.j_roots(5, 2, 3, False) y, v, m = orth.j_roots(5, 2, 3, True) assert_allclose(x, y, 1e-14, 1e-14) assert_allclose(w, v, 1e-14, 1e-14) muI, muI_err = integrate.quad(wf(2, 3), -1, 1) assert_allclose(m, muI, rtol=muI_err) assert_raises(ValueError, orth.j_roots, 0, 1, 1) assert_raises(ValueError, orth.j_roots, 3.3, 1, 1) assert_raises(ValueError, orth.j_roots, 3, -2, 1) assert_raises(ValueError, orth.j_roots, 3, 1, -2) assert_raises(ValueError, orth.j_roots, 3, -2, -2)
def test_j_roots(): roots = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu) evalf = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x) verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 5) verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 25, atol=1e-12) verify_gauss_quad(roots(-0.5, -0.75), evalf(-0.5, -0.75), 100, atol=1e-11) verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 5) verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 25, atol=1e-13) verify_gauss_quad(roots(0.5, -0.5), evalf(0.5, -0.5), 100, atol=1e-12) verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 5, atol=2e-13) verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 25, atol=2e-13) verify_gauss_quad(roots(1, 0.5), evalf(1, 0.5), 100, atol=1e-12) verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 5) verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 25, atol=1e-13) verify_gauss_quad(roots(0.9, 2), evalf(0.9, 2), 100, atol=2e-13) verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 5) verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 25) verify_gauss_quad(roots(18.24, 27.3), evalf(18.24, 27.3), 100, atol=1e-13) verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 5, atol=1e-13) verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 25, atol=1e-13) verify_gauss_quad(roots(47.1, -0.2), evalf(47.1, -0.2), 100, atol=1e-11) verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 5) verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 25, atol=1e-13) verify_gauss_quad(roots(2.25, 68.9), evalf(2.25, 68.9), 100, atol=1e-13) # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x) xj, wj = orth.j_roots(6, 0.0, 0.0) xl, wl = orth.p_roots(6) assert_allclose(xj, xl, 1e-14, 1e-14) assert_allclose(wj, wl, 1e-14, 1e-14) # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x) xj, wj = orth.j_roots(6, 4.0, 4.0) xc, wc = orth.cg_roots(6, 4.5) assert_allclose(xj, xc, 1e-14, 1e-14) assert_allclose(wj, wc, 1e-14, 1e-14) x, w = orth.j_roots(5, 2, 3, False) y, v, m = orth.j_roots(5, 2, 3, True) assert_allclose(x, y, 1e-14, 1e-14) assert_allclose(w, v, 1e-14, 1e-14) assert_raises(ValueError, orth.j_roots, 0, 1, 1) assert_raises(ValueError, orth.j_roots, 3.3, 1, 1) assert_raises(ValueError, orth.j_roots, 3, -2, 1) assert_raises(ValueError, orth.j_roots, 3, 1, -2) assert_raises(ValueError, orth.j_roots, 3, -2, -2)
def setQuadrature(self,maxOrder,verbose=False): super(Beta,self).setQuadrature(maxOrder) pts,wts = quads.j_roots(self.quadOrd,self.alpha,self.beta) self.pts=self.convertToActual(pts) for w,wt in enumerate(wts): #TODO wts[w]=wt/(2.*self.hrange) self.wts=wts
def setQuadrature(self, maxOrder, verbose=False): super(Beta, self).setQuadrature(maxOrder) pts, wts = quads.j_roots(self.quadOrd, self.alpha, self.beta) self.pts = self.convertToActual(pts) for w, wt in enumerate(wts): #TODO wts[w] = wt / (2. * self.hrange) self.wts = wts
def j_roots(n, alpha, beta, method='newton'): """ Returns the roots of the nth order Jacobi polynomial, P^(alpha,beta)_n(x) and weights (w) to use in Gaussian Quadrature over [-1,1] with weighting function (1-x)**alpha (1+x)**beta with alpha,beta > -1. Parameters ---------- n : integer number of roots alpha,beta : scalars defining shape of Jacobi polynomial method : 'newton' or 'eigenvalue' uses Newton Raphson to find zeros of the Hermite polynomial (Fast) or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and weights, respectively. Returns ------- x : ndarray roots w : ndarray weights Examples -------- >>> [x,w]= j_roots(10,0,0) >>> sum(x*w) 2.7755575615628914e-16 See also -------- qrule, gaussq References ---------- [1] Golub, G. H. and Welsch, J. H. (1969) 'Calculation of Gaussian Quadrature Rules' Mathematics of Computation, vol 23,page 221-230, [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas', prentice-hall, Englewood cliffs, n.j. """ _assert((-1 < alpha) & (-1 < beta), 'alpha and beta must be greater than -1') if not method.startswith('n'): return ort.j_roots(n, alpha, beta) return _j_roots_newton(n, alpha, beta)
def j_roots(n, alpha, beta, method='newton'): ''' Returns the roots of the nth order Jacobi polynomial, P^(alpha,beta)_n(x) and weights (w) to use in Gaussian Quadrature over [-1,1] with weighting function (1-x)**alpha (1+x)**beta with alpha,beta > -1. Parameters ---------- n : integer number of roots alpha,beta : scalars defining shape of Jacobi polynomial method : 'newton' or 'eigenvalue' uses Newton Raphson to find zeros of the Hermite polynomial (Fast) or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and weights, respectively. Returns ------- x : ndarray roots w : ndarray weights Example -------- >>> [x,w]= j_roots(10,0,0) >>> sum(x*w) 2.7755575615628914e-16 See also -------- qrule, gaussq Reference --------- [1] Golub, G. H. and Welsch, J. H. (1969) 'Calculation of Gaussian Quadrature Rules' Mathematics of Computation, vol 23,page 221-230, [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas', prentice-hall, Englewood cliffs, n.j. ''' if not method.startswith('n'): return ort.j_roots(n, alpha, beta) return _j_roots_newton(n, alpha, beta)
def initQuad(self, orderquad): import scipy.special.orthogonal as op self.level2(orderquad) self.quadphi_over[:,0] = op.j_roots(self.orderquad,0,1)[0] self.quadphi_over[:,1] = op.j_roots(self.orderquad,0,1)[1] self.quadphi_under[:,0] = op.j_roots(self.orderquad,0,0)[0] self.quadphi_under[:,1] = op.j_roots(self.orderquad,0,0)[1] self.quadsingular[:,0] = op.j_roots(self.orderquad,2,-0.5)[0] self.quadsingular[:,1] = map(lambda y: y, op.j_roots(self.orderquad,2,-0.5)[1]/ map(lambda x: (1-x)**2*(1+x)**(-0.5), op.j_roots(self.orderquad,2,-0.5)[0]))
def initQuad(self, orderquad): import scipy.special.orthogonal as op self.level2(orderquad) self.quadphi_over[:, 0] = op.j_roots(self.orderquad, 0, 1)[0] self.quadphi_over[:, 1] = op.j_roots(self.orderquad, 0, 1)[1] self.quadphi_under[:, 0] = op.j_roots(self.orderquad, 0, 0)[0] self.quadphi_under[:, 1] = op.j_roots(self.orderquad, 0, 0)[1] self.quadsingular[:, 0] = op.j_roots(self.orderquad, 2, -0.5)[0] self.quadsingular[:, 1] = map( lambda y: y, op.j_roots(self.orderquad, 2, -0.5)[1] / map(lambda x: (1 - x)**2 * (1 + x)**(-0.5), op.j_roots(self.orderquad, 2, -0.5)[0]))
def test_j_roots(): rf = lambda a, b: lambda n, mu: orth.j_roots(n, a, b, mu) ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x) wf = lambda a, b: lambda x: (1 - x)**a * (1 + x)**b vgq = verify_gauss_quad vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 5) vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 25, atol=1e-12) vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 100, atol=1e-11) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 5) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 25, atol=1.5e-13) vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 100, atol=1e-12) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 5, atol=2e-13) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 25, atol=2e-13) vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 100, atol=1e-12) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 5) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 25, atol=1e-13) vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 100, atol=2e-13) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 5) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 25) vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 100, atol=1e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 5, atol=1e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 25, atol=2e-13) vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 100, atol=1e-11) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 5) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 25, atol=1e-13) vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 100, atol=1e-13) # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x) xj, wj = orth.j_roots(6, 0.0, 0.0) xl, wl = orth.p_roots(6) assert_allclose(xj, xl, 1e-14, 1e-14) assert_allclose(wj, wl, 1e-14, 1e-14) # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x) xj, wj = orth.j_roots(6, 4.0, 4.0) xc, wc = orth.cg_roots(6, 4.5) assert_allclose(xj, xc, 1e-14, 1e-14) assert_allclose(wj, wc, 1e-14, 1e-14) x, w = orth.j_roots(5, 2, 3, False) y, v, m = orth.j_roots(5, 2, 3, True) assert_allclose(x, y, 1e-14, 1e-14) assert_allclose(w, v, 1e-14, 1e-14) muI, muI_err = integrate.quad(wf(2, 3), -1, 1) assert_allclose(m, muI, rtol=muI_err) assert_raises(ValueError, orth.j_roots, 0, 1, 1) assert_raises(ValueError, orth.j_roots, 3.3, 1, 1) assert_raises(ValueError, orth.j_roots, 3, -2, 1) assert_raises(ValueError, orth.j_roots, 3, 1, -2) assert_raises(ValueError, orth.j_roots, 3, -2, -2)
def j_roots(n, alpha, beta, method='newton'): ''' Returns the roots (x) of the nth order Jacobi polynomial, P^(alpha,beta)_n(x) and weights (w) to use in Gaussian Quadrature over [-1,1] with weighting function (1-x)**alpha (1+x)**beta with alpha,beta > -1. Parameters ---------- n : integer number of roots alpha,beta : scalars defining shape of Jacobi polynomial method : 'newton' or 'eigenvalue' uses Newton Raphson to find zeros of the Hermite polynomial (Fast) or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and weights, respectively. Returns ------- x : ndarray roots w : ndarray weights Example -------- >>> [x,w]= j_roots(10,0,0) >>> sum(x*w) 2.7755575615628914e-16 See also -------- qrule, gaussq Reference --------- [1] Golub, G. H. and Welsch, J. H. (1969) 'Calculation of Gaussian Quadrature Rules' Mathematics of Computation, vol 23,page 221-230, [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas', prentice-hall, Englewood cliffs, n.j. ''' if not method.startswith('n'): [x, w] = ort.j_roots(n, alpha, beta) else: max_iter = 10 releps = 3e-14 # Initial approximations to the roots go into z. alfbet = alpha + beta z = np.cos(np.pi * (np.arange(1, n + 1) - 0.25 + 0.5 * alpha) / (n + 0.5 * (alfbet + 1))) L = zeros((3, len(z))) k0 = 0 kp1 = 1 for _its in xrange(max_iter): #Newton's method carried out simultaneously on the roots. tmp = 2 + alfbet L[k0, :] = 1 L[kp1, :] = (alpha - beta + tmp * z) / 2 for j in xrange(2, n + 1): #Loop up the recurrence relation to get the Jacobi #polynomials evaluated at z. km1 = k0 k0 = kp1 kp1 = np.mod(kp1 + 1, 3) a = 2. * j * (j + alfbet) * tmp tmp = tmp + 2 c = 2 * (j - 1 + alpha) * (j - 1 + beta) * tmp b = (tmp - 1) * (alpha ** 2 - beta ** 2 + tmp * (tmp - 2) * z) L[kp1, :] = (b * L[k0, :] - c * L[km1, :]) / a #L now contains the desired Jacobi polynomials. #We next compute pp, the derivatives with a standard # relation involving the polynomials of one lower order. pp = (n * (alpha - beta - tmp * z) * L[kp1, :] + 2 * (n + alpha) * (n + beta) * L[k0, :]) / (tmp * (1 - z ** 2)) dz = L[kp1, :] / pp z = z - dz # Newton's formula. if not any(abs(dz) > releps * abs(z)): break else: warnings.warn('too many iterations in jrule') x = z # %Store the root and the weight. w = np.exp(sp.gammaln(alpha + n) + sp.gammaln(beta + n) - sp.gammaln(n + 1) - sp.gammaln(alpha + beta + n + 1)) * tmp * 2 ** alfbet / (pp * L[k0, :]) return x, w
def j_roots(n, alpha, beta, method='newton'): ''' Returns the roots of the nth order Jacobi polynomial, P^(alpha,beta)_n(x) and weights (w) to use in Gaussian Quadrature over [-1,1] with weighting function (1-x)**alpha (1+x)**beta with alpha,beta > -1. Parameters ---------- n : integer number of roots alpha,beta : scalars defining shape of Jacobi polynomial method : 'newton' or 'eigenvalue' uses Newton Raphson to find zeros of the Hermite polynomial (Fast) or eigenvalue of the jacobi matrix (Slow) to obtain the nodes and weights, respectively. Returns ------- x : ndarray roots w : ndarray weights Example -------- >>> [x,w]= j_roots(10,0,0) >>> sum(x*w) 2.7755575615628914e-16 See also -------- qrule, gaussq Reference --------- [1] Golub, G. H. and Welsch, J. H. (1969) 'Calculation of Gaussian Quadrature Rules' Mathematics of Computation, vol 23,page 221-230, [2]. Stroud and Secrest (1966), 'gaussian quadrature formulas', prentice-hall, Englewood cliffs, n.j. ''' if not method.startswith('n'): [x, w] = ort.j_roots(n, alpha, beta) else: max_iter = 10 releps = 3e-14 # Initial approximations to the roots go into z. alfbet = alpha + beta z = np.cos(np.pi * (np.arange(1, n + 1) - 0.25 + 0.5 * alpha) / (n + 0.5 * (alfbet + 1))) L = zeros((3, len(z))) k0 = 0 kp1 = 1 for _its in xrange(max_iter): # Newton's method carried out simultaneously on the roots. tmp = 2 + alfbet L[k0, :] = 1 L[kp1, :] = (alpha - beta + tmp * z) / 2 for j in xrange(2, n + 1): # Loop up the recurrence relation to get the Jacobi # polynomials evaluated at z. km1 = k0 k0 = kp1 kp1 = np.mod(kp1 + 1, 3) a = 2. * j * (j + alfbet) * tmp tmp = tmp + 2 c = 2 * (j - 1 + alpha) * (j - 1 + beta) * tmp b = (tmp - 1) * (alpha ** 2 - beta ** 2 + tmp * (tmp - 2) * z) L[kp1, :] = (b * L[k0, :] - c * L[km1, :]) / a # L now contains the desired Jacobi polynomials. # We next compute pp, the derivatives with a standard # relation involving the polynomials of one lower order. pp = ((n * (alpha - beta - tmp * z) * L[kp1, :] + 2 * (n + alpha) * (n + beta) * L[k0, :]) / (tmp * (1 - z ** 2))) dz = L[kp1, :] / pp z = z - dz # Newton's formula. if not any(abs(dz) > releps * abs(z)): break else: warnings.warn('too many iterations in jrule') x = z # %Store the root and the weight. f = (sp.gammaln(alpha + n) + sp.gammaln(beta + n) - sp.gammaln(n + 1) - sp.gammaln(alpha + beta + n + 1)) w = (np.exp(f) * tmp * 2 ** alfbet / (pp * L[k0, :])) return x, w