# Chapter 19: Code optimization¶

Robert Johansson

Source code listings for Numerical Python - Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib (ISBN 978-1-484242-45-2).

In [1]:
import numba

In [2]:
import pyximport

In [3]:
import cython

In [4]:
import numpy as np

In [5]:
%matplotlib inline
import matplotlib.pyplot as plt


# Numba¶

In [6]:
np.random.seed(0)

In [7]:
data = np.random.randn(50000)

In [8]:
def py_sum(data):
s = 0
for d in data:
s += d
return s

In [9]:
def py_cumsum(data):
out = np.zeros(len(data), dtype=np.float64)
s = 0
for n in range(len(data)):
s += data[n]
out[n] = s

return out

In [10]:
%timeit py_sum(data)

7.25 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [11]:
assert abs(py_sum(data) - np.sum(data)) < 1e-10

In [12]:
%timeit np.sum(data)

27.4 µs ± 523 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [13]:
%timeit py_cumsum(data)

13.8 ms ± 512 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [14]:
assert np.allclose(np.cumsum(data), py_cumsum(data))

In [15]:
%timeit np.cumsum(data)

150 µs ± 455 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [16]:
@numba.jit
def jit_sum(data):
s = 0
for d in data:
s += d

return s

In [17]:
assert abs(jit_sum(data) - np.sum(data)) < 1e-10

In [18]:
%timeit jit_sum(data)

47 µs ± 394 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [19]:
jit_cumsum = numba.jit()(py_cumsum)

In [20]:
assert np.allclose(np.cumsum(data), jit_cumsum(data))

In [21]:
%timeit jit_cumsum(data)

64.6 µs ± 499 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)


## Julia fractal¶

In [22]:
def py_julia_fractal(z_re, z_im, j):
for m in range(len(z_re)):
for n in range(len(z_im)):
z = z_re[m] + 1j * z_im[n]
for t in range(256):
z = z ** 2 - 0.05 + 0.68j
if np.abs(z) > 2.0:
#if (z.real * z.real + z.imag * z.imag) > 4.0:  # a bit faster
j[m, n] = t
break

In [23]:
jit_julia_fractal = numba.jit(nopython=True)(py_julia_fractal)

In [24]:
N = 1024
j = np.zeros((N, N), np.int64)
z_real = np.linspace(-1.5, 1.5, N)
z_imag = np.linspace(-1.5, 1.5, N)

In [25]:
jit_julia_fractal(z_real, z_imag, j)

In [26]:
fig, ax = plt.subplots(figsize=(14, 14))
ax.imshow(j, cmap=plt.cm.RdBu_r,
extent=[-1.5, 1.5, -1.5, 1.5])
ax.set_xlabel("$\mathrm{Re}(z)$", fontsize=18)
ax.set_ylabel("$\mathrm{Im}(z)$", fontsize=18)
fig.tight_layout()
fig.savefig("ch19-numba-julia-fractal.pdf")

In [27]:
%timeit py_julia_fractal(z_real, z_imag, j)

1min 47s ± 20.8 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [28]:
%timeit jit_julia_fractal(z_real, z_imag, j)

145 ms ± 6.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


## Vectorize¶

In [29]:
def py_Heaviside(x):
if x == 0.0:
return 0.5

if x < 0.0:
return 0.0
else:
return 1.0

In [30]:
x = np.linspace(-2, 2, 50001)

In [31]:
%timeit [py_Heaviside(xx) for xx in x]

18.6 ms ± 261 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [32]:
np_vec_Heaviside = np.vectorize(py_Heaviside)

In [33]:
np_vec_Heaviside(x)

Out[33]:
array([0., 0., 0., ..., 1., 1., 1.])
In [34]:
%timeit np_vec_Heaviside(x)

9.45 ms ± 84.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [35]:
def np_Heaviside(x):
return (x > 0.0) + (x == 0.0)/2.0

In [36]:
%timeit np_Heaviside(x)

206 µs ± 3.51 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [37]:
@numba.vectorize([numba.float32(numba.float32),
numba.float64(numba.float64)])
def jit_Heaviside(x):
if x == 0.0:
return 0.5

if x < 0:
return 0.0
else:
return 1.0

In [38]:
%timeit jit_Heaviside(x)

34.7 µs ± 662 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [39]:
jit_Heaviside([-1, -0.5, 0.0, 0.5, 1.0])

Out[39]:
array([0. , 0. , 0.5, 1. , 1. ])

# Cython¶

In [40]:
!rm cy_sum.*

In [41]:
%%writefile cy_sum.pyx

def cy_sum(data):
s = 0.0
for d in data:
s += d
return s

Writing cy_sum.pyx

In [42]:
!cython cy_sum.pyx

/Users/rob/miniconda3/lib/python3.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /Users/rob/Desktop/numerical-python-apress-revision/numerical-python-book-code/cy_sum.pyx
tree = Parsing.p_module(s, pxd, full_module_name)

In [43]:
# 5 lines of python code -> 1470 lines of C code ...
!wc cy_sum.c

    2647    9105  103591 cy_sum.c

In [59]:
%%writefile setup.py

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

import numpy as np
setup(ext_modules=cythonize('cy_sum.pyx'),
include_dirs=[np.get_include()],
requires=['Cython', 'numpy'] )

Overwriting setup.py

In [73]:
!/Users/rob/miniconda3/envs/py3.6/bin/python setup.py build_ext --inplace > /dev/null

In [74]:
from cy_sum import cy_sum

In [75]:
cy_sum(data)

Out[75]:
-189.70046227549025
In [76]:
%timeit cy_sum(data)

5.14 ms ± 43 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [77]:
%timeit py_sum(data)

7 ms ± 85.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [78]:
%%writefile cy_cumsum.pyx

cimport numpy
import numpy

def cy_cumsum(data):
out = numpy.zeros_like(data)
s = 0
for n in range(len(data)):
s += data[n]
out[n] = s

return out

Overwriting cy_cumsum.pyx

In [79]:
pyximport.install(setup_args={'include_dirs': np.get_include()});

In [80]:
pyximport.install(setup_args=dict(include_dirs=np.get_include()));

In [81]:
from cy_cumsum import cy_cumsum

In [82]:
%timeit cy_cumsum(data)

5.96 ms ± 27.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [83]:
%timeit py_cumsum(data)

13.3 ms ± 207 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)


## Using IPython cython command¶

In [84]:
%load_ext cython

In [85]:
%%cython -a
def cy_sum(data):
s = 0.0
for d in data:
s += d
return s

Out[85]:
Cython: _cython_magic_9889852309bc2332c4f81a8792f01ec6.pyx

Generated by Cython 0.29.7

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+1: def cy_sum(data):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_1cy_sum = {"cy_sum", (PyCFunction)__pyx_pw_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_1cy_sum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_sum (wrapper)", 0);
__pyx_r = __pyx_pf_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_cy_sum(__pyx_self, ((PyObject *)__pyx_v_data));

/* function exit code */
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_cy_sum(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_v_s = NULL;
PyObject *__pyx_v_d = NULL;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_sum", 0);
/* … */
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__Pyx_XDECREF(__pyx_t_4);
__pyx_r = NULL;
__pyx_L0:;
__Pyx_XDECREF(__pyx_v_s);
__Pyx_XDECREF(__pyx_v_d);
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
/* … */
__pyx_tuple_ = PyTuple_Pack(3, __pyx_n_s_data, __pyx_n_s_s, __pyx_n_s_d); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple_);
__Pyx_GIVEREF(__pyx_tuple_);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_9889852309bc2332c4f81a8792f01ec6_1cy_sum, NULL, __pyx_n_s_cython_magic_9889852309bc2332c4); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_sum, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+2:     s = 0.0
  __Pyx_INCREF(__pyx_float_0_0);
__pyx_v_s = __pyx_float_0_0;

