Complex square root function csqrt

45 views
Skip to first unread message

Ilhan Polat

unread,
Mar 24, 2022, 8:57:35 AM3/24/22
to cython-users
Dear all,

I have a rather simple question but this comes so often that I want to understand it a bit generally.

I have a fused type

ctypedef fused lapack_t:
    float
    double
    (float complex)
    (double complex)

and I would like to get the square root of the argument of lapack_t type. Obviously if the first two are negative I have to provide a simple machinery to cast them as complex and send it to csqrt. I am not sure if this is going to be handled in Cython 3 better with overloading (say, like "abs") but for now all I can find online is to read from complex.h or to get it from libc or libcpp.

This gets slightly complicated because if I do that import I get messages like the following

_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c c:\users\ilhan\appdata\local\programs\python\python39\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1297): error C2061: syntax error: identifier '__pyx_t_float_complex' C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1297): error C2059: syntax error: ';' C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1302): error C2061: syntax error: identifier '__pyx_t_float_complex_from_parts' C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1302): error C2059: syntax error: ';' C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1302): error C2059: syntax error: '<parameter-list>' C:\Users\ilhan\.ipython\cython\_cython_magic_86a41435db5ca7ce33368ba3df64c3b7.c(1309): error C2061: syntax error: identifier '__pyx_t_double_complex'

Might be the fact that it is picking up MSVC somehow or some other problem happening here. But just to keep it in scope, and apart from these complications, I'd like to ask how the generic way to handle these libc and libcpp imports pre v3.0. Because it seems like they are not in the includes in the 0.29.x series but they are in master branch.

As you can tell I confused myself quite a bit about this point and would like to ask for some canonical way or any guidance.

Thanks in advance.

Ilhan Polat

unread,
Mar 27, 2022, 2:05:58 AM3/27/22
to cython-users
For future visitors,  the following works in a notebook for the npymath access

%%cython -a -f
# distutils: libraries = npymath
# distutils: include_dirs = c:\users\......\appdata\local\programs\python\python39\lib\site-packages\numpy-1.23.0.dev0+942.g2c89e154c-py3.9-win-amd64.egg\numpy\core\include
# distutils: library_dirs = c:\users\.....\appdata\local\programs\python\python39\lib\site-packages\numpy-1.23.0.dev0+942.g2c89e154c-py3.9-win-amd64.egg\numpy\core\lib
cimport cython
cimport numpy as cnp
import numpy as np
cnp.import_array()
   
cdef extern from "numpy/npy_math.h":
    cnp.npy_cdouble npy_csqrt(cnp.npy_cdouble z) nogil
    cnp.npy_cfloat npy_csqrtf(cnp.npy_cfloat x) nogil
Reply all
Reply to author
Forward
0 new messages