1

My setup.py are :

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np

extensions = [
    Extension('_hmmc', ['_hmmc.pyx'], include_dirs = [np.get_include()]),
    ]

setup(
    ext_modules = cythonize(extensions)
    )

and I'm experimenting with cimport to get it to work.

from numpy.math cimport expl

import numpy as np
print(expl(5-2))

However, the erros are

error LNK2001: unresolved external symbol _npy_expl

Any idea? I have checked that my cython/includes/numpy/math.pxd had this:

long double expl "npy_expl"(long double x)

Any ideas?

Do Wen Rei
  • 23
  • 5

1 Answers1

1

Probably to keep it simple, one could use exp from the standard library, otherwise there are some hoops to jump through, to get it work with npy_expl.

The usual Numpy-API is header-only (more precisely, only headers are needed at compile-/linktime, see here), but this is not the case with math-functions. There is define NPY_INLINE_MATH, which would also present numpy's math library as inline functions, but this will not work on installed numpy-distributionen, because those lack the core/src-folder, where the definitions of math-functions are given.

So you have to add the precompiled static numpy's math library to your setup. It can be found in folder core/lib and is called (at least on linux) libnpymath.a.

The most robust way is to use get_info from numpy.distutils.misc_util, which returns a dictionary providing values for 'define_macros', 'include_dirs', 'libraries' and 'library_dirs' - i.e. values we need to pass to Extension, e.g.

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

from numpy.distutils.misc_util import get_info
npymath_info = get_info('npymath')

extensions = [
    Extension('_hmmc', ['_hmmc.pyx'],
               **npymath_info 
               ),
    ]

setup(
    ext_modules = cythonize(extensions)
    )

There is also the function get_mathlibs from numpy.distutils.misc_util but it only works if _numpyconfig.h is present, which is not the case at least in my installation.

ead
  • 32,758
  • 6
  • 90
  • 153