### Install quadpy using pip Source: https://github.com/sigma-py/quadpy/blob/main/README.md Install the quadpy library from PyPI using pip. Ensure you have Python and pip installed. ```bash pip install quadpy ``` -------------------------------- ### Example command-line output for a scheme Source: https://github.com/sigma-py/quadpy/blob/main/README.md This output provides details about a specific quadrature scheme, including its name, source, degree, number of points, weight ratios, and tolerance. ```text name: Rabinowitz-Richter 2 source: Perfectly Symmetric Two-Dimensional Integration Formulas with Minimal Numbers of Points Philip Rabinowitz, Nira Richter Mathematics of Computation, vol. 23, no. 108, pp. 765-779, 1969 https://doi.org/10.1090/S0025-5718-1969-0258281-4 degree: 9 num points/weights: 21 max/min weight ratio: 7.632e+01 test tolerance: 9.417e-15 point position: outside all weights positive: True ``` -------------------------------- ### Explore schemes via command line Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use the quadpy command-line interface to get information about specific quadrature schemes, such as the Rabinowitz-Richter scheme for S2. ```bash quadpy info s2 rabinowitz_richter_3 ``` -------------------------------- ### Define a function for quadpy integration Source: https://github.com/sigma-py/quadpy/wiki/Dimensionality-of-input-and-output-arrays Examples showing both iterative and vectorized approaches for defining functions to be integrated by quadpy. ```python def f(x): out = numpy.empty(x.shape[1:]) for i in range(x.shape[1]): for j in range(x.shape[2]): out[i, j] = numpy.sin(x[0, i, j]) * numpy.sin(x[1, i, j]) return out ``` ```python def f(x): return numpy.sin(x[0]) * numpy.sin(x[1]) ``` -------------------------------- ### Vectorized Function Example Source: https://github.com/sigma-py/quadpy/blob/main/README.md Example of a function that takes a vectorized input and returns a list of results. ```python def f(x): return [np.sin(x[0]), np.sin(x[1])] ``` -------------------------------- ### Generate Gauss Quadrature Schemes with mpmath Source: https://github.com/sigma-py/quadpy/wiki/Recreating-classical-schemes-with-arbitrary-precision Use this snippet to generate various Gauss quadrature schemes (Legendre, Hermite, Laguerre) with custom precision settings via mpmath. Ensure mpmath is installed and imported. ```python from mpmath import mp mp.dps = 30 scheme = quadpy.c1.gauss_legendre(96, mode="mpmath") mp.dps = 20 scheme = quadpy.e1r2.gauss_hermite(14, mode="mpmath") mp.dps = 50 scheme = quadpy.e1r.gauss_laguerre(13, mode="mpmath") print(scheme.points_symbolic) print(scheme.weights_symbolic) ``` -------------------------------- ### Explore quadrature schemes via CLI Source: https://context7.com/sigma-py/quadpy/llms.txt Use the command line interface to retrieve scheme information and visualize specific quadrature schemes. ```bash quadpy info s2 rabinowitz_richter_3 quadpy show t2 dunavant_10 ``` -------------------------------- ### Disk Integration with Different Functions and Centers Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate functions over disks centered at the origin and an offset point. Demonstrates integration over a unit disk and a disk with a specified radius and center. ```python import numpy as np import quadpy # Get a good scheme of degree 6 for disk integration scheme = quadpy.s2.get_good_scheme(6) # Integrate exp(x) over a disk centered at origin with radius 1 val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0], 1.0) print(f"Integral over unit disk: {val}") # Integrate over a disk centered at (2, 3) with radius 0.5 val = scheme.integrate(lambda x: x[0]**2 + x[1]**2, [2.0, 3.0], 0.5) print(f"Integral over offset disk: {val}") ``` -------------------------------- ### Import necessary libraries Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Import the required libraries: orthopy for orthogonal polynomials, quadpy for quadrature, and sympy for symbolic mathematics. ```python import orthopy import quadpy import sympy N = 10 ``` -------------------------------- ### Visualize a quadrature scheme Source: https://context7.com/sigma-py/quadpy/llms.txt Display a quadrature scheme using the library's built-in visualization method. ```python scheme.show() ``` -------------------------------- ### Integrate over a disk Source: https://github.com/sigma-py/quadpy/blob/main/README.md Retrieves a quadrature scheme for a disk and performs numerical integration over a specified center and radius. ```python import numpy as np import quadpy scheme = quadpy.s2.get_good_scheme(6) scheme.show() val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0], 1.0) ``` -------------------------------- ### Access symbolic points and weights Source: https://github.com/sigma-py/quadpy/blob/main/README.md Some quadrature schemes offer symbolic representations of their points and weights, accessible via points_symbolic and weights_symbolic attributes. ```python scheme.points_symbolic scheme.weights_symbolic ``` -------------------------------- ### Create Custom Gauss Quadrature Source: https://context7.com/sigma-py/quadpy/llms.txt Generate custom Gauss quadrature schemes from weight functions using orthopy. ```python import orthopy import quadpy import sympy N = 10 # Compute moments for weight function x^2 on [-1, +1] # int_{-1}^{+1} x^2 * x^k dx moments = [sympy.S(1 + (-1) ** kk) / (kk + 3) for kk in range(2 * N)] # Get recurrence coefficients using Chebyshev algorithm alpha, beta, int_1 = orthopy.tools.chebyshev(moments) # Convert to floats for numerical computation alpha = [float(sympy.N(a)) for a in alpha] beta = [float(sympy.N(b)) for b in beta] # Generate Gauss points and weights points, weights = quadpy.tools.scheme_from_rc(alpha, beta, int_1) print(f"Custom Gauss points: {points}") print(f"Custom Gauss weights: {weights}") # Transform back to recurrence coefficients alpha_back, beta_back = quadpy.tools.coefficients_from_gauss(points, weights) ``` -------------------------------- ### Show 2D Integration Scheme (E2r2) Source: https://github.com/sigma-py/quadpy/blob/main/README.md Displays a 2D integration scheme for a space with an exponential weight function exp(-r^2). ```python import quadpy scheme = quadpy.e2r2.get_good_scheme(3) scheme.show() val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### Integrate with exp(-r^2) weight Source: https://github.com/sigma-py/quadpy/blob/main/README.md Perform integration in 3D space using a scheme optimized for the exp(-r^2) weight function. ```python import quadpy scheme = quadpy.e3r2.get_good_scheme(6) # scheme.show() val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### Generate Quadrature Scheme from Recurrence Coefficients Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Convert symbolic recurrence coefficients (alpha, beta) and the first moment (int_1) to floating-point arrays and then generate quadrature points and weights. Requires `orthopy` and `sympy`. ```python alpha = [float(sympy.N(a)) for a in alpha] beta = [float(sympy.N(b)) for b in beta] int_1 = float(int_1) points, weights = quadpy.tools.scheme_from_rc(alpha, beta, int_1) print(points) print(weights) ``` -------------------------------- ### Integrate with exp(-r) weight Source: https://github.com/sigma-py/quadpy/blob/main/README.md Perform integration in 3D space using a scheme optimized for the exp(-r) weight function. ```python import quadpy scheme = quadpy.e3r.get_good_scheme(5) # scheme.show() val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### Compute Recurrence Coefficients using Chebyshev Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Compute recurrence coefficients (alpha, beta) and the first moment (int_1) using the Chebyshev method with symbolic moments. Requires `orthopy` and `sympy`. ```python import orthopy import sympy N = 10 moments = [sympy.S(1 + (-1) ** kk) / (kk + 3) for kk in range(2 * N)] print(moments) print() alpha, beta, int_1 = orthopy.tools.chebyshev(moments) print(alpha) print(beta) print(int_1) ``` -------------------------------- ### Quadrilateral Integration over Unit Square and Rectangle Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate functions over a unit square and a general rectangle using quadrilateral schemes. Shows integration over a quadrilateral defined by corners and using a convenience function for axis-aligned rectangles. ```python import numpy as np import quadpy scheme = quadpy.c2.get_good_scheme(7) # Integrate over a quadrilateral defined by 4 corners # Points specified as [[[lower-left, lower-right], [upper-left, upper-right]]] val = scheme.integrate( lambda x: np.exp(x[0]), [[[0.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], ) print(f"Integral over unit square: {val}") # Use convenience function for axis-aligned rectangles rect_points = quadpy.c2.rectangle_points([0.0, 2.0], [0.0, 1.0]) val = scheme.integrate(lambda x: x[0] * x[1], rect_points) print(f"Integral over rectangle [0,2]x[0,1]: {val}") ``` -------------------------------- ### 3D Weighted Space Integration (E3r, E3r2) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over 3D space with exponential weight functions. ```python import quadpy # 3D space with weight function exp(-r) scheme_e3r = quadpy.e3r.get_good_scheme(5) val = scheme_e3r.integrate(lambda x: x[0] ** 2) print(f"E3r integral: {val}") # 3D space with weight function exp(-r^2) scheme_e3r2 = quadpy.e3r2.get_good_scheme(6) val = scheme_e3r2.integrate(lambda x: x[0] ** 2) print(f"E3r2 integral: {val}") ``` -------------------------------- ### Access Scheme Properties Source: https://context7.com/sigma-py/quadpy/llms.txt Retrieve metadata and properties from quadrature schemes, including degree and point information. ```python import quadpy # Access any scheme from the schemes dictionary scheme = quadpy.t2.get_good_scheme(12) # Access scheme properties print(f"Polynomial degree: {scheme.degree}") print(f"Number of points: {len(scheme.points)}") print(f"Points shape: {scheme.points.shape}") print(f"Weights shape: {scheme.weights.shape}") print(f"Source: {scheme.source}") print(f"Test tolerance: {scheme.test_tolerance}") ``` -------------------------------- ### Wedge Integration (W3) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a wedge defined by two triangular faces. ```python import numpy as np import quadpy scheme = quadpy.w3.felippa_3() # Define wedge with bottom and top triangular faces wedge = [ [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.7, 0.0]], # bottom triangle [[0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.5, 0.7, 1.0]], # top triangle ] val = scheme.integrate(lambda x: np.exp(x[0]), wedge) print(f"Integral over wedge: {val}") ``` -------------------------------- ### Show 2D Integration Scheme (E2r) Source: https://github.com/sigma-py/quadpy/blob/main/README.md Displays a 2D integration scheme for a space with an exponential weight function exp(-r). ```python import quadpy scheme = quadpy.e2r.get_good_scheme(5) scheme.show() val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### 2D Weighted Space Integration (E2r, E2r2) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over 2D space with exponential weight functions. ```python import quadpy # 2D space with weight function exp(-r) scheme_e2r = quadpy.e2r.get_good_scheme(5) val = scheme_e2r.integrate(lambda x: x[0] ** 2) print(f"E2r integral: {val}") # 2D space with weight function exp(-r^2) scheme_e2r2 = quadpy.e2r2.get_good_scheme(3) val = scheme_e2r2.integrate(lambda x: x[0] ** 2) print(f"E2r2 integral: {val}") ``` -------------------------------- ### Pyramid Integration (P3) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a pyramid defined by four base vertices and an apex. ```python import numpy as np import quadpy scheme = quadpy.p3.felippa_5() # Define pyramid with 4 base vertices and 1 apex pyramid = [ [0.0, 0.0, 0.0], # base vertex 1 [1.0, 0.0, 0.0], # base vertex 2 [0.5, 0.7, 0.0], # base vertex 3 [0.3, 0.9, 0.0], # base vertex 4 [0.0, 0.1, 1.0], # apex ] val = scheme.integrate(lambda x: np.exp(x[0]), pyramid) print(f"Integral over pyramid: {val}") ``` -------------------------------- ### Circle Integration (U2) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a circle using Krylov schemes. ```python import numpy as np import quadpy scheme = quadpy.u2.get_good_scheme(7) # Integrate over circle centered at origin with radius 1 val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0], 1.0) print(f"Integral around unit circle: {val}") ``` -------------------------------- ### Basic 1D Integration with quad() Source: https://context7.com/sigma-py/quadpy/llms.txt Use quadpy.quad() for SciPy-like integration of 1D functions, supporting complex, vector, and matrix-valued integrands. ```python import numpy as np import quadpy def f(x): return np.sin(x) - x # Integrate f(x) from 0 to 6 val, err = quadpy.quad(f, 0.0, 6.0) print(f"Integral value: {val}") print(f"Error estimate: {err}") # Output: Integral value: -17.720584907840... ``` -------------------------------- ### Compute Recurrence Coefficients using Modified Chebyshev Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Compute recurrence coefficients (alpha, beta) and the first moment (int_1) using the Modified Chebyshev method with symbolic moments and recurrence coefficients. Requires `orthopy`, `quadpy`, and `sympy`. ```python import orthopy import quadpy import sympy N = 10 x = sympy.Symbol("x") evaluator = orthopy.c1.legendre.Eval(x, "monic") moments = [] for _ in range(2 * N): leg = next(evaluator) val = sympy.integrate(x ** 2 * leg, (x, -1, 1)) moments.append(val) print(moments) print() rc = orthopy.c1.legendre.RecurrenceCoefficients("monic", symbolic=True) rc = [rc[k] for k in range(2 * N)] alpha, beta, int_1 = orthopy.tools.chebyshev_modified(moments, rc) print(alpha) print(beta) print(int_1) ``` -------------------------------- ### Integrate Adaptive on Triangles Source: https://github.com/sigma-py/quadpy/wiki/Adaptive-quadrature Use `integrate_adaptive` for triangular domains. Requires importing `quadpy` and `numpy.sin`. The function to integrate takes a vector `x` and the domain is defined by a list of vertices. ```python import quadpy from numpy import sin val, error_estimate = quadpy.t2.integrate_adaptive( lambda x: x[0] * sin(5 * x[1]), [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], 1.0e-10 ) ``` -------------------------------- ### Access quadrature scheme properties Source: https://github.com/sigma-py/quadpy/blob/main/README.md Quadrature schemes in quadpy provide access to their points, weights, degree, source information, and test tolerance. You can also visualize the scheme using the show() method. ```python scheme.points scheme.weights scheme.degree scheme.source scheme.test_tolerance scheme.show() scheme.integrate( # ... ) ``` -------------------------------- ### Integrate Adaptive on Line Segments Source: https://github.com/sigma-py/quadpy/wiki/Adaptive-quadrature Use `integrate_adaptive` for line segments. Requires importing `quadpy` and `numpy.pi`, `numpy.sin`. The function to integrate can be a lambda expression. ```python import quadpy from numpy import pi, sin val, error_estimate = quadpy.c1.integrate_adaptive( lambda x: x * sin(5 * x), [0.0, pi], 1.0e-10 ) ``` -------------------------------- ### Solid Sphere Integration (S3) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate functions over a solid ball using schemes like Ditkin or Hammer-Stroud. ```python import numpy as np import quadpy scheme = quadpy.s3.get_good_scheme(4) # Integrate exp(x) over unit ball centered at origin val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0, 0.0], 1.0) print(f"Integral over unit ball: {val}") # Integrate over ball centered at (1, 2, 3) with radius 2 val = scheme.integrate(lambda x: x[0]**2, [1.0, 2.0, 3.0], 2.0) print(f"Integral over offset ball: {val}") ``` -------------------------------- ### 1D Space Gauss-Hermite Scheme Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over a 1D space using the Gauss-Hermite scheme. Requires importing quadpy. ```python import quadpy scheme = quadpy.e1r2.gauss_hermite(5) scheme.show() val = scheme.integrate(lambda x: x**2) ``` -------------------------------- ### Integrate over Weighted Spaces Source: https://context7.com/sigma-py/quadpy/llms.txt Perform integration over n-dimensional spaces with exponential weight functions. ```python import quadpy dim = 4 # nD space with weight function exp(-r) scheme_enr = quadpy.enr.stroud_enr_5_4(dim) val = scheme_enr.integrate(lambda x: x[0] ** 2) print(f"Enr integral in {dim}D: {val}") # nD space with weight function exp(-r^2) scheme_enr2 = quadpy.enr2.stroud_enr2_5_2(dim) val = scheme_enr2.integrate(lambda x: x[0] ** 2) print(f"Enr2 integral in {dim}D: {val}") ``` -------------------------------- ### Gauss-Hermite Integration (E1r2) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a 1D full space with a Gaussian weight function. ```python import quadpy scheme = quadpy.e1r2.gauss_hermite(5) # Integrates x^2 * exp(-x^2) from -infinity to infinity val = scheme.integrate(lambda x: x**2) print(f"Integral of x^2 * exp(-x^2): {val}") # Exact value: sqrt(pi)/2 ``` -------------------------------- ### Arbitrary Precision Quadrature Source: https://context7.com/sigma-py/quadpy/llms.txt Generate Gauss quadrature schemes with arbitrary precision using mpmath. ```python from mpmath import mp import quadpy # Gauss-Legendre with 30 decimal places mp.dps = 30 scheme = quadpy.c1.gauss_legendre(96, mode="mpmath") print(f"High precision points: {scheme.points_symbolic[:3]}") # Gauss-Hermite with 20 decimal places mp.dps = 20 scheme = quadpy.e1r2.gauss_hermite(14, mode="mpmath") # Gauss-Laguerre with 50 decimal places mp.dps = 50 scheme = quadpy.e1r.gauss_laguerre(13, mode="mpmath") print(f"Symbolic weights: {scheme.weights_symbolic[:3]}") ``` -------------------------------- ### 1D Half-Space Gauss-Laguerre Scheme Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over a 1D half-space using the Gauss-Laguerre scheme with a specified alpha value. Requires importing quadpy. ```python import quadpy scheme = quadpy.e1r.gauss_laguerre(5, alpha=0) scheme.show() val = scheme.integrate(lambda x: x**2) ``` -------------------------------- ### Hexahedron Integration (C3) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a hexahedron using product schemes. ```python import numpy as np import quadpy # Create a product scheme from Newton-Cotes scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3)) # Define a cube using convenience function cube = quadpy.c3.cube_points([0.0, 1.0], [-0.3, 0.4], [1.0, 2.1]) val = scheme.integrate(lambda x: np.exp(x[0]), cube) print(f"Integral over hexahedron: {val}") ``` -------------------------------- ### Line Segment Integration with Gauss-Patterson Scheme Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate exp(x) over [0, 1] using the Gauss-Patterson scheme with 5 levels. Access scheme properties like degree and number of points. ```python import numpy as np import quadpy # Use Gauss-Patterson scheme with 5 levels scheme = quadpy.c1.gauss_patterson(5) # Access scheme properties print(f"Degree: {scheme.degree}") print(f"Number of points: {len(scheme.points)}") # Integrate exp(x) over [0, 1] val = scheme.integrate(lambda x: np.exp(x), [0.0, 1.0]) print(f"Integral of exp(x) from 0 to 1: {val}") # Output: ~1.7182818284590453 (e - 1) # Visualize the scheme scheme.show() ``` -------------------------------- ### Compute Recurrence Coefficients using Stieltjes Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Use the Stieltjes method to compute recurrence coefficients (alpha, beta) and the first moment (int_1) from a symbolic integration function. Requires `orthopy` and `sympy`. ```python import orthopy import sympy N = 10 alpha, beta, int_1 = orthopy.tools.stieltjes( lambda t, ft: sympy.integrate(t ** 2 * ft, (t, -1, 1)), N ) print(alpha) print(beta) print(int_1) ``` -------------------------------- ### Integrate a function over a triangle Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrate a 2D function over a triangular domain using a specific quadrature scheme. Obtain a scheme using get_good_scheme and then call its integrate method. ```python import numpy as np import quadpy def f(x): return np.sin(x[0]) * np.sin(x[1]) triangle = np.array([[0.0, 0.0], [1.0, 0.0], [0.7, 0.5]]) # get a "good" scheme of degree 10 scheme = quadpy.t2.get_good_scheme(10) val = scheme.integrate(f, triangle) ``` -------------------------------- ### Integrate over a Wedge Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use a Felippa scheme to integrate a function over a wedge defined by its vertices. ```python import numpy as np import quadpy scheme = quadpy.w3.felippa_3() val = scheme.integrate( lambda x: np.exp(x[0]), [ [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.7, 0.0]], [[0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.5, 0.7, 1.0]], ], ) ``` -------------------------------- ### Integrate over a Quadrilateral Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over a quadrilateral domain using a specified scheme. The points defining the quadrilateral must be in a (2, 2, ...) shape array. ```python import numpy as np import quadpy scheme = quadpy.c2.get_good_scheme(7) val = scheme.integrate( lambda x: np.exp(x[0]), [[[0.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], ) ``` -------------------------------- ### Integrate function over nD space with exp(-r^2) weight Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over an n-dimensional space with a weight function of exp(-r^2) using a Stroud ENR2 scheme. Requires quadpy. ```python import quadpy dim = 4 scheme = quadpy.enr2.stroud_enr2_5_2(dim) val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### Circle Krylov Scheme Integration Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over a circle using a good scheme obtained from quadpy. Requires importing numpy and quadpy. ```python import numpy as np import quadpy scheme = quadpy.u2.get_good_scheme(7) scheme.show() val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0], 1.0) ``` -------------------------------- ### Integrate over a triangle Source: https://github.com/sigma-py/quadpy/blob/main/README.md Retrieves a quadrature scheme for a triangle and performs numerical integration over a specified domain. ```python import numpy as np import quadpy scheme = quadpy.t2.get_good_scheme(12) scheme.show() val = scheme.integrate(lambda x: np.exp(x[0]), [[0.0, 0.0], [1.0, 0.0], [0.5, 0.7]]) ``` -------------------------------- ### Gauss-Laguerre Integration (E1r) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a 1D half-space with an exponential weight function. ```python import quadpy # Gauss-Laguerre with 5 points and alpha parameter scheme = quadpy.e1r.gauss_laguerre(5, alpha=0) # Integrates x^2 * exp(-x) from 0 to infinity val = scheme.integrate(lambda x: x**2) print(f"Integral of x^2 * exp(-x): {val}") # Exact value: 2 (Gamma(3) = 2!) ``` -------------------------------- ### Integrate function over nD space with exp(-r) weight Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over an n-dimensional space with a weight function of exp(-r) using a Stroud ENR scheme. Requires quadpy. ```python import quadpy dim = 4 scheme = quadpy.enr.stroud_enr_5_4(dim) val = scheme.integrate(lambda x: x[0] ** 2) ``` -------------------------------- ### Triangle Integration with Custom Function Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate a 2D function over a triangle defined by its vertices. Demonstrates integration over a single triangle and vectorized integration over multiple triangles. ```python import numpy as np import quadpy def f(x): return np.sin(x[0]) * np.sin(x[1]) # Define a triangle by its three vertices triangle = np.array([[0.0, 0.0], [1.0, 0.0], [0.7, 0.5]]) # Get a "good" scheme of degree 10 scheme = quadpy.t2.get_good_scheme(10) # Integrate over the triangle val = scheme.integrate(f, triangle) print(f"Integral value: {val}") # Integrate over multiple triangles at once (vectorized) triangles = np.stack([ [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], [[1.2, 0.6], [1.3, 0.7], [1.4, 0.8]], [[0.1, 0.3], [0.4, 0.4], [0.7, 0.1]], ], axis=-2) # shape (3, 5, 2) = (corners, num_triangles, xy_coords) vals = scheme.integrate(f, triangles) print(f"Integrals over 3 triangles: {vals}") ``` -------------------------------- ### Perform Gautschi Test for Moment-Based Schemes Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps This function implements the Gautschi test to verify moment-based schemes for generating orthogonal polynomials. It requires moments, alpha, and beta coefficients as input. ```python err = orthopy.tools.gautschi_test_3(moments, alpha, beta) ``` -------------------------------- ### Integrate over n-Ball Source: https://context7.com/sigma-py/quadpy/llms.txt Perform integration over the interior of an n-dimensional ball. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.sn.dobrodeev_1970(dim) # Integrate over 4D ball centered at origin with radius 1 val = scheme.integrate(lambda x: np.exp(x[0]), np.zeros(dim), 1.0) print(f"Integral over 4D ball: {val}") ``` -------------------------------- ### Integrate over a Pyramid Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use a Felippa scheme to integrate a function over a pyramid defined by its vertices. ```python import numpy as np import quadpy scheme = quadpy.p3.felippa_5() val = scheme.integrate( lambda x: np.exp(x[0]), [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.7, 0.0], [0.3, 0.9, 0.0], [0.0, 0.1, 1.0], ], ) ``` -------------------------------- ### Integrate function over n-Cube Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over an n-dimensional cube using a specified Stroud CN scheme. Requires numpy and quadpy. The integration points can be customized. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.cn.stroud_cn_3_3(dim) val = scheme.integrate( lambda x: np.exp(x[0]), quadpy.cn.ncube_points([0.0, 1.0], [0.1, 0.9], [-1.0, 1.0], [-1.0, -0.5]), ) ``` -------------------------------- ### Compute Recurrence Coefficients using Golub-Welsch Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Compute recurrence coefficients (alpha, beta) using the Golub-Welsch method from a list of moments. This method is suitable for floating-point arithmetic. Requires `orthopy`. ```python import orthopy N = 10 moments = [(1 + (-1) ** kk) / (kk + 3) for kk in range(2 * N + 1)] print(moments) print() alpha, beta = orthopy.tools.golub_welsch(moments) print(alpha) print(beta) ``` -------------------------------- ### Integrate over an n-Simplex Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use a Grundmann-Möller scheme to integrate a function over an n-dimensional simplex. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.tn.grundmann_moeller(dim, 3) val = scheme.integrate( lambda x: np.exp(x[0]), np.array( [ [0.0, 0.0, 0.0, 0.0], [1.0, 2.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 3.0, 1.0, 0.0], [0.0, 0.0, 4.0, 1.0], ] ), ) ``` -------------------------------- ### Adaptive Quadrature Integration Source: https://context7.com/sigma-py/quadpy/llms.txt Perform automatic error-estimated integration over line segments and triangles. ```python import quadpy from numpy import pi, sin # Adaptive integration over line segment val, error_estimate = quadpy.c1.integrate_adaptive( lambda x: x * sin(5 * x), [0.0, pi], 1.0e-10 # tolerance ) print(f"Line integral: {val}, error estimate: {error_estimate}") # Adaptive integration over triangle val, error_estimate = quadpy.t2.integrate_adaptive( lambda x: x[0] * sin(5 * x[1]), [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], 1.0e-10 # tolerance ) print(f"Triangle integral: {val}, error estimate: {error_estimate}") ``` -------------------------------- ### Line Segment Gauss-Patterson Scheme Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function over a line segment using the Gauss-Patterson scheme. Requires importing numpy and quadpy. ```python import numpy as np import quadpy scheme = quadpy.c1.gauss_patterson(5) scheme.show() val = scheme.integrate(lambda x: np.exp(x), [0.0, 1.0]) ``` -------------------------------- ### Integrate over n-Sphere Surface Source: https://context7.com/sigma-py/quadpy/llms.txt Perform integration over the surface of an n-dimensional sphere. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.un.dobrodeev_1978(dim) # Integrate over 4D sphere surface centered at origin with radius 1 val = scheme.integrate(lambda x: np.exp(x[0]), np.zeros(dim), 1.0) print(f"Integral over 4D sphere surface: {val}") ``` -------------------------------- ### Integrate over an n-Ball Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use a Dobrodeev scheme to integrate a function over an n-dimensional ball. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.sn.dobrodeev_1970(dim) val = scheme.integrate(lambda x: np.exp(x[0]), np.zeros(dim), 1.0) ``` -------------------------------- ### Integrate over n-Cube Source: https://context7.com/sigma-py/quadpy/llms.txt Perform integration over n-dimensional hypercubes defined by ranges for each dimension. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.cn.stroud_cn_3_3(dim) # Define 4D hypercube with ranges for each dimension hypercube = quadpy.cn.ncube_points( [0.0, 1.0], # x1 range [0.1, 0.9], # x2 range [-1.0, 1.0], # x3 range [-1.0, -0.5], # x4 range ) val = scheme.integrate(lambda x: np.exp(x[0]), hypercube) print(f"Integral over 4D hypercube: {val}") ``` -------------------------------- ### Sphere Surface Integration Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate a function over the surface of a unit sphere centered at the origin using a specified scheme degree. ```python import numpy as np import quadpy scheme = quadpy.u3.get_good_scheme(19) # Integrate exp(x) over unit sphere surface centered at origin val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0, 0.0], 1.0) print(f"Integral over sphere surface: {val}") ``` -------------------------------- ### Perform vectorized integration Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate scalar or vector-valued functions over geometric domains using vectorized operations for performance. ```python import numpy as np import quadpy # Vectorized scalar-valued function def f_scalar(x): return np.sin(x[0]) * np.sin(x[1]) # Vectorized vector-valued function (returns 2D vector at each point) def f_vector(x): return [np.sin(x[0]), np.sin(x[1])] # Input x has shape (d, n, p) where: # d = geometric dimension (e.g., 2 for triangles) # n = number of domains # p = number of integration points scheme = quadpy.t2.get_good_scheme(10) # Single triangle triangle = np.array([[0.0, 0.0], [1.0, 0.0], [0.5, 0.7]]) val = scheme.integrate(f_scalar, triangle) print(f"Scalar integral: {val}") # Vector-valued output vals = scheme.integrate(f_vector, triangle) print(f"Vector integral shape: {vals.shape}") # (2,) ``` -------------------------------- ### Transform Gaussian Points to Recurrence Coefficients Source: https://github.com/sigma-py/quadpy/wiki/Creating-your-own-Gauss-quadrature-in-two-simple-steps Use this function to convert Gaussian quadrature points and weights back into recurrence coefficients (alpha and beta). This is useful for reconstructing orthogonal polynomials from quadrature rules. ```python alpha, beta = quadpy.tools.coefficients_from_gauss(points, weights) ``` -------------------------------- ### Integrate over an n-Sphere Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use a Dobrodeev scheme to integrate a function over an n-dimensional sphere. ```python import numpy as np import quadpy dim = 4 scheme = quadpy.un.dobrodeev_1978(dim) val = scheme.integrate(lambda x: np.exp(x[0]), np.zeros(dim), 1.0) ``` -------------------------------- ### Integrate a function over a 1D interval Source: https://github.com/sigma-py/quadpy/blob/main/README.md Use the quadpy.quad function to numerically integrate a given function over a specified 1D interval. This function is similar to scipy.integrate.quad but also handles complex-valued and vector-valued integrands. ```python import numpy as np import quadpy def f(x): return np.sin(x) - x val, err = quadpy.quad(f, 0.0, 6.0) ``` -------------------------------- ### Define Triangle Mesh Source: https://github.com/sigma-py/quadpy/blob/main/README.md Defines a mesh of triangles with shape (corners, num_triangles, xy_coords). ```python triangles = np.stack( [ [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], [[1.2, 0.6], [1.3, 0.7], [1.4, 0.8]], [[26.0, 31.0], [24.0, 27.0], [33.0, 28]], [[0.1, 0.3], [0.4, 0.4], [0.7, 0.1]], [[8.6, 6.0], [9.4, 5.6], [7.5, 7.4]], ], axis=-2, ) ``` -------------------------------- ### Tetrahedron Integration (T3) Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate over a tetrahedron defined by four vertices. ```python import numpy as np import quadpy scheme = quadpy.t3.get_good_scheme(5) # Define tetrahedron by its 4 vertices tetrahedron = [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.7, 0.0], [0.3, 0.9, 1.0], ] val = scheme.integrate(lambda x: np.exp(x[0]), tetrahedron) print(f"Integral over tetrahedron: {val}") ``` -------------------------------- ### Integrate Function on Ball Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function defined in Cartesian coordinates over a ball using a specified scheme. Requires numpy and quadpy. The function should accept a list or array of coordinates and return a scalar. ```python import numpy as np import quadpy scheme = quadpy.s3.get_good_scheme(4) # scheme.show() val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0, 0.0], 1.0) ``` -------------------------------- ### Integrate Function on Sphere Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function defined in Cartesian coordinates over a sphere using a specified scheme. Requires numpy and quadpy. Ensure the function takes a list or array of coordinates and returns a scalar. ```python import numpy as np import quadpy scheme = quadpy.u3.get_good_scheme(19) # scheme.show() val = scheme.integrate(lambda x: np.exp(x[0]), [0.0, 0.0, 0.0], 1.0) ``` -------------------------------- ### Spherical Coordinate Integration Source: https://context7.com/sigma-py/quadpy/llms.txt Integrate a function defined in spherical coordinates. ```python def f_spherical(theta_phi): theta, phi = theta_phi return np.sin(phi) ** 2 * np.sin(theta) val = scheme.integrate_spherical(f_spherical) print(f"Integral in spherical coords: {val}") ``` -------------------------------- ### Generate Rectangle Points Source: https://github.com/sigma-py/quadpy/blob/main/README.md Convenience function to generate the point array for a rectangle aligned with the coordinate axes. ```python quadpy.c2.rectangle_points([x0, x1], [y0, y1]) ``` -------------------------------- ### Integrate Function on Hexahedron Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function defined in Cartesian coordinates over a hexahedron (cube). Requires numpy and quadpy. The function should accept a list or array of coordinates and return a scalar. The hexahedron is defined by its corner points. ```python import numpy as np import quadpy scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3)) # scheme.show() val = scheme.integrate( lambda x: np.exp(x[0]), quadpy.c3.cube_points([0.0, 1.0], [-0.3, 0.4], [1.0, 2.1])) ``` -------------------------------- ### Integrate Spherical Function Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function defined in spherical coordinates (theta, phi) over a sphere. Requires numpy and quadpy. The function should accept a tuple or list of (theta, phi) and return a scalar. ```python import numpy as np import quadpy def f(theta_phi): theta, phi = theta_phi return np.sin(phi) ** 2 * np.sin(theta) scheme = quadpy.u3.get_good_scheme(19) val = scheme.integrate_spherical(f) ``` -------------------------------- ### Integrate Function on Tetrahedron Source: https://github.com/sigma-py/quadpy/blob/main/README.md Integrates a function defined in Cartesian coordinates over a tetrahedron. Requires numpy and quadpy. The function should accept a list or array of coordinates and return a scalar. The tetrahedron is defined by its four vertices. ```python import numpy as np import quadpy scheme = quadpy.t3.get_good_scheme(5) # scheme.show() val = scheme.integrate( lambda x: np.exp(x[0]), [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 0.7, 0.0], [0.3, 0.9, 1.0]]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.