Skip to content

Commit

Permalink
refactor quadrature function layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathf committed Mar 18, 2021
1 parent b267aac commit c3dd222
Show file tree
Hide file tree
Showing 36 changed files with 1,763 additions and 2,010 deletions.
4 changes: 3 additions & 1 deletion chaospy/distributions/sampler/sequences/chebyshev.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"""
import numpy

import chaospy
from chaospy.quadrature import utils


def create_chebyshev_samples(order, dim=1):
Expand All @@ -60,7 +62,7 @@ def create_chebyshev_samples(order, dim=1):
``[0, 1]^dim`` hyper-cube and ``shape == (dim, order)``.
"""
x_data = .5*numpy.cos(numpy.arange(order, 0, -1)*numpy.pi/(order+1)) + .5
x_data = chaospy.quadrature.combine([x_data]*dim)
x_data = utils.combine([x_data]*dim)
return x_data.T


Expand Down
4 changes: 3 additions & 1 deletion chaospy/distributions/sampler/sequences/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"""
import numpy

import chaospy
from chaospy.quadrature import utils


def create_grid_samples(order, dim=1):
Expand All @@ -59,7 +61,7 @@ def create_grid_samples(order, dim=1):
Regular grid with ``shape == (dim, order)``.
"""
x_data = numpy.arange(1, order+1)/(order+1.)
x_data = chaospy.quadrature.combine([x_data]*dim)
x_data = utils.combine([x_data]*dim)
return x_data.T


Expand Down
89 changes: 73 additions & 16 deletions chaospy/quadrature/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,75 @@
r"""Collection of quadrature methods."""
import logging
from functools import wraps

from .frontend import generate_quadrature
from .sparse_grid import construct_sparse_grid
from .combine import combine

from .clenshaw_curtis import quad_clenshaw_curtis
from .discrete import quad_discrete
from .fejer import quad_fejer
from .gaussian import quad_gaussian
from .gauss_patterson import quad_gauss_patterson
from .gauss_legendre import quad_gauss_legendre
from .gauss_lobatto import quad_gauss_lobatto
from .gauss_kronrod import quad_gauss_kronrod, kronrod_jacobi
from .gauss_radau import quad_gauss_radau
from .genz_keister import quad_genz_keister
from .grid import quad_grid
from .leja import quad_leja
from .newton_cotes import quad_newton_cotes
from .sparse_grid import sparse_grid
from .utils import combine

from .chebyshev import chebyshev_1, chebyshev_2
from .clenshaw_curtis import clenshaw_curtis
from .discrete import discrete
from .fejer_1 import fejer_1
from .fejer_2 import fejer_2
from .gaussian import gaussian
from .gegenbauer import gegenbauer
from .grid import grid
from .hermite import hermite
from .jacobi import jacobi
from .kronrod import kronrod, kronrod_jacobi
from .laguerre import laguerre
from .legendre import legendre, legendre_proxy
from .leja import leja
from .lobatto import lobatto
from .newton_cotes import newton_cotes
from .patterson import patterson
from .radau import radau


INTEGRATION_COLLECTION = {
"clenshaw_curtis": clenshaw_curtis,
"discrete": discrete,
"fejer_1": fejer_1,
"fejer_2": fejer_2,
"gaussian": gaussian,
"grid": grid,
"kronrod": kronrod,
"legendre": legendre_proxy,
"leja": leja,
"lobatto": lobatto,
"newton_cotes": newton_cotes,
"patterson": patterson,
"radau": radau,
}


def quadrature_deprecation_warning(name, func=None):
"""Announce deprecation warning for quad-func."""

if func is None:
func = globals()[name]
quad_name = "quad_%s" % name

@wraps(func)
def wrapped(*args, **kwargs):
"""Function wrapper adds warnings."""
logger = logging.getLogger(__name__)
logger.warning("chaospy.%s name is to be deprecated; "
"Use chaospy.quadrature.%s instead",
quad_name, func.__name__)
return func(*args, **kwargs)

globals()[quad_name] = wrapped

