Handle addition of Newton polynomials with a compatible grid.
Handle addition of Newton polynomials with a compatible grid.
If two polynomial instances in the Newton basis have a compatible underlying grid, then adding them will follow simpler monomial-based polynomial addition (just like canonical basis) because the Newton basis remains have the same meaning in the summed grid.
With this change, newton_poly + newton_poly + newton_poly
is now equal in value to 3 * newton_poly
.
Previously it was not because adding any Newton polynomials together will go via Lagrange coefficients which are then transformed to the Newton coefficients; this process introduces small numerical error.
While the error was small (the results still pass np.allclose
), but as far as sanity check goes just like above, it's not very satisfying.
Furthermore, adding a scalar polynomial (i.e., polynomial with a single multi-index element (0, \ldots, 0) both as defined for the polynomial and the grid) will behave the same way as adding a scalar because the underlying grids can be thought as always compatible.
Additionally:
- The method
is_compatible()
is introduced in theGrid
class to verify compatibility between two grid instances based on their generating functions and points. This method encapsulates the process of checking whether two grid is compatible such that addition/subtraction of polynomials in the Newton basis can be carried out directly based on the monomials without any transformation. - The test suite has been updated to verify the changes.
This commit should resolve Issue #172 (closed) and part of refactoring and extending the support for arithmetic operations involving polynomial (see Issue #142 (closed)).