+3:     for d in data:
  if (likely(PyList_CheckExact(__pyx_v_data)) || PyTuple_CheckExact(__pyx_v_data)) {
__pyx_t_1 = __pyx_v_data; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;
__pyx_t_3 = NULL;
} else {
__pyx_t_2 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_v_data); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_3 = Py_TYPE(__pyx_t_1)->tp_iternext; if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 3, __pyx_L1_error)
}
for (;;) {
if (likely(!__pyx_t_3)) {
if (likely(PyList_CheckExact(__pyx_t_1))) {
if (__pyx_t_2 >= PyList_GET_SIZE(__pyx_t_1)) break;
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
__pyx_t_4 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) __PYX_ERR(0, 3, __pyx_L1_error)
#else
__pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 3, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_4);
#endif
} else {
if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
__pyx_t_4 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_4); __pyx_t_2++; if (unlikely(0 < 0)) __PYX_ERR(0, 3, __pyx_L1_error)
#else
__pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 3, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_4);
#endif
}
} else {
__pyx_t_4 = __pyx_t_3(__pyx_t_1);
if (unlikely(!__pyx_t_4)) {
PyObject* exc_type = PyErr_Occurred();
if (exc_type) {
if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
else __PYX_ERR(0, 3, __pyx_L1_error)
}
break;
}
__Pyx_GOTREF(__pyx_t_4);
}
__Pyx_XDECREF_SET(__pyx_v_d, __pyx_t_4);
__pyx_t_4 = 0;
/* … */
}
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+4:         s += d
    __pyx_t_4 = PyNumber_InPlaceAdd(__pyx_v_s, __pyx_v_d); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 4, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_4);
__Pyx_DECREF_SET(__pyx_v_s, __pyx_t_4);
__pyx_t_4 = 0;

+5:     return s
  __Pyx_XDECREF(__pyx_r);
__Pyx_INCREF(__pyx_v_s);
__pyx_r = __pyx_v_s;
goto __pyx_L0;

In [86]:
%timeit cy_sum(data)

5.2 ms ± 63.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [87]:
%timeit py_sum(data)

7.25 ms ± 128 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [88]:
assert np.allclose(np.sum(data), cy_sum(data))

In [89]:
%%cython -a
cimport numpy
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
def cy_sum(numpy.ndarray[numpy.float64_t, ndim=1] data):
cdef numpy.float64_t s = 0.0
#cdef int n, N = data.shape[0]
cdef int n, N = len(data)
for n in range(N):
s += data[n]
return s

Out[89]:
Cython: _cython_magic_ca1303e3f76e8cc692e7b9140687359d.pyx

Generated by Cython 0.29.7

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: cimport numpy
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

 02: cimport cython
 03:
 04: @cython.boundscheck(False)
 05: @cython.wraparound(False)
+06: def cy_sum(numpy.ndarray[numpy.float64_t, ndim=1] data):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_1cy_sum = {"cy_sum", (PyCFunction)__pyx_pw_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_1cy_sum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_sum (wrapper)", 0);
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_data), __pyx_ptype_5numpy_ndarray, 1, "data", 0))) __PYX_ERR(0, 6, __pyx_L1_error)
__pyx_r = __pyx_pf_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_cy_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));

/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__pyx_r = NULL;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_cy_sum(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_data) {
__pyx_t_5numpy_float64_t __pyx_v_s;
int __pyx_v_n;
int __pyx_v_N;
__Pyx_LocalBuf_ND __pyx_pybuffernd_data;
__Pyx_Buffer __pyx_pybuffer_data;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_sum", 0);
__pyx_pybuffer_data.pybuffer.buf = NULL;
__pyx_pybuffer_data.refcount = 0;
__pyx_pybuffernd_data.data = NULL;
__pyx_pybuffernd_data.rcbuffer = &__pyx_pybuffer_data;
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_data.rcbuffer->pybuffer, (PyObject*)__pyx_v_data, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float64_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 6, __pyx_L1_error)
}
__pyx_pybuffernd_data.diminfo[0].strides = __pyx_pybuffernd_data.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_data.diminfo[0].shape = __pyx_pybuffernd_data.rcbuffer->pybuffer.shape[0];
/* … */
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_6);
{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
__pyx_r = NULL;
goto __pyx_L2;
__pyx_L0:;
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__pyx_L2:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
/* … */
__pyx_tuple__8 = PyTuple_Pack(4, __pyx_n_s_data, __pyx_n_s_s, __pyx_n_s_n, __pyx_n_s_N); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__8);
__Pyx_GIVEREF(__pyx_tuple__8);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_ca1303e3f76e8cc692e7b9140687359d_1cy_sum, NULL, __pyx_n_s_cython_magic_ca1303e3f76e8cc692); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_sum, __pyx_t_1) < 0) __PYX_ERR(0, 6, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+07:     cdef numpy.float64_t s = 0.0
  __pyx_v_s = 0.0;

 08:     #cdef int n, N = data.shape[0]
+09:     cdef int n, N = len(data)
  __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_v_data)); if (unlikely(__pyx_t_1 == ((Py_ssize_t)-1))) __PYX_ERR(0, 9, __pyx_L1_error)
__pyx_v_N = __pyx_t_1;

+10:     for n in range(N):
  __pyx_t_2 = __pyx_v_N;
__pyx_t_3 = __pyx_t_2;
for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
__pyx_v_n = __pyx_t_4;

+11:         s += data[n]
    __pyx_t_5 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_5, __pyx_pybuffernd_data.diminfo[0].strides)));
}

+12:     return s
  __Pyx_XDECREF(__pyx_r);
__pyx_t_6 = PyFloat_FromDouble(__pyx_v_s); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_r = __pyx_t_6;
__pyx_t_6 = 0;
goto __pyx_L0;

In [90]:
%timeit cy_sum(data)

48.6 µs ± 1.04 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [91]:
%timeit jit_sum(data)

47.6 µs ± 722 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [92]:
%timeit np.sum(data)

27.5 µs ± 888 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)


## Cummulative sum¶

In [93]:
%%cython -a
cimport numpy
import numpy
cimport cython

ctypedef numpy.float64_t FTYPE_t

@cython.boundscheck(False)
@cython.wraparound(False)
def cy_cumsum(numpy.ndarray[FTYPE_t, ndim=1] data):
cdef int n, N = data.size
cdef numpy.ndarray[FTYPE_t, ndim=1] out = numpy.zeros(N, dtype=data.dtype)
cdef numpy.float64_t s = 0.0
for n in range(N):
s += data[n]
out[n] = s
return out

Out[93]:
Cython: _cython_magic_11b25028d61ca29697d68d22b6117b41.pyx

Generated by Cython 0.29.7

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: cimport numpy
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+02: import numpy
  __pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_numpy, __pyx_t_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

 03: cimport cython
 04:
 05: ctypedef numpy.float64_t FTYPE_t
 06:
 07: @cython.boundscheck(False)
 08: @cython.wraparound(False)
+09: def cy_cumsum(numpy.ndarray[FTYPE_t, ndim=1] data):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_11b25028d61ca29697d68d22b6117b41_1cy_cumsum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_11b25028d61ca29697d68d22b6117b41_1cy_cumsum = {"cy_cumsum", (PyCFunction)__pyx_pw_46_cython_magic_11b25028d61ca29697d68d22b6117b41_1cy_cumsum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_11b25028d61ca29697d68d22b6117b41_1cy_cumsum(PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_cumsum (wrapper)", 0);
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_data), __pyx_ptype_5numpy_ndarray, 1, "data", 0))) __PYX_ERR(0, 9, __pyx_L1_error)
__pyx_r = __pyx_pf_46_cython_magic_11b25028d61ca29697d68d22b6117b41_cy_cumsum(__pyx_self, ((PyArrayObject *)__pyx_v_data));

