Getting started

Installation

Installation with pip:

pip install multivar_horner

For efficiency this package is compiling the instructions required for polynomial evaluation to C by default. If you don’t have a C compiler (gcc or cc) installed you also need to install numba for using an alternative method:

pip install multivar_horner[numba]

Basics

Let’s consider this example multivariate polynomial:

\(p(x) = 5 + 1 x_1^3 x_2^1 + 2 x_1^2 x_3^1 + 3 x_1^1 x_2^1 x_3^1\)

Which can also be written as:

\(p(x) = 5 x_1^0 x_2^0 x_3^0 + 1 x_1^3 x_2^1 x_3^0 + 2 x_1^2 x_2^0 x_3^1 + 3 x_1^1 x_2^1 x_3^1\)

A polynomial is a sum of monomials. Our example polynomial has \(M = 4\) monomials and dimensionality \(N = 3\).

The coefficients of our example polynomial are: 5.0, 1.0, 2.0, 3.0

The exponent vectors of the corresponding monomials are:

  • [0, 0, 0]

  • [3, 1, 0]

  • [2, 0, 1]

  • [1, 1, 1]

To represent polynomials this package requires the coefficients and the exponent vectors as input.

This code shows how to compute the Horner factorisation of our example polynomial \(p\) and evaluating \(p\) at a point \(x\):

import numpy as np
from multivar_horner import HornerMultivarPolynomial

coefficients = np.array([[5.0], [1.0], [2.0], [3.0]], dtype=np.float64)  # shape: (M,1)
exponents = np.array(
    [[0, 0, 0], [3, 1, 0], [2, 0, 1], [1, 1, 1]], dtype=np.uint32
)  # shape: (M,N)
p = HornerMultivarPolynomial(coefficients, exponents)

x = np.array([-2.0, 3.0, 1.0], dtype=np.float64)  # shape: (1,N)
p_x = p(x)  # -29.0

Note

with the default settings the input is required to have these data types and shapes

With the class HornerMultivarPolynomial a polynomial can be represented in Horner factorisation.

With the class HornerMultivarPolynomialOpt a polynomial can be represented in an optimal Horner factorisation.

With the class MultivarPolynomial a polynomial can be represented in canonical form.

All available features of this package are explained HERE.

The API documentation can be found HERE.