python 3.x - Using c++ complex functions in Cython -
i trying complex exponential in cython.
i have been able cobble following code pyx
:
from libc.math cimport sin, cos, acos, exp, sqrt, fabs, m_pi, floor, ceil cdef extern "complex.h": double complex cexp(double complex z) import numpy np cimport numpy np import cython cython.parallel cimport prange, parallel def try_cexp(): cdef: double complex rr1 double complex rr2 rr1 = 1j rr2 = 2j print(rr1*rr2) #print(cexp(rr1))
note print(cexp(rr1))
commented. when line active, following error when running setup.py
:
error: command 'c:\\winpython\\winpython-64bit-3.4.3.6\\python-3.4.3.amd64\\scripts\\gcc.exe' failed exit status 1
note when cexp
commented out, runs expected... can run setup.py
, , when test function prints out product of 2 complex numbers.
here setup.py
file. note includes code run openmp
in cython using g++:
from distutils.core import setup cython.build import cythonize distutils.extension import extension cython.distutils import build_ext import numpy np import os os.environ["cc"] = "g++-4.7" os.environ["cxx"] = "g++-4.7" # these added based on examples had seen of cexp in cython. no effect. #import pyximport #pyximport.install(reload_support=true) ext_modules = [ extension('complex_test', ['complex_test.pyx'], language="c++", extra_compile_args=['-fopenmp'], extra_link_args=['-fopenmp', '-lm']) # note '-lm' # added due example mentioned g++ required # this. same results , without it. ] setup( name='complex_test', cmdclass={'build_ext': build_ext}, ext_modules=ext_modules, include_dirs=[np.get_include()] )
ultimately goal speed calcualtion looks k*exp(z)
, k
, z
complex. using numerical expressions, has large memory overhead, , believe it's possible optimize further can.
thank help.
you're using cexp
instead of exp
in c++
. change cdef extern
to:
cdef extern "<complex.h>" namespace "std": double complex exp(double complex z) float complex exp(float complex z) # overload
and print call to:
print(exp(rr1))
and should work charm.
i know compilation messages lengthy, in there can find error points culprit:
complex_test.cpp: in function ‘pyobject* __pyx_pf_12complex_test_try_cexp(pyobject*)’: complex_test.cpp:1270:31: error: cannot convert ‘__pyx_t_double_complex {aka std::complex<double>}’ ‘__complex__ double’ argument ‘1’ ‘__complex__ double cexp(__complex__ double)’ __pyx_t_3 = cexp(__pyx_v_rr1);
it's messy, can see cause. *you're supplying c++
defined type (__pyx_t_double_complex
in cython jargon) c
function expects different type (__complex__ double
).
Comments
Post a Comment