/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__pyx_r = NULL;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_11b25028d61ca29697d68d22b6117b41_cy_cumsum(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_data) {
int __pyx_v_n;
int __pyx_v_N;
PyArrayObject *__pyx_v_out = 0;
__pyx_t_5numpy_float64_t __pyx_v_s;
__Pyx_LocalBuf_ND __pyx_pybuffernd_data;
__Pyx_Buffer __pyx_pybuffer_data;
__Pyx_LocalBuf_ND __pyx_pybuffernd_out;
__Pyx_Buffer __pyx_pybuffer_out;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_cumsum", 0);
__pyx_pybuffer_out.pybuffer.buf = NULL;
__pyx_pybuffer_out.refcount = 0;
__pyx_pybuffernd_out.data = NULL;
__pyx_pybuffernd_out.rcbuffer = &__pyx_pybuffer_out;
__pyx_pybuffer_data.pybuffer.buf = NULL;
__pyx_pybuffer_data.refcount = 0;
__pyx_pybuffernd_data.data = NULL;
__pyx_pybuffernd_data.rcbuffer = &__pyx_pybuffer_data;
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_data.rcbuffer->pybuffer, (PyObject*)__pyx_v_data, &__Pyx_TypeInfo_nn___pyx_t_46_cython_magic_11b25028d61ca29697d68d22b6117b41_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 9, __pyx_L1_error)
}
__pyx_pybuffernd_data.diminfo[0].strides = __pyx_pybuffernd_data.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_data.diminfo[0].shape = __pyx_pybuffernd_data.rcbuffer->pybuffer.shape[0];
/* … */
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__Pyx_XDECREF(__pyx_t_3);
__Pyx_XDECREF(__pyx_t_4);
__Pyx_XDECREF(__pyx_t_5);
{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_out.rcbuffer->pybuffer);
__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
__pyx_r = NULL;
goto __pyx_L2;
__pyx_L0:;
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_out.rcbuffer->pybuffer);
__pyx_L2:;
__Pyx_XDECREF((PyObject *)__pyx_v_out);
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
/* … */
__pyx_tuple__8 = PyTuple_Pack(5, __pyx_n_s_data, __pyx_n_s_n, __pyx_n_s_N, __pyx_n_s_out, __pyx_n_s_s); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 9, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__8);
__Pyx_GIVEREF(__pyx_tuple__8);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_11b25028d61ca29697d68d22b6117b41_1cy_cumsum, NULL, __pyx_n_s_cython_magic_11b25028d61ca29697); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 9, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_cumsum, __pyx_t_1) < 0) __PYX_ERR(0, 9, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+10:     cdef int n, N = data.size
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_data), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_N = __pyx_t_2;

+11:     cdef numpy.ndarray[FTYPE_t, ndim=1] out = numpy.zeros(N, dtype=data.dtype)
  __Pyx_GetModuleGlobalName(__pyx_t_1, __pyx_n_s_numpy); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_zeros); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_3);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_N); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_4);
__Pyx_GIVEREF(__pyx_t_1);
PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_1);
__pyx_t_1 = 0;
__pyx_t_1 = __Pyx_PyDict_NewPresized(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_data), __pyx_n_s_dtype); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_5);
if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_dtype, __pyx_t_5) < 0) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
__pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_4, __pyx_t_1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 11, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_5);
__Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
__Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 11, __pyx_L1_error)
__pyx_t_6 = ((PyArrayObject *)__pyx_t_5);
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_out.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_46_cython_magic_11b25028d61ca29697d68d22b6117b41_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) {
__pyx_v_out = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_out.rcbuffer->pybuffer.buf = NULL;
__PYX_ERR(0, 11, __pyx_L1_error)
} else {__pyx_pybuffernd_out.diminfo[0].strides = __pyx_pybuffernd_out.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_out.diminfo[0].shape = __pyx_pybuffernd_out.rcbuffer->pybuffer.shape[0];
}
}
__pyx_t_6 = 0;
__pyx_v_out = ((PyArrayObject *)__pyx_t_5);
__pyx_t_5 = 0;

+12:     cdef numpy.float64_t s = 0.0
  __pyx_v_s = 0.0;

+13:     for n in range(N):
  __pyx_t_2 = __pyx_v_N;
__pyx_t_7 = __pyx_t_2;
for (__pyx_t_8 = 0; __pyx_t_8 < __pyx_t_7; __pyx_t_8+=1) {
__pyx_v_n = __pyx_t_8;

+14:         s += data[n]
    __pyx_t_9 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_46_cython_magic_11b25028d61ca29697d68d22b6117b41_FTYPE_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_data.diminfo[0].strides)));

+15:         out[n] = s
    __pyx_t_10 = __pyx_v_n;
*__Pyx_BufPtrStrided1d(__pyx_t_46_cython_magic_11b25028d61ca29697d68d22b6117b41_FTYPE_t *, __pyx_pybuffernd_out.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_out.diminfo[0].strides) = __pyx_v_s;
}

+16:     return out
  __Pyx_XDECREF(__pyx_r);
__Pyx_INCREF(((PyObject *)__pyx_v_out));
__pyx_r = ((PyObject *)__pyx_v_out);
goto __pyx_L0;

In [94]:
%timeit py_cumsum(data)

13.8 ms ± 163 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [95]:
%timeit cy_cumsum(data)

70.2 µs ± 897 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [96]:
%timeit jit_cumsum(data)

64.9 µs ± 404 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [97]:
%timeit np.cumsum(data)

151 µs ± 728 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [98]:
assert np.allclose(cy_cumsum(data), np.cumsum(data))


## Fused types¶

In [99]:
py_sum([1.0, 2.0, 3.0, 4.0, 5.0])

Out[99]:
15.0
In [100]:
py_sum([1, 2, 3, 4, 5])

Out[100]:
15
In [101]:
cy_sum(np.array([1.0, 2.0, 3.0, 4.0, 5.0]))

Out[101]:
15.0
In [104]:
cy_sum(np.array([1, 2, 3, 4, 5]))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
----> 1 cy_sum(np.array([1, 2, 3, 4, 5]))

_cython_magic_ca1303e3f76e8cc692e7b9140687359d.pyx in _cython_magic_ca1303e3f76e8cc692e7b9140687359d.cy_sum()

ValueError: Buffer dtype mismatch, expected 'float64_t' but got 'long'
In [105]:
%%cython -a
cimport numpy
cimport cython

ctypedef fused I_OR_F_t:
numpy.int64_t
numpy.float64_t

@cython.boundscheck(False)
@cython.wraparound(False)
def cy_fused_sum(numpy.ndarray[I_OR_F_t, ndim=1] data):
cdef I_OR_F_t s = 0
cdef int n, N = data.size
for n in range(N):
s += data[n]
return s

Out[105]:
Cython: _cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6.pyx

Generated by Cython 0.29.7

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: cimport numpy
  __pyx_t_3 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_3);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_3) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;

 02: cimport cython
 03:
 04: ctypedef fused I_OR_F_t:
 05:     numpy.int64_t
 06:     numpy.float64_t
 07:
 08: @cython.boundscheck(False)
 09: @cython.wraparound(False)
+10: def cy_fused_sum(numpy.ndarray[I_OR_F_t, ndim=1] data):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_1cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_1cy_fused_sum = {"cy_fused_sum", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_1cy_fused_sum, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_1cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
PyObject *__pyx_v_signatures = 0;
PyObject *__pyx_v_args = 0;
PyObject *__pyx_v_kwargs = 0;
CYTHON_UNUSED PyObject *__pyx_v_defaults = 0;
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("__pyx_fused_cpdef (wrapper)", 0);
{
static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_signatures,&__pyx_n_s_args,&__pyx_n_s_kwargs,&__pyx_n_s_defaults,0};
PyObject* values[4] = {0,0,0,0};
if (unlikely(__pyx_kwds)) {
Py_ssize_t kw_args;
const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
switch (pos_args) {
case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
CYTHON_FALLTHROUGH;
case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
CYTHON_FALLTHROUGH;
case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
CYTHON_FALLTHROUGH;
case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
CYTHON_FALLTHROUGH;
case  0: break;
default: goto __pyx_L5_argtuple_error;
}
kw_args = PyDict_Size(__pyx_kwds);
switch (pos_args) {
case  0:
if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_signatures)) != 0)) kw_args--;
else goto __pyx_L5_argtuple_error;
CYTHON_FALLTHROUGH;
case  1:
if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_args)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 1); __PYX_ERR(0, 10, __pyx_L3_error)
}
CYTHON_FALLTHROUGH;
case  2:
if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_kwargs)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 2); __PYX_ERR(0, 10, __pyx_L3_error)
}
CYTHON_FALLTHROUGH;
case  3:
if (likely((values[3] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_defaults)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 3); __PYX_ERR(0, 10, __pyx_L3_error)
}
}
if (unlikely(kw_args > 0)) {
if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__pyx_fused_cpdef") < 0)) __PYX_ERR(0, 10, __pyx_L3_error)
}
} else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
goto __pyx_L5_argtuple_error;
} else {
values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
}
__pyx_v_signatures = values[0];
__pyx_v_args = values[1];
__pyx_v_kwargs = values[2];
__pyx_v_defaults = values[3];
}
goto __pyx_L4_argument_unpacking_done;
__pyx_L5_argtuple_error:;
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 10, __pyx_L3_error)
__pyx_L3_error:;
__Pyx_RefNannyFinishContext();
return NULL;
__pyx_L4_argument_unpacking_done:;
__pyx_r = __pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_cy_fused_sum(__pyx_self, __pyx_v_signatures, __pyx_v_args, __pyx_v_kwargs, __pyx_v_defaults);

