Structure and Evaluation of Multivariate Polynomials [code201.1]¶
Demonstrates how to create and evaluate a multivariate polynomial using
MPoly in vector exponent notation
\(\tilde{\alpha}^\mathsf{T}(x^q \otimes y^r)\).
Six evaluation cases are shown:
- Univariate
MPoly(single variable, no cross terms) — print. - Evaluate at a scalar.
- Evaluate over a 1-D array.
- Bivariate
MPoly(factorised coefficients) — scalar evaluation. - Array-valued variable inputs — output shape matches input shape.
- High-dimensional array inputs — confirm broadcast output shape.
Note: No graphical output.
Console Output¶
-- 1 --
(array([-0.3 , 0.08 , -0.004]),), (array([0, 2, 4]),)
-- 2 --
0.09599999999999997
-- 3 --
[-0.3 -0.224 -0.044 0.096 -0.044]
-- 4 --
0.007680000000000015
-- 5 --
[ 0.00768 -0.00352 -0.0224 -0.042 ]
shape: 4
-- 6 --
shape: (3, 2, 5)
Code¶
r"""
Structure and Evaluation of Multivariate Polynomials [code201.1]
==============================================================
Demonstrates how to create and evaluate a multivariate polynomial using
[`MPoly`][lmlib.polynomial.poly.MPoly] in vector exponent notation
$\tilde{\alpha}^\mathsf{T}(x^q \otimes y^r)$.
Six evaluation cases are shown:
1. Univariate [`MPoly`][lmlib.polynomial.poly.MPoly] (single variable, no cross terms) — print.
2. Evaluate at a scalar.
3. Evaluate over a 1-D array.
4. Bivariate [`MPoly`][lmlib.polynomial.poly.MPoly] (factorised coefficients) — scalar evaluation.
5. Array-valued variable inputs — output shape matches input shape.
6. High-dimensional array inputs — confirm broadcast output shape.
"""
import numpy as np
import lmlib as lm
expos = ([0, 2, 4],)
coefs = ([-0.3, 0.08, -0.004],)
m_poly = lm.MPoly(coefs, expos)
print("-- 1 --")
print(m_poly)
print("-- 2 --")
print(m_poly.eval((3,)))
print("-- 3 --")
print(m_poly.eval((np.arange(5),)))
expos = ([0, 1, 2], [0, 2, 4])
coefs = ([0.1, -0.03, 0.01], [-0.3, 0.08, -0.004])
m_poly = lm.MPoly(coefs, expos)
print("-- 4 --")
# Scalar variable inputs yields a scalar output
print(m_poly.eval((1, 3)))
print("-- 5 --")
# Array_like variable inputs yields into a array_like output of the same shape
variables = ([1, 2, 3, 4], [3, 2, 1, 0])
out = m_poly.eval(variables)
print(out)
print('shape: ', len(out))
print("-- 6 --")
x = np.arange(3*2*5).reshape([3, 2, 5])
y = np.arange(3*2*5).reshape([3, 2, 5])-10
print('shape: ', m_poly.eval((x, y)).shape)