newton_polynomial#

This module contains the NewtonPolynomial class.

The NewtonPolynomial class is a concrete implementation of the abstract base class MultivariatePolynomialSingleABC for polynomials in the Newton basis.

Background information#

The relevant section of the documentation on Newton basis contains a more detailed explanation regarding the polynomials in the Newton basis.

Implementation details#

NewtonPolynomial is currently the preferred polynomial basis in Minterpy for various operations. An instance of NewtonPolynomial can be evaluated on a set of query points (unlike LagrangePolynomial) and the evaluation is numerically stable (unlike CanonicalPolynomial).

A wide range of arithmetic operations are supported for polynomials in the Lagrange basis, namely:

  • addition and subtraction by a scalar number

  • addition and subtraction by another Newton polynomial

  • multiplication by a scalar number

  • multiplication by another Newton polynomial

Moreover, basic calculus operations are also supported, namely: differentiation and definite integration.

class minterpy.polynomials.newton_polynomial.NewtonPolynomial(multi_index, coeffs=None, internal_domain=None, user_domain=None, grid=None)[source]#

Bases: MultivariatePolynomialSingleABC

Concrete implementations of polynomials in the Newton basis.

For a definition of the Newton basis, see Newton basis.

Properties

coeffs

The coefficients of the polynomial(s).

num_active_monomials

The number of active monomials of the polynomial(s).

spatial_dimension

Spatial dimension.

unisolvent_nodes

Unisolvent nodes the polynomial(s) is(are) defined on.

Methods

add_points(exponents)

Extend grid and multi_index

diff(order, **kwargs)

Return the partial derivative poly.

expand_dim(target_dimension[, ...])

Expand the spatial dimension of the polynomial instance.

from_degree(spatial_dimension, poly_degree, ...)

Initialise Polynomial from given coefficients and the default construction for given polynomial degree, spatial dimension and \(l_p\) degree.

from_grid(grid[, coeffs, internal_domain, ...])

Create an instance of polynomial with a Grid instance.

from_poly(polynomial[, new_coeffs])

constructs a new polynomial instance based on the properties of an input polynomial

generate_internal_domain(domain, ...)

Building and verification of domains.

generate_user_domain(domain, spatial_dimension)

Building and verification of domains.

has_matching_dimension(other)

Return True if the polynomials have matching dimensions.

has_matching_domain(other[, tol])

Return True if the polynomials have matching domains.

integrate_over([bounds])

Compute the definite integral of the polynomial over the bounds.

make_complete()

returns a possibly new polynomial instance with a complete multi index set.

partial_diff(dim[, order])

Return the partial derivative poly.

Parameters:
static _eval(poly, xx)#

Evaluate polynomial(s) in the Newton basis on a set of query points.

This is a wrapper for the evaluation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in the Newton basis to evaluate.

  • xx (numpy.ndarray) – Array of query points in the at which the polynomial(s) is evaluated. The array is of shape (N, m) where N is the number of points and m is the spatial dimension of the polynomial.

Return type:

ndarray

See also

minterpy.utils.polynomials.newton.eval_newton_polynomials

The actual implementation of the evaluation of polynomials in the Newton basis.

static _add(poly_1, poly_2)#

Add two instances of polynomials in the Newton basis.

This is the concrete implementation of _add() method in the MultivariatePolynomialSingleABC abstract class specifically for handling polynomials in the Newton basis.

Parameters:
  • poly_1 (NewtonPolynomial) – Left operand of the addition/subtraction expression.

  • poly_2 (NewtonPolynomial) – Right operand of the addition/subtraction expression.

Returns:

The sum of two polynomials in the Newton basis as a new instance of polynomial in the Newton basis.

Return type:

NewtonPolynomial

Notes

  • This function assumes: both polynomials must be in the Newton basis, they must be initialized (coefficients are not None), have the same dimension and their domains are matching, and the number of polynomials per instance are the same. These conditions are not explicitly checked in this function; the caller is responsible for the verification.

static _sub(*args, **kwargs)#

A placeholder function to indicate a feature that is not supported.

Warning

This feature is not implemented yet!

Raises:

NotImplementedError – Any time this function or method is called.

Return type:

None

static _mul(poly_1, poly_2)#

Multiply instances of polynomials in the Newton basis.

This is the concrete implementation of _mul() method in the MultivariatePolynomialSingleABC abstract class specifically for handling polynomials in the Newton basis.

Parameters:
Returns:

The product of two polynomials in the Newton basis as a new instance of polynomial in the Newton basis.

Return type:

NewtonPolynomial