/* function exit code */
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_cy_fused_sum(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_signatures, PyObject *__pyx_v_args, PyObject *__pyx_v_kwargs, CYTHON_UNUSED PyObject *__pyx_v_defaults) {
PyObject *__pyx_v_dest_sig = NULL;
Py_ssize_t __pyx_v_i;
PyTypeObject *__pyx_v_ndarray = 0;
__Pyx_memviewslice __pyx_v_memslice;
Py_ssize_t __pyx_v_itemsize;
int __pyx_v_dtype_signed;
char __pyx_v_kind;
int __pyx_v____pyx_int64_t_is_signed;
PyObject *__pyx_v_arg = NULL;
PyObject *__pyx_v_dtype = NULL;
PyObject *__pyx_v_arg_base = NULL;
PyObject *__pyx_v_candidates = NULL;
PyObject *__pyx_v_sig = NULL;
int __pyx_v_match_found;
PyObject *__pyx_v_src_sig = NULL;
PyObject *__pyx_v_dst_type = NULL;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_fused_sum", 0);
__Pyx_INCREF(__pyx_v_kwargs);
__pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_INCREF(Py_None);
__Pyx_GIVEREF(Py_None);
PyList_SET_ITEM(__pyx_t_1, 0, Py_None);
__pyx_v_dest_sig = ((PyObject*)__pyx_t_1);
__pyx_t_1 = 0;
__pyx_t_3 = (__pyx_v_kwargs != Py_None);
__pyx_t_4 = (__pyx_t_3 != 0);
if (__pyx_t_4) {
} else {
__pyx_t_2 = __pyx_t_4;
goto __pyx_L4_bool_binop_done;
}
__pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_v_kwargs); if (unlikely(__pyx_t_4 < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_3 = ((!__pyx_t_4) != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L4_bool_binop_done:;
if (__pyx_t_2) {
__Pyx_INCREF(Py_None);
__Pyx_DECREF_SET(__pyx_v_kwargs, Py_None);
}
__pyx_t_1 = ((PyObject *)__Pyx_ImportNumPyArrayTypeIfAvailable()); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_v_ndarray = ((PyTypeObject*)__pyx_t_1);
__pyx_t_1 = 0;
__pyx_v_itemsize = -1L;
__pyx_v____pyx_int64_t_is_signed = (!((((__pyx_t_5numpy_int64_t)-1L) > 0) != 0));
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_5 = PyTuple_GET_SIZE(((PyObject*)__pyx_v_args)); if (unlikely(__pyx_t_5 == ((Py_ssize_t)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_2 = ((0 < __pyx_t_5) != 0);
if (__pyx_t_2) {
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_1 = PyTuple_GET_ITEM(((PyObject*)__pyx_v_args), 0);
__Pyx_INCREF(__pyx_t_1);
__pyx_v_arg = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L6;
}
__pyx_t_3 = (__pyx_v_kwargs != Py_None);
__pyx_t_4 = (__pyx_t_3 != 0);
if (__pyx_t_4) {
} else {
__pyx_t_2 = __pyx_t_4;
goto __pyx_L7_bool_binop_done;
}
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_4 = (__Pyx_PyDict_ContainsTF(__pyx_n_s_data, ((PyObject*)__pyx_v_kwargs), Py_EQ)); if (unlikely(__pyx_t_4 < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_3 = (__pyx_t_4 != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L7_bool_binop_done:;
if (__pyx_t_2) {
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_1 = __Pyx_PyDict_GetItem(((PyObject*)__pyx_v_kwargs), __pyx_n_s_data); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_v_arg = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L6;
}
/*else*/ {
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_5 = PyTuple_GET_SIZE(((PyObject*)__pyx_v_args)); if (unlikely(__pyx_t_5 == ((Py_ssize_t)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_1 = PyInt_FromSsize_t(__pyx_t_5); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_6 = PyTuple_New(3); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_INCREF(__pyx_int_1);
__Pyx_GIVEREF(__pyx_int_1);
PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_int_1);
__Pyx_INCREF(__pyx_kp_s_);
__Pyx_GIVEREF(__pyx_kp_s_);
PyTuple_SET_ITEM(__pyx_t_6, 1, __pyx_kp_s_);
__Pyx_GIVEREF(__pyx_t_1);
PyTuple_SET_ITEM(__pyx_t_6, 2, __pyx_t_1);
__pyx_t_1 = 0;
__pyx_t_1 = __Pyx_PyString_Format(__pyx_kp_s_Expected_at_least_d_argument_s_g, __pyx_t_6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_t_6 = __Pyx_PyObject_CallOneArg(__pyx_builtin_TypeError, __pyx_t_1); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__Pyx_Raise(__pyx_t_6, 0, 0, 0);
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_L6:;
while (1) {
__pyx_t_2 = (__pyx_v_ndarray != ((PyTypeObject*)Py_None));
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_3 = __Pyx_TypeCheck(__pyx_v_arg, __pyx_v_ndarray);
__pyx_t_2 = (__pyx_t_3 != 0);
if (__pyx_t_2) {
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_dtype); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_v_dtype = __pyx_t_6;
__pyx_t_6 = 0;
goto __pyx_L12;
}
__pyx_t_2 = __pyx_memoryview_check(__pyx_v_arg);
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_base); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_v_arg_base = __pyx_t_6;
__pyx_t_6 = 0;
__pyx_t_3 = __Pyx_TypeCheck(__pyx_v_arg_base, __pyx_v_ndarray);
__pyx_t_2 = (__pyx_t_3 != 0);
if (__pyx_t_2) {
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg_base, __pyx_n_s_dtype); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_v_dtype = __pyx_t_6;
__pyx_t_6 = 0;
goto __pyx_L13;
}
/*else*/ {
__Pyx_INCREF(Py_None);
__pyx_v_dtype = Py_None;
}
__pyx_L13:;
goto __pyx_L12;
}
/*else*/ {
__Pyx_INCREF(Py_None);
__pyx_v_dtype = Py_None;
}
__pyx_L12:;
__pyx_v_itemsize = -1L;
__pyx_t_2 = (__pyx_v_dtype != Py_None);
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_dtype, __pyx_n_s_itemsize); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_t_6); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_v_itemsize = __pyx_t_5;
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_dtype, __pyx_n_s_kind); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_t_7 = __Pyx_PyObject_Ord(__pyx_t_6); if (unlikely(__pyx_t_7 == ((long)(long)(Py_UCS4)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_v_kind = __pyx_t_7;
__pyx_v_dtype_signed = (__pyx_v_kind == 'i');
switch (__pyx_v_kind) {
case 'i':
case 'u':
__pyx_t_2 = (((sizeof(__pyx_t_5numpy_int64_t)) == __pyx_v_itemsize) != 0);
if (__pyx_t_2) {
} else {
__pyx_t_3 = __pyx_t_2;
goto __pyx_L16_bool_binop_done;
}
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_ndim); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_t_6); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_t_2 = ((((Py_ssize_t)__pyx_t_5) == 1) != 0);
if (__pyx_t_2) {
} else {
__pyx_t_3 = __pyx_t_2;
goto __pyx_L16_bool_binop_done;
}
__pyx_t_2 = ((!((__pyx_v____pyx_int64_t_is_signed ^ __pyx_v_dtype_signed) != 0)) != 0);
__pyx_t_3 = __pyx_t_2;
__pyx_L16_bool_binop_done:;
if (__pyx_t_3) {
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, __pyx_n_s_int64_t, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
goto __pyx_L10_break;
}
break;
case 'f':
__pyx_t_2 = (((sizeof(__pyx_t_5numpy_float64_t)) == __pyx_v_itemsize) != 0);
if (__pyx_t_2) {
} else {
__pyx_t_3 = __pyx_t_2;
goto __pyx_L20_bool_binop_done;
}
__pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_ndim); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_t_5 = __Pyx_PyIndex_AsSsize_t(__pyx_t_6); if (unlikely((__pyx_t_5 == (Py_ssize_t)-1) && PyErr_Occurred())) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_t_2 = ((((Py_ssize_t)__pyx_t_5) == 1) != 0);
__pyx_t_3 = __pyx_t_2;
__pyx_L20_bool_binop_done:;
if (__pyx_t_3) {
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, __pyx_n_s_float64_t, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
goto __pyx_L10_break;
}
break;
case 'c':
break;
case 'O':
break;
default: break;
}
}
}
__pyx_t_2 = ((__pyx_v_itemsize == -1L) != 0);
if (!__pyx_t_2) {
} else {
__pyx_t_3 = __pyx_t_2;
goto __pyx_L23_bool_binop_done;
}
__pyx_t_2 = ((__pyx_v_itemsize == (sizeof(__pyx_t_5numpy_int64_t))) != 0);
__pyx_t_3 = __pyx_t_2;
__pyx_L23_bool_binop_done:;
if (__pyx_t_3) {
__pyx_t_8 = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_int64_t(__pyx_v_arg, 0);
__pyx_v_memslice = __pyx_t_8;
__pyx_t_3 = (__pyx_v_memslice.memview != 0);
if (__pyx_t_3) {
__PYX_XDEC_MEMVIEW((&__pyx_v_memslice), 1);
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, __pyx_n_s_int64_t, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
goto __pyx_L10_break;
}
/*else*/ {
PyErr_Clear();
}
}
__pyx_t_2 = ((__pyx_v_itemsize == -1L) != 0);
if (!__pyx_t_2) {
} else {
__pyx_t_3 = __pyx_t_2;
goto __pyx_L27_bool_binop_done;
}
__pyx_t_2 = ((__pyx_v_itemsize == (sizeof(__pyx_t_5numpy_float64_t))) != 0);
__pyx_t_3 = __pyx_t_2;
__pyx_L27_bool_binop_done:;
if (__pyx_t_3) {
__pyx_t_8 = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_float64_t(__pyx_v_arg, 0);
__pyx_v_memslice = __pyx_t_8;
__pyx_t_3 = (__pyx_v_memslice.memview != 0);
if (__pyx_t_3) {
__PYX_XDEC_MEMVIEW((&__pyx_v_memslice), 1);
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, __pyx_n_s_float64_t, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
goto __pyx_L10_break;
}
/*else*/ {
PyErr_Clear();
}
}
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, Py_None, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
goto __pyx_L10_break;
}
__pyx_L10_break:;
__pyx_t_6 = PyList_New(0); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_v_candidates = ((PyObject*)__pyx_t_6);
__pyx_t_6 = 0;
__pyx_t_5 = 0;
if (unlikely(__pyx_v_signatures == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_1 = __Pyx_dict_iterator(((PyObject*)__pyx_v_signatures), 1, ((PyObject *)NULL), (&__pyx_t_9), (&__pyx_t_10)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_XDECREF(__pyx_t_6);
__pyx_t_6 = __pyx_t_1;
__pyx_t_1 = 0;
while (1) {
__pyx_t_11 = __Pyx_dict_iter_next(__pyx_t_6, __pyx_t_9, &__pyx_t_5, &__pyx_t_1, NULL, NULL, __pyx_t_10);
if (unlikely(__pyx_t_11 == 0)) break;
if (unlikely(__pyx_t_11 == -1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_XDECREF_SET(__pyx_v_sig, __pyx_t_1);
__pyx_t_1 = 0;
__pyx_v_match_found = 0;
__pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_v_sig, __pyx_n_s_strip); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_13);
__pyx_t_14 = NULL;
if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_13))) {
__pyx_t_14 = PyMethod_GET_SELF(__pyx_t_13);
if (likely(__pyx_t_14)) {
PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_13);
__Pyx_INCREF(__pyx_t_14);
__Pyx_INCREF(function);
__Pyx_DECREF_SET(__pyx_t_13, function);
}
}
__pyx_t_12 = (__pyx_t_14) ? __Pyx_PyObject_Call2Args(__pyx_t_13, __pyx_t_14, __pyx_kp_s__2) : __Pyx_PyObject_CallOneArg(__pyx_t_13, __pyx_kp_s__2);
__Pyx_XDECREF(__pyx_t_14); __pyx_t_14 = 0;
if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_12);
__Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
__pyx_t_13 = __Pyx_PyObject_GetAttrStr(__pyx_t_12, __pyx_n_s_split); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_13);
__Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;
__pyx_t_12 = NULL;
if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_13))) {
__pyx_t_12 = PyMethod_GET_SELF(__pyx_t_13);
if (likely(__pyx_t_12)) {
PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_13);
__Pyx_INCREF(__pyx_t_12);
__Pyx_INCREF(function);
__Pyx_DECREF_SET(__pyx_t_13, function);
}
}
__pyx_t_1 = (__pyx_t_12) ? __Pyx_PyObject_Call2Args(__pyx_t_13, __pyx_t_12, __pyx_kp_s__3) : __Pyx_PyObject_CallOneArg(__pyx_t_13, __pyx_kp_s__3);
__Pyx_XDECREF(__pyx_t_12); __pyx_t_12 = 0;
if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
__Pyx_XDECREF_SET(__pyx_v_src_sig, __pyx_t_1);
__pyx_t_1 = 0;
__pyx_t_15 = PyList_GET_SIZE(__pyx_v_dest_sig); if (unlikely(__pyx_t_15 == ((Py_ssize_t)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_16 = __pyx_t_15;
for (__pyx_t_17 = 0; __pyx_t_17 < __pyx_t_16; __pyx_t_17+=1) {
__pyx_v_i = __pyx_t_17;
__pyx_t_1 = PyList_GET_ITEM(__pyx_v_dest_sig, __pyx_v_i);
__Pyx_INCREF(__pyx_t_1);
__Pyx_XDECREF_SET(__pyx_v_dst_type, __pyx_t_1);
__pyx_t_1 = 0;
__pyx_t_3 = (__pyx_v_dst_type != Py_None);
__pyx_t_2 = (__pyx_t_3 != 0);
if (__pyx_t_2) {
__pyx_t_1 = __Pyx_GetItemInt(__pyx_v_src_sig, __pyx_v_i, Py_ssize_t, 1, PyInt_FromSsize_t, 0, 0, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_13 = PyObject_RichCompare(__pyx_t_1, __pyx_v_dst_type, Py_EQ); __Pyx_XGOTREF(__pyx_t_13); if (unlikely(!__pyx_t_13)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_13); if (unlikely(__pyx_t_2 < 0)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_13); __pyx_t_13 = 0;
if (__pyx_t_2) {
__pyx_v_match_found = 1;
goto __pyx_L35;
}
/*else*/ {
__pyx_v_match_found = 0;
goto __pyx_L33_break;
}
__pyx_L35:;
}
}
__pyx_L33_break:;
__pyx_t_2 = (__pyx_v_match_found != 0);
if (__pyx_t_2) {
__pyx_t_18 = __Pyx_PyList_Append(__pyx_v_candidates, __pyx_v_sig); if (unlikely(__pyx_t_18 == ((int)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
}
}
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__pyx_t_2 = (PyList_GET_SIZE(__pyx_v_candidates) != 0);
__pyx_t_3 = ((!__pyx_t_2) != 0);
if (__pyx_t_3) {
__pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_Raise(__pyx_t_6, 0, 0, 0);
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_9 = PyList_GET_SIZE(__pyx_v_candidates); if (unlikely(__pyx_t_9 == ((Py_ssize_t)-1))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_t_3 = ((__pyx_t_9 > 1) != 0);
if (__pyx_t_3) {
/* … */
__pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_No_matching_signature_found); if (unlikely(!__pyx_tuple__4)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__4);
__Pyx_GIVEREF(__pyx_tuple__4);
__pyx_t_6 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_Raise(__pyx_t_6, 0, 0, 0);
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
__PYX_ERR(0, 10, __pyx_L1_error)
}
/*else*/ {
__Pyx_XDECREF(__pyx_r);
if (unlikely(__pyx_v_signatures == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_t_6 = __Pyx_PyDict_GetItem(((PyObject*)__pyx_v_signatures), PyList_GET_ITEM(__pyx_v_candidates, 0)); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__pyx_r = __pyx_t_6;
__pyx_t_6 = 0;
goto __pyx_L0;
}

/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__Pyx_XDECREF(__pyx_t_6);
__Pyx_XDECREF(__pyx_t_12);
__Pyx_XDECREF(__pyx_t_13);
__Pyx_XDECREF(__pyx_t_14);
__pyx_r = NULL;
__pyx_L0:;
__Pyx_XDECREF(__pyx_v_dest_sig);
__Pyx_XDECREF(__pyx_v_ndarray);
__Pyx_XDECREF(__pyx_v_arg);
__Pyx_XDECREF(__pyx_v_dtype);
__Pyx_XDECREF(__pyx_v_arg_base);
__Pyx_XDECREF(__pyx_v_candidates);
__Pyx_XDECREF(__pyx_v_sig);
__Pyx_XDECREF(__pyx_v_src_sig);
__Pyx_XDECREF(__pyx_v_dst_type);
__Pyx_XDECREF(__pyx_v_kwargs);
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_fuse_0__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_3cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_fuse_0__pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_3cy_fused_sum = {"__pyx_fuse_0cy_fused_sum", (PyCFunction)__pyx_fuse_0__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_3cy_fused_sum, METH_O, 0};
static PyObject *__pyx_fuse_0__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_3cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_fused_sum (wrapper)", 0);
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_data), __pyx_ptype_5numpy_ndarray, 1, "data", 0))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_r = __pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_2cy_fused_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));

/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__pyx_r = NULL;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_2cy_fused_sum(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_data) {
__pyx_t_5numpy_int64_t __pyx_v_s;
int __pyx_v_n;
int __pyx_v_N;
__Pyx_LocalBuf_ND __pyx_pybuffernd_data;
__Pyx_Buffer __pyx_pybuffer_data;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("__pyx_fuse_0cy_fused_sum", 0);
__pyx_pybuffer_data.pybuffer.buf = NULL;
__pyx_pybuffer_data.refcount = 0;
__pyx_pybuffernd_data.data = NULL;
__pyx_pybuffernd_data.rcbuffer = &__pyx_pybuffer_data;
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_data.rcbuffer->pybuffer, (PyObject*)__pyx_v_data, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int64_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_pybuffernd_data.diminfo[0].strides = __pyx_pybuffernd_data.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_data.diminfo[0].shape = __pyx_pybuffernd_data.rcbuffer->pybuffer.shape[0];
/* … */
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
__pyx_r = NULL;
goto __pyx_L2;
__pyx_L0:;
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__pyx_L2:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_fuse_1__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_5cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_fuse_1__pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_5cy_fused_sum = {"__pyx_fuse_1cy_fused_sum", (PyCFunction)__pyx_fuse_1__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_5cy_fused_sum, METH_O, 0};
static PyObject *__pyx_fuse_1__pyx_pw_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_5cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data) {
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_fused_sum (wrapper)", 0);
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_data), __pyx_ptype_5numpy_ndarray, 1, "data", 0))) __PYX_ERR(0, 10, __pyx_L1_error)
__pyx_r = __pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_4cy_fused_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));

