pythonnumba

numba doesn't work with numpy.polynomial.polynomial.Polynomial?


This code

import numba
import numpy

@numba.jit
def test(*coeffs):
    poly = numpy.polynomial.polynomial.Polynomial(coeffs)
    return poly(10)

c = (2,1)
test(*c)

Generates the error

No implementation of function Function(<class 'numpy.polynomial.polynomial.Polynomial'>) found for signature:
 
 >>> Polynomial(UniTuple(int64 x 2))
 
There are 2 candidate implementations:
      - Of which 2 did not match due to:
      Overload of function 'Polynomial': File: numba\core\extending.py: Line 40.
        With argument(s): '(UniTuple(int64 x 2))':
       No match.
During: resolving callee type: Function(<class 'numpy.polynomial.polynomial.Polynomial'>)
During: typing of call at <ipython-input-22-2355bd6d2aa0> (7)
File "<ipython-input-22-2355bd6d2aa0>", line 7:
def test(*coeffs):
    poly = numpy.polynomial.polynomial.Polynomial(coeffs)
    ^

This is despite being on numba version 0.60 which should support the new numpy polynomial API numpy.polynomial.polynomial.Polynomial


Solution

  • First, you need to convert that tuple into an array, like so:

        poly = numpy.polynomial.polynomial.Polynomial(numpy.array(coeffs))
    

    However, this still does not work. It gives the following error:

    Invalid use of PolynomialType(array(float64, 1d, C), array(int64, 1d, C), array(int64, 1d, C), 1) with parameters (Literal[int](10))
    No type info available for PolynomialType(array(float64, 1d, C), array(int64, 1d, C), array(int64, 1d, C), 1) as a callable.
    During: resolving callee type: PolynomialType(array(float64, 1d, C), array(int64, 1d, C), array(int64, 1d, C), 1)
    During: typing of call at /tmp/ipykernel_15571/2463428636.py (11)
    

    Reading the source code of Numba and the test associated with it, I don't think they mean that it supports the methods of Polynomial. Rather, it just supports constructing and returning a Polynomial object. The __call__ method of Polynomial is not supported.

    As an alternative, you could evaluate the polynomial using polyval().

    Example:

    import numba
    import numpy
    
    
    @numba.jit
    def test(*coeffs):
        return numpy.polynomial.polynomial.polyval(10, coeffs)