Introduce from_grid() factory method for MultivariatePolynomialSingleABC
A new factory method from_degree()
for the abstract class MultivariatePolynomialSingleABC
shall be introduced as it covers a very common use case.
Currently to create an instance of polynomial in one of the supported bases, a multi-index set is required. To define an interpolating polynomial (especially in the Lagrange basis) a Grid
instance is also created such that the function values at the unisolvent nodes can be evaluated. To create a Grid
instance, a MultiIndexSet
is also required.
While it is possible to define two different multi-index sets, one for the polynomial and another for the interpolating grid, the most common use case is that the two are the same.
Furthermore, there might be a potential pitfall in the polynomial construction.
Consider the following example of creating an interpolating polynomial in the Lagrange basis:
mi = mp.MultiIndexSet.from_degree(3, 2, 1.0)
grd = mp.Grid(mi) # Implicit Leja-Ordered Chebyshev-Lobatto points
lag_coeffs = grd(fun) # Evaluate a given function on the unisolvent nodes
# Create a Lagrange polynomial
lag_poly = mp.LagrangePolynomial(mi, lag_coeffs) # Grid is reconstructed from within assuming the default generating function
Above code works and it is a common "pattern" to construct a Lagrange polynomial, the entry point to polynomial in Minterpy.
The problem with the above code is if grd
is created with different generating function or points or even multi-index set, then the resulting lag_poly
will be inconsistent because the Grid
instance that will be created automatically (because a Grid
instance is not specified) will be different than the one used to evaluate the Lagrange coefficients.
Given an alternative from_grid()
factory method, we can always be sure that the polynomial will use the Grid
we already constructed if that's indeed the intention. The main assumption of using this method is that the multi-index sets of the polynomial and the grid will be identical. In other words, from_grid()
should cover the cases where the multi-index sets are the same which, I would argue, is the most common.
lag_poly = mp.LagrangePolynomial.from_grid(grd, lag_coeffs)
There is no additional change to the default constructor by introducing this new method.