/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__pyx_r = NULL;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_4cy_fused_sum(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_data) {
__pyx_t_5numpy_float64_t __pyx_v_s;
int __pyx_v_n;
int __pyx_v_N;
__Pyx_LocalBuf_ND __pyx_pybuffernd_data;
__Pyx_Buffer __pyx_pybuffer_data;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("__pyx_fuse_1cy_fused_sum", 0);
__pyx_pybuffer_data.pybuffer.buf = NULL;
__pyx_pybuffer_data.refcount = 0;
__pyx_pybuffernd_data.data = NULL;
__pyx_pybuffernd_data.rcbuffer = &__pyx_pybuffer_data;
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_data.rcbuffer->pybuffer, (PyObject*)__pyx_v_data, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float64_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 10, __pyx_L1_error)
}
__pyx_pybuffernd_data.diminfo[0].strides = __pyx_pybuffernd_data.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_data.diminfo[0].shape = __pyx_pybuffernd_data.rcbuffer->pybuffer.shape[0];
/* … */
/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
__pyx_r = NULL;
goto __pyx_L2;
__pyx_L0:;
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer);
__pyx_L2:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
__pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_Function_call_with_ambiguous_arg); if (unlikely(!__pyx_tuple__5)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__5);
__Pyx_GIVEREF(__pyx_tuple__5);
/* … */
__pyx_tuple__31 = PyTuple_Pack(4, __pyx_n_s_data, __pyx_n_s_s, __pyx_n_s_n, __pyx_n_s_N); if (unlikely(!__pyx_tuple__31)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__31);
__Pyx_GIVEREF(__pyx_tuple__31);
/* … */
__pyx_t_1 = __Pyx_PyDict_NewPresized(2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __pyx_FusedFunction_NewEx(&__pyx_fuse_0__pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_3cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_b4146ff13fa5f55c0c, __pyx_d, ((PyObject *)__pyx_codeobj__32)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_CyFunction_SetDefaultsTuple(__pyx_t_2, __pyx_empty_tuple);
if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_int64_t, __pyx_t_2) < 0) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_2 = __pyx_FusedFunction_NewEx(&__pyx_fuse_1__pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_5cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_b4146ff13fa5f55c0c, __pyx_d, ((PyObject *)__pyx_codeobj__32)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_CyFunction_SetDefaultsTuple(__pyx_t_2, __pyx_empty_tuple);
if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_float64_t, __pyx_t_2) < 0) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_2 = __pyx_FusedFunction_NewEx(&__pyx_mdef_46_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6_1cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_b4146ff13fa5f55c0c, __pyx_d, ((PyObject *)__pyx_codeobj__32)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_CyFunction_SetDefaultsTuple(__pyx_t_2, __pyx_empty_tuple);
((__pyx_FusedFunctionObject *) __pyx_t_2)->__signatures__ = __pyx_t_1;
__Pyx_GIVEREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_fused_sum, __pyx_t_2) < 0) __PYX_ERR(0, 10, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_codeobj__32 = (PyObject*)__Pyx_PyCode_New(1, 0, 4, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__31, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_rob_ipython_cython__cytho, __pyx_n_s_cy_fused_sum, 10, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__32)) __PYX_ERR(0, 10, __pyx_L1_error)

