polynomials_on_simplices.polynomial.polynomials_base module¶
Abstract base class and basic functionality for polynomials.
-
class
PolynomialBase
(coeff, r=None, m=1)[source]¶ Bases:
abc.ABC
Abstract base class for a polynomial. The space of polynomials \(\mathcal{P} = \mathcal{P}(\mathbb{R}^m, \mathbb{R}^n)\) is defined as
\[\mathcal{P} = \{ p : \mathbb{R}^m \to \mathbb{R}^n | p(x) = \sum_{\nu} a_{\nu} x^{\nu}, \nu \in \mathbb{N}_0^m, a_{\nu} \in \mathbb{R}^n \}.\]The domain dimension m and the target dimension n of the polynomial is given by the
domain_dimension()
andtarget_dimension()
functions respectively.For a computable polynomial we must have \(a_{\nu} = 0\) for all but finitely many \(\nu\), and then the degree of \(p\) is defined as \(r = \deg{p} = \max_{\nu : a_{\nu} \neq 0} |\nu|\). The degree is given by the
degree()
method.There are many common bases for the space of polynomials. The basis used for a specific polynomial is given by the
basis()
method.This class also defines the basic algebraic and differentiable structures of the space of polynomials.
Ring structure:
Addition: \(+ : \mathcal{P} \times \mathcal{P} \to \mathcal{P}, (p_1 + p_2)(x) = p_1(x) + p_2(x)\).
Multiplication: \(\cdot : \mathcal{P} \times \mathcal{P} \to \mathcal{P}, (p_1 \cdot p_2)(x) = p_1(x) \cdot p_2(x)\).
Vector space structure:
Scalar multiplication: \(\cdot : \mathbb{R} \times \mathcal{P} \to \mathcal{P}, (s \cdot p)(x) = s \cdot p(x)\).
Differentiable structure:
i:th partial derivative: \(\partial_i : \mathcal{P} \to \mathcal{P}, (\partial_i p)(x) = \frac{\partial p(x)}{\partial x^i}\).
Parameters: - coeff – Coefficients for the polynomial in the chosen basis for \(\mathcal{P}_r (\mathbb{R}^m)\)
(see
basis()
). If p is expressed in the chosen basis \(\{ b_{\nu, r} \}\) as \(p(x) = \sum_{\nu} a_{\nu} b_{\nu, r}(x)\) then \(\text{coeff}[i] = a_{\nu(i)}\), where \(\nu(i)\) is the i:th multi-index in the sequence of all multi-indices of dimension m with norm \(\leq r\) (seepolynomials_on_simplices.algebra.multiindex.generate()
function). Array of scalars for a scalar valued polynomial (n = 1) and array of n-dimensional vectors for a vector valued polynomial (\(n \geq 2\)). - r (int) – Degree of the polynomial space. Optional, will be inferred from the number of polynomial coefficients if not specified.
- m (int) – Dimension of the domain of the polynomial.
-
basis
()[source]¶ Get basis for the space \(\mathcal{P}_r (\mathbb{R}^m)\) used to express this polynomial.
Returns: Unique identifier for the basis used. Return type: str
-
code_str
(fn_name)[source]¶ Generate a function code string for evaluating this polynomial.
Parameters: fn_name (str) – Name for the function in the generated code. Returns: Code string for evaluating this polynomial. Return type: str
-
degree_elevate
(s)[source]¶ Express the polynomial using a higher degree basis.
Let \(p(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}} a_{\nu} b_{\nu, r}(x)\) be this polynomial, where \(\{ b_{\nu, r} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq r}}\) is the chosen basis for \(\mathcal{P}_r (\mathbb{R}^m)\). Let \(\{ b_{\nu, s} \}_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}}, s \geq r\) be the corresponding basis for \(\mathcal{P}_s (\mathbb{R}^m)\). Then this function returns a polynomial \(q(x)\)
\[\begin{split}q(x) = \sum_{\substack{\nu \in \mathbb{N}_0^m \\ |\nu| \leq s}} \tilde{a}_{\nu} b_{\nu, s}(x),\end{split}\]such that \(p(x) = q(x) \, \forall x \in \mathbb{R}^m\).
Parameters: s (int) – New degree for the polynomial basis the polynomial should be expressed in. Returns: Elevation of this polynomial to the higher degree basis.
-
domain_dimension
()[source]¶ Get dimension of the polynomial domain.
Returns: Dimension of the domain of the polynomial. Return type: int
-
latex_str
()[source]¶ Generate a Latex string for this polynomial.
Returns: Latex string for this polynomial. Return type: str
-
multiply_with_constant
(c)[source]¶ Multiplication of this polynomial with a constant scalar or a vector (only for a scalar valued polynomial), self * c.
Parameters: c (Union[float, Numpy array
]) – Scalar or vector we should multiply this polynomial with.Returns: Product of this polynomial with the constant. Return type: Instance of self.__class__
-
partial_derivative
(i=0)[source]¶ Compute the i:th partial derivative of the polynomial.
Parameters: i (int) – Index of partial derivative. Returns: i:th partial derivative of this polynomial.
- coeff – Coefficients for the polynomial in the chosen basis for \(\mathcal{P}_r (\mathbb{R}^m)\)
(see
-
class
PolynomialComponentsIterator
(p)[source]¶ Bases:
object
Iterator for iterating over the components of a vector valued polynomial.
Parameters: p (Instance of PolynomialBase) – Vector valued polynomial.
-
get_degree_from_dimension
(dim, n)[source]¶ Get the maximum polynomial degree r for a polynomial in the space of all polynomials on an n-dimensional domain with given dimension. In a sense this is the inverse of the
get_dimension()
function.Parameters: Returns: Maximum polynomial degree.
Return type:
-
get_dimension
(r, n)[source]¶ Get the dimension of the space of polynomials of degree <= r on an n-dimensional domain.
Parameters: Returns: Dimension of the space of polynomials (number of basis functions needed to span the space).
Return type:
-
polynomials_equal
(p1, p2, r, m, rel_tol=1e-09, abs_tol=1e-07)[source]¶ Check if two polynomials p1 and p2 are approximately equal.
For scalar valued polynomials, the two polynomials are considered equal if
math.isclose(p1(xi), p2(xi), rel_tol=rel_tol, abs_tol=abs_tol)
is true for a set of random points \(\{ x_i \}_{i = 0}^{d - 1}\) from the m-dimensional unit cube, where \(d\) is the dimension of the polynomial space p1 and p2 belongs to (as given by the
get_dimension()
function).For vector valued polynomials the same check is done component wise.
Parameters: - p1 (Callable p1(x)) – First polynomial.
- p2 (Callable p2(x)) – Second polynomial.
- r (int) – Degree of the polynomials.
- m (int) – Dimension of the domain of the polynomials.
- rel_tol (float) – Tolerance for the relative error. See
math.isclose
for details. - abs_tol (float) – Tolerance for the absolute error. See
math.isclose
for details.
Returns: Whether or not the two polynomials are approximately equal.
Return type: