def __init__(self, polygon, center=0, n_node=8, method='krylov'): """ polygon = target polygon of SC transformation center = point in polygon where disk center is mapped to n_node = number of nodes for gaussian quadrature method = used for root finding """ p = polygon.copy() p.roll() w = p.vertex beta = p.angle n = len(w) k_rat = p.FiniteEdge([0, n - 1], n - 3) k_fix = p.InfVertex() if len(k_fix) == 0: k_fix = [1] node = np.empty([n + 1, n_node]) weight = np.empty_like(node) for k in range(n): if np.isinf(w[k]): continue (node[k], weight[k]) = roots_jacobi(n_node, 0, -beta[k]) (node[n], weight[n]) = roots_legendre(n_node) self.prevertex = np.empty(n, dtype=np.complex) self.vertex = w self.angle = beta self.node = node self.weight = weight self.A = center self.map = np.vectorize(self.map) self.invmap = np.vectorize(self.invmap) y = np.zeros(n - 1) f = np.empty_like(y) def scfun(y): z = self.yztran(y) C = (center - w[-1]) / self.zquad(1, 0, n - 1) i = 0 for k in k_fix: q = w[k - 1] - center + C * self.zquad(z[k - 1], 0, k - 1) f[i] = np.real(q) f[i + 1] = np.imag(q) i += 2 for k in k_rat: q = self.zquad(z[k], z[k + 1], k, k + 1) f[i] = np.abs(w[k + 1] - w[k]) - np.abs(C * q) i += 1 self.C = C return f sol = root(scfun, y, method=method, options={'disp': True}) self.yztran(sol.x)
def zernike_quad(nmax, mmax): ''' Construct a list of quadrature points and associated weights. These must be adapted to compute scalar products involved in the analysis (Zernike transform), hence must have >= 2mmax+1 radial branches, and >= nmax+1 radial points (which allows integration of polynomials up to degree 2nmax+1). Note that the radial measure is proportional to radius for Zernike polynomials orthogonality, hence the shifted Jacobi polynomial P_{nmax+1}^{0,1}(2r-1) is chosen for the radial quadrature. ''' # First define radial points and weights from scipy.special import roots_jacobi xx, w8 = roots_jacobi(nmax + 1, 0, 1) # xx in [-1,1] r = (xx + 1.) / 2. theta = np.arange(2 * mmax + 1) / (2. * mmax + 1.) * 2. * np.pi ctheta = np.cos(theta) stheta = np.sin(theta) X = np.outer(r, ctheta).flatten() Y = np.outer(r, stheta).flatten() # Normalize weights: factor of 4 for shifted polynomial variable change, factor of (2*mmax+1)/2. for angular integration w8 /= 2. * (2 * mmax + 1.) W = np.outer(w8, np.ones_like(ctheta)).flatten() return X, Y, W
def jacobi_quad(nmax): # Here 2 = d-1 = 3-1 in 3D xx, ww = ss.roots_jacobi(nmax + 1, 0, 2) t = (xx + 1.) / 2. return (t, ww)
def get_GLquadrature(k, a=0., b=1., quad='Gauss-Legendre'): """Get the weights and points of the k-th order Gauss-Legendre quadrature. Return the points shifted in (a, b).""" bmao2 = .5 * (b - a) if quad == 'Gauss-Legendre': # Gauss-Legendre (default interval is [-1, 1]) x, w = np.polynomial.legendre.leggauss(k) elif quad == 'Gauss-Jacobi': # Gauss-Jacobi, overcoming the singularity of Ki3 at 0 (tangent) # warning: weights and points differ from Hebert's book! # the first parameters used by Hebert is unknown x, w = roots_jacobi(k, 1, 0) elif quad == 'Rectangular': x, w = np.linspace(-1., 1., k + 1), np.full(k, 2. / k) x = (x[1:] + x[:-1]) / 2. else: raise ValueError("Unknown type of numerical quadrature.") # Translate x values from the interval [-1, 1] to [a, b] ## return (x + 1) * bmao2 + a, w * bmao2 c = 1 - x if quad == 'Gauss-Jacobi': c *= .5 * (1 - x) return b - c * bmao2, w * bmao2
def __init__(self, polygon, n_node=8, method='krylov'): """ polygon = target polygon of SC transformation n_node = number of nodes for gaussian quadrature method = used for root finding """ if np.any(np.isinf(polygon.vertex)): print('infinite vertex is not allowed'); exit() p = polygon.copy() p.flip() p.roll(1) w = p.vertex b = p.angle b[b==-1] = 1 n = len(w) node = np.empty([n+1, n_node]) weight = np.empty_like(node) for k in range(n): if np.isfinite(w[k]): (node[k], weight[k]) = roots_jacobi(n_node, 0, b[k]) (node[n], weight[n]) = roots_legendre(n_node) self.prevertex = np.empty(n, dtype=np.complex) self.vertex = w self.angle = b self.node = node self.weight = weight self.map = np.vectorize(self.map) y = np.zeros(n-1) f = np.empty_like(y) def scfun(y): z = self.yztran(y) C = (w[-1] - w[0])/self.zquad(z[0], z[-1], 0, n-1) for k in range(n-3): q = self.zquad(z[k], z[k+1], k, k+1) f[k] = np.abs(w[k+1] - w[k]) - np.abs(C*q) r = np.sum(b/z) f[n-3] = np.real(r) f[n-2] = np.imag(r) self.C = C return f sol = root(scfun, y, method=method, options={'disp': True}) self.yztran(sol.x)
def __init__(self, z, n_node=8, method='krylov'): """ z = vertices as complex numbers on boundary n_node = number of nodes for gaussian quadrature method = used for root finding """ if len(z)<2: print('there must be 2 or more vertices'); exit() if len(z)==2: z.insert(1, (z[0]+z[1])/2) n = len(z) - 1 beta = np.empty(n+1) node = np.empty([n+1, n_node]) weight = np.empty_like(node) (node[n], weight[n]) = roots_legendre(n_node) (node[0], weight[0]) = (node[n], weight[n]) gamma = np.angle(z[1] - z[0])/np.pi for k in range(1,n): beta[k] = np.angle(z[k+1] - z[k])/np.pi - gamma beta[k] = (beta[k] + 1)%2 - 1 if beta[k]-1 > 1.e-8: beta[k] = -1 gamma += beta[k] (node[k], weight[k]) = roots_jacobi(n_node, 0, -beta[k]) self.prevertex = np.empty(n+1, dtype=np.complex) self.prevertex[0] = -1 self.vertex = np.array(z, dtype=np.complex) self.angle = beta self.node = node self.weight = weight self.reflect = False self.map = np.vectorize(self.map) y = np.zeros(n-1) f = np.empty_like(y) def kfun(y): x = self.yxtran(y) C = (z[1] - z[0])/self.xquad(x[0], x[1], 0, 1) for k in range(1,n): q = self.xquad(x[k], x[k+1], k, k+1) f[k-1] = np.abs(z[k+1] - z[k]) - np.abs(C*q) self.C = C return f sol = root(kfun, y, method=method, options={'disp': True}) self.yxtran(sol.x)
def points_and_weights(self, N=None, map_true_domain=False, weighted=True, **kw): if N is None: N = self.N assert self.quad == "JG" points, weights = roots_jacobi(N, 0, 0) if map_true_domain is True: points = self.map_true_domain(points) return points, weights
def jacobz(n, alpha, beta): """ compute zeros jacobi polynomial P_n^{alpha,beta} :param n: order of jacobi polynomial, integer :param alpha: parameter, alpha > -1 :param beta: parameter, beta > -1 :return: zeros of jacobi polynomials """ if n <= 0: return None z, _ = roots_jacobi(n, alpha, beta) return z
def points_and_weights(self, N=None, map_true_domain=False, weighted=True, **kw): if N is None: N = self.shape(False) assert self.quad == "JG" points, weights = roots_jacobi(N, self.alpha + 1, self.beta + 1) if map_true_domain is True: points = self.map_true_domain(points) return points, weights
def zwgj(n, alpha, beta): """ compute Gauss-Jacobi quadrature points and weights associated with polynomial P_n^{alpha,beta} :param n: order of Jacobi polynomial :param alpha: parameter, alpha > -1 :param beta: parameter, beta > -1 :return: tuple (z,w), where z contains quadrature points, w contains weights """ if n <= 0: return None, None else: return roots_jacobi(n, alpha, beta)
def gauss_jacobi(n, alpha, beta, lower=-1, upper=1): ''' Gauss-Jacobi quadrature: A rule of order 2*n-1 on the interval [-1, 1] with respect to the weight function w(x) = (1-x)**alpha*(1+x)**beta. ''' nodes, weights = special.roots_jacobi(n, alpha, beta) if lower != -1 or upper != 1: nodes = (upper+lower)/2 + (upper-lower)/2*nodes weights = (upper-lower)/2*weights return nodes, weights
def cumulative_sfs(k1: int, k2: int, n: int, c: float, t: float, theta: float, m_max: int, order: int = None) -> float: """Compute the CDF of the expected joint site frequency spectrum. Parameters ---------- k1 : int the allele count in population 1 k2 : int the allele count in population 2 n : int the sample size in each population c : float the admixture proportion in the present t : float the population-scaled split time theta : float the population-scaled mutation rate m_max : int the largest basis function to compute order : int, optional the quadrature order (default=2n) Returns ------- float """ m = np.arange(m_max + 1) lambdas = jacobi_eigenval(m, theta) norms = jacobi_norm(m, theta) # The jacobi_norm(0, theta) scales the density to integrate to 1 weights = np.exp(-2 * lambdas * t) / norms / jacobi_norm(0, theta) if order is None: order = 2 * n x, w = roots_jacobi(order, theta - 1, theta - 1) x = (x + 1) / 2 w /= 2**(2 * theta - 1) W = w * w[:, np.newaxis] evals = __integrand(x, x[:, np.newaxis], k1, k2, n, c, t, theta, m, weights) return np.sum(W * evals)
def gll(n): """Compute the points and weights of the Gouss-Lenendre-Lobbato quadrature Parameters ---------- n, int The number of points. """ # Special case if n == 2: return np.array([-1, 1]), np.array([1, 1]) x, w = roots_jacobi(n - 2, 1, 1) for i in range(x.size): w[i] /= 1 - x[i]**2 x = np.append(-1, np.append(x, 1)) w = np.append(2 / (n * (n - 1)), np.append(w, 2 / (n * (n - 1)))) return x, w
def gauss_lobatto_jacobi_weights(n, a, b): X = roots_jacobi(n - 2, a + 1, b + 1)[0] Wl = (b + 1) * 2**(a + b + 1) * gamma(a + n) * gamma(b + n) / ( (n - 1) * gamma(n) * gamma(a + b + n + 1) * (jacobi_polynomial(n - 1, a, b, -1)**2)) W = 2**(a + b + 1) * gamma(a + n) * gamma(b + n) / ( (n - 1) * gamma(n) * gamma(a + b + n + 1) * (jacobi_polynomial(n - 1, a, b, X)**2)) Wr = (a + 1) * 2**(a + b + 1) * gamma(a + n) * gamma(b + n) / ( (n - 1) * gamma(n) * gamma(a + b + n + 1) * (jacobi_polynomial(n - 1, a, b, 1)**2)) W = np.append(W, Wr) W = np.append(Wl, W) X = np.append(-1, X) X = np.append(X, 1) return [X, W]
def GaussLobattoJacobiWeights(Q: int, a, b): W = [] X = roots_jacobi(Q - 2, a + 1, b + 1)[0] if a == 0 and b == 0: W = 2 / ((Q - 1) * (Q) * (Jacobi(Q - 1, 0, 0, X)**2)) Wl = 2 / ((Q - 1) * (Q) * (Jacobi(Q - 1, 0, 0, -1)**2)) Wr = 2 / ((Q - 1) * (Q) * (Jacobi(Q - 1, 0, 0, 1)**2)) else: W = 2**(a + b + 1) * gamma(a + Q) * gamma(b + Q) / ( (Q - 1) * gamma(Q) * gamma(a + b + Q + 1) * (Jacobi(Q - 1, a, b, X)**2)) Wl = (b + 1) * 2**(a + b + 1) * gamma(a + Q) * gamma(b + Q) / ( (Q - 1) * gamma(Q) * gamma(a + b + Q + 1) * (Jacobi(Q - 1, a, b, -1)**2)) Wr = (a + 1) * 2**(a + b + 1) * gamma(a + Q) * gamma(b + Q) / ( (Q - 1) * gamma(Q) * gamma(a + b + Q + 1) * (Jacobi(Q - 1, a, b, 1)**2)) W = np.append(W, Wr) W = np.append(Wl, W) X = np.append(X, 1) X = np.append(-1, X) return [X, W]
def test_roots_jacobi(): rf = lambda a, b: lambda n, mu: sc.roots_jacobi(n, a, b, mu) ef = lambda a, b: lambda n, x: sc.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=2e-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=3e-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, atol=1.1e-14) 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 = sc.roots_jacobi(6, 0.0, 0.0) xl, wl = sc.roots_legendre(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 = sc.roots_jacobi(6, 4.0, 4.0) xc, wc = sc.roots_gegenbauer(6, 4.5) assert_allclose(xj, xc, 1e-14, 1e-14) assert_allclose(wj, wc, 1e-14, 1e-14) x, w = sc.roots_jacobi(5, 2, 3, False) y, v, m = sc.roots_jacobi(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, sc.roots_jacobi, 0, 1, 1) assert_raises(ValueError, sc.roots_jacobi, 3.3, 1, 1) assert_raises(ValueError, sc.roots_jacobi, 3, -2, 1) assert_raises(ValueError, sc.roots_jacobi, 3, 1, -2) assert_raises(ValueError, sc.roots_jacobi, 3, -2, -2)
def JacobiGQ(alpha, beta, N): x, w = sci.roots_jacobi(N, alpha, beta) return x, w
import numpy as np from numpy import sqrt from numpy.testing import assert_allclose import scipy.special as sc import scipy.special.orthogonal as orth rf = lambda a, b: lambda n, mu: sc.roots_jacobi(n, a, b, mu) ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x) rtol = 1e-15 atol = 1e-14 N = 25 root_func = rf(18.24, 27.3) eval_func = ef(18.24, 27.3) x, w, mu = root_func(N, True) # test orthogonality. Note that the results need to be normalized, # otherwise the huge values that can arise from fast growing # functions like Laguerre can be very confusing. n = np.arange(N) v = eval_func(n[:, np.newaxis], x) vv = np.dot(v * w, v.T) vd = 1 / np.sqrt(vv.diagonal()) vv = vd[:, np.newaxis] * vv * vd assert_allclose(vv, np.eye(N), rtol, atol)
def __init__(self, z, alpha, n_node=8, method='krylov'): """ z = vertices as complex numbers on boundary alpha = interior angles between edges n_node = number of nodes for gaussian quadrature method = used for root finding """ if len(z) < 3: print('there must be 3 or more vertices') exit() if z.count(np.inf) != 1: print('there must be one Infinity') exit() if np.isinf(z[0]) or np.isinf(z[-1]): print('z[0], and z[-1] must be finite') exit() if np.isinf(z[1]): z.insert(1, z[0] + np.exp(-np.pi * alpha[0] * 1j)) alpha[0] = 1 n = len(z) - 1 L = z.index(np.inf) beta = np.empty(n + 1) gamma = np.angle(z[1] - z[0]) / np.pi for k in range(1, n): if k == L or k + 1 == L: beta[k] = 1 - alpha[k + 1 - L] else: beta[k] = np.angle(z[k + 1] - z[k]) / np.pi - gamma beta[k] = (beta[k] + 1) % 2 - 1 if beta[k] - 1 > 1.e-8: beta[k] = -1 gamma += beta[k] beta[L] -= 1 node = np.empty([n + 1, n_node]) weight = np.empty_like(node) for k in range(1, n): if k == L: continue (node[k], weight[k]) = roots_jacobi(n_node, 0, -beta[k]) (node[0], weight[0]) = roots_jacobi(n_node, 0, 1) (node[n], weight[n]) = roots_legendre(n_node) self.prevertex = np.empty(n + 1, dtype=np.complex) self.prevertex[0] = -1 self.vertex = np.array(z, dtype=np.complex) self.angle = beta self.node = node self.weight = weight self.L = L self.map = np.vectorize(self.map) y = np.zeros(n - 1) f = np.empty_like(y) def jfun(y): s = self.ystran(y) C = (z[1] - z[0]) / self.squad(s[0], s[1], 0, 1) q = self.squad(s[0], 0.5j, 0) - self.squad(s[n], 0.5j, n) q = z[n] - z[0] - C * q f[L - 2] = np.real(q) f[L - 1] = np.imag(q) for k in range(1, n): if k == L or k + 1 == L: continue q = self.squad(s[k], s[k + 1], k, k + 1) f[k - 1] = np.abs(z[k + 1] - z[k]) - np.abs(C * q) self.C = C return f sol = root(jfun, y, method=method, options={'disp': True}) self.ystran(sol.x) s = np.real(self.prevertex[1:-1]) t = 1 / (1 + s[L - 1]**2) self.q = np.abs(self.C) * np.pi * t self.theta = np.angle(self.C)\ - np.dot(beta[1:-1], np.pi/2 + 2 * np.arctan(s)) t *= 2 * s[L - 1] self.w1 = self.q * (np.log((1 + t) / (1 - t)) / np.pi + 1j) self.sigma_L = t
def test_roots_jacobi(): rf = lambda a, b: lambda n, mu: sc.roots_jacobi(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 = sc.roots_jacobi(6, 0.0, 0.0) xl, wl = sc.roots_legendre(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 = sc.roots_jacobi(6, 4.0, 4.0) xc, wc = sc.roots_gegenbauer(6, 4.5) assert_allclose(xj, xc, 1e-14, 1e-14) assert_allclose(wj, wc, 1e-14, 1e-14) x, w = sc.roots_jacobi(5, 2, 3, False) y, v, m = sc.roots_jacobi(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, sc.roots_jacobi, 0, 1, 1) assert_raises(ValueError, sc.roots_jacobi, 3.3, 1, 1) assert_raises(ValueError, sc.roots_jacobi, 3, -2, 1) assert_raises(ValueError, sc.roots_jacobi, 3, 1, -2) assert_raises(ValueError, sc.roots_jacobi, 3, -2, -2)
def GaussJacobiWeights(Q: int, a, b): [X, W] = roots_jacobi(Q, a, b) return [X, W]
def __init__(self, polygon, n_node=8, method='krylov'): """ polygon = target polygon of SC transformation n_node = number of nodes for gaussian quadrature method = used for root finding """ p = polygon.copy() p.roll() w = p.vertex beta = p.angle n = len(w) node = np.empty([n + 1, n_node]) weight = np.empty_like(node) for k in range(n): if np.isinf(w[k]): continue (node[k], weight[k]) = roots_jacobi(n_node, 0, -beta[k]) (node[n], weight[n]) = roots_legendre(n_node) self.prevertex = np.empty(n, dtype=np.complex) self.prevertex[[0, -1, -2]] = [1, 0, np.inf] self.vertex = w self.angle = beta self.node = node self.weight = weight self.map = np.vectorize(self.map) self.invmap = np.vectorize(self.invmap) if n == 3: self.C = (w[0] - w[-1]) / self.zquad(0, 1, n - 1, 0) return k_rat = p.FiniteEdge([0, n - 3]) k_fix = p.InfVertex([1, n - 3]) y = np.zeros(n - 3) f = np.empty_like(y) def scfun(y): z = self.yztran(y) C = (w[0] - w[-1]) / self.zquad(0, 1, n - 1, 0) i = 0 for k in k_rat: q = self.zquad(z[k], z[k + 1], k, k + 1) f[i] = np.abs(w[k + 1] - w[k]) - np.abs(C * q) i += 1 for k in k_fix: zm = (z[k - 1] + z[k + 1] + (z[k + 1] - z[k - 1]) * 1j) / 2 q = self.zquad(z[k - 1], zm, k - 1) - self.zquad( z[k + 1], zm, k + 1) q = w[k + 1] - w[k - 1] - C * q f[i] = np.real(q) f[i + 1] = np.imag(q) i += 2 self.C = C return f sol = root(scfun, y, method=method, options={'disp': True}) self.yztran(sol.x)
#1.double the legendre n = 5 position_x, weight_x = np.polynomial.legendre.leggauss(n) position_y = position_x weight_y = weight_x sum = 0 for i in range(len(position_x)): for j in range(len(position_y)): sum += weight_x[i] * weight_y[j] * f(position_x[i], position_y[j]) print(sum) #2.half jacobi- half legendre - such as i get it position_y, weight_y = sp.roots_jacobi(n, 0, 0) sum = 0 for i in range(len(position_x)): for j in range(len(position_y)): sum += weight_x[i] * weight_y[j] * f(position_x[i], position_y[j]) * position_x[i] print(sum) #5. import sobol_seq def f(x, y): return math.exp(-(x * x + y * y)) answer = 0.25 * math.pi * sp.erf(1) ** 2 n = 2000 arr = sobol_seq.i4_sobol_generate(2, n)
def JacobiGQ(alpha, beta, N): """Compute N'th order Gauss quadrature points and weights""" x, w = sci.roots_jacobi(N, alpha, beta) return x, w