quadrature_deprecation_warning("clenshaw_curtis", clenshaw_curtis)
quadrature_deprecation_warning("discrete", discrete)
quadrature_deprecation_warning("fejer", fejer_2)
quadrature_deprecation_warning("grid", grid)
quadrature_deprecation_warning("gaussian", gaussian)
quadrature_deprecation_warning("newton_cotes", newton_cotes)
quadrature_deprecation_warning("leja", leja)
quadrature_deprecation_warning("gauss_legendre", legendre_proxy)
quadrature_deprecation_warning("gauss_kronrod", kronrod)
quadrature_deprecation_warning("gauss_lobatto", lobatto)
quadrature_deprecation_warning("gauss_patterson", patterson)
quadrature_deprecation_warning("gauss_radau", radau)
108 changes: 108 additions & 0 deletions chaospy/quadrature/chebyshev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""Chebyshev-Gauss quadrature rule of the first kind."""
import numpy
import chaospy

from .hypercube import hypercube_quadrature


def chebyshev_1(order, lower=-1, upper=1, physicist=False):
r"""
Chebyshev-Gauss quadrature rule of the first kind.
Compute the sample points and weights for Chebyshev-Gauss quadrature. The
sample points are the roots of the nth degree Chebyshev polynomial. These
sample points and weights correctly integrate polynomials of degree
:math:`2N-1` or less.
Gaussian quadrature come in two variants: physicist and probabilist. For
first order Chebyshev-Gauss physicist means a weight function
:math:`1/\sqrt{1-x^2}` and weights that sum to :math`1/2`, and probabilist
means a weight function is :math:`1/\sqrt{x (1-x)}` and sum to 1.
Args:
order (int):
The quadrature order.
lower (float):
Lower bound for the integration interval.
upper (float):
Upper bound for the integration interval.
physicist (bool):
Use physicist weights instead of probabilist.
Returns:
abscissas (numpy.ndarray):
The ``order+1`` quadrature points for where to evaluate the model
function with.
weights (numpy.ndarray):
The quadrature weights associated with each abscissas.
Examples:
>>> abscissas, weights = chaospy.quadrature.chebyshev_1(3)
>>> abscissas
array([[-0.92387953, -0.38268343, 0.38268343, 0.92387953]])
>>> weights
array([0.25, 0.25, 0.25, 0.25])
See also:
:func:`chaospy.quadrature.chebyshev_2`
:func:`chaospy.quadrature.gaussian`
"""
order = int(order)
coefficients = chaospy.construct_recurrence_coefficients(
order=order, dist=chaospy.Beta(0.5, 0.5, lower, upper))
[abscissas], [weights] = chaospy.coefficients_to_quadrature(coefficients)
weights *= 0.5 if physicist else 1
return abscissas[numpy.newaxis], weights



def chebyshev_2(order, lower=-1, upper=1, physicist=False):
r"""
Chebyshev-Gauss quadrature rule of the second kind.
Compute the sample points and weights for Chebyshev-Gauss quadrature. The
sample points are the roots of the nth degree Chebyshev polynomial. These
sample points and weights correctly integrate polynomials of degree
:math:`2N-1` or less.
Gaussian quadrature come in two variants: physicist and probabilist. For
second order Chebyshev-Gauss physicist means a weight function
:math:`\sqrt{1-x^2}` and weights that sum to :math`2`, and probabilist
means a weight function is :math:`\sqrt{x (1-x)}` and sum to 1.
Args:
order (int):
The quadrature order.
lower (float):
Lower bound for the integration interval.
upper (float):
Upper bound for the integration interval.
physicist (bool):
Use physicist weights instead of probabilist.
Returns:
abscissas (numpy.ndarray):
The ``order+1`` quadrature points for where to evaluate the model
function with.
weights (numpy.ndarray):
The quadrature weights associated with each abscissas.
Examples:
>>> abscissas, weights = chaospy.quadrature.chebyshev_2(3)
>>> abscissas
array([[-0.80901699, -0.30901699, 0.30901699, 0.80901699]])
>>> weights
array([0.1381966, 0.3618034, 0.3618034, 0.1381966])
See also:
:func:`chaospy.quadrature.chebyshev_1`
:func:`chaospy.quadrature.gaussian`
"""
order = int(order)
coefficients = chaospy.construct_recurrence_coefficients(
order=order, dist=chaospy.Beta(1.5, 1.5, lower, upper))
[abscissas], [weights] = chaospy.coefficients_to_quadrature(coefficients)
weights *= 2 if physicist else 1
return abscissas[numpy.newaxis], weights
Loading

0 comments on commit c3dd222

Please sign in to comment.