Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[30] Rename basis function function interface to vander #32

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/01_hermite_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from matplotlib import pyplot as plt

from robust_fourier import hermite_function_basis
from robust_fourier import hermite_function_vander

plt.style.use(
os.path.join(os.path.dirname(__file__), "../docs/robust_fourier.mplstyle")
Expand Down Expand Up @@ -61,7 +61,7 @@
)

# the Hermite functions are computed and plotted
hermite_basis = hermite_function_basis(
hermite_basis = hermite_function_vander(
x=x_values,
n=ORDERS,
alpha=alpha,
Expand Down
4 changes: 2 additions & 2 deletions examples/02_hermite_function_stability.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from matplotlib import pyplot as plt

from robust_fourier import hermite_function_basis
from robust_fourier import hermite_function_vander

plt.style.use(
os.path.join(os.path.dirname(__file__), "../docs/robust_fourier.mplstyle")
Expand Down Expand Up @@ -46,7 +46,7 @@

# all Hermite basis functions are evaluated ...
x_values = np.linspace(start=X_FROM, stop=X_TO, num=NUM_X)
hermite_basis = hermite_function_basis(
hermite_basis = hermite_function_vander(
x=x_values,
n=max(ORDERS),
alpha=ALPHA,
Expand Down
8 changes: 4 additions & 4 deletions examples/03_hermite_functions_performance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"from matplotlib import pyplot as plt\n",
"\n",
"from robust_fourier import (\n",
" hermite_function_basis,\n",
" hermite_function_vander,\n",
" single_hermite_function,\n",
")\n",
"\n",
Expand Down Expand Up @@ -66,7 +66,7 @@
"\n",
"# the Numba-based version of the Hermite functions is pre-compiled to avoid the\n",
"# compilation overhead in the performance comparison\n",
"_ = hermite_function_basis(\n",
"_ = hermite_function_vander(\n",
" x=np.linspace(start=X_FROM, stop=X_TO, num=NUM_X),\n",
" n=ORDERS[0],\n",
" alpha=ALPHA,\n",
Expand All @@ -92,13 +92,13 @@
"perfplot_kwargs = dict(\n",
" setup=lambda n: (np.linspace(start=X_FROM, stop=X_TO, num=NUM_X), n, ALPHA),\n",
" kernels=[\n",
" lambda x, n, alpha: hermite_function_basis(\n",
" lambda x, n, alpha: hermite_function_vander(\n",
" x=x,\n",
" n=n,\n",
" alpha=alpha,\n",
" jit=False,\n",
" ),\n",
" lambda x, n, alpha: hermite_function_basis(\n",
" lambda x, n, alpha: hermite_function_vander(\n",
" x=x,\n",
" n=n,\n",
" alpha=alpha,\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/05_chebyshev_polynomials.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from matplotlib import pyplot as plt

from robust_fourier import chebyshev_poly_basis
from robust_fourier import chebyshev_polyvander

plt.style.use(
os.path.join(os.path.dirname(__file__), "../docs/robust_fourier.mplstyle")
Expand Down Expand Up @@ -69,7 +69,7 @@

# the Chebyshev polynomials are computed and plotted
x_values = np.linspace(start=mu - alpha, stop=mu + alpha, num=NUM_X)
chebyshev_basis = chebyshev_poly_basis(
chebyshev_basis = chebyshev_polyvander(
x=x_values,
n=ORDERS,
alpha=alpha,
Expand Down
8 changes: 4 additions & 4 deletions examples/06_chebyshev_polynomials_performance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"from matplotlib import pyplot as plt\n",
"\n",
"from robust_fourier import (\n",
" chebyshev_poly_basis,\n",
" chebyshev_polyvander,\n",
")\n",
"\n",
"%matplotlib widget"
Expand Down Expand Up @@ -62,7 +62,7 @@
"\n",
"# the Numba-based version of the Chebyshev polynomials is pre-compiled to avoid the\n",
"# compilation overhead in the performance comparison\n",
"_ = chebyshev_poly_basis(\n",
"_ = chebyshev_polyvander(\n",
" x=np.linspace(start=X_FROM, stop=X_TO, num=NUM_X),\n",
" n=ORDERS[0],\n",
" alpha=ALPHA,\n",
Expand All @@ -89,14 +89,14 @@
"perfplot_kwargs = dict(\n",
" setup=lambda n: (np.linspace(start=X_FROM, stop=X_TO, num=NUM_X), n, ALPHA),\n",
" kernels=[\n",
" lambda x, n, alpha: chebyshev_poly_basis(\n",
" lambda x, n, alpha: chebyshev_polyvander(\n",
" x=x,\n",
" n=n,\n",
" alpha=alpha,\n",
" kind=1,\n",
" jit=False,\n",
" ),\n",
" lambda x, n, alpha: chebyshev_poly_basis(\n",
" lambda x, n, alpha: chebyshev_polyvander(\n",
" x=x,\n",
" n=n,\n",
" alpha=alpha,\n",
Expand Down
4 changes: 2 additions & 2 deletions src/robust_fourier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

from .chebyshev_polynomials import ( # noqa: F401
ChebyshevPolynomialBasis,
chebyshev_poly_basis,
chebyshev_polyvander,
)
from .hermite_functions import ( # noqa: F401
HermiteFunctionBasis,
approximate_hermite_funcs_fadeout_x,
approximate_hermite_funcs_largest_extrema_x,
approximate_hermite_funcs_largest_zeros_x,
hermite_function_basis,
hermite_function_vander,
single_hermite_function,
)

Expand Down
2 changes: 1 addition & 1 deletion src/robust_fourier/chebyshev_polynomials/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# === Imports ===

from ._class_interface import ChebyshevPolynomialBasis # noqa: F401
from ._func_interface import chebyshev_poly_basis # noqa: F401
from ._func_interface import chebyshev_polyvander # noqa: F401
4 changes: 2 additions & 2 deletions src/robust_fourier/chebyshev_polynomials/_class_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class :class:`ChebyshevPolynomialBasis`.
get_validated_offset_along_axis,
get_validated_order,
)
from ._func_interface import chebyshev_poly_basis, get_validated_chebyshev_kind
from ._func_interface import chebyshev_polyvander, get_validated_chebyshev_kind

# === Classes ===

Expand Down Expand Up @@ -255,7 +255,7 @@ def eval(

""" # noqa: E501

return chebyshev_poly_basis(
return chebyshev_polyvander(
x=x,
n=n,
alpha=alpha,
Expand Down
29 changes: 16 additions & 13 deletions src/robust_fourier/chebyshev_polynomials/_func_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
get_validated_x_values,
normalise_x_values,
)
from ._numba_funcs import nb_chebyshev_poly_bases as _nb_chebyshev_poly_bases
from ._numpy_funcs import _chebyshev_poly_bases as _np_chebyshev_poly_bases
from ._numba_funcs import nb_chebyshev_polyvander as _nb_chebyshev_polyvander
from ._numpy_funcs import _chebyshev_polyvander as _np_chebyshev_polyvander

# === Constants ===

Expand Down Expand Up @@ -107,7 +107,7 @@ def get_validated_chebyshev_kind(


@overload
def chebyshev_poly_basis(
def chebyshev_polyvander(
x: Union[RealScalar, ArrayLike],
n: IntScalar,
alpha: RealScalar = 1.0,
Expand All @@ -120,7 +120,7 @@ def chebyshev_poly_basis(


@overload
def chebyshev_poly_basis(
def chebyshev_polyvander(
x: Union[RealScalar, ArrayLike],
n: IntScalar,
alpha: RealScalar = 1.0,
Expand All @@ -132,7 +132,7 @@ def chebyshev_poly_basis(
) -> Tuple[NDArray[np.float64], NDArray[np.float64]]: ...


def chebyshev_poly_basis(
def chebyshev_polyvander(
x: Union[RealScalar, ArrayLike],
n: IntScalar,
alpha: RealScalar = 1.0,
Expand All @@ -143,9 +143,10 @@ def chebyshev_poly_basis(
validate_parameters: bool = True,
) -> Union[NDArray[np.float64], Tuple[NDArray[np.float64], NDArray[np.float64]]]:
"""
Computes the basis of dilated Chebyshev polynomials up to order ``n`` for the given
points ``x``. It makes use of a combined recursion formula that links the first and
second kind of Chebyshev polynomials to evaluate them in a numerically stable way.
Computes the basis (Vandermonde matrix) of dilated Chebyshev polynomials up to order
``n`` for the given points ``x``. It makes use of a combined recursion formula that
links the first and second kind of Chebyshev polynomials to evaluate them in a
numerically stable way.

Parameters
----------
Expand Down Expand Up @@ -189,14 +190,16 @@ def chebyshev_poly_basis(

Returns
-------
chebyshev_t1_basis : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the first kind at the points ``x``.
chebyshev_t1_vander : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the first kind evaluated at the
points ``x`` represented as a Vandermonde matrix.
It will always be 2D even if ``x`` is a scalar.
It is only returned if ``kind`` is ``1``, ``"first"``, ``"both"`` (for the
latter, it is the first element of the tuple).

chebyshev_u2_basis : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the second kind at the points ``x``.
chebyshev_u2_vander : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the second kind evaluated at the
points ``x`` represented as a Vandermonde matrix.
It will always be 2D even if ``x`` is a scalar.
It is only returned if ``kind`` is ``2``, ``"second"``, ``"both"`` (for the
latter, it is the second element of the tuple).
Expand Down Expand Up @@ -272,7 +275,7 @@ def chebyshev_poly_basis(
# if requested, the Numba-accelerated implementation is used
# NOTE: this does not have to necessarily involve Numba because it can also be
# the NumPy-based implementation under the hood
func = _nb_chebyshev_poly_bases if jit else _np_chebyshev_poly_bases
func = _nb_chebyshev_polyvander if jit else _np_chebyshev_polyvander
chebyshev_bases = func( # type: ignore
x=x_internal, # type: ignore
n=n, # type: ignore
Expand Down
8 changes: 4 additions & 4 deletions src/robust_fourier/chebyshev_polynomials/_numba_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# === Imports ===

from .._utils._numba_helpers import do_numba_normal_jit_action
from ._numpy_funcs import _chebyshev_poly_bases
from ._numpy_funcs import _chebyshev_polyvander

# === Functions ===

Expand All @@ -30,14 +30,14 @@
from .._utils import no_jit as jit

# if it is enabled, the functions are compiled
nb_chebyshev_poly_bases = jit(
nb_chebyshev_polyvander = jit(
nopython=True,
cache=True,
)(_chebyshev_poly_bases)
)(_chebyshev_polyvander)


# otherwise, the NumPy-based implementation of the Hermite functions is declared as the
# Numba-based implementation
except ImportError: # pragma: no cover

nb_chebyshev_poly_bases = _chebyshev_poly_bases
nb_chebyshev_polyvander = _chebyshev_polyvander
28 changes: 15 additions & 13 deletions src/robust_fourier/chebyshev_polynomials/_numpy_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
# === Functions ===


def _chebyshev_poly_bases(
def _chebyshev_polyvander(
x: np.ndarray,
n: int,
) -> Tuple[np.ndarray, np.ndarray]:
"""
Simultaneously evaluates the complete basis of Chebyshev polynomials of the first
and second kind by making use of a numerically stabilised combined recurrence
relation.
Simultaneously evaluates the complete basis (Vandermonde matrix) of Chebyshev
polynomials of the first and second kind by making use of a numerically stabilised
combined recurrence relation.

Parameters
----------
Expand All @@ -33,11 +33,13 @@ def _chebyshev_poly_bases(

Returns
-------
chebyshev_t1_basis : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the first kind.
chebyshev_t1_vander : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the first kind evaluated at the
points ``x`` represented as a Vandermonde matrix.

chebyshev_u2_basis : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the second kind.
chebyshev_u2_vander : :class:`numpy.ndarray` of shape (m, n + 1)
The values of the Chebyshev polynomials of the second kind evaluated at the
points ``x`` represented as a Vandermonde matrix.

References
----------
Expand Down Expand Up @@ -68,8 +70,8 @@ def _chebyshev_poly_bases(
# the combined recurrence relation is started with the initial value
# - 1 for the Chebyshev polynomial of the first kind T_0(x)
# - 0 for the Chebyshev polynomial of the second kind U_{-1}(x)
chebyshev_t1_basis = np.empty(shape=(n + 1, x.size))
chebyshev_u2_basis = np.empty_like(chebyshev_t1_basis)
chebyshev_t1_vander = np.empty(shape=(n + 1, x.size))
chebyshev_u2_vander = np.empty_like(chebyshev_t1_vander)
t_i_minus_1 = np.ones_like(x)
u_i_minus_2 = np.zeros_like(x)
one_minus_x_squared = 1.0 - (x * x)
Expand All @@ -79,10 +81,10 @@ def _chebyshev_poly_bases(
# T_{n}(x) = x * T_{n-1}(x) - (1 - x * x) * U_{n-2}(x)
# U_{n-1}(x) = x * U_{n-2}(x) + T_{n-1}(x)
for iter_j in range(0, n + 1):
chebyshev_t1_basis[iter_j] = t_i_minus_1 # NOTE: is not a view
chebyshev_t1_vander[iter_j] = t_i_minus_1 # NOTE: is not a view
u_i_minus_1 = x * u_i_minus_2 + t_i_minus_1
chebyshev_u2_basis[iter_j] = u_i_minus_1 # NOTE: is not a view
chebyshev_u2_vander[iter_j] = u_i_minus_1 # NOTE: is not a view
t_i_minus_1 = x * t_i_minus_1 - one_minus_x_squared * u_i_minus_2
u_i_minus_2 = u_i_minus_1

return chebyshev_t1_basis, chebyshev_u2_basis
return chebyshev_t1_vander, chebyshev_u2_vander
2 changes: 1 addition & 1 deletion src/robust_fourier/hermite_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
)
from ._class_interface import HermiteFunctionBasis # noqa: F401
from ._func_interface import ( # noqa: F401
hermite_function_basis,
hermite_function_vander,
single_hermite_function,
)
4 changes: 2 additions & 2 deletions src/robust_fourier/hermite_functions/_class_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
get_validated_offset_along_axis,
get_validated_order,
)
from ._func_interface import hermite_function_basis
from ._func_interface import hermite_function_vander

# === Classes ===

Expand Down Expand Up @@ -221,7 +221,7 @@ def eval(

""" # noqa: E501

return hermite_function_basis(
return hermite_function_vander(
x=x,
n=n,
alpha=alpha,
Expand Down
Loading