Negating a Polynomial Returns a Polynomial with an Inconsistent Grid
Negating a polynomial via the unary operator -
utilizing the dunder method __neg__()
returns a polynomial with different Grid.
Consider the following example, a polynomial with equidistant points as the interpolating grid:
>>> mi = mp.MultiIndexSet.from_degree(3, 5, 1)
>>> coeffs = np.random.rand(len(mi))
>>> grd = mp.Grid.from_value_set(mi, np.linspace(-1, 1, mi.poly_degree+1))[:, np.newaxis]) # equidistant point
>>> lag_poly = mp.LagrangePolynomial(mi, coeffs, grid=grd)
>>> lag_poly.grid.generating_points
array([[ 1. , -1. , 1. ],
[ 0.6, -0.6, 0.6],
[ 0.2, -0.2, 0.2],
[-0.2, 0.2, -0.2],
[-0.6, 0.6, -0.6],
[-1. , 1. , -1. ]])
>>> neg_lag_poly = -lag_poly
>>> neg_lag_poly.grid.generating_points
array([[ 1. , -1. , 1. ],
[-1. , 1. , -1. ],
[ 0.30901699, -0.30901699, 0.30901699],
[-0.30901699, 0.30901699, -0.30901699],
[ 0.80901699, -0.80901699, 0.80901699],
[-0.80901699, 0.80901699, -0.80901699]])
The generating points have been reverted to the default (the Chebyshev-Lobatto) nodes. Specifically, the grid before and after negation are not the same which is an unexpected behavior because negating a polynomial should only alter the coefficients (change their signs):
>>> lag_poly.grid == neg_lag_poly.grid
False
Upon inspection, it seems the negation does not include the grid
argument when constructing a new instance of negated polynomial.
Edited by Damar Wicaksono