cristal.helper_classes.polynomial_basis module#

Polynomials basis classes and generator for polynomial combinations.

class cristal.helper_classes.polynomial_basis.ChebyshevT1Basis#

Bases: BasePolynomialBasis

Class for generating first order Chebyshev polynomials as a polynomial basis. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight. 1, x, 2x^2 - 1, …, T_n(x) = cos(n * arccos(x))

static func(x, monomials_matrix)#

Compute the first order Chebyshev polynomials. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • monomials_matrix (np.ndarray (s(n), d)) – The matrix of monomials. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.

Returns:

The computed Chebyshev T_1 polynomials.

Return type:

np.ndarray (s(n), d)

class cristal.helper_classes.polynomial_basis.ChebyshevT2Basis#

Bases: BasePolynomialBasis

Class for generating second order Chebyshev polynomials as a polynomial basis. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight. 1, 2x, 4x^2 - 1, …, U_n(x) = sin((n + 1) * arccos(x)) / sin(arccos(x)) U_n(x) = sum_{k=0}^{floor(n/2)} (-1)^k * C(n-k, k) * (2x)^{n-2k}

static chebyshev_t_2(x, n)#

Compute the Chebyshev T_2 polynomial. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight.

Return type:

int

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • n (int) – The degree of the polynomial.

Returns:

The computed Chebyshev T_2 polynomial.

Return type:

int

static func(x, monomials_matrix)#

Compute the Chebyshev T_2 polynomials. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • monomials_matrix (np.ndarray (s(n), d)) – The matrix of monomials. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.

Returns:

The computed Chebyshev T_2 polynomials.

Return type:

np.ndarray (s(n), d)

class cristal.helper_classes.polynomial_basis.ChebyshevUBasis#

Bases: BasePolynomialBasis

Class for generating Chebyshev U polynomials as a polynomial basis. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight. 1, 2x, 4x^2 - 1, …, U_n(x) = sin((n + 1) * arccos(x)) / sin(arccos(x)) U_n(x) = sum_{k=0}^{floor(n/2)} (-1)^k * C(n-k, k) * (2x)^{n-2k}

static chebyshev_u(x, n)#

Compute the Chebyshev U_n polynomial. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight.

Return type:

int

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • n (int) – The degree of the polynomial.

Returns:

The computed Chebyshev U_n polynomial.

Return type:

int

static func(x, monomials_matrix)#

Compute the Chebyshev U polynomials. Orthonormal on [-1, 1] with respect to the Lebesgue measure with 1 / sqrt(1 - x**2) as weight.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • monomials_matrix (np.ndarray (s(n), d)) – The matrix of monomials. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.

Returns:

The computed Chebyshev U polynomials.

Return type:

np.ndarray (s(n), d)

class cristal.helper_classes.polynomial_basis.IMPLEMENTED_POLYNOMIAL_BASIS(*values)#

Bases: Enum

The implemented polynomial basis classes.

CHEBYSHEV_T_1 = <class 'cristal.helper_classes.polynomial_basis.ChebyshevT1Basis'>#
CHEBYSHEV_T_2 = <class 'cristal.helper_classes.polynomial_basis.ChebyshevT2Basis'>#
CHEBYSHEV_U = <class 'cristal.helper_classes.polynomial_basis.ChebyshevUBasis'>#
LEGENDRE = <class 'cristal.helper_classes.polynomial_basis.LegendreBasis'>#
MONOMIALS = <class 'cristal.helper_classes.polynomial_basis.MonomialsBasis'>#
class cristal.helper_classes.polynomial_basis.LegendreBasis#

Bases: BasePolynomialBasis

Class for generating Legendre polynomials as a polynomial basis. Orthonormal on [-1, 1] with respect to the Lebesgue measure. 1, x, (3x^2 - 1) / 2, …, P_n(x) = (1 / 2^n) * sum_{k=0}^{floor(n/2)} C(n, k) * (2x)^{n-2k}

static func(x, monomials_matrix)#

Compute the Legendre polynomials. Orthonormal on [-1, 1] with respect to the Lebesgue measure.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • monomials_matrix (np.ndarray (s(n), d)) – The matrix of monomials. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.

Returns:

The computed Legendre polynomials.

Return type:

np.ndarray (s(n), d)

static legendre(x, n)#

Compute the Legendre polynomial. Orthonormal on [-1, 1] with respect to the Lebesgue measure.

Return type:

int

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • n (int) – The degree of the polynomial.

Returns:

The computed Legendre polynomial.

Return type:

int

class cristal.helper_classes.polynomial_basis.MonomialsBasis#

Bases: BasePolynomialBasis

Class for generating monomials as a polynomial basis. 1, x, x^2, …, x^n

static func(x, monomials_matrix)#

Compute the monomials of the input data.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data.

  • monomials_matrix (np.ndarray (s(n), d)) – The monomials matrix. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.

Returns:

The computed monomials.

Return type:

np.ndarray (s(n), d)

class cristal.helper_classes.polynomial_basis.PolynomialsBasisGenerator#

Bases: object

Class for generating polynomial combinations and applying polynomial basis functions.

static apply_combinations(x, monomials_matrix, basis_class)#

Applies the polynomial basis to the input data.

Return type:

ndarray

Parameters:
  • x (np.ndarray (1, d) or (d,)) – The input data to transform.

  • monomials_matrix (np.ndarray (s(n), d)) – The monomials matrix generated by generate_combinations().

  • basis_class (type[BasePolynomialBasis]) – The basis class to apply.

Returns:

The transformed data.

Return type:

np.ndarray (s(n), 1)

static generate_combinations(max_degree, dimensions)#

Generate all combinations of monomials of a given degree and dimensions.

Return type:

ndarray

Parameters:
  • max_degree (int) – The maximum degree of the monomials.

  • dimensions (int) – The number of dimensions.

Returns:

An array of shape (s(n), d) containing the combinations of monomials, where s(n) is the number of monomials and d is the number of dimensions.

Return type:

np.ndarray (s(n), d)

static make_design_matrix(x, monomials_matrix, basis_class, allow_parallelization=False)#

Compute the design matrix for the given data points and monomials. If s(n) > 500 and N > 1000, it will use a parallelized approach to compute the design matrix.

Return type:

ndarray

Parameters:
  • x (np.ndarray (N, d)) – The input data to transform.

  • monomials_matrix (np.ndarray (s(n), d)) – The monomials matrix generated by generate_combinations().

  • basis_class (type[BasePolynomialBasis]) – The basis class to use for the transformation.

  • allow_parallelization (bool, optional) – Whether to allow parallelization, by default False

Returns:

The design matrix for the given data points and monomials.

Return type:

np.ndarray (N, s(n))