Skip to content

Commit ec60249

Browse files
committed
refactor orthogonal->expansion
1 parent c3dd222 commit ec60249

27 files changed

+577
-243
lines changed

chaospy/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
import chaospy.descriptives
1414
import chaospy.distributions
15-
import chaospy.orthogonal
15+
import chaospy.expansion
1616
import chaospy.spectral
1717
import chaospy.quadrature
1818
import chaospy.saltelli
1919
import chaospy.regression
2020
import chaospy.recurrence
2121

2222
from chaospy.distributions import *
23-
from chaospy.orthogonal import *
23+
from chaospy.expansion import *
2424
from chaospy.spectral import *
2525
from chaospy.quadrature import *
2626
from chaospy.saltelli import *

chaospy/expansion/__init__.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
r"""Collection of polynomial expansion constructors."""
2+
import logging
3+
from functools import wraps
4+
5+
from .chebyshev import chebyshev_1, chebyshev_2
6+
from .cholesky import cholesky
7+
from .frontend import generate_expansion
8+
from .gegenbauer import gegenbauer
9+
from .gram_schmidt import gram_schmidt
10+
from .hermite import hermite
11+
from .jacobi import jacobi
12+
from .stieltjes import stieltjes
13+
from .lagrange import lagrange
14+
from .laguerre import laguerre
15+
from .legendre import legendre
16+
17+
__all__ = ["generate_expansion"]
18+
19+
20+
def expansion_deprecation_warning(name, func):
21+
22+
@wraps(func)
23+
def wrapped(*args, **kwargs):
24+
"""Function wrapper adds warnings."""
25+
logger = logging.getLogger(__name__)
26+
logger.warning("chaospy.%s name is to be deprecated; "
27+
"Use chaospy.expansion.%s instead",
28+
name, func.__name__)
29+
return func(*args, **kwargs)
30+
31+
globals()[name] = wrapped
32+
__all__.append(name)
33+
34+
35+
expansion_deprecation_warning("orth_ttr", stieltjes)
36+
expansion_deprecation_warning("orth_chol", cholesky)
37+
expansion_deprecation_warning("orth_gs", gram_schmidt)
38+
expansion_deprecation_warning("lagrange_polynomial", lagrange)