Notes

  • This function assumes: both polynomials must be in the Newton basis, they must be initialized (coefficients are not None), have the same dimension and their domains are matching, and the number of polynomials per instance are the same. These conditions are not explicitly checked in this function; the caller is responsible for the verification.

static _div(*args, **kwargs)#

A placeholder function to indicate a feature that is not supported.

Warning

This feature is not implemented yet!

Raises:

NotImplementedError – Any time this function or method is called.

Return type:

None

static _pow(*args, **kwargs)#

A placeholder function to indicate a feature that is not supported.

Warning

This feature is not implemented yet!

Raises:

NotImplementedError – Any time this function or method is called.

Return type:

None

static _scalar_add(poly, scalar)#

Add an instance of polynomial with a real scalar based on the monomial.

Monomial-based scalar addition add the scalar to the polynomial coefficient that corresponds to the multi-index set element of \((0, \ldots, 0)\) (if exists). If the element does not exist, meaning that the polynomial does not have a constant term, then the multi-index set is extended.

Parameters:
  • poly (MultivariatePolynomialSingleABC) – A polynomial instance to be added with a scalar.

  • scalar (SCALAR) – The real scalar number to be added to the polynomial instance.

Returns:

The summed polynomial; the polynomial is a new instance.

Return type:

MultivariatePolynomialSingleABC

Notes

  • Currently NewtonPolynomial, CanonicalPolynomial, and ChebyshevPolynomial follow monomial-based scalar addition, while LagrangePolynomial does not.

static _partial_diff(poly, dim, order, *, backend='numba')#

Differentiate polynomial(s) in the Newton basis with respect to a given dimension and order of derivative.

This is a wrapper for the partial differentiation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in Newton form to differentiate.

  • dim (int) – Spatial dimension with respect to which the differentiation is taken. The dimension starts at 0 (i.e., the first dimension).

  • order (int) – Order of partial derivative.

  • backend (str) –

    Computational backend to carry out the differentiation. Supported values are:

    • "numpy": implementation based on NumPy; not performant, only applicable for a very small problem size (small degree, low dimension).

    • "numba" (default): implementation based on compiled code with the help of Numba; applicable up to moderate problem size.

    • "numba-par": parallelized (CPU) implementation based on compiled code with the help of Numba for relatively large problem sizes.

Returns:

A new instance of NewtonPolynomial that represents the partial derivative of the original polynomial of the given order of derivative with respect to the specified dimension.

Return type:

NewtonPolynomial

Notes

  • The abstract class is responsible to validate dim and order; no additional validation regarding those two parameters are required here.

See also

NewtonPolynomial.partial_diff

The public method to differentiate the polynomial instance of a specified order of derivative with respect to a given dimension.

NewtonPolynomial.diff

The public method to differentiate the polynomial instance of the given orders of derivative along each dimension.

static _diff(poly, order, *, backend='numba')#

Differentiate polynomial(s) in the Newton basis of specified orders of derivatives along each dimension.

The orders must be specified for each dimension. This is a wrapper for the differentiation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in Newton form to differentiate.

  • order (numpy.ndarray) – A one-dimensional integer array specifying the orders of derivative along each dimension. The length of the array must be m where m is the spatial dimension of the polynomial.

  • backend (str) –

    Computational backend to carry out the differentiation. Supported values are:

    • "numpy": implementation based on NumPy; not performant, only applicable for a very small problem size (small degree, low dimension).

    • "numba" (default): implementation based on compiled code with the help of Numba; for up to moderate problem size.

    • "numba-par": parallelized (CPU) implementation based compiled code with the help of Numba for relatively large problem sizes.

Returns:

A new instance of NewtonPolynomial that represents the partial derivative of the original polynomial of the given order of derivative with respect to the specified dimension.

Return type:

NewtonPolynomial

Notes

  • The abstract class is responsible to validate order; no additional validation regarding that parameter is required here.

  • The transformation of computed Lagrange coefficients of the differentiated polynomial to the Newton coefficients is carried out using multivariate divided-difference scheme (DDS).

See also

NewtonPolynomial.diff

The public method to differentiate the polynomial instance of the given orders of derivative along each dimension.

NewtonPolynomial.partial_diff

The public method to differentiate the polynomial instance of a specified order of derivative with respect to a given dimension.

static _integrate_over(poly, bounds)#

Compute the definite integral of polynomial(s) in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The polynomial of which the integration is carried out.

  • bounds (numpy.ndarray) – The bounds (lower and upper, respectively) of the definite integration, specified as an (M,2) array, where M is the spatial dimension of the polynomial.