+11:     cdef I_OR_F_t s = 0
  __pyx_v_s = 0;
/* … */
__pyx_v_s = 0.0;

+12:     cdef int n, N = data.size
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_data), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_N = __pyx_t_2;
/* … */
__pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_data), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_N = __pyx_t_2;

+13:     for n in range(N):
  __pyx_t_2 = __pyx_v_N;
__pyx_t_3 = __pyx_t_2;
for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
__pyx_v_n = __pyx_t_4;
/* … */
__pyx_t_2 = __pyx_v_N;
__pyx_t_3 = __pyx_t_2;
for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
__pyx_v_n = __pyx_t_4;

+14:         s += data[n]
    __pyx_t_5 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int64_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_5, __pyx_pybuffernd_data.diminfo[0].strides)));
}
/* … */
__pyx_t_5 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_5, __pyx_pybuffernd_data.diminfo[0].strides)));
}

+15:     return s
  __Pyx_XDECREF(__pyx_r);
__pyx_t_1 = __Pyx_PyInt_From_npy_int64(__pyx_v_s); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;
/* … */
__Pyx_XDECREF(__pyx_r);
__pyx_t_1 = PyFloat_FromDouble(__pyx_v_s); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;

In [106]:
cy_fused_sum(np.array([1.0, 2.0, 3.0, 4.0, 5.0]))

Out[106]:
15.0
In [107]:
cy_fused_sum(np.array([1, 2, 3, 4, 5]))

Out[107]:
15

## Julia fractal¶

