Skip to content

MPoly

MPoly(coefs, expos)

Bases: _PolyBase


              flowchart TD
              lmlib.polynomial.poly.MPoly[MPoly]
              lmlib.polynomial.poly._PolyBase[_PolyBase]

                              lmlib.polynomial.poly._PolyBase --> lmlib.polynomial.poly.MPoly
                


              click lmlib.polynomial.poly.MPoly href "" "lmlib.polynomial.poly.MPoly"
              click lmlib.polynomial.poly._PolyBase href "" "lmlib.polynomial.poly._PolyBase"
            

Multivariate polynomials \({\tilde{\alpha}}^\mathsf{T} (x^q \otimes y^r)\), or with factorized coefficient vector \((\alpha \otimes \beta )^\mathsf{T} (x^q \otimes y^r)\).

This polynomial class is for multivariate polynomials in vector exponent notation, see [Wildhaber2019], Chapter 6.

Such a multivariate polynomial is in general given by

\[ p(x) = \tilde{\alpha}^\mathsf{T}(x^q \otimes y^r) \ , \]

where \(\tilde{\alpha} \in \mathbb{R}^{Q \times R}\) is the coefficient vectors, \(q \in \mathbb{Z}_{\geq 0}^Q\) and \(r \in \mathbb{Z}_{\geq 0}^R\) the exponent vectors, and \(x \in \mathbb{R}\) and \(y \in \mathbb{R}\) the independent variables.

As a special case, if the coefficient vector is in the form of a Kronecker product, i.e.,

\[ p(x) = (\alpha \otimes \beta)^\mathsf{T}(x^q \otimes y^r) \ , \]

where \(\alpha \in \mathbb{R}^Q\) and \(\beta \in \mathbb{R}^R\) are coefficient vectors, we denote a polynomial as factorized. This form often leads to algebraic simplifications (if it exists).

Example

>>> # Bivariate (x,y) polynomial with factorized coefficients ([.2,.7],[-1.0,2.0,.1]) and terms x^1, x^2, y^1, y^2, y^3, and cross terms
>>> l= MPoly(([.2,.7],[-1.0,2.0,.1]),([1,2],[1,2,3]))
>>> l.coefs # gets coefficients
(array([0.2, 0.7]), array([-1. ,  2. ,  0.1]))
>>> l.coef_fac # gets factorized coefficients (if available)
array([-0.2 ,  0.4 ,  0.02, -0.7 ,  1.4 ,  0.07])
>>> l.eval([.3,.7])  # evaluating polynomial for x=.3 and y=.7
array(0.0386589)
>>> # Bivariate (x,y) polynomial with non-factorized coefficients ([.2,.7,1.3,1.4,.2,-1.6]) and terms x^1, x^2, y^1, y^2, y^3, and cross terms
>>> l= MPoly(([.2,.7,1.3,1.4,.2,-1.6],),([1,2],[1,2,3]))
>>> l.eval([.3,.7])  # evaluating polynomial for x=.3 and y=.7
array(0.326298)

Parameters:

  • coefs (tuple of array_like) –

    Set of coefficient vector(s)

  • expos (tuple of array_like) –

    Set of exponent vector(s)

Constructor method

Methods:

  • eval

    Evaluates the polynomial for given values (variables)

Attributes:

  • expos

    tuple of ndarray : Exponent vectors

  • coefs_fac

    tuple of ndarray, or None : Factorized coefficient vectors

  • coefs

    tuple of ndarray : Coefficient vector (i.e., not factorized)

  • variable_count

    int : Number of dependent variables

Source code in lmlib/polynomial/poly.py
def __init__(self, coefs, expos):
    """
    Constructor method
    """
    coefs = tuple(np.asarray(coef) for coef in coefs)
    expos = tuple(np.asarray(expo) for expo in expos)
    super(MPoly, self).__init__(coefs, expos)

Methods

eval

eval(variables)

Evaluates the polynomial for given values (variables)

Parameters:

  • variables (tuple) –

    Dependent variables of a polynomial. Each element in variables has the same shape which is also the output shape.

Returns:

  • out ( ndarray ) –

    Output of evaluated polynomial. Shape is identical as a dependent variable

Example
# evaluate bivariate polynomial at multiple positions (x=.1 ... .4, y=.5)
>>> l = MPoly(([.2, .7], [1.3, 1.4, -.9],), ([0, 1], [0, 1, 2]))
>>> l.eval(([.1, .2, .3, .4], [.5, .5, .5, .5]))
array([0.47925, 0.6035, 0.72775, 0.852])
Source code in lmlib/polynomial/poly.py
def eval(self, variables):
    r"""
    Evaluates the polynomial for given values (variables)

    Parameters
    ----------
    variables : tuple
        Dependent variables of a polynomial.
        Each element in variables has the same shape which is also the output shape.

    Returns
    -------
    out : ndarray
        Output of evaluated polynomial.
        Shape is identical as a dependent variable

    Example
    -------
    ```python
    # evaluate bivariate polynomial at multiple positions (x=.1 ... .4, y=.5)
    >>> l = MPoly(([.2, .7], [1.3, 1.4, -.9],), ([0, 1], [0, 1, 2]))
    >>> l.eval(([.1, .2, .3, .4], [.5, .5, .5, .5]))
    array([0.47925, 0.6035, 0.72775, 0.852])
    ```
    """


    _check_input_variables(variables, self.variable_count)
    variables = np.array([np.asarray(v, dtype=float) for v in variables])  # <-- fix
    out = np.empty_like(variables[0], dtype=float)
    it = np.nditer(out, flags=['multi_index'])
    while not it.finished:
        out[it.multi_index] = self._eval_scalar([variable[it.multi_index] for variable in variables])
        it.iternext()
    return out