From e5c87ea00fe1c6995f537cf5370d3fb92af7ed97 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Tue, 6 Nov 2018 15:33:05 -0600 Subject: [PATCH 1/4] unify getting quadrature order from all methods --- modepy/quadrature/__init__.py | 18 +++++++++++++---- modepy/quadrature/jacobi_gauss.py | 32 ++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/modepy/quadrature/__init__.py b/modepy/quadrature/__init__.py index b53cc34..1534a53 100644 --- a/modepy/quadrature/__init__.py +++ b/modepy/quadrature/__init__.py @@ -45,7 +45,12 @@ class Quadrature(object): .. attribute :: weights A vector of length *nnodes*. + + .. attribute :: exact_to + + Total polynomial degree up to which the quadrature is exact. """ + def __init__(self, nodes, weights): self.nodes = nodes self.weights = weights @@ -59,9 +64,13 @@ class Quadrature(object): """ return np.dot(self.weights, f(self.nodes)) + @property + def exact_to(self): + raise NotImplementedError("unknown quadrature order") + class Transformed1DQuadrature(Quadrature): - """A quadrature rule on an arbitrary interval :math:`(a,b)`. """ + """A quadrature rule on an arbitrary interval :math:`(a, b)`.""" def __init__(self, quad, left, right): """Transform a given 1D quadrature rule *quad* onto the @@ -71,8 +80,9 @@ class Transformed1DQuadrature(Quadrature): self.right = right length = right-left - assert length > 0 half_length = length / 2 + assert length > 0 + Quadrature.__init__(self, - left + (quad.nodes+1)/2*length, - quad.weights*half_length) + left + (quad.nodes + 1) / 2 * length, + quad.weights * half_length) diff --git a/modepy/quadrature/jacobi_gauss.py b/modepy/quadrature/jacobi_gauss.py index ab268e1..47577d9 100644 --- a/modepy/quadrature/jacobi_gauss.py +++ b/modepy/quadrature/jacobi_gauss.py @@ -31,25 +31,31 @@ from modepy.quadrature import Quadrature class JacobiGaussQuadrature(Quadrature): r"""An Gauss quadrature of order *N* associated with the - Jacobi weight :math:`(1-x)^\alpha(1+x)^\beta`. + Jacobi weight :math:`(1 - x)^\alpha (1 + x)^\beta`. - Assumes :math:`\alpha,\beta > -1` with - :math:`(\alpha,\beta)\not\in\{(-1/2,-1/2)\}`. + Assumes :math:`\alpha, \beta > -1` with + :math:`(\alpha, \beta) \not \in \{(-1/2, -1/2)\}`. - Integrates on the interval (-1,1). - The quadrature rule is exact up to degree :math:`2N+1`. + Integrates on the interval :math:`(-1, 1)`. + The quadrature rule is exact up to degree :math:`2N + 1`. Inherits from :class:`modepy.Quadrature`. See there for the interface to obtain nodes and weights. """ + def __init__(self, alpha, beta, N): # noqa x, w = self.compute_weights_and_nodes(N, alpha, beta) Quadrature.__init__(self, x, w) @staticmethod def compute_weights_and_nodes(N, alpha, beta): # noqa - """Return (nodes, weights) for an n-th order Gauss quadrature - with the Jacobi polynomials of type (alpha, beta). + """ + :arg N: order of the Gauss quadrature (the order of exactly + integrated polynomials is :math:`2 N + 1`). + :arg alpha: power of :math:`1 - x` in the Jacobi polynomial weight. + :arg beta: power of :math:`1 + x` in the Jacobi polynomial weight. + + :return: a tuple ``(nodes, weights)`` of quadrature notes and weights. """ # FIXME: This could stand to be upgraded to the Rokhlin algorithm. @@ -117,12 +123,16 @@ class JacobiGaussQuadrature(Quadrature): return nodes, weights + @property + def exact_to(self): + return 2 * self.nodes.size + 1 + class LegendreGaussQuadrature(JacobiGaussQuadrature): """An Gauss quadrature associated with weight 1. - Integrates on the interval (-1,1). - The quadrature rule is exact up to degree :math:`2N+1`. + Integrates on the interval :math:`(-1, 1)`. + The quadrature rule is exact up to degree :math:`2N + 1`. Inherits from :class:`modepy.Quadrature`. See there for the interface to obtain nodes and weights. @@ -137,7 +147,7 @@ def jacobi_gauss_lobatto_nodes(alpha, beta, N): # noqa nodes corresponding to the :class:`JacobiGaussQuadrature` with the same parameters. - Exact to degree :math:`2N-3`. + Exact to degree :math:`2N - 3`. """ x = np.zeros((N+1,)) @@ -156,6 +166,6 @@ def jacobi_gauss_lobatto_nodes(alpha, beta, N): # noqa def legendre_gauss_lobatto_nodes(N): # noqa """Compute the Legendre-Gauss-Lobatto quadrature nodes. - Exact to degree :math:`2N-1`. + Exact to degree :math:`2N - 1`. """ return jacobi_gauss_lobatto_nodes(0, 0, N) -- GitLab From 8c27be039fa37f6c170a049c17f5ead720850b9c Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Tue, 6 Nov 2018 21:13:40 -0600 Subject: [PATCH 2/4] random pep8 fixes --- modepy/matrices.py | 6 ++-- modepy/nodes.py | 2 +- modepy/quadrature/__init__.py | 4 +-- modepy/quadrature/grundmann_moeller.py | 28 ++++++++-------- modepy/quadrature/jacobi_gauss.py | 46 +++++++++----------------- modepy/quadrature/vr_quad_data_tet.py | 1 + modepy/quadrature/vr_quad_data_tri.py | 1 + modepy/tools.py | 27 ++++++++------- setup.cfg | 2 +- 9 files changed, 50 insertions(+), 67 deletions(-) diff --git a/modepy/matrices.py b/modepy/matrices.py index 5c0fbc4..14a7713 100644 --- a/modepy/matrices.py +++ b/modepy/matrices.py @@ -235,10 +235,8 @@ class _FaceMap(object): self.face_dim = vol_dim - 1 def __call__(self, points): - return ( - self.origin[:, np.newaxis] - + - np.einsum("ad,dn->an", self.span, points*0.5 + 0.5)) + return (self.origin[:, np.newaxis] + + np.einsum("ad,dn->an", self.span, points*0.5 + 0.5)) def modal_face_mass_matrix(trial_basis, order, face_vertices, test_basis=None): diff --git a/modepy/nodes.py b/modepy/nodes.py index 911640c..3ee11fe 100644 --- a/modepy/nodes.py +++ b/modepy/nodes.py @@ -146,7 +146,7 @@ def warp_and_blend_nodes_2d(n, node_tuples=None): # {{{ 3D nodes _alpha_opt_3d = [ - 0, 0, 0, 0.1002, 1.1332, 1.5608, 1.3413, 1.2577, 1.1603, + 0, 0, 0, 0.1002, 1.1332, 1.5608, 1.3413, 1.2577, 1.1603, 1.10153, 0.6080, 0.4523, 0.8856, 0.8717, 0.9655] diff --git a/modepy/quadrature/__init__.py b/modepy/quadrature/__init__.py index 1534a53..ff421d6 100644 --- a/modepy/quadrature/__init__.py +++ b/modepy/quadrature/__init__.py @@ -79,10 +79,10 @@ class Transformed1DQuadrature(Quadrature): self.left = left self.right = right - length = right-left + length = right - left half_length = length / 2 assert length > 0 Quadrature.__init__(self, - left + (quad.nodes + 1) / 2 * length, + left + (quad.nodes+1) / 2 * length, quad.weights * half_length) diff --git a/modepy/quadrature/grundmann_moeller.py b/modepy/quadrature/grundmann_moeller.py index 09c2a73..1e2d6ca 100644 --- a/modepy/quadrature/grundmann_moeller.py +++ b/modepy/quadrature/grundmann_moeller.py @@ -49,7 +49,7 @@ def _extended_euclidean(q, r): quot, t = divmod(q, r) T = Q[0] - quot*R[0], Q[1] - quot*R[1] # noqa: N806 q, r = r, t - Q, R = R, T + Q, R = R, T # noqa: N806 return q, Q[0], Q[1] @@ -68,7 +68,7 @@ class GrundmannMoellerSimplexQuadrature(Quadrature): r"""Cubature on an *n*-simplex. This cubature rule has both negative and positive weights. - It is exact for polynomials up to order :math:`2s+1`, where + It is exact for polynomials up to order :math:`2s + 1`, where :math:`s` is given as *order*. The integration domain is the unit simplex. (see :ref:`tri-coords` @@ -82,7 +82,7 @@ class GrundmannMoellerSimplexQuadrature(Quadrature): * A. Grundmann and H.M. Moeller, Invariant integration formulas for the n-simplex by combinatorial methods, - SIAM J. Numer. Anal. 15 (1978), 282--290. + SIAM J. Numer. Anal. 15 (1978), 282--290. http://dx.doi.org/10.1137/0715019 """ @@ -96,7 +96,7 @@ class GrundmannMoellerSimplexQuadrature(Quadrature): """ s = order n = dimension - d = 2*s+1 + d = 2*s + 1 self.exact_to = d @@ -115,17 +115,17 @@ class GrundmannMoellerSimplexQuadrature(Quadrature): points_to_weights = {} - for i in range(s+1): + for i in range(s + 1): weight = (-1)**i * 2**(-2*s) \ - * (d + n-2*i)**d \ + * (d + n - 2*i)**d \ / factorial(i) \ - / factorial(d+n-i) + / factorial(d + n - i) - for t in generate_decreasing_nonnegative_tuples_summing_to(s-i, n+1): + for t in generate_decreasing_nonnegative_tuples_summing_to(s - i, n + 1): for beta in generate_unique_permutations(t): - denominator = d+n-2*i + denominator = d + n - 2*i point = tuple( - _simplify_fraction((2*beta_i+1, denominator)) + _simplify_fraction((2*beta_i + 1, denominator)) for beta_i in beta) points_to_weights[point] = \ @@ -133,17 +133,17 @@ class GrundmannMoellerSimplexQuadrature(Quadrature): from operator import add - vertices = [-1 * np.ones((n,))] \ + vertices = ([-1 * np.ones((n,))] + [np.array(x) - for x in wandering_element(n, landscape=-1, wanderer=1)] + for x in wandering_element(n, landscape=-1, wanderer=1)]) nodes = [] weights = [] dim_factor = 2**n for p, w in six.iteritems(points_to_weights): - real_p = reduce(add, (a/b*v for (a, b), v in zip(p, vertices))) + real_p = reduce(add, (a/b * v for (a, b), v in zip(p, vertices))) nodes.append(real_p) - weights.append(dim_factor*w) + weights.append(dim_factor * w) Quadrature.__init__(self, np.array(nodes).T, np.array(weights)) diff --git a/modepy/quadrature/jacobi_gauss.py b/modepy/quadrature/jacobi_gauss.py index 47577d9..27527f8 100644 --- a/modepy/quadrature/jacobi_gauss.py +++ b/modepy/quadrature/jacobi_gauss.py @@ -74,42 +74,28 @@ class JacobiGaussQuadrature(Quadrature): # see Appendix A of Hesthaven/Warburton for these formulas def a(n): - return ( - 2/(2*n+apb) - * - sqrt( - (n*(n+apb)*(n+alpha)*(n+beta)) - / - ((2*n+apb-1)*(2*n+apb+1)) - ) - ) + return (2 / (2*n + apb) + * sqrt((n * (n+apb) * (n+alpha) * (n+beta)) + / ((2*n + apb - 1) * (2*n + apb + 1)))) def b(n): if n == 0: - return ( - -(alpha-beta) - / - (apb+2) - ) + return -(alpha-beta) / (apb+2) else: - return ( - -(alpha**2-beta**2) - / - ((2*n+apb)*(2*n+apb+2)) - ) + return -(alpha**2 - beta**2) / ((2*n + apb) * (2*n + apb + 2)) - T = np.zeros((N+1, N+1)) # noqa + T = np.zeros((N + 1, N + 1)) # noqa: N806 - for n in range(N+1): + for n in range(N + 1): T[n, n] = b(n) if n > 0: - T[n, n-1] = current_a # noqa + T[n, n-1] = current_a # noqa: F821 if n < N: - next_a = a(n+1) + next_a = a(n + 1) T[n, n+1] = next_a - current_a = next_a # noqa + current_a = next_a # noqa: F841 - assert la.norm(T-T.T) < 1e-12 + assert la.norm(T - T.T) < 1.0e-12 eigval, eigvec = la.eigh(T) assert la.norm(np.dot(T, eigvec) - np.dot(eigvec, np.diag(eigval))) < 1e-12 @@ -119,13 +105,13 @@ class JacobiGaussQuadrature(Quadrature): p0 = partial(jacobi, alpha, beta, 0) # that's a constant, sure nodes = eigval weights = np.array( - [eigvec[0, i]**2 / p0(nodes[i])**2 for i in range(N+1)]) + [eigvec[0, i]**2 / p0(nodes[i])**2 for i in range(N + 1)]) return nodes, weights @property def exact_to(self): - return 2 * self.nodes.size + 1 + return 2*self.nodes.size + 1 class LegendreGaussQuadrature(JacobiGaussQuadrature): @@ -150,16 +136,14 @@ def jacobi_gauss_lobatto_nodes(alpha, beta, N): # noqa Exact to degree :math:`2N - 3`. """ - x = np.zeros((N+1,)) + x = np.zeros((N + 1,)) x[0] = -1 x[-1] = 1 if N == 1: return x - x[1:-1] = np.array( - JacobiGaussQuadrature(alpha+1, beta+1, N-2).nodes - ).real + x[1:-1] = np.array(JacobiGaussQuadrature(alpha + 1, beta + 1, N - 2).nodes).real return x diff --git a/modepy/quadrature/vr_quad_data_tet.py b/modepy/quadrature/vr_quad_data_tet.py index df8ba46..a41d11f 100644 --- a/modepy/quadrature/vr_quad_data_tet.py +++ b/modepy/quadrature/vr_quad_data_tet.py @@ -23,6 +23,7 @@ def process_rule(table): return result + tetrahedron_data = process_rule({ # noqa 0: {points: [[0.00000000000000000000000000000000e+00], [0.00000000000000000000000000000000e+00], diff --git a/modepy/quadrature/vr_quad_data_tri.py b/modepy/quadrature/vr_quad_data_tri.py index 5c58da5..2e890f8 100644 --- a/modepy/quadrature/vr_quad_data_tri.py +++ b/modepy/quadrature/vr_quad_data_tri.py @@ -23,6 +23,7 @@ def process_rule(table): return result + triangle_data = process_rule({ # noqa 0: {points: [[0.00000000000000000000000000000000e+00], [0.00000000000000000000000000000000e+00], diff --git a/modepy/tools.py b/modepy/tools.py index 4989c00..1656192 100644 --- a/modepy/tools.py +++ b/modepy/tools.py @@ -100,10 +100,9 @@ class Monomial: from pytools import factorial from operator import mul - return (self.factor*2**len(self.exponents) * - reduce(mul, (factorial(alpha) for alpha in self.exponents)) - / - factorial(len(self.exponents)+sum(self.exponents))) + return (self.factor * 2**len(self.exponents) + * reduce(mul, (factorial(alpha) for alpha in self.exponents)) + / factorial(len(self.exponents)+sum(self.exponents))) def diff(self, coordinate): diff_exp = list(self.exponents) @@ -146,14 +145,14 @@ class AffineMap: EQUILATERAL_TO_UNIT_MAP = { 1: AffineMap([[1]], [0]), 2: AffineMap([ - [1, -1 / sqrt(3)], - [0, 2 / sqrt(3)]], - [-1/3, -1/3]), + [1, -1/sqrt(3)], + [0, 2/sqrt(3)]], + [-1/3, -1/3]), 3: AffineMap([ [1, -1/sqrt(3), -1/sqrt(6)], [0, 2/sqrt(3), -1/sqrt(6)], [0, 0, sqrt(6)/2]], - [-1/2, -1/2, -1/2]) + [-1/2, -1/2, -1/2]) } @@ -206,14 +205,14 @@ EQUILATERAL_VERTICES = { ]), 2: np.array([ [-1, -1/sqrt(3)], - [1, -1/sqrt(3)], - [0, 2/sqrt(3)], + [1, -1/sqrt(3)], + [0, 2/sqrt(3)], ]), 3: np.array([ [-1, -1/sqrt(3), -1/sqrt(6)], - [1, -1/sqrt(3), -1/sqrt(6)], - [0, 2/sqrt(3), -1/sqrt(6)], - [0, 0, 3/sqrt(6)], + [1, -1/sqrt(3), -1/sqrt(6)], + [0, 2/sqrt(3), -1/sqrt(6)], + [0, 0, 3/sqrt(6)], ]) } @@ -232,7 +231,7 @@ def pick_random_simplex_unit_coordinate(rng, dims): r = np.zeros(dims, np.float64) for j in range(dims): rn = rng.uniform(0, remaining) - r[j] = base+rn + r[j] = base + rn remaining -= rn return r diff --git a/setup.cfg b/setup.cfg index 7d2560e..9328bab 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,3 @@ [flake8] -ignore = E126,E127,E128,E123,E226,E241,E242,E402,W503 +ignore = E123,E126,E127,E128,E226,E241,E242,E402,W503 max-line-length=85 -- GitLab From e13e1f656fec77de5b1f16188c78f1f845359d89 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Thu, 8 Nov 2018 21:00:22 -0600 Subject: [PATCH 3/4] remove property from base class --- modepy/quadrature/__init__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modepy/quadrature/__init__.py b/modepy/quadrature/__init__.py index ff421d6..8f3c733 100644 --- a/modepy/quadrature/__init__.py +++ b/modepy/quadrature/__init__.py @@ -64,10 +64,6 @@ class Quadrature(object): """ return np.dot(self.weights, f(self.nodes)) - @property - def exact_to(self): - raise NotImplementedError("unknown quadrature order") - class Transformed1DQuadrature(Quadrature): """A quadrature rule on an arbitrary interval :math:`(a, b)`.""" -- GitLab From 0d989f71464c838390d1b64e460b5953149ed4f4 Mon Sep 17 00:00:00 2001 From: Alex Fikl Date: Sat, 10 Nov 2018 16:17:16 -0600 Subject: [PATCH 4/4] use exact_to in quadrature tests --- modepy/quadrature/jacobi_gauss.py | 6 ++---- test/test_quadrature.py | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/modepy/quadrature/jacobi_gauss.py b/modepy/quadrature/jacobi_gauss.py index 27527f8..91d3f3a 100644 --- a/modepy/quadrature/jacobi_gauss.py +++ b/modepy/quadrature/jacobi_gauss.py @@ -45,6 +45,8 @@ class JacobiGaussQuadrature(Quadrature): def __init__(self, alpha, beta, N): # noqa x, w = self.compute_weights_and_nodes(N, alpha, beta) + self.exact_to = 2*N + 1 + Quadrature.__init__(self, x, w) @staticmethod @@ -109,10 +111,6 @@ class JacobiGaussQuadrature(Quadrature): return nodes, weights - @property - def exact_to(self): - return 2*self.nodes.size + 1 - class LegendreGaussQuadrature(JacobiGaussQuadrature): """An Gauss quadrature associated with weight 1. diff --git a/test/test_quadrature.py b/test/test_quadrature.py index 4f39568..07ba925 100644 --- a/test/test_quadrature.py +++ b/test/test_quadrature.py @@ -33,31 +33,33 @@ def test_transformed_quadrature(): """Test 1D quadrature on arbitrary intervals""" def gaussian_density(x, mu, sigma): - return 1/(sigma*np.sqrt(2*np.pi))*np.exp(-(x-mu)**2/(2*sigma**2)) + return 1 / (sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2 * sigma**2)) from modepy.quadrature import Transformed1DQuadrature from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature mu = 17 sigma = 12 - tq = Transformed1DQuadrature(LegendreGaussQuadrature(20), mu-6*sigma, mu+6*sigma) + tq = Transformed1DQuadrature(LegendreGaussQuadrature(20), + mu - 6*sigma, mu + 6*sigma) result = tq(lambda x: gaussian_density(x, mu, sigma)) - assert abs(result - 1) < 1e-9 + assert abs(result - 1) < 1.0e-9 def test_gauss_quadrature(): from modepy.quadrature.jacobi_gauss import LegendreGaussQuadrature - for s in range(9+1): - cub = LegendreGaussQuadrature(s) - for deg in range(2*s+1 + 1): + for s in range(9 + 1): + quad = LegendreGaussQuadrature(s) + for deg in range(quad.exact_to + 1): def f(x): return x**deg - i_f = cub(f) - i_f_true = 1/(deg+1)*(1-(-1)**(deg+1)) + + i_f = quad(f) + i_f_true = 1 / (deg+1) * (1 - (-1)**(deg + 1)) err = abs(i_f - i_f_true) - assert err < 2e-15 + assert err < 2.0e-15, (s, deg, err, i_f, i_f_true) @pytest.mark.parametrize(("quad_class", "highest_order"), [ @@ -77,7 +79,7 @@ def test_simplex_quadrature(quad_class, highest_order, dim): try: quad = quad_class(order, dim) except mp.QuadratureRuleUnavailable: - print(("UNAVAIL", quad_class, order)) + print(("UNAVAILABLE", quad_class, order)) break if isinstance(quad_class, mp.VioreanuRokhlinSimplexQuadrature): -- GitLab