In [108]:
%%cython -a
cimport numpy
cimport cython

ctypedef numpy.int64_t ITYPE_t
ctypedef numpy.float64_t FTYPE_t

cpdef inline double abs2(double complex z):
return z.real * z.real + z.imag * z.imag

@cython.boundscheck(False)
@cython.wraparound(False)
def cy_julia_fractal(numpy.ndarray[FTYPE_t, ndim=1] z_re,
numpy.ndarray[FTYPE_t, ndim=1] z_im,
numpy.ndarray[ITYPE_t, ndim=2] j):
cdef int m, n, t, M = z_re.size, N = z_im.size
cdef double complex z
for m in range(M):
for n in range(N):
z = z_re[m] + 1.0j * z_im[n]
for t in range(256):
z = z ** 2 - 0.05 + 0.68j
if abs2(z) > 4.0:
j[m, n] = t
break

Out[108]:
Cython: _cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6.pyx

Generated by Cython 0.29.7

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: cimport numpy
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

 02: cimport cython
 03:
+04: ctypedef numpy.int64_t ITYPE_t
typedef __pyx_t_5numpy_int64_t __pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_ITYPE_t;

 05: ctypedef numpy.float64_t FTYPE_t
 06:
+07: cpdef inline double abs2(double complex z):
static PyObject *__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_1abs2(PyObject *__pyx_self, PyObject *__pyx_arg_z); /*proto*/
static CYTHON_INLINE double __pyx_f_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_abs2(__pyx_t_double_complex __pyx_v_z, CYTHON_UNUSED int __pyx_skip_dispatch) {
double __pyx_r;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("abs2", 0);
/* … */
/* function exit code */
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_1abs2(PyObject *__pyx_self, PyObject *__pyx_arg_z); /*proto*/
static PyObject *__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_1abs2(PyObject *__pyx_self, PyObject *__pyx_arg_z) {
__pyx_t_double_complex __pyx_v_z;
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("abs2 (wrapper)", 0);
assert(__pyx_arg_z); {
__pyx_v_z = __Pyx_PyComplex_As___pyx_t_double_complex(__pyx_arg_z); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 7, __pyx_L3_error)
}
goto __pyx_L4_argument_unpacking_done;
__pyx_L3_error:;
__Pyx_RefNannyFinishContext();
return NULL;
__pyx_L4_argument_unpacking_done:;
__pyx_r = __pyx_pf_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_abs2(__pyx_self, __pyx_v_z);

/* function exit code */
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_abs2(CYTHON_UNUSED PyObject *__pyx_self, __pyx_t_double_complex __pyx_v_z) {
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("abs2", 0);
__Pyx_XDECREF(__pyx_r);
__pyx_t_1 = PyFloat_FromDouble(__pyx_f_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_abs2(__pyx_v_z, 0)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 7, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;

/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__pyx_r = NULL;
__pyx_L0:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

+08:     return z.real * z.real + z.imag * z.imag
  __pyx_r = ((__Pyx_CREAL(__pyx_v_z) * __Pyx_CREAL(__pyx_v_z)) + (__Pyx_CIMAG(__pyx_v_z) * __Pyx_CIMAG(__pyx_v_z)));
goto __pyx_L0;

 09:
 10: @cython.boundscheck(False)
 11: @cython.wraparound(False)
+12: def cy_julia_fractal(numpy.ndarray[FTYPE_t, ndim=1] z_re,
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_3cy_julia_fractal(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_3cy_julia_fractal = {"cy_julia_fractal", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_3cy_julia_fractal, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_3cy_julia_fractal(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
PyArrayObject *__pyx_v_z_re = 0;
PyArrayObject *__pyx_v_z_im = 0;
PyArrayObject *__pyx_v_j = 0;
PyObject *__pyx_r = 0;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_julia_fractal (wrapper)", 0);
{
static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_z_re,&__pyx_n_s_z_im,&__pyx_n_s_j,0};
PyObject* values[3] = {0,0,0};
if (unlikely(__pyx_kwds)) {
Py_ssize_t kw_args;
const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
switch (pos_args) {
case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
CYTHON_FALLTHROUGH;
case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
CYTHON_FALLTHROUGH;
case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
CYTHON_FALLTHROUGH;
case  0: break;
default: goto __pyx_L5_argtuple_error;
}
kw_args = PyDict_Size(__pyx_kwds);
switch (pos_args) {
case  0:
if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_z_re)) != 0)) kw_args--;
else goto __pyx_L5_argtuple_error;
CYTHON_FALLTHROUGH;
case  1:
if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_z_im)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("cy_julia_fractal", 1, 3, 3, 1); __PYX_ERR(0, 12, __pyx_L3_error)
}
CYTHON_FALLTHROUGH;
case  2:
if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_j)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("cy_julia_fractal", 1, 3, 3, 2); __PYX_ERR(0, 12, __pyx_L3_error)
}
}
if (unlikely(kw_args > 0)) {
if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cy_julia_fractal") < 0)) __PYX_ERR(0, 12, __pyx_L3_error)
}
} else if (PyTuple_GET_SIZE(__pyx_args) != 3) {
goto __pyx_L5_argtuple_error;
} else {
values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
}
__pyx_v_z_re = ((PyArrayObject *)values[0]);
__pyx_v_z_im = ((PyArrayObject *)values[1]);
__pyx_v_j = ((PyArrayObject *)values[2]);
}
goto __pyx_L4_argument_unpacking_done;
__pyx_L5_argtuple_error:;
__Pyx_RaiseArgtupleInvalid("cy_julia_fractal", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 12, __pyx_L3_error)
__pyx_L3_error:;
__Pyx_RefNannyFinishContext();
return NULL;
__pyx_L4_argument_unpacking_done:;
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_z_re), __pyx_ptype_5numpy_ndarray, 1, "z_re", 0))) __PYX_ERR(0, 12, __pyx_L1_error)
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_z_im), __pyx_ptype_5numpy_ndarray, 1, "z_im", 0))) __PYX_ERR(0, 13, __pyx_L1_error)
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_j), __pyx_ptype_5numpy_ndarray, 1, "j", 0))) __PYX_ERR(0, 14, __pyx_L1_error)
__pyx_r = __pyx_pf_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_2cy_julia_fractal(__pyx_self, __pyx_v_z_re, __pyx_v_z_im, __pyx_v_j);

