# Chapter 19: Code optimization¶

Robert Johansson

Source code listings for Numerical Python - A Practical Techniques Approach for Industry (ISBN 978-1-484205-54-9).

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)

100 loops, best of 3: 7.99 ms per loop

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

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

10000 loops, best of 3: 28.8 µs per loop

In [13]:
%timeit py_cumsum(data)

100 loops, best of 3: 13.1 ms per loop

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

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

10000 loops, best of 3: 146 µs per loop

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)

10000 loops, best of 3: 47.4 µs per loop

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)

10000 loops, best of 3: 78.9 µs per loop


## 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)

1 loops, best of 3: 59.6 s per loop

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

10 loops, best of 3: 126 ms per loop


## 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]

10 loops, best of 3: 20 ms per loop

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)

100 loops, best of 3: 12 ms per loop

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

In [36]:
%timeit np_Heaviside(x)

1000 loops, best of 3: 255 µs per loop

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)

10000 loops, best of 3: 43.6 µs per loop

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

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

    1519    5191   57293 cy_sum.c

In [44]:
%%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 [45]:
!python setup.py build_ext --inplace > /dev/null

cy_sum.c:1377:28: warning: unused function '__Pyx_PyObject_AsString' [-Wunused-function]
static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {
^
cy_sum.c:1374:32: warning: unused function '__Pyx_PyUnicode_FromString' [-Wunused-function]
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
^
cy_sum.c:322:29: warning: unused function '__Pyx_Py_UNICODE_strlen' [-Wunused-function]
static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
^
cy_sum.c:1439:26: warning: unused function '__Pyx_PyObject_IsTrue' [-Wunused-function]
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
^
cy_sum.c:1489:33: warning: unused function '__Pyx_PyIndex_AsSsize_t' [-Wunused-function]
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
^
cy_sum.c:1514:33: warning: unused function '__Pyx_PyInt_FromSize_t' [-Wunused-function]
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
^
cy_sum.c:1072:32: warning: unused function '__Pyx_PyInt_From_long' [-Wunused-function]
static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {
^
cy_sum.c:1119:27: warning: function '__Pyx_PyInt_As_long' is not needed and will not be emitted [-Wunneeded-internal-declaration]
static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {
^
cy_sum.c:1224:26: warning: function '__Pyx_PyInt_As_int' is not needed and will not be emitted [-Wunneeded-internal-declaration]
static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {
^
9 warnings generated.

In [46]:
from cy_sum import cy_sum

In [47]:
cy_sum(data)

Out[47]:
-189.70046227549025
In [48]:
%timeit cy_sum(data)

100 loops, best of 3: 4.96 ms per loop

In [49]:
%timeit py_sum(data)

100 loops, best of 3: 8.09 ms per loop

In [50]:
%%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 [51]:
pyximport.install(setup_args={'include_dirs': np.get_include()});

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

In [53]:
from cy_cumsum import cy_cumsum

In [54]:
%timeit cy_cumsum(data)

100 loops, best of 3: 6.32 ms per loop

In [55]:
%timeit py_cumsum(data)

100 loops, best of 3: 13.4 ms per loop


## Using IPython cython command¶

In [56]:
%load_ext cython

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

Out[57]:
Cython: _cython_magic_c879406be9ebc78f7d3a9c5050513ba5.pyx

Generated by Cython 0.22.1

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_c879406be9ebc78f7d3a9c5050513ba5_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_c879406be9ebc78f7d3a9c5050513ba5_1cy_sum = {"cy_sum", (PyCFunction)__pyx_pw_46_cython_magic_c879406be9ebc78f7d3a9c5050513ba5_1cy_sum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_c879406be9ebc78f7d3a9c5050513ba5_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_c879406be9ebc78f7d3a9c5050513ba5_cy_sum(__pyx_self, ((PyObject *)__pyx_v_data));

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

static PyObject *__pyx_pf_46_cython_magic_c879406be9ebc78f7d3a9c5050513ba5_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_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple_);
__Pyx_GIVEREF(__pyx_tuple_);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_c879406be9ebc78f7d3a9c5050513ba5_1cy_sum, NULL, __pyx_n_s_cython_magic_c879406be9ebc78f7d); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_sum, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_3 = Py_TYPE(__pyx_t_1)->tp_iternext; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __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_COMPILING_IN_CPYTHON
__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_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
#else
__pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_4);
#endif
} else {
if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;
#if CYTHON_COMPILING_IN_CPYTHON
__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_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
#else
__pyx_t_4 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __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(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __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 [58]:
%timeit cy_sum(data)

100 loops, best of 3: 4.97 ms per loop

In [59]:
%timeit py_sum(data)

100 loops, best of 3: 7.82 ms per loop

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

In [61]:
%%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 = len(data)
for n in range(N):
s += data[n]
return s

Out[61]:
Cython: _cython_magic_5634d0627d9a45c8eb4964708c73457e.pyx

Generated by Cython 0.22.1

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 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __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_5634d0627d9a45c8eb4964708c73457e_1cy_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_5634d0627d9a45c8eb4964708c73457e_1cy_sum = {"cy_sum", (PyCFunction)__pyx_pw_46_cython_magic_5634d0627d9a45c8eb4964708c73457e_1cy_sum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_5634d0627d9a45c8eb4964708c73457e_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_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_r = __pyx_pf_46_cython_magic_5634d0627d9a45c8eb4964708c73457e_cy_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));
CYTHON_UNUSED int __pyx_lineno = 0;
CYTHON_UNUSED const char *__pyx_filename = NULL;
CYTHON_UNUSED int __pyx_clineno = 0;

/* 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_5634d0627d9a45c8eb4964708c73457e_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_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __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_5);
{ 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__7 = PyTuple_Pack(4, __pyx_n_s_data, __pyx_n_s_s, __pyx_n_s_n, __pyx_n_s_N); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__7);
__Pyx_GIVEREF(__pyx_tuple__7);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_5634d0627d9a45c8eb4964708c73457e_1cy_sum, NULL, __pyx_n_s_cython_magic_5634d0627d9a45c8eb); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_sum, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __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 = len(data)
  __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_v_data)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_v_N = __pyx_t_1;

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

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

+11:     return s
  __Pyx_XDECREF(__pyx_r);
__pyx_t_5 = PyFloat_FromDouble(__pyx_v_s); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_5);
__pyx_r = __pyx_t_5;
__pyx_t_5 = 0;
goto __pyx_L0;

In [62]:
%timeit cy_sum(data)

10000 loops, best of 3: 47.5 µs per loop

In [63]:
%timeit jit_sum(data)

10000 loops, best of 3: 47.3 µs per loop

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

10000 loops, best of 3: 28.4 µs per loop


## Cummulative sum¶

In [65]:
%%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 = 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[65]:
Cython: _cython_magic_8f49b2e306583689516129454767dc6c.pyx

Generated by Cython 0.22.1

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 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __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, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_numpy, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __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_8f49b2e306583689516129454767dc6c_1cy_cumsum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_8f49b2e306583689516129454767dc6c_1cy_cumsum = {"cy_cumsum", (PyCFunction)__pyx_pw_46_cython_magic_8f49b2e306583689516129454767dc6c_1cy_cumsum, METH_O, 0};
static PyObject *__pyx_pw_46_cython_magic_8f49b2e306583689516129454767dc6c_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_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_r = __pyx_pf_46_cython_magic_8f49b2e306583689516129454767dc6c_cy_cumsum(__pyx_self, ((PyArrayObject *)__pyx_v_data));
CYTHON_UNUSED int __pyx_lineno = 0;
CYTHON_UNUSED const char *__pyx_filename = NULL;
CYTHON_UNUSED int __pyx_clineno = 0;

/* 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_8f49b2e306583689516129454767dc6c_cy_cumsum(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_data) {
int __pyx_v_N;
PyArrayObject *__pyx_v_out = 0;
__pyx_t_5numpy_float64_t __pyx_v_s;
int __pyx_v_n;
__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_8f49b2e306583689516129454767dc6c_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __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__7 = PyTuple_Pack(5, __pyx_n_s_data, __pyx_n_s_N, __pyx_n_s_out, __pyx_n_s_s, __pyx_n_s_n); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__7);
__Pyx_GIVEREF(__pyx_tuple__7);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_8f49b2e306583689516129454767dc6c_1cy_cumsum, NULL, __pyx_n_s_cython_magic_8f49b2e30658368951); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_cumsum, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;

+10:     cdef int N = data.size
  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_data), __pyx_n_s_size); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_numpy); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_5);
if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_dtype, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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_8f49b2e306583689516129454767dc6c_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_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __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;
for (__pyx_t_7 = 0; __pyx_t_7 < __pyx_t_2; __pyx_t_7+=1) {
__pyx_v_n = __pyx_t_7;

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

+15:         out[n] = s
    __pyx_t_9 = __pyx_v_n;
*__Pyx_BufPtrStrided1d(__pyx_t_46_cython_magic_8f49b2e306583689516129454767dc6c_FTYPE_t *, __pyx_pybuffernd_out.rcbuffer->pybuffer.buf, __pyx_t_9, __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 [66]:
%timeit py_cumsum(data)

100 loops, best of 3: 13.6 ms per loop

In [67]:
%timeit cy_cumsum(data)

The slowest run took 23.50 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 69.7 µs per loop

In [68]:
%timeit jit_cumsum(data)

10000 loops, best of 3: 76.6 µs per loop

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

10000 loops, best of 3: 155 µs per loop

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


## Fused types¶

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

Out[71]:
15.0
In [72]:
py_sum([1, 2, 3, 4, 5])

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

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

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-74-ce555589c7fc> in <module>()
----> 1 cy_sum(np.array([1, 2, 3, 4, 5]))

_cython_magic_5634d0627d9a45c8eb4964708c73457e.pyx in _cython_magic_5634d0627d9a45c8eb4964708c73457e.cy_sum (/Users/rob/.ipython/cython/_cython_magic_5634d0627d9a45c8eb4964708c73457e.c:1219)()

ValueError: Buffer dtype mismatch, expected 'float64_t' but got 'long'
In [75]:
%%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[75]:
Cython: _cython_magic_dc04e60ae319cefa604a507986656ec7.pyx

Generated by Cython 0.22.1

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 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_3);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_3) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __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_dc04e60ae319cefa604a507986656ec7_1cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_dc04e60ae319cefa604a507986656ec7_1cy_fused_sum = {"cy_fused_sum", (PyCFunction)__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_1cy_fused_sum, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_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);
case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
case  0: break;
default: goto __pyx_L5_argtuple_error;
}
kw_args = PyDict_Size(__pyx_kwds);
switch (pos_args) {
case  0:
if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_signatures)) != 0)) kw_args--;
else goto __pyx_L5_argtuple_error;
case  1:
if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_args)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
}
case  2:
if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_kwargs)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
}
case  3:
if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_defaults)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("__pyx_fused_cpdef", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
__pyx_L3_error:;
__Pyx_RefNannyFinishContext();
return NULL;
__pyx_L4_argument_unpacking_done:;
__pyx_r = __pyx_pf_46_cython_magic_dc04e60ae319cefa604a507986656ec7_cy_fused_sum(__pyx_self, __pyx_v_signatures, __pyx_v_args, __pyx_v_kwargs, __pyx_v_defaults);
int __pyx_lineno = 0;
const char *__pyx_filename = NULL;
int __pyx_clineno = 0;

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

static PyObject *__pyx_pf_46_cython_magic_dc04e60ae319cefa604a507986656ec7_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;
PyObject *__pyx_v_ndarray = 0;
PyObject *__pyx_v_numpy = NULL;
__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_type = 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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_2 = (__pyx_v_kwargs == Py_None);
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF_SET(__pyx_v_kwargs, __pyx_t_1);
__pyx_t_1 = 0;
goto __pyx_L3;
}
__pyx_L3:;
{
/*try:*/ {
__pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_v_numpy = __pyx_t_1;
__pyx_t_1 = 0;
__pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_numpy, __pyx_n_s_ndarray); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
__Pyx_GOTREF(__pyx_t_1);
if (!(likely(PyType_CheckExact(__pyx_t_1))||((__pyx_t_1) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "type", Py_TYPE(__pyx_t_1)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L4_error;}
__pyx_v_ndarray = ((PyObject*)__pyx_t_1);
__pyx_t_1 = 0;
}
__Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
__Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
__Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
goto __pyx_L11_try_end;
__pyx_L4_error:;
__Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_t_7 = PyErr_ExceptionMatches(__pyx_builtin_ImportError) || PyErr_ExceptionMatches(__pyx_builtin_AttributeError) || PyErr_ExceptionMatches(__pyx_builtin_TypeError);
if (__pyx_t_7) {
if (__Pyx_GetException(&__pyx_t_1, &__pyx_t_8, &__pyx_t_9) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L6_except_error;}
__Pyx_GOTREF(__pyx_t_1);
__Pyx_GOTREF(__pyx_t_8);
__Pyx_GOTREF(__pyx_t_9);
__Pyx_INCREF(Py_None);
__Pyx_XDECREF_SET(__pyx_v_ndarray, ((PyObject*)Py_None));
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
goto __pyx_L5_exception_handled;
}
goto __pyx_L6_except_error;
__pyx_L6_except_error:;
__Pyx_XGIVEREF(__pyx_t_4);
__Pyx_XGIVEREF(__pyx_t_5);
__Pyx_XGIVEREF(__pyx_t_6);
__Pyx_ExceptionReset(__pyx_t_4, __pyx_t_5, __pyx_t_6);
goto __pyx_L1_error;
__pyx_L5_exception_handled:;
__Pyx_XGIVEREF(__pyx_t_4);
__Pyx_XGIVEREF(__pyx_t_5);
__Pyx_XGIVEREF(__pyx_t_6);
__Pyx_ExceptionReset(__pyx_t_4, __pyx_t_5, __pyx_t_6);
__pyx_L11_try_end:;
}
__pyx_v_itemsize = -1;
__pyx_v____pyx_int64_t_is_signed = (((__pyx_t_5numpy_int64_t)-1) < 0);
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_10 = PyTuple_GET_SIZE(((PyObject*)__pyx_v_args)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_3 = ((0 < __pyx_t_10) != 0);
if (__pyx_t_3) {
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_9 = PyTuple_GET_ITEM(((PyObject*)__pyx_v_args), 0);
__Pyx_INCREF(__pyx_t_9);
__pyx_v_arg = __pyx_t_9;
__pyx_t_9 = 0;
goto __pyx_L14;
}
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_3 = (__Pyx_PyDict_Contains(__pyx_n_s_data, ((PyObject*)__pyx_v_kwargs), Py_EQ)); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_2 = (__pyx_t_3 != 0);
if (__pyx_t_2) {
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_9 = __Pyx_PyDict_GetItem(((PyObject*)__pyx_v_kwargs), __pyx_n_s_data); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
__Pyx_GOTREF(__pyx_t_9);
__pyx_v_arg = __pyx_t_9;
__pyx_t_9 = 0;
goto __pyx_L14;
}
/*else*/ {
if (unlikely(__pyx_v_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_10 = PyTuple_GET_SIZE(((PyObject*)__pyx_v_args)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_9 = PyInt_FromSsize_t(__pyx_t_10); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__pyx_t_8 = __Pyx_PyString_Format(__pyx_kp_s_Expected_at_least_d_arguments, __pyx_t_9); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_GIVEREF(__pyx_t_8);
PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
__pyx_t_8 = 0;
__pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__Pyx_Raise(__pyx_t_8, 0, 0, 0);
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_L14:;
if (0) {
goto __pyx_L15;
}
/*else*/ {
while (1) {
if (!1) break;
__pyx_t_2 = (__pyx_v_ndarray != ((PyObject*)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_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_dtype); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_v_dtype = __pyx_t_8;
__pyx_t_8 = 0;
goto __pyx_L19;
}
__pyx_t_2 = (__pyx_memoryview_check(__pyx_v_arg) != 0);
if (__pyx_t_2) {
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_base); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_v_arg_base = __pyx_t_8;
__pyx_t_8 = 0;
__pyx_t_2 = __Pyx_TypeCheck(__pyx_v_arg_base, __pyx_v_ndarray);
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg_base, __pyx_n_s_dtype); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_v_dtype = __pyx_t_8;
__pyx_t_8 = 0;
goto __pyx_L20;
}
/*else*/ {
__Pyx_INCREF(Py_None);
__pyx_v_dtype = Py_None;
}
__pyx_L20:;
goto __pyx_L19;
}
/*else*/ {
__Pyx_INCREF(Py_None);
__pyx_v_dtype = Py_None;
}
__pyx_L19:;
__pyx_v_itemsize = -1;
__pyx_t_3 = (__pyx_v_dtype != Py_None);
__pyx_t_2 = (__pyx_t_3 != 0);
if (__pyx_t_2) {
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_dtype, __pyx_n_s_itemsize); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_t_10 = __Pyx_PyIndex_AsSsize_t(__pyx_t_8); if (unlikely((__pyx_t_10 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__pyx_v_itemsize = __pyx_t_10;
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_dtype, __pyx_n_s_kind); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_GIVEREF(__pyx_t_8);
PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_8);
__pyx_t_8 = 0;
__pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ord, __pyx_t_9, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__pyx_t_11 = __Pyx_PyInt_As_char(__pyx_t_8); if (unlikely((__pyx_t_11 == (char)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__pyx_v_kind = __pyx_t_11;
__pyx_v_dtype_signed = (__pyx_v_kind == 'i');
switch (__pyx_v_kind) {
case 'i':
case 'u':
__pyx_t_3 = (((sizeof(__pyx_t_5numpy_int64_t)) == __pyx_v_itemsize) != 0);
if (__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L23_bool_binop_done;
}
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_ndim); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_t_10 = __Pyx_PyIndex_AsSsize_t(__pyx_t_8); if (unlikely((__pyx_t_10 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__pyx_t_3 = ((((Py_ssize_t)__pyx_t_10) == 1) != 0);
if (__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L23_bool_binop_done;
}
__pyx_t_3 = ((!((__pyx_v____pyx_int64_t_is_signed ^ __pyx_v_dtype_signed) != 0)) != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L23_bool_binop_done:;
if (__pyx_t_2) {
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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L17_break;
}
break;
case 'f':
__pyx_t_3 = (((sizeof(__pyx_t_5numpy_float64_t)) == __pyx_v_itemsize) != 0);
if (__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L27_bool_binop_done;
}
__pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_v_arg, __pyx_n_s_ndim); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_t_10 = __Pyx_PyIndex_AsSsize_t(__pyx_t_8); if (unlikely((__pyx_t_10 == (Py_ssize_t)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__pyx_t_3 = ((((Py_ssize_t)__pyx_t_10) == 1) != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L27_bool_binop_done:;
if (__pyx_t_2) {
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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L17_break;
}
break;
case 'c':
break;
case 'O':
break;
default: break;
}
goto __pyx_L21;
}
__pyx_L21:;
goto __pyx_L18;
}
__pyx_L18:;
__pyx_t_3 = ((__pyx_v_itemsize == -1) != 0);
if (!__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L30_bool_binop_done;
}
__pyx_t_3 = ((__pyx_v_itemsize == (sizeof(__pyx_t_5numpy_int64_t))) != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L30_bool_binop_done:;
if (__pyx_t_2) {
__pyx_v_memslice = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_int64_t(__pyx_v_arg);
__pyx_t_2 = (__pyx_v_memslice.memview != 0);
if (__pyx_t_2) {
__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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L17_break;
}
/*else*/ {
PyErr_Clear();
}
goto __pyx_L29;
}
__pyx_L29:;
__pyx_t_3 = ((__pyx_v_itemsize == -1) != 0);
if (!__pyx_t_3) {
} else {
__pyx_t_2 = __pyx_t_3;
goto __pyx_L34_bool_binop_done;
}
__pyx_t_3 = ((__pyx_v_itemsize == (sizeof(__pyx_t_5numpy_float64_t))) != 0);
__pyx_t_2 = __pyx_t_3;
__pyx_L34_bool_binop_done:;
if (__pyx_t_2) {
__pyx_v_memslice = __Pyx_PyObject_to_MemoryviewSlice_ds_nn___pyx_t_5numpy_float64_t(__pyx_v_arg);
__pyx_t_2 = (__pyx_v_memslice.memview != 0);
if (__pyx_t_2) {
__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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L17_break;
}
/*else*/ {
PyErr_Clear();
}
goto __pyx_L33;
}
__pyx_L33:;
if (unlikely(__Pyx_SetItemInt(__pyx_v_dest_sig, 0, Py_None, long, 1, __Pyx_PyInt_From_long, 1, 0, 0) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L17_break;
}
__pyx_L17_break:;
}
__pyx_L15:;
__pyx_t_8 = PyList_New(0); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__pyx_v_candidates = ((PyObject*)__pyx_t_8);
__pyx_t_8 = 0;
__pyx_t_10 = 0;
if (unlikely(__pyx_v_signatures == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_9 = __Pyx_dict_iterator(((PyObject*)__pyx_v_signatures), 1, ((PyObject *)NULL), (&__pyx_t_12), (&__pyx_t_7)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_XDECREF(__pyx_t_8);
__pyx_t_8 = __pyx_t_9;
__pyx_t_9 = 0;
while (1) {
__pyx_t_13 = __Pyx_dict_iter_next(__pyx_t_8, __pyx_t_12, &__pyx_t_10, &__pyx_t_9, NULL, NULL, __pyx_t_7);
if (unlikely(__pyx_t_13 == 0)) break;
if (unlikely(__pyx_t_13 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_XDECREF_SET(__pyx_v_sig, __pyx_t_9);
__pyx_t_9 = 0;
__pyx_v_match_found = 0;
__pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_v_sig, __pyx_n_s_strip); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__pyx_t_9 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_split); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
/* … */
__pyx_tuple__2 = PyTuple_Pack(1, __pyx_kp_s_); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__2);
__Pyx_GIVEREF(__pyx_tuple__2);
__pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_9, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__pyx_t_9 = PyTuple_New(2); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__Pyx_GIVEREF(__pyx_t_1);
PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_1);
__Pyx_INCREF(__pyx_v_dest_sig);
__Pyx_GIVEREF(__pyx_v_dest_sig);
PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_v_dest_sig);
__pyx_t_1 = 0;
__pyx_t_1 = __Pyx_PyObject_Call(__pyx_builtin_zip, __pyx_t_9, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
if (likely(PyList_CheckExact(__pyx_t_1)) || PyTuple_CheckExact(__pyx_t_1)) {
__pyx_t_9 = __pyx_t_1; __Pyx_INCREF(__pyx_t_9); __pyx_t_14 = 0;
__pyx_t_15 = NULL;
} else {
__pyx_t_14 = -1; __pyx_t_9 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_9);
__pyx_t_15 = Py_TYPE(__pyx_t_9)->tp_iternext; if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
for (;;) {
if (likely(!__pyx_t_15)) {
if (likely(PyList_CheckExact(__pyx_t_9))) {
if (__pyx_t_14 >= PyList_GET_SIZE(__pyx_t_9)) break;
#if CYTHON_COMPILING_IN_CPYTHON
__pyx_t_1 = PyList_GET_ITEM(__pyx_t_9, __pyx_t_14); __Pyx_INCREF(__pyx_t_1); __pyx_t_14++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
#else
__pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_14); __pyx_t_14++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
#endif
} else {
if (__pyx_t_14 >= PyTuple_GET_SIZE(__pyx_t_9)) break;
#if CYTHON_COMPILING_IN_CPYTHON
__pyx_t_1 = PyTuple_GET_ITEM(__pyx_t_9, __pyx_t_14); __Pyx_INCREF(__pyx_t_1); __pyx_t_14++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
#else
__pyx_t_1 = PySequence_ITEM(__pyx_t_9, __pyx_t_14); __pyx_t_14++; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
#endif
}
} else {
__pyx_t_1 = __pyx_t_15(__pyx_t_9);
if (unlikely(!__pyx_t_1)) {
PyObject* exc_type = PyErr_Occurred();
if (exc_type) {
if (likely(exc_type == PyExc_StopIteration || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();
else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
break;
}
__Pyx_GOTREF(__pyx_t_1);
}
if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) {
PyObject* sequence = __pyx_t_1;
#if CYTHON_COMPILING_IN_CPYTHON
Py_ssize_t size = Py_SIZE(sequence);
#else
Py_ssize_t size = PySequence_Size(sequence);
#endif
if (unlikely(size != 2)) {
if (size > 2) __Pyx_RaiseTooManyValuesError(2);
else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
#if CYTHON_COMPILING_IN_CPYTHON
if (likely(PyTuple_CheckExact(sequence))) {
__pyx_t_16 = PyTuple_GET_ITEM(sequence, 0);
__pyx_t_17 = PyTuple_GET_ITEM(sequence, 1);
} else {
__pyx_t_16 = PyList_GET_ITEM(sequence, 0);
__pyx_t_17 = PyList_GET_ITEM(sequence, 1);
}
__Pyx_INCREF(__pyx_t_16);
__Pyx_INCREF(__pyx_t_17);
#else
__pyx_t_16 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_16)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_16);
__pyx_t_17 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_17)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_17);
#endif
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
} else {
Py_ssize_t index = -1;
__pyx_t_18 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_18);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_t_19 = Py_TYPE(__pyx_t_18)->tp_iternext;
index = 0; __pyx_t_16 = __pyx_t_19(__pyx_t_18); if (unlikely(!__pyx_t_16)) goto __pyx_L41_unpacking_failed;
__Pyx_GOTREF(__pyx_t_16);
index = 1; __pyx_t_17 = __pyx_t_19(__pyx_t_18); if (unlikely(!__pyx_t_17)) goto __pyx_L41_unpacking_failed;
__Pyx_GOTREF(__pyx_t_17);
if (__Pyx_IternextUnpackEndCheck(__pyx_t_19(__pyx_t_18), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_19 = NULL;
__Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
goto __pyx_L42_unpacking_done;
__pyx_L41_unpacking_failed:;
__Pyx_DECREF(__pyx_t_18); __pyx_t_18 = 0;
__pyx_t_19 = NULL;
if (__Pyx_IterFinish() == 0) __Pyx_RaiseNeedMoreValuesError(index);
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_L42_unpacking_done:;
}
__Pyx_XDECREF_SET(__pyx_v_src_type, __pyx_t_16);
__pyx_t_16 = 0;
__Pyx_XDECREF_SET(__pyx_v_dst_type, __pyx_t_17);
__pyx_t_17 = 0;
__pyx_t_2 = (__pyx_v_dst_type != Py_None);
__pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
__pyx_t_1 = PyObject_RichCompare(__pyx_v_src_type, __pyx_v_dst_type, Py_EQ); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
if (__pyx_t_3) {
__pyx_v_match_found = 1;
goto __pyx_L44;
}
/*else*/ {
__pyx_v_match_found = 0;
goto __pyx_L40_break;
}
__pyx_L44:;
goto __pyx_L43;
}
__pyx_L43:;
}
__pyx_L40_break:;
__Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;
__pyx_t_3 = (__pyx_v_match_found != 0);
if (__pyx_t_3) {
__pyx_t_20 = __Pyx_PyList_Append(__pyx_v_candidates, __pyx_v_sig); if (unlikely(__pyx_t_20 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
goto __pyx_L45;
}
__pyx_L45:;
}
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
__pyx_t_3 = (__pyx_v_candidates != Py_None) && (PyList_GET_SIZE(__pyx_v_candidates) != 0);
__pyx_t_2 = ((!__pyx_t_3) != 0);
if (__pyx_t_2) {
__pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s__3); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__4);
__Pyx_GIVEREF(__pyx_tuple__4);
__pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__Pyx_Raise(__pyx_t_8, 0, 0, 0);
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_12 = PyList_GET_SIZE(__pyx_v_candidates); if (unlikely(__pyx_t_12 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_t_2 = ((__pyx_t_12 > 1) != 0);
if (__pyx_t_2) {
__pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_No_matching_signature_found); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__5);
__Pyx_GIVEREF(__pyx_tuple__5);
__pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_TypeError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_8);
__Pyx_Raise(__pyx_t_8, 0, 0, 0);
__Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
}
__pyx_t_8 = __Pyx_PyDict_GetItem(((PyObject*)__pyx_v_signatures), PyList_GET_ITEM(__pyx_v_candidates, 0)); if (unlikely(__pyx_t_8 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;};
__Pyx_GOTREF(__pyx_t_8);
__pyx_r = __pyx_t_8;
__pyx_t_8 = 0;
goto __pyx_L0;
}

/* function exit code */
__pyx_L1_error:;
__Pyx_XDECREF(__pyx_t_1);
__Pyx_XDECREF(__pyx_t_8);
__Pyx_XDECREF(__pyx_t_9);
__Pyx_XDECREF(__pyx_t_16);
__Pyx_XDECREF(__pyx_t_17);
__Pyx_XDECREF(__pyx_t_18);
__pyx_r = NULL;
__pyx_L0:;
__Pyx_XDECREF(__pyx_v_dest_sig);
__Pyx_XDECREF(__pyx_v_ndarray);
__Pyx_XDECREF(__pyx_v_numpy);
__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_type);
__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_dc04e60ae319cefa604a507986656ec7_3cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_fuse_0__pyx_mdef_46_cython_magic_dc04e60ae319cefa604a507986656ec7_3cy_fused_sum = {"__pyx_fuse_0cy_fused_sum", (PyCFunction)__pyx_fuse_0__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_3cy_fused_sum, METH_O, 0};
static PyObject *__pyx_fuse_0__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_r = __pyx_pf_46_cython_magic_dc04e60ae319cefa604a507986656ec7_2cy_fused_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));
CYTHON_UNUSED int __pyx_lineno = 0;
CYTHON_UNUSED const char *__pyx_filename = NULL;
CYTHON_UNUSED int __pyx_clineno = 0;

/* 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_dc04e60ae319cefa604a507986656ec7_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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_dc04e60ae319cefa604a507986656ec7_5cy_fused_sum(PyObject *__pyx_self, PyObject *__pyx_v_data); /*proto*/
static PyMethodDef __pyx_fuse_1__pyx_mdef_46_cython_magic_dc04e60ae319cefa604a507986656ec7_5cy_fused_sum = {"__pyx_fuse_1cy_fused_sum", (PyCFunction)__pyx_fuse_1__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_5cy_fused_sum, METH_O, 0};
static PyObject *__pyx_fuse_1__pyx_pw_46_cython_magic_dc04e60ae319cefa604a507986656ec7_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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_r = __pyx_pf_46_cython_magic_dc04e60ae319cefa604a507986656ec7_4cy_fused_sum(__pyx_self, ((PyArrayObject *)__pyx_v_data));
CYTHON_UNUSED int __pyx_lineno = 0;
CYTHON_UNUSED const char *__pyx_filename = NULL;
CYTHON_UNUSED int __pyx_clineno = 0;

/* 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_dc04e60ae319cefa604a507986656ec7_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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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__6 = PyTuple_Pack(1, __pyx_kp_s_Function_call_with_ambiguous_arg); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__6);
__Pyx_GIVEREF(__pyx_tuple__6);
/* … */
__pyx_tuple__25 = PyTuple_Pack(4, __pyx_n_s_data, __pyx_n_s_s, __pyx_n_s_n, __pyx_n_s_N); if (unlikely(!__pyx_tuple__25)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__25);
__Pyx_GIVEREF(__pyx_tuple__25);
/* … */
__pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_t_2 = __pyx_FusedFunction_NewEx(&__pyx_fuse_0__pyx_mdef_46_cython_magic_dc04e60ae319cefa604a507986656ec7_3cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_dc04e60ae319cefa60, __pyx_d, ((PyObject *)__pyx_codeobj__26)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_dc04e60ae319cefa604a507986656ec7_5cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_dc04e60ae319cefa60, __pyx_d, ((PyObject *)__pyx_codeobj__26)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_2 = __pyx_FusedFunction_NewEx(&__pyx_mdef_46_cython_magic_dc04e60ae319cefa604a507986656ec7_1cy_fused_sum, 0, __pyx_n_s_cy_fused_sum, NULL, __pyx_n_s_cython_magic_dc04e60ae319cefa60, __pyx_d, ((PyObject *)__pyx_codeobj__26)); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_codeobj__26 = (PyObject*)__Pyx_PyCode_New(1, 0, 4, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__25, __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__26)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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;
for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
__pyx_v_n = __pyx_t_3;
/* … */
__pyx_t_2 = __pyx_v_N;
for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
__pyx_v_n = __pyx_t_3;

+14:         s += data[n]
    __pyx_t_4 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int64_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_4, __pyx_pybuffernd_data.diminfo[0].strides)));
}
/* … */
__pyx_t_4 = __pyx_v_n;
__pyx_v_s = (__pyx_v_s + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_pybuffernd_data.rcbuffer->pybuffer.buf, __pyx_t_4, __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;

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

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

Out[77]:
15

## Julia fractal¶

In [78]:
%%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[78]:
Cython: _cython_magic_5a0a1cf19007e43958539590969e7f52.pyx

Generated by Cython 0.22.1

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 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __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_5a0a1cf19007e43958539590969e7f52_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_5a0a1cf19007e43958539590969e7f52_1abs2(PyObject *__pyx_self, PyObject *__pyx_arg_z); /*proto*/
static CYTHON_INLINE double __pyx_f_46_cython_magic_5a0a1cf19007e43958539590969e7f52_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_5a0a1cf19007e43958539590969e7f52_1abs2(PyObject *__pyx_self, PyObject *__pyx_arg_z); /*proto*/
static PyObject *__pyx_pw_46_cython_magic_5a0a1cf19007e43958539590969e7f52_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_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __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_5a0a1cf19007e43958539590969e7f52_abs2(__pyx_self, __pyx_v_z);
int __pyx_lineno = 0;
const char *__pyx_filename = NULL;
int __pyx_clineno = 0;

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

static PyObject *__pyx_pf_46_cython_magic_5a0a1cf19007e43958539590969e7f52_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_5a0a1cf19007e43958539590969e7f52_abs2(__pyx_v_z, 0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; __pyx_clineno = __LINE__; goto __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_5a0a1cf19007e43958539590969e7f52_3cy_julia_fractal(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_5a0a1cf19007e43958539590969e7f52_3cy_julia_fractal = {"cy_julia_fractal", (PyCFunction)__pyx_pw_46_cython_magic_5a0a1cf19007e43958539590969e7f52_3cy_julia_fractal, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_5a0a1cf19007e43958539590969e7f52_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);
case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
case  0: break;
default: goto __pyx_L5_argtuple_error;
}
kw_args = PyDict_Size(__pyx_kwds);
switch (pos_args) {
case  0:
if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_z_re)) != 0)) kw_args--;
else goto __pyx_L5_argtuple_error;
case  1:
if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_z_im)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("cy_julia_fractal", 1, 3, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
}
case  2:
if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_j)) != 0)) kw_args--;
else {
__Pyx_RaiseArgtupleInvalid("cy_julia_fractal", 1, 3, 3, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_z_im), __pyx_ptype_5numpy_ndarray, 1, "z_im", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_j), __pyx_ptype_5numpy_ndarray, 1, "j", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__pyx_r = __pyx_pf_46_cython_magic_5a0a1cf19007e43958539590969e7f52_2cy_julia_fractal(__pyx_self, __pyx_v_z_re, __pyx_v_z_im, __pyx_v_j);
int __pyx_lineno = 0;
const char *__pyx_filename = NULL;
int __pyx_clineno = 0;

/* 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_5a0a1cf19007e43958539590969e7f52_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_5a0a1cf19007e43958539590969e7f52_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_5a0a1cf19007e43958539590969e7f52_FTYPE_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_5a0a1cf19007e43958539590969e7f52_ITYPE_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack) == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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__7 = 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__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_tuple__7);
__Pyx_GIVEREF(__pyx_tuple__7);
/* … */
__pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_5a0a1cf19007e43958539590969e7f52_3cy_julia_fractal, NULL, __pyx_n_s_cython_magic_5a0a1cf19007e43958); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
__Pyx_GOTREF(__pyx_t_1);
if (PyDict_SetItem(__pyx_d, __pyx_n_s_cy_julia_fractal, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __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_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __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;
for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
__pyx_v_m = __pyx_t_3;

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

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

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

+21:                 z = z ** 2 - 0.05 + 0.68j
        __pyx_v_z = __Pyx_c_sum(__Pyx_c_diff(__Pyx_c_pow(__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_11 = ((__pyx_f_46_cython_magic_5a0a1cf19007e43958539590969e7f52_abs2(__pyx_v_z, 0) > 4.0) != 0);
if (__pyx_t_11) {

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

+24:                     break
          goto __pyx_L8_break;
}
}
__pyx_L8_break:;
}
}

In [79]:
N = 1024

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

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

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

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

10 loops, best of 3: 117 ms per loop

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

10 loops, best of 3: 126 ms per loop

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

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

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

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

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


## Calling C function¶

In [90]:
%%cython

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

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

In [91]:
%timeit cy_acos1(0.5)

The slowest run took 22.38 times longer than the fastest. This could mean that an intermediate result is being cached
10000000 loops, best of 3: 95.9 ns per loop

In [92]:
%%cython

from libc.math cimport acos

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

In [93]:
%timeit cy_acos2(0.5)

The slowest run took 24.16 times longer than the fastest. This could mean that an intermediate result is being cached
10000000 loops, best of 3: 88.8 ns per loop

In [94]:
from numpy import arccos

In [95]:
%timeit arccos(0.5)

The slowest run took 24.56 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 932 ns per loop

In [96]:
from math import acos

In [97]:
%timeit acos(0.5)

The slowest run took 21.63 times longer than the fastest. This could mean that an intermediate result is being cached
10000000 loops, best of 3: 99.2 ns per loop

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

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


# Versions¶

In [100]:
%reload_ext version_information

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

Out[101]:
SoftwareVersion
Python2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]
IPython3.2.1
OSDarwin 14.1.0 x86_64 i386 64bit
numpy1.9.2
cython0.22.1
numba0.20.0
matplotlib1.4.3