chaospy/expansion/chebyshev.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Chebyshev polynomials of the first kind."""
2+
import numpy
3+
import chaospy
4+
5+
6+
def chebyshev_1(
7+
order,
8+
lower=-1,
9+
upper=1,
10+
physicist=False,
11+
normed=False,
12+
retall=False,
13+
):
14+
"""
15+
Chebyshev polynomials of the first kind.
16+
17+
Args:
18+
order (int):
19+
The polynomial order.
20+
lower (float):
21+
Lower bound for the integration interval.
22+
upper (float):
23+
Upper bound for the integration interval.
24+
physicist (bool):
25+
Use physicist weights instead of probabilist.
26+
27+
Returns:
28+
(numpoly.ndpoly, numpy.ndarray):
29+
Chebyshev polynomial expansion. Norms of the orthogonal
30+
expansion on the form ``E(orth**2, dist)``.
31+
32+
Examples:
33+
>>> polynomials, norms = chaospy.expansion.chebyshev_1(4, retall=True)
34+
>>> polynomials
35+
polynomial([1.0, q0, q0**2-0.5, q0**3-0.75*q0, q0**4-q0**2+0.125])
36+
>>> norms
37+
array([1. , 0.5 , 0.125 , 0.03125 , 0.0078125])
38+
>>> chaospy.expansion.chebyshev_1(3, physicist=True)
39+
polynomial([1.0, q0, 2.0*q0**2-1.0, 4.0*q0**3-2.5*q0])
40+
>>> chaospy.expansion.chebyshev_1(3, lower=0.5, upper=1.5, normed=True).round(3)
41+
polynomial([1.0, 2.828*q0-2.828, 11.314*q0**2-22.627*q0+9.899,
42+
45.255*q0**3-135.765*q0**2+127.279*q0-36.77])
43+
44+
"""
45+
multiplier = 1+numpy.arange(order).astype(bool) if physicist else 1
46+
_, [polynomials], [norms] = chaospy.recurrence.analytical_stieltjes(
47+
order, chaospy.Beta(0.5, 0.5, lower, upper), multiplier=multiplier)
48+
if normed:
49+
polynomials = chaospy.true_divide(polynomials, numpy.sqrt(norms))
50+
norms[:] = 1.
51+
return (polynomials, norms) if retall else polynomials
52+
53+
54+
def chebyshev_2(
55+
order,
56+
lower=-1,
57+
upper=1,
58+
physicist=False,
59+
normed=False,
60+
retall=False,
61+
):
62+
"""
63+
Chebyshev polynomials of the second kind.
64+
65+
Args:
66+
order (int):
67+
The quadrature order.
68+
lower (float):
69+
Lower bound for the integration interval.
70+
upper (float):
71+
Upper bound for the integration interval.
72+
physicist (bool):
73+
Use physicist weights instead of probabilist.
74+
75+
Returns:
76+
(numpoly.ndpoly, numpy.ndarray):
77+
Chebyshev polynomial expansion. Norms of the orthogonal
78+
expansion on the form ``E(orth**2, dist)``.
79+
80+
Examples:
81+
>>> polynomials, norms = chaospy.expansion.chebyshev_2(4, retall=True)
82+
>>> polynomials
83+
polynomial([1.0, q0, q0**2-0.25, q0**3-0.5*q0, q0**4-0.75*q0**2+0.0625])
84+
>>> norms
85+
array([1. , 0.25 , 0.0625 , 0.015625 , 0.00390625])
86+
>>> chaospy.expansion.chebyshev_2(3, physicist=True)
87+
polynomial([1.0, 2.0*q0, 4.0*q0**2-0.5, 8.0*q0**3-2.0*q0])
88+
>>> chaospy.expansion.chebyshev_2(3, lower=0.5, upper=1.5, normed=True).round(3)
89+
polynomial([1.0, 4.0*q0-4.0, 16.0*q0**2-32.0*q0+15.0,
90+
64.0*q0**3-192.0*q0**2+184.0*q0-56.0])
91+
92+
"""
93+
multiplier = 2 if physicist else 1
94+
_, [polynomials], [norms] = chaospy.recurrence.analytical_stieltjes(
95+
order, chaospy.Beta(1.5, 1.5, lower, upper), multiplier=multiplier)
96+
if normed:
97+
polynomials= chaospy.true_divide(polynomials, numpy.sqrt(norms))
98+
norms[:] = 1.
99+
return (polynomials, norms) if retall else polynomials

chaospy/orthogonal/cholesky.py chaospy/expansion/cholesky.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131

32-
def orth_chol(
32+
def cholesky(
3333
order,
3434
dist,
3535
normed=False,
@@ -66,7 +66,7 @@ def orth_chol(
6666
6767
Examples:
6868
>>> distribution = chaospy.Normal()
69-
>>> expansion, norms = chaospy.orth_chol(3, distribution, retall=True)
69+
>>> expansion, norms = chaospy.expansion.cholesky(3, distribution, retall=True)
7070
>>> expansion.round(4)
7171
polynomial([1.0, q0, q0**2-1.0, q0**3-3.0*q0])
7272
>>> norms

chaospy/orthogonal/frontend.py chaospy/expansion/frontend.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"""Frontend function for generating polynomial expansions."""
2-
from .three_terms_recurrence import orth_ttr
3-
from .cholesky import orth_chol
4-
from .gram_schmidt import orth_gs
5-
from .lagrange import lagrange_polynomial
2+
from .stieltjes import stieltjes
3+
from .cholesky import cholesky
4+
from .gram_schmidt import gram_schmidt
65

76
EXPANSION_NAMES = {
8-
"ttr": "three_terms_recurrence", "three_terms_recurrence": "three_terms_recurrence",
7+
"ttr": "stieltjes", "three_terms_recurrence": "stieltjes", "stieltjes": "stieltjes",
98
"chol": "cholesky", "cholesky": "cholesky",
109
"gs": "gram_schmidt", "gram_schmidt": "gram_schmidt",
1110
}
1211
EXPANSION_FUNCTIONS = {
13-
"three_terms_recurrence": orth_ttr,
14-
"cholesky": orth_chol,
15-
"gram_schmidt": orth_gs,
12+
"stieltjes": stieltjes,
13+
"cholesky": cholesky,
14+
"gram_schmidt": gram_schmidt,
1615
}
1716

1817

chaospy/expansion/gegenbauer.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import numpy
2+
import chaospy
3+
4+
5+
def gegenbauer(
6+
order,
7+
alpha,
8+
lower=-1,
9+
upper=1,
10+
physicist=False,
11+
normed=False,
12+
retall=False,
13+
):
14+
"""
15+
Gegenbauer polynomials.
16+
17+
Args:
18+
order (int):
19+
The polynomial order.
20+
alpha (float):
21+
Gegenbauer shape parameter.
22+
lower (float):
23+
Lower bound for the integration interval.
24+
upper (float):
25+
Upper bound for the integration interval.
26+
physicist (bool):
27+
Use physicist weights instead of probabilist.
28+
29+
Examples:
30+
>>> polynomials, norms = chaospy.expansion.gegenbauer(4, 1, retall=True)
31+
>>> polynomials
32+
polynomial([1.0, q0, q0**2-0.25, q0**3-0.5*q0, q0**4-0.75*q0**2+0.0625])
33+
>>> norms
34+
array([1. , 0.25 , 0.0625 , 0.015625 , 0.00390625])
35+
>>> chaospy.expansion.gegenbauer(3, 1, physicist=True)
36+
polynomial([1.0, 2.0*q0, 4.0*q0**2-0.5, 8.0*q0**3-2.0*q0])
37+
>>> chaospy.expansion.gegenbauer(3, 1, lower=0.5, upper=1.5, normed=True).round(3)
38+
polynomial([1.0, 4.0*q0-4.0, 16.0*q0**2-32.0*q0+15.0,
39+
64.0*q0**3-192.0*q0**2+184.0*q0-56.0])
40+
41+
"""
42+
multiplier = 1
43+
if physicist:
44+
multiplier = numpy.arange(1, order+1)
45+
multiplier = 2*(multiplier+alpha-1)/multiplier
46+
_, [polynomials], [norms] = chaospy.recurrence.analytical_stieltjes(
47+
order, chaospy.Beta(alpha+0.5, alpha+0.5, lower, upper), multiplier=multiplier)
48+
if normed:
49+
polynomials = chaospy.true_divide(polynomials, numpy.sqrt(norms))
50+
norms[:] = 1.
51+
return (polynomials, norms) if retall else polynomials

chaospy/orthogonal/gram_schmidt.py chaospy/expansion/gram_schmidt.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import chaospy
77

88

9-
def orth_gs(order, dist, normed=False, graded=True, reverse=True,
9+
def gram_schmidt(order, dist, normed=False, graded=True, reverse=True,
1010
retall=False, cross_truncation=1., **kws):
1111
"""
1212
Gram-Schmidt process for generating orthogonal polynomials.
@@ -41,12 +41,12 @@ def orth_gs(order, dist, normed=False, graded=True, reverse=True,
4141
4242
Examples:
4343
>>> distribution = chaospy.J(chaospy.Normal(), chaospy.Normal())
44-
>>> polynomials, norms = chaospy.orth_gs(2, distribution, retall=True)
44+
>>> polynomials, norms = chaospy.expansion.gram_schmidt(2, distribution, retall=True)
4545
>>> polynomials.round(10)
4646
polynomial([1.0, q1, q0, q1**2-1.0, q0*q1, q0**2-1.0])
4747
>>> norms.round(10)
4848
array([1., 1., 1., 2., 1., 2.])
49-
>>> polynomials = chaospy.orth_gs(2, distribution, normed=True)
49+
>>> polynomials = chaospy.expansion.gram_schmidt(2, distribution, normed=True)
5050
>>> polynomials.round(3)
5151
polynomial([1.0, q1, q0, 0.707*q1**2-0.707, q0*q1, 0.707*q0**2-0.707])
5252

chaospy/expansion/hermite.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Hermite orthogonal polynomial expansion."""
2+
import numpy
3+
import chaospy
4+
5+
6+
def hermite(
7+
order,
8+
mu=0.,
9+
sigma=1.,
10+
physicist=False,
11+
normed=False,
12+
retall=False,
13+
):
14+
"""
15+
Hermite orthogonal polynomial expansion.
16+
17+
Args:
18+
order (int):
19+
The quadrature order.
20+
mu (float):
21+
Non-centrality parameter.
22+
sigma (float):
23+
Scale parameter.
24+
physicist (bool):
25+
Use physicist weights instead of probabilist variant.
26+
normed (bool):
27+
If True orthonormal polynomials will be used.
28+
retall (bool):
29+
If true return numerical stabilized norms as well. Roughly the same
30+
as ``cp.E(orth**2, dist)``.
31+
32+
Returns:
33+
(numpoly.ndpoly, numpy.ndarray):
34+
Hermite polynomial expansion. Norms of the orthogonal
35+
expansion on the form ``E(orth**2, dist)``.
36+
37+
Examples:
38+
>>> polynomials, norms = chaospy.expansion.hermite(4, retall=True)
39+
>>> polynomials
40+
polynomial([1.0, q0, q0**2-1.0, q0**3-3.0*q0, q0**4-6.0*q0**2+3.0])
41+
>>> norms
42+
array([ 1., 1., 2., 6., 24.])
43+
>>> chaospy.expansion.hermite(3, physicist=True)
44+
polynomial([1.0, 2.0*q0, 4.0*q0**2-2.0, 8.0*q0**3-12.0*q0])
45+
>>> chaospy.expansion.hermite(3, sigma=2.5, normed=True).round(3)
46+
polynomial([1.0, 0.4*q0, 0.113*q0**2-0.707, 0.026*q0**3-0.49*q0])
47+
48+
"""
49+
multiplier = 2 if physicist else 1
50+
_, [polynomials], [norms] = chaospy.recurrence.analytical_stieltjes(
51+
order, chaospy.Normal(mu, sigma), multiplier=multiplier)
52+
if normed:
53+
polynomials = chaospy.true_divide(polynomials, numpy.sqrt(norms))
54+
norms[:] = 1.
55+
return (polynomials, norms) if retall else polynomials

chaospy/expansion/jacobi.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import numpy
2+
import chaospy
3+
4+
5+
def jacobi(
6+
order,
7+
alpha,
8+
beta,
9+
lower=-1,
10+
upper=1,
11+
physicist=False,
12+
normed=False,
13+
retall=False,
14+
):
15+
"""
16+
Jacobi polynomial expansion.
17+
18+
Examples:
19+
>>> polynomials, norms = chaospy.expansion.jacobi(4, 0.5, 0.5, retall=True)
20+
>>> polynomials
21+
polynomial([1.0, q0, q0**2-0.5, q0**3-0.75*q0, q0**4-q0**2+0.125])
22+
>>> norms
23+
array([1. , 0.5 , 0.125 , 0.03125 , 0.0078125])
24+
>>> chaospy.expansion.jacobi(3, 0.5, 0.5, physicist=True).round(4)
25+
polynomial([1.0, 1.5*q0, 2.5*q0**2-0.8333, 4.375*q0**3-2.1146*q0])
26+
>>> chaospy.expansion.jacobi(3, 1.5, 0.5, normed=True)
27+
polynomial([1.0, 2.0*q0, 4.0*q0**2-1.0, 8.0*q0**3-4.0*q0])
28+
29+
"""
30+
multiplier = 1
31+
if physicist:
32+
multiplier = numpy.arange(1, order+1)
33+
multiplier = ((2*multiplier+alpha+beta-1)*(2*multiplier+alpha+beta)/
34+
(2*multiplier*(multiplier+alpha+beta)))
35+
_, [polynomials], [norms] = chaospy.recurrence.analytical_stieltjes(
36+
order, chaospy.Beta(alpha, beta, lower=lower, upper=upper), multiplier=multiplier)
37+
if normed:
38+
polynomials = chaospy.true_divide(polynomials, numpy.sqrt(norms))
39+
norms[:] = 1.
40+
return (polynomials, norms) if retall else polynomials

0 commit comments

Comments
 (0)