Support for Polynomial-Real Scalar Multiplication
In extending Minterpy capabilities to carry out polynomial algebra, we should add scalar (real) number multiplication support. Polynomials are closed under scalar multiplication, meaning that the result of scalar multiplication is also a polynomial having the same multi-index set.
Multiplying a polynomial in a given basis with a scalar would multiply all the coefficients with the scalar and this rule is universal and it applies to all polynomial basis without controversies.
For instance, polynomial in the Lagrange basis:
>>> mi = mp.MultiIndexSet.from_degree(2, 2, 1.0)
>>> coeffs = np.arange(1, len(mi) + 1, dtype=np.float_)
>>> lag_poly = mp.LagrangePolynomial(mi, coeffs)
>>> lag_poly.coeffs
array([1., 2., 3., 4., 5., 6.])
Multiplication with a scalar
>>> lag_poly_2 = 5 * lag_poly
>>> lag_poly_2.coeffs
array([ 5., 10., 15., 20., 25., 30.])
The same rule applies to all the other bases.
Additional properties that might be useful for testing:
- Order of multiplication should not matter, so if
a
andp
are scalar and polynomial, respectively thena * p
should be the same asp*a
. - Scalar multiplicative identity, i.e.,
1 * p
should give the same polynomial.
Note that, although scalar multiplication is also carried in polynomial transformation (i.e., multiplied polynomial in a given basis is equal to multiplied polynomial in another basis transformed to the former basis), exact equality check may fail because some precision may be lost during the transformation.
For instance,
>>> mi = mp.MultiIndexSet.from_degree(2, 2, 1.0)
>>> coeffs = np.arange(1, len(mi) + 1, dtype=np.float_)
>>> lag_poly = mp.LagrangePolynomial(mi, coeffs)
>>> nwt_poly = mp.LagrangeToNewton(lag_poly)()
The two polynomials above are the same but in different bases. Now:
>>> lag_poly = 5 * lag_poly
>>> nwt_poly = 5 * nwt_poly
>>> lag_poly_2 = mp.NewtonToLagrange(nwt_poly)()
>>> lag_poly == lag_poly_2
False
This is to be expected, because the transformation from Newton to Lagrange may not be (most probably not be) exact. However, a test via np.allclose
on the coefficients directly should return True
. ==
represents exact equality check by convention.
Notes on implementation
Because the scalar multiplication rule applies to all concrete classes of polynomials, the implementation may be put directly in MultivariatePolynomialSingleABC
.