Differentiating multiple polynomials
This may be an unexpected behavior. As we know, the polynomials in minterpy may be defined using multiple sets of coefficients denoting multiple polynomials (sharing the same basis).
mi = mp.MultiIndexSet.from_degree(2, 3, 1)
newton_coeffs = np.random.rand(len(mi),5)
newton_poly = mp.NewtonPolynomial(mi, newton_coeffs)
xx = -1 + 2 * np.random.rand(100,2)
newton_poly(xx).shape
# (100, 5)
However, differentiating such polynomials (partial or otherwise) gives a cryptic error message:
newton_poly.partial_diff(1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [11], in <cell line: 1>()
----> 1 newton_poly.partial_diff(1)
File ~\Anaconda3\envs\mp-opt-temp\lib\site-packages\minterpy\core\ABC\multivariate_polynomial_abstract.py:798, in MultivariatePolynomialSingleABC.partial_diff(self, dim, order)
795 if order < 0:
796 raise ValueError(f"order <{order}> must be a non-negative integer")
--> 798 return self._partial_diff(self, dim, order)
File ~\Anaconda3\envs\mp-opt-temp\lib\site-packages\minterpy\polynomials\newton_polynomial.py:67, in _newton_partial_diff(poly, dim, order)
65 deriv_order_along = [0]*spatial_dim
66 deriv_order_along[dim] = order
---> 67 return _newton_diff(poly, deriv_order_along)
File ~\Anaconda3\envs\mp-opt-temp\lib\site-packages\minterpy\polynomials\newton_polynomial.py:79, in _newton_diff(poly, order)
70 """ Partial differentiation in Newton basis.
71
72 Notes
73 -----
74 Performs a transformation from Lagrange to Newton using DDS.
75 """
77 # When you evaluate the derivatives on the unisolvent nodes, you get the coefficients for
78 # the derivative polynomial in Lagrange basis.
---> 79 lag_coeffs = deriv_newt_eval(poly.grid.unisolvent_nodes, poly.coeffs,
80 poly.grid.multi_index.exponents,
81 poly.grid.generating_points, order)
83 # DDS returns a 2D array, converting it to 1d
84 newt_coeffs = dds(lag_coeffs, poly.grid.tree).reshape(-1)
File ~\Anaconda3\envs\mp-opt-temp\lib\site-packages\minterpy\polynomials\utils.py:110, in deriv_newt_eval(x, coefficients, exponents, generating_points, derivative_order_along)
107 newt_mon_val = 0.0
108 monomial_vals[j] = newt_mon_val
--> 110 results[point_nr] = np.dot(coefficients, monomial_vals)
112 return results
File <__array_function__ internals>:180, in dot(*args, **kwargs)
ValueError: shapes (10,5) and (10,) not aligned: 5 (dim 1) != 10 (dim 0)
Is there a more gracious way of raising an error message, or perhaps even parameterizing the differentiation to choose which polynomial when there are multiple of them? What do you think @thekke48?