/* function exit code */
goto __pyx_L0;
__pyx_L1_error:;
__pyx_r = NULL;
__pyx_L0:;
__Pyx_RefNannyFinishContext();
return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_2cy_julia_fractal(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_z_re, PyArrayObject *__pyx_v_z_im, PyArrayObject *__pyx_v_j) {
int __pyx_v_m;
int __pyx_v_n;
int __pyx_v_t;
int __pyx_v_M;
int __pyx_v_N;
__pyx_t_double_complex __pyx_v_z;
__Pyx_LocalBuf_ND __pyx_pybuffernd_j;
__Pyx_Buffer __pyx_pybuffer_j;
__Pyx_LocalBuf_ND __pyx_pybuffernd_z_im;
__Pyx_Buffer __pyx_pybuffer_z_im;
__Pyx_LocalBuf_ND __pyx_pybuffernd_z_re;
__Pyx_Buffer __pyx_pybuffer_z_re;
PyObject *__pyx_r = NULL;
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("cy_julia_fractal", 0);
__pyx_pybuffer_z_re.pybuffer.buf = NULL;
__pyx_pybuffer_z_re.refcount = 0;
__pyx_pybuffernd_z_re.data = NULL;
__pyx_pybuffernd_z_re.rcbuffer = &__pyx_pybuffer_z_re;
__pyx_pybuffer_z_im.pybuffer.buf = NULL;
__pyx_pybuffer_z_im.refcount = 0;
__pyx_pybuffernd_z_im.data = NULL;
__pyx_pybuffernd_z_im.rcbuffer = &__pyx_pybuffer_z_im;
__pyx_pybuffer_j.pybuffer.buf = NULL;
__pyx_pybuffer_j.refcount = 0;
__pyx_pybuffernd_j.data = NULL;
__pyx_pybuffernd_j.rcbuffer = &__pyx_pybuffer_j;
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_z_re.rcbuffer->pybuffer, (PyObject*)__pyx_v_z_re, &__Pyx_TypeInfo_nn___pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 12, __pyx_L1_error)
}
__pyx_pybuffernd_z_re.diminfo[0].strides = __pyx_pybuffernd_z_re.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_z_re.diminfo[0].shape = __pyx_pybuffernd_z_re.rcbuffer->pybuffer.shape[0];
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_z_im.rcbuffer->pybuffer, (PyObject*)__pyx_v_z_im, &__Pyx_TypeInfo_nn___pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 12, __pyx_L1_error)
}
__pyx_pybuffernd_z_im.diminfo[0].strides = __pyx_pybuffernd_z_im.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_z_im.diminfo[0].shape = __pyx_pybuffernd_z_im.rcbuffer->pybuffer.shape[0];
{
__Pyx_BufFmt_StackElem __pyx_stack[1];
if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_j.rcbuffer->pybuffer, (PyObject*)__pyx_v_j, &__Pyx_TypeInfo_nn___pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_ITYPE_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack) == -1)) __PYX_ERR(0, 12, __pyx_L1_error)
}
__pyx_pybuffernd_j.diminfo[0].strides = __pyx_pybuffernd_j.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_j.diminfo[0].shape = __pyx_pybuffernd_j.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_j.diminfo[1].strides = __pyx_pybuffernd_j.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_j.diminfo[1].shape = __pyx_pybuffernd_j.rcbuffer->pybuffer.shape[1];
/* … */
/* function exit code */
__pyx_r = Py_None; __Pyx_INCREF(Py_None);
goto __pyx_L0;
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;
__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_j.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_z_im.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_z_re.rcbuffer->pybuffer);
__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}
__pyx_r = NULL;
goto __pyx_L2;
__pyx_L0:;
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_j.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_z_im.rcbuffer->pybuffer);
__Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_z_re.rcbuffer->pybuffer);
__pyx_L2:;
__Pyx_XGIVEREF(__pyx_r);
__Pyx_RefNannyFinishContext();
return __pyx_r;
}
/* … */
__pyx_tuple__8 = PyTuple_Pack(9, __pyx_n_s_z_re, __pyx_n_s_z_im, __pyx_n_s_j, __pyx_n_s_m, __pyx_n_s_n, __pyx_n_s_t, __pyx_n_s_M, __pyx_n_s_N, __pyx_n_s_z); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_GOTREF(__pyx_tuple__8);
__Pyx_GIVEREF(__pyx_tuple__8);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_3cy_julia_fractal, NULL, __pyx_n_s_cython_magic_2d6248e2a8b34a7fee); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_julia_fractal, __pyx_t_1) < 0) __PYX_ERR(0, 12, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

 13:                      numpy.ndarray[FTYPE_t, ndim=1] z_im,
 14:                      numpy.ndarray[ITYPE_t, ndim=2] j):
+15:     cdef int m, n, t, M = z_re.size, N = z_im.size
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_z_re), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_M = __pyx_t_2;
__pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_z_im), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __Pyx_PyInt_As_int(__pyx_t_1); if (unlikely((__pyx_t_2 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 15, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_N = __pyx_t_2;

 16:     cdef double complex z
+17:     for m in range(M):
  __pyx_t_2 = __pyx_v_M;
__pyx_t_3 = __pyx_t_2;
for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {
__pyx_v_m = __pyx_t_4;

+18:         for n in range(N):
    __pyx_t_5 = __pyx_v_N;
__pyx_t_6 = __pyx_t_5;
for (__pyx_t_7 = 0; __pyx_t_7 < __pyx_t_6; __pyx_t_7+=1) {
__pyx_v_n = __pyx_t_7;

+19:             z = z_re[m] + 1.0j * z_im[n]
      __pyx_t_8 = __pyx_v_m;
__pyx_t_9 = __pyx_t_double_complex_from_parts(0, 1.0);
__pyx_t_10 = __pyx_v_n;
__pyx_t_11 = __Pyx_c_sum_npy_float64(__pyx_t_npy_float64_complex_from_parts((*__Pyx_BufPtrStrided1d(__pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_FTYPE_t *, __pyx_pybuffernd_z_re.rcbuffer->pybuffer.buf, __pyx_t_8, __pyx_pybuffernd_z_re.diminfo[0].strides)), 0), __Pyx_c_prod_npy_float64(__pyx_t_npy_float64_complex_from_parts(__Pyx_CREAL(__pyx_t_9), __Pyx_CIMAG(__pyx_t_9)), __pyx_t_npy_float64_complex_from_parts((*__Pyx_BufPtrStrided1d(__pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_FTYPE_t *, __pyx_pybuffernd_z_im.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_z_im.diminfo[0].strides)), 0)));
__pyx_v_z = __pyx_t_double_complex_from_parts(__Pyx_CREAL(__pyx_t_11), __Pyx_CIMAG(__pyx_t_11));

+20:             for t in range(256):
      for (__pyx_t_12 = 0; __pyx_t_12 < 0x100; __pyx_t_12+=1) {
__pyx_v_t = __pyx_t_12;

+21:                 z = z ** 2 - 0.05 + 0.68j
        __pyx_v_z = __Pyx_c_sum_double(__Pyx_c_diff_double(__Pyx_c_pow_double(__pyx_v_z, __pyx_t_double_complex_from_parts(2, 0)), __pyx_t_double_complex_from_parts(0.05, 0)), __pyx_t_double_complex_from_parts(0, 0.68));

+22:                 if abs2(z) > 4.0:
        __pyx_t_13 = ((__pyx_f_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_abs2(__pyx_v_z, 0) > 4.0) != 0);
if (__pyx_t_13) {
/* … */
}
}
__pyx_L8_break:;
}
}

+23:                     j[m, n] = t
          __pyx_t_14 = __pyx_v_m;
__pyx_t_15 = __pyx_v_n;
*__Pyx_BufPtrStrided2d(__pyx_t_46_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6_ITYPE_t *, __pyx_pybuffernd_j.rcbuffer->pybuffer.buf, __pyx_t_14, __pyx_pybuffernd_j.diminfo[0].strides, __pyx_t_15, __pyx_pybuffernd_j.diminfo[1].strides) = __pyx_v_t;

+24:                     break
          goto __pyx_L8_break;

In [109]:
N = 1024

In [110]:
j = np.zeros((N, N), dtype=np.int64)

In [111]:
z_real = np.linspace(-1.5, 1.5, N)

In [112]:
z_imag = np.linspace(-1.5, 1.5, N)

In [113]:
%timeit cy_julia_fractal(z_real, z_imag, j)

120 ms ± 1.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [114]:
%timeit jit_julia_fractal(z_real, z_imag, j)

136 ms ± 5.66 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [115]:
j1 = np.zeros((N, N), dtype=np.int64)

In [116]:
cy_julia_fractal(z_real, z_imag, j1)

In [117]:
j2 = np.zeros((N, N), dtype=np.int64)

In [118]:
jit_julia_fractal(z_real, z_imag, j2)

In [119]:
assert np.allclose(j1, j2)


## Calling C function¶

In [120]:
%%cython

cdef extern from "math.h":
double acos(double)

def cy_acos1(double x):
return acos(x)

In [121]:
%timeit cy_acos1(0.5)

77.9 ns ± 0.495 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [122]:
%%cython

from libc.math cimport acos

def cy_acos2(double x):
return acos(x)

In [123]:
%timeit cy_acos2(0.5)

92.4 ns ± 5.29 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [124]:
from numpy import arccos

In [125]:
%timeit arccos(0.5)

1.12 µs ± 39.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [126]:
from math import acos

In [127]:
%timeit acos(0.5)

95.6 ns ± 0.591 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [128]:
assert cy_acos1(0.5) == acos(0.5)

In [129]:
assert cy_acos2(0.5) == acos(0.5)


# Versions¶

In [130]:
%reload_ext version_information

In [131]:
%version_information numpy, cython, numba, matplotlib

Out[131]:
SoftwareVersion
Python3.6.8 64bit [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
IPython7.5.0
OSDarwin 18.5.0 x86_64 i386 64bit
numpy1.14.3
cython0.29.7
numba0.43.1
matplotlib3.0.3
Mon May 06 22:35:03 2019 JST