Returns:

The integral value of the polynomial over the given domain.

Return type:

numpy.ndarray

static generate_internal_domain(domain, spatial_dimension)#

Building and verification of domains.

This function builds a suitable domain as the cartesian product of a one- dimensional domain, or verifies the domain shape, of a multivariate domain is passed. If None is passed, the default domain is build from [-1,1].

Parameters:
  • domain (array_like, None) – Either one-dimensional domain (min,max), or a stack of domains for each domain with shape (spatial_dimension,2). If None is passed, the DEFAULT_DOMAIN is repeated for each spatial dimentsion.

  • spatial_dimension (int) – Dimentsion of the domain space.

Return verified_domain:

Stack of domains for each dimension with shape (spatial_dimension,2).

Return type:

np.ndarray

Raises:

ValueError – If no domain with the expected shape can be constructed from the input.

static generate_user_domain(domain, spatial_dimension)#

Building and verification of domains.

This function builds a suitable domain as the cartesian product of a one- dimensional domain, or verifies the domain shape, of a multivariate domain is passed. If None is passed, the default domain is build from [-1,1].

Parameters:
  • domain (array_like, None) – Either one-dimensional domain (min,max), or a stack of domains for each domain with shape (spatial_dimension,2). If None is passed, the DEFAULT_DOMAIN is repeated for each spatial dimentsion.

  • spatial_dimension (int) – Dimentsion of the domain space.

Return verified_domain:

Stack of domains for each dimension with shape (spatial_dimension,2).

Return type:

np.ndarray

Raises:

ValueError – If no domain with the expected shape can be constructed from the input.

__add__(other)#

Add the polynomial(s) with another polynomial(s) or a real scalar.

This function is called when:

  • two polynomials are added: P1 + P2, where P1 (i.e., self) and P2 (other) are both instances of a concrete polynomial class.

  • a polynomial is added with a real scalar number: P1 + a, where a (other) is a real scalar number.

Polynomials are closed under scalar addition, meaning that the result of the addition is also a polynomial with the same underlying multi-index set; only the coefficients are altered.

Parameters:

other (Union[MultivariatePolynomialSingleABC, SCALAR]) – The right operand, either an instance of polynomial (of the same concrete class as the right operand) or a real scalar number.

Returns:

The result of the addition, an instance of summed polynomial.

Return type:

MultivariatePolynomialSingleABC

Notes

  • The concrete implementation of polynomial-polynomial and polynomial- scalar addition is delegated to the respective polynomial concrete class.

__call__(xx, **kwargs)#

Evaluate the polynomial on a set of query points.

The function is called when an instance of a polynomial is called with a set of query points, i.e., \(p(\mathbf{X})\) where \(\mathbf{X}\) is a matrix of values with \(k\) rows and each row is of length \(m\) (i.e., a point in \(m\)-dimensional space).

Parameters:
  • xx (numpy.ndarray) – The set of query points to evaluate as a two-dimensional array of shape (k, m) where k is the number of query points and m is the spatial dimension of the polynomial.

  • **kwargs – Additional keyword-only arguments that change the behavior of the underlying evaluation (see the concrete implementation).

Returns:

The values of the polynomial evaluated at query points.

  • If there is only a single polynomial (i.e., a single set of coefficients), then a one-dimensional array of length k is returned.

  • If there are multiple polynomials (i.e., multiple sets of coefficients), then a two-dimensional array of shape (k, np) is returned where np is the number of coefficient sets.

Return type:

numpy.ndarray

Notes

  • The function calls the concrete implementation of the static method _eval().

See also

_eval

The underlying static method to evaluate the polynomial(s) instance on a set of query points.

__copy__()#

Creates of a shallow copy.

This function is called, if one uses the top-level function copy() on an instance of this class.

Returns:

The copy of the current instance.

Return type:

MultivariatePolynomialSingleABC

See also

copy.copy

copy operator form the python standard library.

__deepcopy__(mem)#

Creates of a deepcopy.

This function is called, if one uses the top-level function deepcopy() on an instance of this class.

Returns:

The deepcopy of the current instance.

Return type:

MultivariatePolynomialSingleABC

See also

copy.deepcopy

copy operator form the python standard library.

__eq__(other)#

Compare two concrete polynomial instances for exact equality.

Two polynomial instances are equal if and only if:

  • both are of the same concrete class, and

  • the underlying multi-index sets are equal, and

  • the underlying grid instances are equal, and

  • the coefficients of the polynomials are equal.

Parameters:

