differentiation breaks for multi-value coefs
Description of the problem
If one uses the differentiation function on Newton polynomials with vector-valued coefficients, an error is thrown
MWE
import numpy as np
import minterpy as mp
print("mp version: ", minterpy.__version__)
mi = mp.MultiIndexSet.from_degree(2,2,np.inf)
len(mi)
c = np.random.rand(len(mi),2)
poly = mp.NewtonPolynomial(mi,c)
poly.diff([0,1])
mp version: 0.2.0a0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [2], in <cell line: 12>()
9 c = np.random.rand(len(mi),2)
10 poly = mp.NewtonPolynomial(mi,c)
---> 12 poly.diff([0,1])
File ~/Work/pyProjects/interpol/minterpy/src/minterpy/core/ABC/multivariate_polynomial_abstract.py:826, in MultivariatePolynomialSingleABC.diff(self, order)
822 if len(order) != self.spatial_dimension:
823 raise ValueError(f"inconsistent number of elements in 'order' <{len(order)}>,"
824 f"expected <{self.spatial_dimension}> corresponding to each spatial dimension")
--> 826 return self._diff(self, order)
File ~/Work/pyProjects/interpol/minterpy/src/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 ~/Work/pyProjects/interpol/minterpy/src/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 (9,2) and (9,) not aligned: 2 (dim 1) != 9 (dim 0)
Similar ValueErrors appear, if one uses CanonicalPolynomials:
import numpy as np
import minterpy as mp
print("mp version: ", mp.__version__)
mi = mp.MultiIndexSet.from_degree(2,2,np.inf)
len(mi)
c = np.random.rand(len(mi),2)
poly = mp.CanonicalPolynomial(mi,c)
poly.diff([0,1])
mp version: 0.2.0a0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [3], in <cell line: 12>()
9 c = np.random.rand(len(mi),2)
10 poly = mp.CanonicalPolynomial(mi,c)
---> 12 poly.diff([0,1])
File ~/Work/pyProjects/interpol/minterpy/src/minterpy/core/ABC/multivariate_polynomial_abstract.py:826, in MultivariatePolynomialSingleABC.diff(self, order)
822 if len(order) != self.spatial_dimension:
823 raise ValueError(f"inconsistent number of elements in 'order' <{len(order)}>,"
824 f"expected <{self.spatial_dimension}> corresponding to each spatial dimension")
--> 826 return self._diff(self, order)
File ~/Work/pyProjects/interpol/minterpy/src/minterpy/polynomials/canonical_polynomial.py:316, in _canonical_diff(poly, order)
313 raise ValueError(f"Cannot differentiate as some of the required multi indices are not present.")
315 # coefficients of the differentiated polynomial
--> 316 diff_coeffs = coeffs[diff_exp_mask] * np.prod(factorial(exponents[diff_exp_mask]) / factorial(diff_exponents),
317 axis=1)
319 # The differentiated polynomial being expressed wrt multi indices of the given poly
320 # NOTE: 'find_match_between' assumes 'exponents' is lexicographically ordered
321 map_pos = find_match_between(diff_exponents, exponents)
ValueError: operands could not be broadcast together with shapes (6,2) (6,)
Suggested solution
Use appropriate reshapes to apply the erroring operation on the right axis. Furthermore, I suggest writing a unit test to cover this type of bug, e.g. by testing the functionality on both, scalar and vector-valued coefficients.