other (MultivariatePolynomialSingleABC) – Another instance of concrete implementation of MultivariatePolynomialSingleABC to compare with

Returns:

True if the current instance is equal to the other instance, False otherwise.

Return type:

bool

__floordiv__(other)#

Divide an instance of polynomial with a real scalar number (//).

Parameters:

other (Union[MultivariatePolynomialSingleABC, SCALAR]) – The right operand of the (floor) division expression, a real scalar number.

Returns:

An instance of polynomial, the result of (floor) scalar division of a polynomial.

Return type:

MultivariatePolynomialSingleABC

__hash__ = None#
__init__(multi_index, coeffs=None, internal_domain=None, user_domain=None, grid=None)#
Parameters:
__len__()#

Return the number of polynomials in the instance.

Returns:

The number of polynomial in the instance. A single instance of polynomial may contain multiple polynomials with different coefficient values but sharing the same underlying multi-index set and grid.

Return type:

int

__mul__(other)#

Multiply the polynomial(s) with another polynomial or a real scalar.

This function is called when:

  • two polynomials are multiplied: P1 * P2, where P1 and P2 are both instances of a concrete polynomial class.

  • a polynomial is multiplied with a real scalar number: P1 * a, where a is a real scalar number.

Polynomials are closed under scalar multiplication, meaning that the result of the multiplication is also a polynomial with the same underlying multi-index set; only the coefficients are altered.

Parameters:

other (Union[MultivariatePolynomialSingleABC, SCALAR]) – The right operand, either an instance of polynomial (of the same concrete class as the right operand) or a real scalar number.

Returns:

The result of the multiplication, an instance of multiplied polynomial.

Return type:

MultivariatePolynomialSingleABC

Notes

  • The concrete implementation of polynomial-polynomial multiplication is delegated to the respective polynomial concrete class.

__neg__()#

Negate the polynomial(s) instance.

This function is called when a polynomial is negated via the - operator, e.g., -P.

Returns:

New polynomial(s) instance with negated coefficients.

Return type:

MultivariatePolynomialSingleABC

Notes

  • The resulting polynomial is a deep copy of the original polynomial.

  • -P is not the same as -1 * P, the latter of which is a scalar multiplication. In this case, however, the result is the same; it returns a new instance with negated coefficients.

__pos__()#

Plus sign the polynomial(s) instance.

This function is called when a polynomial is plus signed via the + operator, e.g., +P.

Returns:

The same polynomial

Return type:

MultivariatePolynomialSingleABC

Notes

  • +P is not the same as 1 * P, the latter of which is a scalar multiplication. In this case, the result actually differs because the scalar multiplication 1 * P returns a new instance of polynomial even though the coefficients are not altered.

__pow__(power)#

Take the polynomial instance to the given power.

Parameters:

power (int) – The power in the exponentiation expression; the value must be a non-negative real scalar whole number. The value may not strictly be an integer as long as it is a whole number (e.g., \(2.0\) is acceptable).

Returns:

The result of exponentiation, an instance of a concrete polynomial class.

Return type:

MultivariatePolynomialSingleABC

Notes

  • Exponentiation by zero returns a constant polynomial whose coefficients are zero except for the constant term with respect to the multi-index set which is given a value of \(1.0\). In the case of polynomials in the Lagrange basis whose no constant term with respect to the multi-index set, all coefficients are set to \(1.0\).

__radd__(other)#

Right-sided addition of the polynomial(s) with a real scalar number.

This function is called for the expression a + P where a and P is a real scalar number and an instance of polynomial, respectively.

Parameters:

other (SCALAR) – A real scalar number (the left operand) to be added to the polynomial.

Returns:

The result of adding the scalar value to the polynomial.

Return type:

MultivariatePolynomialSingleABC

Notes

  • If the left operand is not a real scalar number, the right-sided addition is not explicitly supported, and it will rely on the __add__() method of the left operand.

__rmul__(other)#

Right sided multiplication of the polynomial(s) with a real scalar.

This function is called if a real scalar number is multiplied with a polynomial like a * P where a and P are a scalar and a polynomial instance, respectively.

Parameters:

other (SCALAR) – The left operand, a real scalar number.

Returns:

The result of the multiplication, an instance of multiplied polynomial.

Return type:

MultivariatePolynomialSingleABC

__rsub__(other)#

Right-sided subtraction of the polynomial(s) with a real scalar.

This function is called for the expression a - P where a and P is a real scalar number and an instance of polynomial, respectively.

Parameters:

other (SCALAR) – A real scalar number (the left operand) to be substracted by the polynomial.

Returns:

The result of subtracting a scalar value by the polynomial.

Return type:

MultivariatePolynomialSingleABC

Notes

  • If the left operand is not a real scalar number, the right-sided subtraction is not explicitly supported, and it will rely on the __add__() method of the left operand.

  • This operation relies on the negation of a polynomial and scalar addition

__sub__(other)#

Subtract the polynomial(s) with another poly. or a real scalar.

This function is called when:

  • two polynomials are subtracted: P1 - P2, where P1 and P2 are both instances of a concrete polynomial class.

  • a polynomial is added with a real scalar number: P1 - a, where a is a real scalar number.

Polynomials are closed under scalar subtraction, meaning that the result of the subtraction is also a polynomial with the same underlying multi-index set; only the coefficients are altered.

Parameters:

other (Union[MultivariatePolynomialSingleABC, SCALAR]) – The right operand, either an instance of polynomial (of the same concrete class as the right operand) or a real scalar number.

Returns:

The result of the subtraction, an instance of subtracted polynomial.

Return type:

MultivariatePolynomialSingleABC

Notes

  • Under the hood subtraction is an addition operation with a negated operand on the right; no separate concrete implementation is used.

__truediv__(other)#

Divide an instance of polynomial with a real scalar number (/).

Parameters:

other (Union[MultivariatePolynomialSingleABC, SCALAR]) – The right operand of the (true) division expression, a real scalar number.

Returns:

An instance of polynomial, the result of (true) scalar division of a polynomial.

Return type:

MultivariatePolynomialSingleABC

__weakref__#

list of weak references to the object (if defined)

static _gen_grid_default(multi_index)#

Return the default Grid for a given MultiIndexSet instance.

For the default values of the Grid class, see minterpy.Grid.

Parameters:

multi_index (MultiIndexSet) – An instance of MultiIndexSet for which the default Grid shall be build

Returns:

An instance of Grid with the default optional parameters.

Return type:

Grid

_match_dims(other)#

Match the dimension of two polynomials.

Parameters:

other (MultivariatePolynomialSingleABC) – An instance polynomial whose dimension is to match with the current polynomial instance.

Returns:

The two instances of polynomials whose dimensions have been matched.

Return type:

Tuple[MultivariatePolynomialSingleABC, MultivariatePolynomialSingleABC]

Raises:

ValueError – If the dimension of one of the polynomial instance can’t be matched due to, for instance, incompatible domain.

Notes

  • If both polynomials have matching dimension and domains, then the function return the two polynomials as they are.

_new_instance_if_necessary(new_grid, new_indices=None)#

Constructs a new instance only if the multi indices have changed.

Parameters:
  • new_grid (Grid) – Grid instance the polynomial is defined on.

  • new_indices (MultiIndexSet, optional) – MultiIndexSet instance for the polynomial(s), needs to be a subset of the current multi_index. Default is None.

Returns:

Same polynomial instance if grid and multi_index stay the same, otherwise new polynomial instance with the new grid and multi_index.

Return type:

MultivariatePolynomialSingleABC

_verify_operands(other, operation)#

Verify the operands are valid before moving on.

Parameters:
Return type:

Tuple[MultivariatePolynomialSingleABC, MultivariatePolynomialSingleABC]

add_points(exponents)#

Extend grid and multi_index

Adds points grid and exponents to multi_index related to a given set of additional exponents.

Parameters:

exponents (np.ndarray) – Array of exponents added.

Returns:

New polynomial with the added exponents.

Return type:

MultivariatePolynomialSingleABC

property coeffs: ndarray#

The coefficients of the polynomial(s).

Returns:

One- or two-dimensional array that contains the polynomial coefficients. Coefficients of multiple polynomials having common structure are stored in a two-dimensional array of shape (N, P) where N is the number of monomials and P is the number of polynomials.

Return type:

numpy.ndarray

Raises:

ValueError – If the coefficients of an uninitialized polynomial are accessed.

Notes

  • coeffs may be assigned with None to indicate an uninitialized

    polynomial. Accessing such coefficients, however, raises an exception. Many operations involving polynomial instances, require the instance to be initialized and raising the exception here provides a common single point of failure.

diff(order, **kwargs)#

Return the partial derivative poly. of given orders along each dim.

Parameters:
  • order (numpy.ndarray) – A one-dimensional integer array specifying the orders of derivative along each dimension. The length of the array must be m where m is the spatial dimension of the polynomial.

  • **kwargs – Additional keyword-only arguments that change the behavior of the underlying differentiation (see the respective concrete implementations).

Returns:

A new polynomial instance that represents the partial derivative of the original polynomial of the specified orders of derivative along each dimension.

Return type:

MultivariatePolynomialSingleABC

Notes

  • This method calls the concrete implementation of the abstract method _diff() after input validation.

See also

_diff

The underlying static method to differentiate the polynomial of specified orders of derivative along each dimension.

expand_dim(target_dimension, extra_internal_domain=None, extra_user_domain=None)#

Expand the spatial dimension of the polynomial instance.

Parameters:
  • target_dimension (Union[int, MultivariatePolynomialSingleABC]) – The new spatial dimension. It must be larger than or equal to the current dimension of the polynomial. Alternatively, another instance of polynomial that has a higher dimension, a consistent underlying Grid instance is consistent, and a matching domain can also be specified as a target dimension.

  • extra_internal_domain (numpy.ndarray, optional) – The additional internal domains for the expanded polynomial. This parameter is optional; if not specified, the values are either taken from the domain of the higher-dimensional polynomial or from the domain of the other dimensions.

  • extra_user_domain (numpy.ndarray, optional) – The additional user domains for the expanded polynomial. This parameter is optional; if not specified, the values are either taken from the domain of the higher-dimensional polynomial or from the domain of the other dimensions.

Returns:

A new instance of polynomial whose spatial dimension has been expanded to the target.

Return type:

MultivariatePolynomialSingleABC

Raises:

ValueError – If the target dimension is an int, the exception is raised when the user or internal domains cannot be extrapolated to a higher dimension. If the target dimension is an instance of MultivariatePolynomialSingleABC, the exception is raised when the user or internal domains do no match. In both cases, an exception may also be raised by attempting to expand the dimension of the underlying Grid or MultiIndexSet instances.

classmethod from_degree(spatial_dimension, poly_degree, lp_degree, coeffs=None, internal_domain=None, user_domain=None)#

Initialise Polynomial from given coefficients and the default construction for given polynomial degree, spatial dimension and \(l_p\) degree.

Parameters:
  • spatial_dimension (int) – Dimension of the domain space of the polynomial.

  • poly_degree (int) – The degree of the polynomial, i.e. the (integer) supremum of the \(l_p\) norms of the monomials.

  • lp_degree (int) – The \(l_p\) degree used to determine the polynomial degree.

  • coeffs (np.ndarray) – coefficients of the polynomial. These shall be 1D for a single polynomial, where the length of the array is the number of monomials given by the multi_index. For a set of similar polynomials (with the same number of monomials) the array can also be 2D, where the first axis refers to the monomials and the second axis refers to the polynomials.

  • internal_domain (np.ndarray or callable) – the internal domain (factory) where the polynomials are defined on, e.g. \([-1,1]^d\) where \(d\) is the dimension of the domain space. If a callable is passed, it shall get the dimension of the domain space and returns the internal_domain as an np.ndarray.

  • user_domain (np.ndarray or callable) – the domain window (factory), from which the arguments of a polynomial are transformed to the internal domain. If a callable is passed, it shall get the dimension of the domain space and returns the user_domain as an np.ndarray.

classmethod from_grid(grid, coeffs=None, internal_domain=None, user_domain=None)#

Create an instance of polynomial with a Grid instance.

Parameters:
  • grid (Grid) – The grid on which the polynomial is defined.

  • coeffs (numpy.ndarray, optional) – The coefficients of the polynomial(s); a one-dimensional array with the same length as the length of the multi-index set or a two-dimensional array with each column corresponds to the coefficients of a single polynomial on the same grid. This parameter is optional, if not specified the polynomial is considered “uninitialized”.

  • internal_domain (numpy.ndarray, optional) – The internal domain of the polynomial(s).

  • user_domain (numpy.ndarray, optional) – The user domain of the polynomial(s).

Returns:

An instance of polynomial defined on the given grid.

Return type:

MultivariatePolynomialSingleABC

classmethod from_poly(polynomial, new_coeffs=None)#

constructs a new polynomial instance based on the properties of an input polynomial

useful for copying polynomials of other types

Parameters:
  • polynomial (MultivariatePolynomialSingleABC) – input polynomial instance defining the properties to be reused

  • new_coeffs (ndarray | None) – the coefficients the new polynomials should have. using polynomial.coeffs if None

Returns:

new polynomial instance with equal properties

Return type:

MultivariatePolynomialSingleABC

Notes

The coefficients can also be assigned later.

has_matching_dimension(other)#

Return True if the polynomials have matching dimensions.

Parameters:

other (MultivariatePolynomialSingleABC) – The second instance of polynomial to compare.

Returns:

True if the two spatial dimensions match, False otherwise.

Return type:

bool

has_matching_domain(other, tol=1e-16)#

Return True if the polynomials have matching domains.

Parameters:
  • other (MultivariatePolynomialSingleABC) – The second instance of polynomial to compare.

  • tol (float, optional) – The tolerance used to check for matching domains. Default is 1e-16.

Returns:

True if the two domains match, False otherwise.

Return type:

bool

Notes

  • The method checks both the internal and user domains.

  • If the dimensions of the polynomials do not match, the comparison is carried out up to the smallest matching dimension.

integrate_over(bounds=None, **kwargs)#

Compute the definite integral of the polynomial over the bounds.

Parameters:
  • bounds (Union[List[List[float]], np.ndarray], optional) – The bounds of the integral, an (m, 2) array where m is the number of spatial dimensions. Each row corresponds to the bounds in a given dimension. If not given, then the canonical bounds \([-1, 1]^m\) will be used instead.

  • **kwargs – Additional keyword-only arguments that change the behavior of the underlying integration (see the respective concrete implementations).

Returns:

The integral value of the polynomial over the given bounds. If only one polynomial is available, the return value is of a float type.

Return type:

Union[float, numpy.ndarray]

Raises:

ValueError – If the bounds either of inconsistent shape or not in the \([-1, 1]^m\) domain.

Notes

  • This method calls the concrete implementation of the abstract method _integrate_over() after input validation.

See also

_integrate_over

The underlying static method to integrate the polynomial instance over the given bounds.

make_complete()#

returns a possibly new polynomial instance with a complete multi index set.

Returns:

completed polynomial, where additional coefficients setted to zero.

Return type:

MultivariatePolynomialSingleABC

Notes

  • the active monomials stay equal. only the grid (“basis”) changes

  • in the case of a Lagrange polynomial this could be done by evaluating the polynomial on the complete grid

property num_active_monomials: int#

The number of active monomials of the polynomial(s).

The multi-index set that directly defines a polynomial and the grid (where the polynomial lives) may differ. Active monomials are the monomials that are defined by the multi-index set not by the one in the grid.

Returns:

The number of active monomials.

Return type:

int

partial_diff(dim, order=1, **kwargs)#

Return the partial derivative poly. at the given dim. and order.

Parameters:
  • dim (int) – Spatial dimension with respect to which the differentiation is taken. The dimension starts at 0 (i.e., the first dimension).

  • order (int) – Order of partial derivative.

  • **kwargs – Additional keyword-only arguments that change the behavior of the underlying differentiation (see the respective concrete implementations).

Returns:

A new polynomial instance that represents the partial derivative of the original polynomial of the specified order of derivative and with respect to the specified dimension.

Return type:

MultivariatePolynomialSingleABC

Notes

  • This method calls the concrete implementation of the abstract method _partial_diff() after input validation.

See also

_partial_diff

The underlying static method to differentiate the polynomial instance of a specified order of derivative and with respect to a specified dimension.

property spatial_dimension#

Spatial dimension.

The dimension of space where the polynomial(s) live on.

Returns:

Dimension of domain space.

Return type:

int

Notes

This is propagated from the multi_index.spatial_dimension.

property unisolvent_nodes#

Unisolvent nodes the polynomial(s) is(are) defined on.

For definitions of unisolvent nodes see the mathematical introduction.

Returns:

Array of unisolvent nodes.

Return type:

np.ndarray

Notes

This is propagated from from self.grid.unisolvent_nodes.


minterpy.polynomials.newton_polynomial.eval_newton(poly, xx)[source]#

Evaluate polynomial(s) in the Newton basis on a set of query points.

This is a wrapper for the evaluation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in the Newton basis to evaluate.

  • xx (numpy.ndarray) – Array of query points in the at which the polynomial(s) is evaluated. The array is of shape (N, m) where N is the number of points and m is the spatial dimension of the polynomial.

Return type:

ndarray

See also

minterpy.utils.polynomials.newton.eval_newton_polynomials

The actual implementation of the evaluation of polynomials in the Newton basis.

minterpy.polynomials.newton_polynomial.add_newton(poly_1, poly_2)[source]#

Add two instances of polynomials in the Newton basis.

This is the concrete implementation of _add() method in the MultivariatePolynomialSingleABC abstract class specifically for handling polynomials in the Newton basis.

Parameters:
  • poly_1 (NewtonPolynomial) – Left operand of the addition/subtraction expression.

  • poly_2 (NewtonPolynomial) – Right operand of the addition/subtraction expression.

Returns:

The sum of two polynomials in the Newton basis as a new instance of polynomial in the Newton basis.

Return type:

NewtonPolynomial

Notes

  • This function assumes: both polynomials must be in the Newton basis, they must be initialized (coefficients are not None), have the same dimension and their domains are matching, and the number of polynomials per instance are the same. These conditions are not explicitly checked in this function; the caller is responsible for the verification.

minterpy.polynomials.newton_polynomial.mul_newton(poly_1, poly_2)[source]#

Multiply instances of polynomials in the Newton basis.

This is the concrete implementation of _mul() method in the MultivariatePolynomialSingleABC abstract class specifically for handling polynomials in the Newton basis.

Parameters:
Returns:

The product of two polynomials in the Newton basis as a new instance of polynomial in the Newton basis.

Return type:

NewtonPolynomial

Notes

  • This function assumes: both polynomials must be in the Newton basis, they must be initialized (coefficients are not None), have the same dimension and their domains are matching, and the number of polynomials per instance are the same. These conditions are not explicitly checked in this function; the caller is responsible for the verification.

minterpy.polynomials.newton_polynomial.diff_newton(poly, order, *, backend='numba')[source]#

Differentiate polynomial(s) in the Newton basis of specified orders of derivatives along each dimension.

The orders must be specified for each dimension. This is a wrapper for the differentiation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in Newton form to differentiate.

  • order (numpy.ndarray) – A one-dimensional integer array specifying the orders of derivative along each dimension. The length of the array must be m where m is the spatial dimension of the polynomial.

  • backend (str) –

    Computational backend to carry out the differentiation. Supported values are:

    • "numpy": implementation based on NumPy; not performant, only applicable for a very small problem size (small degree, low dimension).

    • "numba" (default): implementation based on compiled code with the help of Numba; for up to moderate problem size.

    • "numba-par": parallelized (CPU) implementation based compiled code with the help of Numba for relatively large problem sizes.

Returns:

A new instance of NewtonPolynomial that represents the partial derivative of the original polynomial of the given order of derivative with respect to the specified dimension.

Return type:

NewtonPolynomial

Notes

  • The abstract class is responsible to validate order; no additional validation regarding that parameter is required here.

  • The transformation of computed Lagrange coefficients of the differentiated polynomial to the Newton coefficients is carried out using multivariate divided-difference scheme (DDS).

See also

NewtonPolynomial.diff

The public method to differentiate the polynomial instance of the given orders of derivative along each dimension.

NewtonPolynomial.partial_diff

The public method to differentiate the polynomial instance of a specified order of derivative with respect to a given dimension.

minterpy.polynomials.newton_polynomial.partial_diff_newton(poly, dim, order, *, backend='numba')[source]#

Differentiate polynomial(s) in the Newton basis with respect to a given dimension and order of derivative.

This is a wrapper for the partial differentiation function in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The instance of polynomial in Newton form to differentiate.

  • dim (int) – Spatial dimension with respect to which the differentiation is taken. The dimension starts at 0 (i.e., the first dimension).

  • order (int) – Order of partial derivative.

  • backend (str) –

    Computational backend to carry out the differentiation. Supported values are:

    • "numpy": implementation based on NumPy; not performant, only applicable for a very small problem size (small degree, low dimension).

    • "numba" (default): implementation based on compiled code with the help of Numba; applicable up to moderate problem size.

    • "numba-par": parallelized (CPU) implementation based on compiled code with the help of Numba for relatively large problem sizes.

Returns:

A new instance of NewtonPolynomial that represents the partial derivative of the original polynomial of the given order of derivative with respect to the specified dimension.

Return type:

NewtonPolynomial

Notes

  • The abstract class is responsible to validate dim and order; no additional validation regarding those two parameters are required here.

See also

NewtonPolynomial.partial_diff

The public method to differentiate the polynomial instance of a specified order of derivative with respect to a given dimension.

NewtonPolynomial.diff

The public method to differentiate the polynomial instance of the given orders of derivative along each dimension.

minterpy.polynomials.newton_polynomial.integrate_over_newton(poly, bounds)[source]#

Compute the definite integral of polynomial(s) in the Newton basis.

Parameters:
  • poly (NewtonPolynomial) – The polynomial of which the integration is carried out.

  • bounds (numpy.ndarray) – The bounds (lower and upper, respectively) of the definite integration, specified as an (M,2) array, where M is the spatial dimension of the polynomial.

Returns:

The integral value of the polynomial over the given domain.

Return type:

numpy.ndarray