## Jupyter Snippet NP ch19-code-listing

Jupyter Snippet NP ch19-code-listing

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

import numba

import pyximport

import cython

import numpy as np

%matplotlib inline
import matplotlib.pyplot as plt


# Numba

np.random.seed(0)

data = np.random.randn(50000)

def py_sum(data):
s = 0
for d in data:
s += d
return s

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

%timeit py_sum(data)

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

assert abs(py_sum(data) - np.sum(data)) < 1e-10

%timeit np.sum(data)

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

%timeit py_cumsum(data)

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

assert np.allclose(np.cumsum(data), py_cumsum(data))

%timeit np.cumsum(data)

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

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

return s

assert abs(jit_sum(data) - np.sum(data)) < 1e-10

%timeit jit_sum(data)

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

jit_cumsum = numba.jit()(py_cumsum)

assert np.allclose(np.cumsum(data), jit_cumsum(data))

%timeit jit_cumsum(data)

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


## Julia fractal

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

jit_julia_fractal = numba.jit(nopython=True)(py_julia_fractal)

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)

jit_julia_fractal(z_real, z_imag, j)

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


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

%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

def py_Heaviside(x):
if x == 0.0:
return 0.5

if x < 0.0:
return 0.0
else:
return 1.0

x = np.linspace(-2, 2, 50001)

%timeit [py_Heaviside(xx) for xx in x]

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

np_vec_Heaviside = np.vectorize(py_Heaviside)

np_vec_Heaviside(x)

array([0., 0., 0., ..., 1., 1., 1.])

%timeit np_vec_Heaviside(x)

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

def np_Heaviside(x):
return (x > 0.0) + (x == 0.0)/2.0

%timeit np_Heaviside(x)

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

@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

%timeit jit_Heaviside(x)

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

jit_Heaviside([-1, -0.5, 0.0, 0.5, 1.0])

array([0. , 0. , 0.5, 1. , 1. ])


# Cython

!rm cy_sum.*

%%writefile cy_sum.pyx

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

Writing cy_sum.pyx

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

# 5 lines of python code -> 1470 lines of C code ...
!wc cy_sum.c

    2647    9105  103591 cy_sum.c

%%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

!/Users/rob/miniconda3/envs/py3.6/bin/python setup.py build_ext --inplace > /dev/null

from cy_sum import cy_sum

cy_sum(data)

-189.70046227549025

%timeit cy_sum(data)

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

%timeit py_sum(data)

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

%%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

pyximport.install(setup_args={'include_dirs': np.get_include()});

pyximport.install(setup_args=dict(include_dirs=np.get_include()));

from cy_cumsum import cy_cumsum

%timeit cy_cumsum(data)

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

%timeit py_cumsum(data)

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


## Using IPython cython command

%load_ext cython

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


body.cython { font-family: courier; font-size: 12; }

.cython.tag { } .cython.line { margin: 0em } .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

.cython.line .run { background-color: #B0FFB0; } .cython.line .mis { background-color: #FFB0B0; } .cython.code.run { border-left: 8px solid #B0FFB0; } .cython.code.mis { border-left: 8px solid #FFB0B0; }

.cython.code .py_c_api { color: red; } .cython.code .py_macro_api { color: #FF7000; } .cython.code .pyx_c_api { color: #FF3000; } .cython.code .pyx_macro_api { color: #FF7000; } .cython.code .refnanny { color: #FFA000; } .cython.code .trace { color: #FFA000; } .cython.code .error_goto { color: #FFA000; }

.cython.code .coerce { color: #008000; border: 1px dotted #008000 } .cython.code .py_attr { color: #FF0000; font-weight: bold; } .cython.code .c_attr { color: #0000FF; } .cython.code .py_call { color: #FF0000; font-weight: bold; } .cython.code .c_call { color: #0000FF; }

.cython.score-0 {background-color: #FFFFff;} .cython.score-1 {background-color: #FFFFe7;} .cython.score-2 {background-color: #FFFFd4;} .cython.score-3 {background-color: #FFFFc4;} .cython.score-4 {background-color: #FFFFb6;} .cython.score-5 {background-color: #FFFFaa;} .cython.score-6 {background-color: #FFFF9f;} .cython.score-7 {background-color: #FFFF96;} .cython.score-8 {background-color: #FFFF8d;} .cython.score-9 {background-color: #FFFF86;} .cython.score-10 {background-color: #FFFF7f;} .cython.score-11 {background-color: #FFFF79;} .cython.score-12 {background-color: #FFFF73;} .cython.score-13 {background-color: #FFFF6e;} .cython.score-14 {background-color: #FFFF6a;} .cython.score-15 {background-color: #FFFF66;} .cython.score-16 {background-color: #FFFF62;} .cython.score-17 {background-color: #FFFF5e;} .cython.score-18 {background-color: #FFFF5b;} .cython.score-19 {background-color: #FFFF57;} .cython.score-20 {background-color: #FFFF55;} .cython.score-21 {background-color: #FFFF52;} .cython.score-22 {background-color: #FFFF4f;} .cython.score-23 {background-color: #FFFF4d;} .cython.score-24 {background-color: #FFFF4b;} .cython.score-25 {background-color: #FFFF48;} .cython.score-26 {background-color: #FFFF46;} .cython.score-27 {background-color: #FFFF44;} .cython.score-28 {background-color: #FFFF43;} .cython.score-29 {background-color: #FFFF41;} .cython.score-30 {background-color: #FFFF3f;} .cython.score-31 {background-color: #FFFF3e;} .cython.score-32 {background-color: #FFFF3c;} .cython.score-33 {background-color: #FFFF3b;} .cython.score-34 {background-color: #FFFF39;} .cython.score-35 {background-color: #FFFF38;} .cython.score-36 {background-color: #FFFF37;} .cython.score-37 {background-color: #FFFF36;} .cython.score-38 {background-color: #FFFF35;} .cython.score-39 {background-color: #FFFF34;} .cython.score-40 {background-color: #FFFF33;} .cython.score-41 {background-color: #FFFF32;} .cython.score-42 {background-color: #FFFF31;} .cython.score-43 {background-color: #FFFF30;} .cython.score-44 {background-color: #FFFF2f;} .cython.score-45 {background-color: #FFFF2e;} .cython.score-46 {background-color: #FFFF2d;} .cython.score-47 {background-color: #FFFF2c;} .cython.score-48 {background-color: #FFFF2b;} .cython.score-49 {background-color: #FFFF2b;} .cython.score-50 {background-color: #FFFF2a;} .cython.score-51 {background-color: #FFFF29;} .cython.score-52 {background-color: #FFFF29;} .cython.score-53 {background-color: #FFFF28;} .cython.score-54 {background-color: #FFFF27;} .cython.score-55 {background-color: #FFFF27;} .cython.score-56 {background-color: #FFFF26;} .cython.score-57 {background-color: #FFFF26;} .cython.score-58 {background-color: #FFFF25;} .cython.score-59 {background-color: #FFFF24;} .cython.score-60 {background-color: #FFFF24;} .cython.score-61 {background-color: #FFFF23;} .cython.score-62 {background-color: #FFFF23;} .cython.score-63 {background-color: #FFFF22;} .cython.score-64 {background-color: #FFFF22;} .cython.score-65 {background-color: #FFFF22;} .cython.score-66 {background-color: #FFFF21;} .cython.score-67 {background-color: #FFFF21;} .cython.score-68 {background-color: #FFFF20;} .cython.score-69 {background-color: #FFFF20;} .cython.score-70 {background-color: #FFFF1f;} .cython.score-71 {background-color: #FFFF1f;} .cython.score-72 {background-color: #FFFF1f;} .cython.score-73 {background-color: #FFFF1e;} .cython.score-74 {background-color: #FFFF1e;} .cython.score-75 {background-color: #FFFF1e;} .cython.score-76 {background-color: #FFFF1d;} .cython.score-77 {background-color: #FFFF1d;} .cython.score-78 {background-color: #FFFF1c;} .cython.score-79 {background-color: #FFFF1c;} .cython.score-80 {background-color: #FFFF1c;} .cython.score-81 {background-color: #FFFF1c;} .cython.score-82 {background-color: #FFFF1b;} .cython.score-83 {background-color: #FFFF1b;} .cython.score-84 {background-color: #FFFF1b;} .cython.score-85 {background-color: #FFFF1a;} .cython.score-86 {background-color: #FFFF1a;} .cython.score-87 {background-color: #FFFF1a;} .cython.score-88 {background-color: #FFFF1a;} .cython.score-89 {background-color: #FFFF19;} .cython.score-90 {background-color: #FFFF19;} .cython.score-91 {background-color: #FFFF19;} .cython.score-92 {background-color: #FFFF19;} .cython.score-93 {background-color: #FFFF18;} .cython.score-94 {background-color: #FFFF18;} .cython.score-95 {background-color: #FFFF18;} .cython.score-96 {background-color: #FFFF18;} .cython.score-97 {background-color: #FFFF17;} .cython.score-98 {background-color: #FFFF17;} .cython.score-99 {background-color: #FFFF17;} .cython.score-100 {background-color: #FFFF17;} .cython.score-101 {background-color: #FFFF16;} .cython.score-102 {background-color: #FFFF16;} .cython.score-103 {background-color: #FFFF16;} .cython.score-104 {background-color: #FFFF16;} .cython.score-105 {background-color: #FFFF16;} .cython.score-106 {background-color: #FFFF15;} .cython.score-107 {background-color: #FFFF15;} .cython.score-108 {background-color: #FFFF15;} .cython.score-109 {background-color: #FFFF15;} .cython.score-110 {background-color: #FFFF15;} .cython.score-111 {background-color: #FFFF15;} .cython.score-112 {background-color: #FFFF14;} .cython.score-113 {background-color: #FFFF14;} .cython.score-114 {background-color: #FFFF14;} .cython.score-115 {background-color: #FFFF14;} .cython.score-116 {background-color: #FFFF14;} .cython.score-117 {background-color: #FFFF14;} .cython.score-118 {background-color: #FFFF13;} .cython.score-119 {background-color: #FFFF13;} .cython.score-120 {background-color: #FFFF13;} .cython.score-121 {background-color: #FFFF13;} .cython.score-122 {background-color: #FFFF13;} .cython.score-123 {background-color: #FFFF13;} .cython.score-124 {background-color: #FFFF13;} .cython.score-125 {background-color: #FFFF12;} .cython.score-126 {background-color: #FFFF12;} .cython.score-127 {background-color: #FFFF12;} .cython.score-128 {background-color: #FFFF12;} .cython.score-129 {background-color: #FFFF12;} .cython.score-130 {background-color: #FFFF12;} .cython.score-131 {background-color: #FFFF12;} .cython.score-132 {background-color: #FFFF11;} .cython.score-133 {background-color: #FFFF11;} .cython.score-134 {background-color: #FFFF11;} .cython.score-135 {background-color: #FFFF11;} .cython.score-136 {background-color: #FFFF11;} .cython.score-137 {background-color: #FFFF11;} .cython.score-138 {background-color: #FFFF11;} .cython.score-139 {background-color: #FFFF11;} .cython.score-140 {background-color: #FFFF11;} .cython.score-141 {background-color: #FFFF10;} .cython.score-142 {background-color: #FFFF10;} .cython.score-143 {background-color: #FFFF10;} .cython.score-144 {background-color: #FFFF10;} .cython.score-145 {background-color: #FFFF10;} .cython.score-146 {background-color: #FFFF10;} .cython.score-147 {background-color: #FFFF10;} .cython.score-148 {background-color: #FFFF10;} .cython.score-149 {background-color: #FFFF10;} .cython.score-150 {background-color: #FFFF0f;} .cython.score-151 {background-color: #FFFF0f;} .cython.score-152 {background-color: #FFFF0f;} .cython.score-153 {background-color: #FFFF0f;} .cython.score-154 {background-color: #FFFF0f;} .cython.score-155 {background-color: #FFFF0f;} .cython.score-156 {background-color: #FFFF0f;} .cython.score-157 {background-color: #FFFF0f;} .cython.score-158 {background-color: #FFFF0f;} .cython.score-159 {background-color: #FFFF0f;} .cython.score-160 {background-color: #FFFF0f;} .cython.score-161 {background-color: #FFFF0e;} .cython.score-162 {background-color: #FFFF0e;} .cython.score-163 {background-color: #FFFF0e;} .cython.score-164 {background-color: #FFFF0e;} .cython.score-165 {background-color: #FFFF0e;} .cython.score-166 {background-color: #FFFF0e;} .cython.score-167 {background-color: #FFFF0e;} .cython.score-168 {background-color: #FFFF0e;} .cython.score-169 {background-color: #FFFF0e;} .cython.score-170 {background-color: #FFFF0e;} .cython.score-171 {background-color: #FFFF0e;} .cython.score-172 {background-color: #FFFF0e;} .cython.score-173 {background-color: #FFFF0d;} .cython.score-174 {background-color: #FFFF0d;} .cython.score-175 {background-color: #FFFF0d;} .cython.score-176 {background-color: #FFFF0d;} .cython.score-177 {background-color: #FFFF0d;} .cython.score-178 {background-color: #FFFF0d;} .cython.score-179 {background-color: #FFFF0d;} .cython.score-180 {background-color: #FFFF0d;} .cython.score-181 {background-color: #FFFF0d;} .cython.score-182 {background-color: #FFFF0d;} .cython.score-183 {background-color: #FFFF0d;} .cython.score-184 {background-color: #FFFF0d;} .cython.score-185 {background-color: #FFFF0d;} .cython.score-186 {background-color: #FFFF0d;} .cython.score-187 {background-color: #FFFF0c;} .cython.score-188 {background-color: #FFFF0c;} .cython.score-189 {background-color: #FFFF0c;} .cython.score-190 {background-color: #FFFF0c;} .cython.score-191 {background-color: #FFFF0c;} .cython.score-192 {background-color: #FFFF0c;} .cython.score-193 {background-color: #FFFF0c;} .cython.score-194 {background-color: #FFFF0c;} .cython.score-195 {background-color: #FFFF0c;} .cython.score-196 {background-color: #FFFF0c;} .cython.score-197 {background-color: #FFFF0c;} .cython.score-198 {background-color: #FFFF0c;} .cython.score-199 {background-color: #FFFF0c;} .cython.score-200 {background-color: #FFFF0c;} .cython.score-201 {background-color: #FFFF0c;} .cython.score-202 {background-color: #FFFF0c;} .cython.score-203 {background-color: #FFFF0b;} .cython.score-204 {background-color: #FFFF0b;} .cython.score-205 {background-color: #FFFF0b;} .cython.score-206 {background-color: #FFFF0b;} .cython.score-207 {background-color: #FFFF0b;} .cython.score-208 {background-color: #FFFF0b;} .cython.score-209 {background-color: #FFFF0b;} .cython.score-210 {background-color: #FFFF0b;} .cython.score-211 {background-color: #FFFF0b;} .cython.score-212 {background-color: #FFFF0b;} .cython.score-213 {background-color: #FFFF0b;} .cython.score-214 {background-color: #FFFF0b;} .cython.score-215 {background-color: #FFFF0b;} .cython.score-216 {background-color: #FFFF0b;} .cython.score-217 {background-color: #FFFF0b;} .cython.score-218 {background-color: #FFFF0b;} .cython.score-219 {background-color: #FFFF0b;} .cython.score-220 {background-color: #FFFF0b;} .cython.score-221 {background-color: #FFFF0b;} .cython.score-222 {background-color: #FFFF0a;} .cython.score-223 {background-color: #FFFF0a;} .cython.score-224 {background-color: #FFFF0a;} .cython.score-225 {background-color: #FFFF0a;} .cython.score-226 {background-color: #FFFF0a;} .cython.score-227 {background-color: #FFFF0a;} .cython.score-228 {background-color: #FFFF0a;} .cython.score-229 {background-color: #FFFF0a;} .cython.score-230 {background-color: #FFFF0a;} .cython.score-231 {background-color: #FFFF0a;} .cython.score-232 {background-color: #FFFF0a;} .cython.score-233 {background-color: #FFFF0a;} .cython.score-234 {background-color: #FFFF0a;} .cython.score-235 {background-color: #FFFF0a;} .cython.score-236 {background-color: #FFFF0a;} .cython.score-237 {background-color: #FFFF0a;} .cython.score-238 {background-color: #FFFF0a;} .cython.score-239 {background-color: #FFFF0a;} .cython.score-240 {background-color: #FFFF0a;} .cython.score-241 {background-color: #FFFF0a;} .cython.score-242 {background-color: #FFFF0a;} .cython.score-243 {background-color: #FFFF0a;} .cython.score-244 {background-color: #FFFF0a;} .cython.score-245 {background-color: #FFFF0a;} .cython.score-246 {background-color: #FFFF09;} .cython.score-247 {background-color: #FFFF09;} .cython.score-248 {background-color: #FFFF09;} .cython.score-249 {background-color: #FFFF09;} .cython.score-250 {background-color: #FFFF09;} .cython.score-251 {background-color: #FFFF09;} .cython.score-252 {background-color: #FFFF09;} .cython.score-253 {background-color: #FFFF09;} .cython.score-254 {background-color: #FFFF09;} .cython .hll { background-color: #ffffcc } .cython { background: #f8f8f8; } .cython .c { color: #408080; font-style: italic } /* Comment */ .cython .err { border: 1px solid #FF0000 } /* Error */ .cython .k { color: #008000; font-weight: bold } /* Keyword */ .cython .o { color: #666666 } /* Operator */ .cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cython .cp { color: #BC7A00 } /* Comment.Preproc */ .cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .cython .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cython .cs { color: #408080; font-style: italic } /* Comment.Special */ .cython .gd { color: #A00000 } /* Generic.Deleted */ .cython .ge { font-style: italic } /* Generic.Emph */ .cython .gr { color: #FF0000 } /* Generic.Error */ .cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .cython .gi { color: #00A000 } /* Generic.Inserted */ .cython .go { color: #888888 } /* Generic.Output */ .cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .cython .gs { font-weight: bold } /* Generic.Strong */ .cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .cython .gt { color: #0044DD } /* Generic.Traceback */ .cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .cython .kp { color: #008000 } /* Keyword.Pseudo */ .cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .cython .kt { color: #B00040 } /* Keyword.Type */ .cython .m { color: #666666 } /* Literal.Number */ .cython .s { color: #BA2121 } /* Literal.String */ .cython .na { color: #7D9029 } /* Name.Attribute */ .cython .nb { color: #008000 } /* Name.Builtin */ .cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .cython .no { color: #880000 } /* Name.Constant */ .cython .nd { color: #AA22FF } /* Name.Decorator */ .cython .ni { color: #999999; font-weight: bold } /* Name.Entity */ .cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .cython .nf { color: #0000FF } /* Name.Function */ .cython .nl { color: #A0A000 } /* Name.Label */ .cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .cython .nt { color: #008000; font-weight: bold } /* Name.Tag */ .cython .nv { color: #19177C } /* Name.Variable */ .cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .cython .w { color: #bbbbbb } /* Text.Whitespace */ .cython .mb { color: #666666 } /* Literal.Number.Bin */ .cython .mf { color: #666666 } /* Literal.Number.Float */ .cython .mh { color: #666666 } /* Literal.Number.Hex */ .cython .mi { color: #666666 } /* Literal.Number.Integer */ .cython .mo { color: #666666 } /* Literal.Number.Oct */ .cython .sa { color: #BA2121 } /* Literal.String.Affix */ .cython .sb { color: #BA2121 } /* Literal.String.Backtick */ .cython .sc { color: #BA2121 } /* Literal.String.Char */ .cython .dl { color: #BA2121 } /* Literal.String.Delimiter */ .cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .cython .s2 { color: #BA2121 } /* Literal.String.Double */ .cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .cython .sh { color: #BA2121 } /* Literal.String.Heredoc */ .cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .cython .sx { color: #008000 } /* Literal.String.Other */ .cython .sr { color: #BB6688 } /* Literal.String.Regex */ .cython .s1 { color: #BA2121 } /* Literal.String.Single */ .cython .ss { color: #19177C } /* Literal.String.Symbol */ .cython .bp { color: #008000 } /* Name.Builtin.Pseudo */ .cython .fm { color: #0000FF } /* Name.Function.Magic */ .cython .vc { color: #19177C } /* Name.Variable.Class */ .cython .vg { color: #19177C } /* Name.Variable.Global */ .cython .vi { color: #19177C } /* Name.Variable.Instance */ .cython .vm { color: #19177C } /* Name.Variable.Magic */ .cython .il { color: #666666 } /* Literal.Number.Integer.Long */

/* 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_AddTraceback("_cython_magic_9889852309bc2332c4f81a8792f01ec6.cy_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __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

%timeit cy_sum(data)

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

%timeit py_sum(data)

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

assert np.allclose(np.sum(data), cy_sum(data))

%%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


body.cython { font-family: courier; font-size: 12; }

.cython.tag { } .cython.line { margin: 0em } .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

.cython.line .run { background-color: #B0FFB0; } .cython.line .mis { background-color: #FFB0B0; } .cython.code.run { border-left: 8px solid #B0FFB0; } .cython.code.mis { border-left: 8px solid #FFB0B0; }

.cython.code .py_c_api { color: red; } .cython.code .py_macro_api { color: #FF7000; } .cython.code .pyx_c_api { color: #FF3000; } .cython.code .pyx_macro_api { color: #FF7000; } .cython.code .refnanny { color: #FFA000; } .cython.code .trace { color: #FFA000; } .cython.code .error_goto { color: #FFA000; }

.cython.code .coerce { color: #008000; border: 1px dotted #008000 } .cython.code .py_attr { color: #FF0000; font-weight: bold; } .cython.code .c_attr { color: #0000FF; } .cython.code .py_call { color: #FF0000; font-weight: bold; } .cython.code .c_call { color: #0000FF; }

.cython.score-0 {background-color: #FFFFff;} .cython.score-1 {background-color: #FFFFe7;} .cython.score-2 {background-color: #FFFFd4;} .cython.score-3 {background-color: #FFFFc4;} .cython.score-4 {background-color: #FFFFb6;} .cython.score-5 {background-color: #FFFFaa;} .cython.score-6 {background-color: #FFFF9f;} .cython.score-7 {background-color: #FFFF96;} .cython.score-8 {background-color: #FFFF8d;} .cython.score-9 {background-color: #FFFF86;} .cython.score-10 {background-color: #FFFF7f;} .cython.score-11 {background-color: #FFFF79;} .cython.score-12 {background-color: #FFFF73;} .cython.score-13 {background-color: #FFFF6e;} .cython.score-14 {background-color: #FFFF6a;} .cython.score-15 {background-color: #FFFF66;} .cython.score-16 {background-color: #FFFF62;} .cython.score-17 {background-color: #FFFF5e;} .cython.score-18 {background-color: #FFFF5b;} .cython.score-19 {background-color: #FFFF57;} .cython.score-20 {background-color: #FFFF55;} .cython.score-21 {background-color: #FFFF52;} .cython.score-22 {background-color: #FFFF4f;} .cython.score-23 {background-color: #FFFF4d;} .cython.score-24 {background-color: #FFFF4b;} .cython.score-25 {background-color: #FFFF48;} .cython.score-26 {background-color: #FFFF46;} .cython.score-27 {background-color: #FFFF44;} .cython.score-28 {background-color: #FFFF43;} .cython.score-29 {background-color: #FFFF41;} .cython.score-30 {background-color: #FFFF3f;} .cython.score-31 {background-color: #FFFF3e;} .cython.score-32 {background-color: #FFFF3c;} .cython.score-33 {background-color: #FFFF3b;} .cython.score-34 {background-color: #FFFF39;} .cython.score-35 {background-color: #FFFF38;} .cython.score-36 {background-color: #FFFF37;} .cython.score-37 {background-color: #FFFF36;} .cython.score-38 {background-color: #FFFF35;} .cython.score-39 {background-color: #FFFF34;} .cython.score-40 {background-color: #FFFF33;} .cython.score-41 {background-color: #FFFF32;} .cython.score-42 {background-color: #FFFF31;} .cython.score-43 {background-color: #FFFF30;} .cython.score-44 {background-color: #FFFF2f;} .cython.score-45 {background-color: #FFFF2e;} .cython.score-46 {background-color: #FFFF2d;} .cython.score-47 {background-color: #FFFF2c;} .cython.score-48 {background-color: #FFFF2b;} .cython.score-49 {background-color: #FFFF2b;} .cython.score-50 {background-color: #FFFF2a;} .cython.score-51 {background-color: #FFFF29;} .cython.score-52 {background-color: #FFFF29;} .cython.score-53 {background-color: #FFFF28;} .cython.score-54 {background-color: #FFFF27;} .cython.score-55 {background-color: #FFFF27;} .cython.score-56 {background-color: #FFFF26;} .cython.score-57 {background-color: #FFFF26;} .cython.score-58 {background-color: #FFFF25;} .cython.score-59 {background-color: #FFFF24;} .cython.score-60 {background-color: #FFFF24;} .cython.score-61 {background-color: #FFFF23;} .cython.score-62 {background-color: #FFFF23;} .cython.score-63 {background-color: #FFFF22;} .cython.score-64 {background-color: #FFFF22;} .cython.score-65 {background-color: #FFFF22;} .cython.score-66 {background-color: #FFFF21;} .cython.score-67 {background-color: #FFFF21;} .cython.score-68 {background-color: #FFFF20;} .cython.score-69 {background-color: #FFFF20;} .cython.score-70 {background-color: #FFFF1f;} .cython.score-71 {background-color: #FFFF1f;} .cython.score-72 {background-color: #FFFF1f;} .cython.score-73 {background-color: #FFFF1e;} .cython.score-74 {background-color: #FFFF1e;} .cython.score-75 {background-color: #FFFF1e;} .cython.score-76 {background-color: #FFFF1d;} .cython.score-77 {background-color: #FFFF1d;} .cython.score-78 {background-color: #FFFF1c;} .cython.score-79 {background-color: #FFFF1c;} .cython.score-80 {background-color: #FFFF1c;} .cython.score-81 {background-color: #FFFF1c;} .cython.score-82 {background-color: #FFFF1b;} .cython.score-83 {background-color: #FFFF1b;} .cython.score-84 {background-color: #FFFF1b;} .cython.score-85 {background-color: #FFFF1a;} .cython.score-86 {background-color: #FFFF1a;} .cython.score-87 {background-color: #FFFF1a;} .cython.score-88 {background-color: #FFFF1a;} .cython.score-89 {background-color: #FFFF19;} .cython.score-90 {background-color: #FFFF19;} .cython.score-91 {background-color: #FFFF19;} .cython.score-92 {background-color: #FFFF19;} .cython.score-93 {background-color: #FFFF18;} .cython.score-94 {background-color: #FFFF18;} .cython.score-95 {background-color: #FFFF18;} .cython.score-96 {background-color: #FFFF18;} .cython.score-97 {background-color: #FFFF17;} .cython.score-98 {background-color: #FFFF17;} .cython.score-99 {background-color: #FFFF17;} .cython.score-100 {background-color: #FFFF17;} .cython.score-101 {background-color: #FFFF16;} .cython.score-102 {background-color: #FFFF16;} .cython.score-103 {background-color: #FFFF16;} .cython.score-104 {background-color: #FFFF16;} .cython.score-105 {background-color: #FFFF16;} .cython.score-106 {background-color: #FFFF15;} .cython.score-107 {background-color: #FFFF15;} .cython.score-108 {background-color: #FFFF15;} .cython.score-109 {background-color: #FFFF15;} .cython.score-110 {background-color: #FFFF15;} .cython.score-111 {background-color: #FFFF15;} .cython.score-112 {background-color: #FFFF14;} .cython.score-113 {background-color: #FFFF14;} .cython.score-114 {background-color: #FFFF14;} .cython.score-115 {background-color: #FFFF14;} .cython.score-116 {background-color: #FFFF14;} .cython.score-117 {background-color: #FFFF14;} .cython.score-118 {background-color: #FFFF13;} .cython.score-119 {background-color: #FFFF13;} .cython.score-120 {background-color: #FFFF13;} .cython.score-121 {background-color: #FFFF13;} .cython.score-122 {background-color: #FFFF13;} .cython.score-123 {background-color: #FFFF13;} .cython.score-124 {background-color: #FFFF13;} .cython.score-125 {background-color: #FFFF12;} .cython.score-126 {background-color: #FFFF12;} .cython.score-127 {background-color: #FFFF12;} .cython.score-128 {background-color: #FFFF12;} .cython.score-129 {background-color: #FFFF12;} .cython.score-130 {background-color: #FFFF12;} .cython.score-131 {background-color: #FFFF12;} .cython.score-132 {background-color: #FFFF11;} .cython.score-133 {background-color: #FFFF11;} .cython.score-134 {background-color: #FFFF11;} .cython.score-135 {background-color: #FFFF11;} .cython.score-136 {background-color: #FFFF11;} .cython.score-137 {background-color: #FFFF11;} .cython.score-138 {background-color: #FFFF11;} .cython.score-139 {background-color: #FFFF11;} .cython.score-140 {background-color: #FFFF11;} .cython.score-141 {background-color: #FFFF10;} .cython.score-142 {background-color: #FFFF10;} .cython.score-143 {background-color: #FFFF10;} .cython.score-144 {background-color: #FFFF10;} .cython.score-145 {background-color: #FFFF10;} .cython.score-146 {background-color: #FFFF10;} .cython.score-147 {background-color: #FFFF10;} .cython.score-148 {background-color: #FFFF10;} .cython.score-149 {background-color: #FFFF10;} .cython.score-150 {background-color: #FFFF0f;} .cython.score-151 {background-color: #FFFF0f;} .cython.score-152 {background-color: #FFFF0f;} .cython.score-153 {background-color: #FFFF0f;} .cython.score-154 {background-color: #FFFF0f;} .cython.score-155 {background-color: #FFFF0f;} .cython.score-156 {background-color: #FFFF0f;} .cython.score-157 {background-color: #FFFF0f;} .cython.score-158 {background-color: #FFFF0f;} .cython.score-159 {background-color: #FFFF0f;} .cython.score-160 {background-color: #FFFF0f;} .cython.score-161 {background-color: #FFFF0e;} .cython.score-162 {background-color: #FFFF0e;} .cython.score-163 {background-color: #FFFF0e;} .cython.score-164 {background-color: #FFFF0e;} .cython.score-165 {background-color: #FFFF0e;} .cython.score-166 {background-color: #FFFF0e;} .cython.score-167 {background-color: #FFFF0e;} .cython.score-168 {background-color: #FFFF0e;} .cython.score-169 {background-color: #FFFF0e;} .cython.score-170 {background-color: #FFFF0e;} .cython.score-171 {background-color: #FFFF0e;} .cython.score-172 {background-color: #FFFF0e;} .cython.score-173 {background-color: #FFFF0d;} .cython.score-174 {background-color: #FFFF0d;} .cython.score-175 {background-color: #FFFF0d;} .cython.score-176 {background-color: #FFFF0d;} .cython.score-177 {background-color: #FFFF0d;} .cython.score-178 {background-color: #FFFF0d;} .cython.score-179 {background-color: #FFFF0d;} .cython.score-180 {background-color: #FFFF0d;} .cython.score-181 {background-color: #FFFF0d;} .cython.score-182 {background-color: #FFFF0d;} .cython.score-183 {background-color: #FFFF0d;} .cython.score-184 {background-color: #FFFF0d;} .cython.score-185 {background-color: #FFFF0d;} .cython.score-186 {background-color: #FFFF0d;} .cython.score-187 {background-color: #FFFF0c;} .cython.score-188 {background-color: #FFFF0c;} .cython.score-189 {background-color: #FFFF0c;} .cython.score-190 {background-color: #FFFF0c;} .cython.score-191 {background-color: #FFFF0c;} .cython.score-192 {background-color: #FFFF0c;} .cython.score-193 {background-color: #FFFF0c;} .cython.score-194 {background-color: #FFFF0c;} .cython.score-195 {background-color: #FFFF0c;} .cython.score-196 {background-color: #FFFF0c;} .cython.score-197 {background-color: #FFFF0c;} .cython.score-198 {background-color: #FFFF0c;} .cython.score-199 {background-color: #FFFF0c;} .cython.score-200 {background-color: #FFFF0c;} .cython.score-201 {background-color: #FFFF0c;} .cython.score-202 {background-color: #FFFF0c;} .cython.score-203 {background-color: #FFFF0b;} .cython.score-204 {background-color: #FFFF0b;} .cython.score-205 {background-color: #FFFF0b;} .cython.score-206 {background-color: #FFFF0b;} .cython.score-207 {background-color: #FFFF0b;} .cython.score-208 {background-color: #FFFF0b;} .cython.score-209 {background-color: #FFFF0b;} .cython.score-210 {background-color: #FFFF0b;} .cython.score-211 {background-color: #FFFF0b;} .cython.score-212 {background-color: #FFFF0b;} .cython.score-213 {background-color: #FFFF0b;} .cython.score-214 {background-color: #FFFF0b;} .cython.score-215 {background-color: #FFFF0b;} .cython.score-216 {background-color: #FFFF0b;} .cython.score-217 {background-color: #FFFF0b;} .cython.score-218 {background-color: #FFFF0b;} .cython.score-219 {background-color: #FFFF0b;} .cython.score-220 {background-color: #FFFF0b;} .cython.score-221 {background-color: #FFFF0b;} .cython.score-222 {background-color: #FFFF0a;} .cython.score-223 {background-color: #FFFF0a;} .cython.score-224 {background-color: #FFFF0a;} .cython.score-225 {background-color: #FFFF0a;} .cython.score-226 {background-color: #FFFF0a;} .cython.score-227 {background-color: #FFFF0a;} .cython.score-228 {background-color: #FFFF0a;} .cython.score-229 {background-color: #FFFF0a;} .cython.score-230 {background-color: #FFFF0a;} .cython.score-231 {background-color: #FFFF0a;} .cython.score-232 {background-color: #FFFF0a;} .cython.score-233 {background-color: #FFFF0a;} .cython.score-234 {background-color: #FFFF0a;} .cython.score-235 {background-color: #FFFF0a;} .cython.score-236 {background-color: #FFFF0a;} .cython.score-237 {background-color: #FFFF0a;} .cython.score-238 {background-color: #FFFF0a;} .cython.score-239 {background-color: #FFFF0a;} .cython.score-240 {background-color: #FFFF0a;} .cython.score-241 {background-color: #FFFF0a;} .cython.score-242 {background-color: #FFFF0a;} .cython.score-243 {background-color: #FFFF0a;} .cython.score-244 {background-color: #FFFF0a;} .cython.score-245 {background-color: #FFFF0a;} .cython.score-246 {background-color: #FFFF09;} .cython.score-247 {background-color: #FFFF09;} .cython.score-248 {background-color: #FFFF09;} .cython.score-249 {background-color: #FFFF09;} .cython.score-250 {background-color: #FFFF09;} .cython.score-251 {background-color: #FFFF09;} .cython.score-252 {background-color: #FFFF09;} .cython.score-253 {background-color: #FFFF09;} .cython.score-254 {background-color: #FFFF09;} .cython .hll { background-color: #ffffcc } .cython { background: #f8f8f8; } .cython .c { color: #408080; font-style: italic } /* Comment */ .cython .err { border: 1px solid #FF0000 } /* Error */ .cython .k { color: #008000; font-weight: bold } /* Keyword */ .cython .o { color: #666666 } /* Operator */ .cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cython .cp { color: #BC7A00 } /* Comment.Preproc */ .cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .cython .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cython .cs { color: #408080; font-style: italic } /* Comment.Special */ .cython .gd { color: #A00000 } /* Generic.Deleted */ .cython .ge { font-style: italic } /* Generic.Emph */ .cython .gr { color: #FF0000 } /* Generic.Error */ .cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .cython .gi { color: #00A000 } /* Generic.Inserted */ .cython .go { color: #888888 } /* Generic.Output */ .cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .cython .gs { font-weight: bold } /* Generic.Strong */ .cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .cython .gt { color: #0044DD } /* Generic.Traceback */ .cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .cython .kp { color: #008000 } /* Keyword.Pseudo */ .cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .cython .kt { color: #B00040 } /* Keyword.Type */ .cython .m { color: #666666 } /* Literal.Number */ .cython .s { color: #BA2121 } /* Literal.String */ .cython .na { color: #7D9029 } /* Name.Attribute */ .cython .nb { color: #008000 } /* Name.Builtin */ .cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .cython .no { color: #880000 } /* Name.Constant */ .cython .nd { color: #AA22FF } /* Name.Decorator */ .cython .ni { color: #999999; font-weight: bold } /* Name.Entity */ .cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .cython .nf { color: #0000FF } /* Name.Function */ .cython .nl { color: #A0A000 } /* Name.Label */ .cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .cython .nt { color: #008000; font-weight: bold } /* Name.Tag */ .cython .nv { color: #19177C } /* Name.Variable */ .cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .cython .w { color: #bbbbbb } /* Text.Whitespace */ .cython .mb { color: #666666 } /* Literal.Number.Bin */ .cython .mf { color: #666666 } /* Literal.Number.Float */ .cython .mh { color: #666666 } /* Literal.Number.Hex */ .cython .mi { color: #666666 } /* Literal.Number.Integer */ .cython .mo { color: #666666 } /* Literal.Number.Oct */ .cython .sa { color: #BA2121 } /* Literal.String.Affix */ .cython .sb { color: #BA2121 } /* Literal.String.Backtick */ .cython .sc { color: #BA2121 } /* Literal.String.Char */ .cython .dl { color: #BA2121 } /* Literal.String.Delimiter */ .cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .cython .s2 { color: #BA2121 } /* Literal.String.Double */ .cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .cython .sh { color: #BA2121 } /* Literal.String.Heredoc */ .cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .cython .sx { color: #008000 } /* Literal.String.Other */ .cython .sr { color: #BB6688 } /* Literal.String.Regex */ .cython .s1 { color: #BA2121 } /* Literal.String.Single */ .cython .ss { color: #19177C } /* Literal.String.Symbol */ .cython .bp { color: #008000 } /* Name.Builtin.Pseudo */ .cython .fm { color: #0000FF } /* Name.Function.Magic */ .cython .vc { color: #19177C } /* Name.Variable.Class */ .cython .vg { color: #19177C } /* Name.Variable.Global */ .cython .vi { color: #19177C } /* Name.Variable.Instance */ .cython .vm { color: #19177C } /* Name.Variable.Magic */ .cython .il { color: #666666 } /* Literal.Number.Integer.Long */

/* 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_PyThreadState_declare __Pyx_PyThreadState_assign __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb); __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer); __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);} __Pyx_AddTraceback("_cython_magic_ca1303e3f76e8cc692e7b9140687359d.cy_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __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

%timeit cy_sum(data)

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

%timeit jit_sum(data)

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

%timeit np.sum(data)

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


## Cummulative sum

%%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


body.cython { font-family: courier; font-size: 12; }

.cython.tag { } .cython.line { margin: 0em } .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

.cython.line .run { background-color: #B0FFB0; } .cython.line .mis { background-color: #FFB0B0; } .cython.code.run { border-left: 8px solid #B0FFB0; } .cython.code.mis { border-left: 8px solid #FFB0B0; }

.cython.code .py_c_api { color: red; } .cython.code .py_macro_api { color: #FF7000; } .cython.code .pyx_c_api { color: #FF3000; } .cython.code .pyx_macro_api { color: #FF7000; } .cython.code .refnanny { color: #FFA000; } .cython.code .trace { color: #FFA000; } .cython.code .error_goto { color: #FFA000; }

.cython.code .coerce { color: #008000; border: 1px dotted #008000 } .cython.code .py_attr { color: #FF0000; font-weight: bold; } .cython.code .c_attr { color: #0000FF; } .cython.code .py_call { color: #FF0000; font-weight: bold; } .cython.code .c_call { color: #0000FF; }

.cython.score-0 {background-color: #FFFFff;} .cython.score-1 {background-color: #FFFFe7;} .cython.score-2 {background-color: #FFFFd4;} .cython.score-3 {background-color: #FFFFc4;} .cython.score-4 {background-color: #FFFFb6;} .cython.score-5 {background-color: #FFFFaa;} .cython.score-6 {background-color: #FFFF9f;} .cython.score-7 {background-color: #FFFF96;} .cython.score-8 {background-color: #FFFF8d;} .cython.score-9 {background-color: #FFFF86;} .cython.score-10 {background-color: #FFFF7f;} .cython.score-11 {background-color: #FFFF79;} .cython.score-12 {background-color: #FFFF73;} .cython.score-13 {background-color: #FFFF6e;} .cython.score-14 {background-color: #FFFF6a;} .cython.score-15 {background-color: #FFFF66;} .cython.score-16 {background-color: #FFFF62;} .cython.score-17 {background-color: #FFFF5e;} .cython.score-18 {background-color: #FFFF5b;} .cython.score-19 {background-color: #FFFF57;} .cython.score-20 {background-color: #FFFF55;} .cython.score-21 {background-color: #FFFF52;} .cython.score-22 {background-color: #FFFF4f;} .cython.score-23 {background-color: #FFFF4d;} .cython.score-24 {background-color: #FFFF4b;} .cython.score-25 {background-color: #FFFF48;} .cython.score-26 {background-color: #FFFF46;} .cython.score-27 {background-color: #FFFF44;} .cython.score-28 {background-color: #FFFF43;} .cython.score-29 {background-color: #FFFF41;} .cython.score-30 {background-color: #FFFF3f;} .cython.score-31 {background-color: #FFFF3e;} .cython.score-32 {background-color: #FFFF3c;} .cython.score-33 {background-color: #FFFF3b;} .cython.score-34 {background-color: #FFFF39;} .cython.score-35 {background-color: #FFFF38;} .cython.score-36 {background-color: #FFFF37;} .cython.score-37 {background-color: #FFFF36;} .cython.score-38 {background-color: #FFFF35;} .cython.score-39 {background-color: #FFFF34;} .cython.score-40 {background-color: #FFFF33;} .cython.score-41 {background-color: #FFFF32;} .cython.score-42 {background-color: #FFFF31;} .cython.score-43 {background-color: #FFFF30;} .cython.score-44 {background-color: #FFFF2f;} .cython.score-45 {background-color: #FFFF2e;} .cython.score-46 {background-color: #FFFF2d;} .cython.score-47 {background-color: #FFFF2c;} .cython.score-48 {background-color: #FFFF2b;} .cython.score-49 {background-color: #FFFF2b;} .cython.score-50 {background-color: #FFFF2a;} .cython.score-51 {background-color: #FFFF29;} .cython.score-52 {background-color: #FFFF29;} .cython.score-53 {background-color: #FFFF28;} .cython.score-54 {background-color: #FFFF27;} .cython.score-55 {background-color: #FFFF27;} .cython.score-56 {background-color: #FFFF26;} .cython.score-57 {background-color: #FFFF26;} .cython.score-58 {background-color: #FFFF25;} .cython.score-59 {background-color: #FFFF24;} .cython.score-60 {background-color: #FFFF24;} .cython.score-61 {background-color: #FFFF23;} .cython.score-62 {background-color: #FFFF23;} .cython.score-63 {background-color: #FFFF22;} .cython.score-64 {background-color: #FFFF22;} .cython.score-65 {background-color: #FFFF22;} .cython.score-66 {background-color: #FFFF21;} .cython.score-67 {background-color: #FFFF21;} .cython.score-68 {background-color: #FFFF20;} .cython.score-69 {background-color: #FFFF20;} .cython.score-70 {background-color: #FFFF1f;} .cython.score-71 {background-color: #FFFF1f;} .cython.score-72 {background-color: #FFFF1f;} .cython.score-73 {background-color: #FFFF1e;} .cython.score-74 {background-color: #FFFF1e;} .cython.score-75 {background-color: #FFFF1e;} .cython.score-76 {background-color: #FFFF1d;} .cython.score-77 {background-color: #FFFF1d;} .cython.score-78 {background-color: #FFFF1c;} .cython.score-79 {background-color: #FFFF1c;} .cython.score-80 {background-color: #FFFF1c;} .cython.score-81 {background-color: #FFFF1c;} .cython.score-82 {background-color: #FFFF1b;} .cython.score-83 {background-color: #FFFF1b;} .cython.score-84 {background-color: #FFFF1b;} .cython.score-85 {background-color: #FFFF1a;} .cython.score-86 {background-color: #FFFF1a;} .cython.score-87 {background-color: #FFFF1a;} .cython.score-88 {background-color: #FFFF1a;} .cython.score-89 {background-color: #FFFF19;} .cython.score-90 {background-color: #FFFF19;} .cython.score-91 {background-color: #FFFF19;} .cython.score-92 {background-color: #FFFF19;} .cython.score-93 {background-color: #FFFF18;} .cython.score-94 {background-color: #FFFF18;} .cython.score-95 {background-color: #FFFF18;} .cython.score-96 {background-color: #FFFF18;} .cython.score-97 {background-color: #FFFF17;} .cython.score-98 {background-color: #FFFF17;} .cython.score-99 {background-color: #FFFF17;} .cython.score-100 {background-color: #FFFF17;} .cython.score-101 {background-color: #FFFF16;} .cython.score-102 {background-color: #FFFF16;} .cython.score-103 {background-color: #FFFF16;} .cython.score-104 {background-color: #FFFF16;} .cython.score-105 {background-color: #FFFF16;} .cython.score-106 {background-color: #FFFF15;} .cython.score-107 {background-color: #FFFF15;} .cython.score-108 {background-color: #FFFF15;} .cython.score-109 {background-color: #FFFF15;} .cython.score-110 {background-color: #FFFF15;} .cython.score-111 {background-color: #FFFF15;} .cython.score-112 {background-color: #FFFF14;} .cython.score-113 {background-color: #FFFF14;} .cython.score-114 {background-color: #FFFF14;} .cython.score-115 {background-color: #FFFF14;} .cython.score-116 {background-color: #FFFF14;} .cython.score-117 {background-color: #FFFF14;} .cython.score-118 {background-color: #FFFF13;} .cython.score-119 {background-color: #FFFF13;} .cython.score-120 {background-color: #FFFF13;} .cython.score-121 {background-color: #FFFF13;} .cython.score-122 {background-color: #FFFF13;} .cython.score-123 {background-color: #FFFF13;} .cython.score-124 {background-color: #FFFF13;} .cython.score-125 {background-color: #FFFF12;} .cython.score-126 {background-color: #FFFF12;} .cython.score-127 {background-color: #FFFF12;} .cython.score-128 {background-color: #FFFF12;} .cython.score-129 {background-color: #FFFF12;} .cython.score-130 {background-color: #FFFF12;} .cython.score-131 {background-color: #FFFF12;} .cython.score-132 {background-color: #FFFF11;} .cython.score-133 {background-color: #FFFF11;} .cython.score-134 {background-color: #FFFF11;} .cython.score-135 {background-color: #FFFF11;} .cython.score-136 {background-color: #FFFF11;} .cython.score-137 {background-color: #FFFF11;} .cython.score-138 {background-color: #FFFF11;} .cython.score-139 {background-color: #FFFF11;} .cython.score-140 {background-color: #FFFF11;} .cython.score-141 {background-color: #FFFF10;} .cython.score-142 {background-color: #FFFF10;} .cython.score-143 {background-color: #FFFF10;} .cython.score-144 {background-color: #FFFF10;} .cython.score-145 {background-color: #FFFF10;} .cython.score-146 {background-color: #FFFF10;} .cython.score-147 {background-color: #FFFF10;} .cython.score-148 {background-color: #FFFF10;} .cython.score-149 {background-color: #FFFF10;} .cython.score-150 {background-color: #FFFF0f;} .cython.score-151 {background-color: #FFFF0f;} .cython.score-152 {background-color: #FFFF0f;} .cython.score-153 {background-color: #FFFF0f;} .cython.score-154 {background-color: #FFFF0f;} .cython.score-155 {background-color: #FFFF0f;} .cython.score-156 {background-color: #FFFF0f;} .cython.score-157 {background-color: #FFFF0f;} .cython.score-158 {background-color: #FFFF0f;} .cython.score-159 {background-color: #FFFF0f;} .cython.score-160 {background-color: #FFFF0f;} .cython.score-161 {background-color: #FFFF0e;} .cython.score-162 {background-color: #FFFF0e;} .cython.score-163 {background-color: #FFFF0e;} .cython.score-164 {background-color: #FFFF0e;} .cython.score-165 {background-color: #FFFF0e;} .cython.score-166 {background-color: #FFFF0e;} .cython.score-167 {background-color: #FFFF0e;} .cython.score-168 {background-color: #FFFF0e;} .cython.score-169 {background-color: #FFFF0e;} .cython.score-170 {background-color: #FFFF0e;} .cython.score-171 {background-color: #FFFF0e;} .cython.score-172 {background-color: #FFFF0e;} .cython.score-173 {background-color: #FFFF0d;} .cython.score-174 {background-color: #FFFF0d;} .cython.score-175 {background-color: #FFFF0d;} .cython.score-176 {background-color: #FFFF0d;} .cython.score-177 {background-color: #FFFF0d;} .cython.score-178 {background-color: #FFFF0d;} .cython.score-179 {background-color: #FFFF0d;} .cython.score-180 {background-color: #FFFF0d;} .cython.score-181 {background-color: #FFFF0d;} .cython.score-182 {background-color: #FFFF0d;} .cython.score-183 {background-color: #FFFF0d;} .cython.score-184 {background-color: #FFFF0d;} .cython.score-185 {background-color: #FFFF0d;} .cython.score-186 {background-color: #FFFF0d;} .cython.score-187 {background-color: #FFFF0c;} .cython.score-188 {background-color: #FFFF0c;} .cython.score-189 {background-color: #FFFF0c;} .cython.score-190 {background-color: #FFFF0c;} .cython.score-191 {background-color: #FFFF0c;} .cython.score-192 {background-color: #FFFF0c;} .cython.score-193 {background-color: #FFFF0c;} .cython.score-194 {background-color: #FFFF0c;} .cython.score-195 {background-color: #FFFF0c;} .cython.score-196 {background-color: #FFFF0c;} .cython.score-197 {background-color: #FFFF0c;} .cython.score-198 {background-color: #FFFF0c;} .cython.score-199 {background-color: #FFFF0c;} .cython.score-200 {background-color: #FFFF0c;} .cython.score-201 {background-color: #FFFF0c;} .cython.score-202 {background-color: #FFFF0c;} .cython.score-203 {background-color: #FFFF0b;} .cython.score-204 {background-color: #FFFF0b;} .cython.score-205 {background-color: #FFFF0b;} .cython.score-206 {background-color: #FFFF0b;} .cython.score-207 {background-color: #FFFF0b;} .cython.score-208 {background-color: #FFFF0b;} .cython.score-209 {background-color: #FFFF0b;} .cython.score-210 {background-color: #FFFF0b;} .cython.score-211 {background-color: #FFFF0b;} .cython.score-212 {background-color: #FFFF0b;} .cython.score-213 {background-color: #FFFF0b;} .cython.score-214 {background-color: #FFFF0b;} .cython.score-215 {background-color: #FFFF0b;} .cython.score-216 {background-color: #FFFF0b;} .cython.score-217 {background-color: #FFFF0b;} .cython.score-218 {background-color: #FFFF0b;} .cython.score-219 {background-color: #FFFF0b;} .cython.score-220 {background-color: #FFFF0b;} .cython.score-221 {background-color: #FFFF0b;} .cython.score-222 {background-color: #FFFF0a;} .cython.score-223 {background-color: #FFFF0a;} .cython.score-224 {background-color: #FFFF0a;} .cython.score-225 {background-color: #FFFF0a;} .cython.score-226 {background-color: #FFFF0a;} .cython.score-227 {background-color: #FFFF0a;} .cython.score-228 {background-color: #FFFF0a;} .cython.score-229 {background-color: #FFFF0a;} .cython.score-230 {background-color: #FFFF0a;} .cython.score-231 {background-color: #FFFF0a;} .cython.score-232 {background-color: #FFFF0a;} .cython.score-233 {background-color: #FFFF0a;} .cython.score-234 {background-color: #FFFF0a;} .cython.score-235 {background-color: #FFFF0a;} .cython.score-236 {background-color: #FFFF0a;} .cython.score-237 {background-color: #FFFF0a;} .cython.score-238 {background-color: #FFFF0a;} .cython.score-239 {background-color: #FFFF0a;} .cython.score-240 {background-color: #FFFF0a;} .cython.score-241 {background-color: #FFFF0a;} .cython.score-242 {background-color: #FFFF0a;} .cython.score-243 {background-color: #FFFF0a;} .cython.score-244 {background-color: #FFFF0a;} .cython.score-245 {background-color: #FFFF0a;} .cython.score-246 {background-color: #FFFF09;} .cython.score-247 {background-color: #FFFF09;} .cython.score-248 {background-color: #FFFF09;} .cython.score-249 {background-color: #FFFF09;} .cython.score-250 {background-color: #FFFF09;} .cython.score-251 {background-color: #FFFF09;} .cython.score-252 {background-color: #FFFF09;} .cython.score-253 {background-color: #FFFF09;} .cython.score-254 {background-color: #FFFF09;} .cython .hll { background-color: #ffffcc } .cython { background: #f8f8f8; } .cython .c { color: #408080; font-style: italic } /* Comment */ .cython .err { border: 1px solid #FF0000 } /* Error */ .cython .k { color: #008000; font-weight: bold } /* Keyword */ .cython .o { color: #666666 } /* Operator */ .cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cython .cp { color: #BC7A00 } /* Comment.Preproc */ .cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .cython .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cython .cs { color: #408080; font-style: italic } /* Comment.Special */ .cython .gd { color: #A00000 } /* Generic.Deleted */ .cython .ge { font-style: italic } /* Generic.Emph */ .cython .gr { color: #FF0000 } /* Generic.Error */ .cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .cython .gi { color: #00A000 } /* Generic.Inserted */ .cython .go { color: #888888 } /* Generic.Output */ .cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .cython .gs { font-weight: bold } /* Generic.Strong */ .cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .cython .gt { color: #0044DD } /* Generic.Traceback */ .cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .cython .kp { color: #008000 } /* Keyword.Pseudo */ .cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .cython .kt { color: #B00040 } /* Keyword.Type */ .cython .m { color: #666666 } /* Literal.Number */ .cython .s { color: #BA2121 } /* Literal.String */ .cython .na { color: #7D9029 } /* Name.Attribute */ .cython .nb { color: #008000 } /* Name.Builtin */ .cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .cython .no { color: #880000 } /* Name.Constant */ .cython .nd { color: #AA22FF } /* Name.Decorator */ .cython .ni { color: #999999; font-weight: bold } /* Name.Entity */ .cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .cython .nf { color: #0000FF } /* Name.Function */ .cython .nl { color: #A0A000 } /* Name.Label */ .cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .cython .nt { color: #008000; font-weight: bold } /* Name.Tag */ .cython .nv { color: #19177C } /* Name.Variable */ .cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .cython .w { color: #bbbbbb } /* Text.Whitespace */ .cython .mb { color: #666666 } /* Literal.Number.Bin */ .cython .mf { color: #666666 } /* Literal.Number.Float */ .cython .mh { color: #666666 } /* Literal.Number.Hex */ .cython .mi { color: #666666 } /* Literal.Number.Integer */ .cython .mo { color: #666666 } /* Literal.Number.Oct */ .cython .sa { color: #BA2121 } /* Literal.String.Affix */ .cython .sb { color: #BA2121 } /* Literal.String.Backtick */ .cython .sc { color: #BA2121 } /* Literal.String.Char */ .cython .dl { color: #BA2121 } /* Literal.String.Delimiter */ .cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .cython .s2 { color: #BA2121 } /* Literal.String.Double */ .cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .cython .sh { color: #BA2121 } /* Literal.String.Heredoc */ .cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .cython .sx { color: #008000 } /* Literal.String.Other */ .cython .sr { color: #BB6688 } /* Literal.String.Regex */ .cython .s1 { color: #BA2121 } /* Literal.String.Single */ .cython .ss { color: #19177C } /* Literal.String.Symbol */ .cython .bp { color: #008000 } /* Name.Builtin.Pseudo */ .cython .fm { color: #0000FF } /* Name.Function.Magic */ .cython .vc { color: #19177C } /* Name.Variable.Class */ .cython .vg { color: #19177C } /* Name.Variable.Global */ .cython .vi { color: #19177C } /* Name.Variable.Instance */ .cython .vm { color: #19177C } /* Name.Variable.Magic */ .cython .il { color: #666666 } /* Literal.Number.Integer.Long */

/* 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_PyThreadState_declare __Pyx_PyThreadState_assign __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_AddTraceback("_cython_magic_11b25028d61ca29697d68d22b6117b41.cy_cumsum", __pyx_clineno, __pyx_lineno, __pyx_filename); __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

%timeit py_cumsum(data)

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

%timeit cy_cumsum(data)

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

%timeit jit_cumsum(data)

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

%timeit np.cumsum(data)

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

assert np.allclose(cy_cumsum(data), np.cumsum(data))


## Fused types

py_sum([1.0, 2.0, 3.0, 4.0, 5.0])

15.0

py_sum([1, 2, 3, 4, 5])

15

cy_sum(np.array([1.0, 2.0, 3.0, 4.0, 5.0]))

15.0

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'

%%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


body.cython { font-family: courier; font-size: 12; }

.cython.tag { } .cython.line { margin: 0em } .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

.cython.line .run { background-color: #B0FFB0; } .cython.line .mis { background-color: #FFB0B0; } .cython.code.run { border-left: 8px solid #B0FFB0; } .cython.code.mis { border-left: 8px solid #FFB0B0; }

.cython.code .py_c_api { color: red; } .cython.code .py_macro_api { color: #FF7000; } .cython.code .pyx_c_api { color: #FF3000; } .cython.code .pyx_macro_api { color: #FF7000; } .cython.code .refnanny { color: #FFA000; } .cython.code .trace { color: #FFA000; } .cython.code .error_goto { color: #FFA000; }

.cython.code .coerce { color: #008000; border: 1px dotted #008000 } .cython.code .py_attr { color: #FF0000; font-weight: bold; } .cython.code .c_attr { color: #0000FF; } .cython.code .py_call { color: #FF0000; font-weight: bold; } .cython.code .c_call { color: #0000FF; }

.cython.score-0 {background-color: #FFFFff;} .cython.score-1 {background-color: #FFFFe7;} .cython.score-2 {background-color: #FFFFd4;} .cython.score-3 {background-color: #FFFFc4;} .cython.score-4 {background-color: #FFFFb6;} .cython.score-5 {background-color: #FFFFaa;} .cython.score-6 {background-color: #FFFF9f;} .cython.score-7 {background-color: #FFFF96;} .cython.score-8 {background-color: #FFFF8d;} .cython.score-9 {background-color: #FFFF86;} .cython.score-10 {background-color: #FFFF7f;} .cython.score-11 {background-color: #FFFF79;} .cython.score-12 {background-color: #FFFF73;} .cython.score-13 {background-color: #FFFF6e;} .cython.score-14 {background-color: #FFFF6a;} .cython.score-15 {background-color: #FFFF66;} .cython.score-16 {background-color: #FFFF62;} .cython.score-17 {background-color: #FFFF5e;} .cython.score-18 {background-color: #FFFF5b;} .cython.score-19 {background-color: #FFFF57;} .cython.score-20 {background-color: #FFFF55;} .cython.score-21 {background-color: #FFFF52;} .cython.score-22 {background-color: #FFFF4f;} .cython.score-23 {background-color: #FFFF4d;} .cython.score-24 {background-color: #FFFF4b;} .cython.score-25 {background-color: #FFFF48;} .cython.score-26 {background-color: #FFFF46;} .cython.score-27 {background-color: #FFFF44;} .cython.score-28 {background-color: #FFFF43;} .cython.score-29 {background-color: #FFFF41;} .cython.score-30 {background-color: #FFFF3f;} .cython.score-31 {background-color: #FFFF3e;} .cython.score-32 {background-color: #FFFF3c;} .cython.score-33 {background-color: #FFFF3b;} .cython.score-34 {background-color: #FFFF39;} .cython.score-35 {background-color: #FFFF38;} .cython.score-36 {background-color: #FFFF37;} .cython.score-37 {background-color: #FFFF36;} .cython.score-38 {background-color: #FFFF35;} .cython.score-39 {background-color: #FFFF34;} .cython.score-40 {background-color: #FFFF33;} .cython.score-41 {background-color: #FFFF32;} .cython.score-42 {background-color: #FFFF31;} .cython.score-43 {background-color: #FFFF30;} .cython.score-44 {background-color: #FFFF2f;} .cython.score-45 {background-color: #FFFF2e;} .cython.score-46 {background-color: #FFFF2d;} .cython.score-47 {background-color: #FFFF2c;} .cython.score-48 {background-color: #FFFF2b;} .cython.score-49 {background-color: #FFFF2b;} .cython.score-50 {background-color: #FFFF2a;} .cython.score-51 {background-color: #FFFF29;} .cython.score-52 {background-color: #FFFF29;} .cython.score-53 {background-color: #FFFF28;} .cython.score-54 {background-color: #FFFF27;} .cython.score-55 {background-color: #FFFF27;} .cython.score-56 {background-color: #FFFF26;} .cython.score-57 {background-color: #FFFF26;} .cython.score-58 {background-color: #FFFF25;} .cython.score-59 {background-color: #FFFF24;} .cython.score-60 {background-color: #FFFF24;} .cython.score-61 {background-color: #FFFF23;} .cython.score-62 {background-color: #FFFF23;} .cython.score-63 {background-color: #FFFF22;} .cython.score-64 {background-color: #FFFF22;} .cython.score-65 {background-color: #FFFF22;} .cython.score-66 {background-color: #FFFF21;} .cython.score-67 {background-color: #FFFF21;} .cython.score-68 {background-color: #FFFF20;} .cython.score-69 {background-color: #FFFF20;} .cython.score-70 {background-color: #FFFF1f;} .cython.score-71 {background-color: #FFFF1f;} .cython.score-72 {background-color: #FFFF1f;} .cython.score-73 {background-color: #FFFF1e;} .cython.score-74 {background-color: #FFFF1e;} .cython.score-75 {background-color: #FFFF1e;} .cython.score-76 {background-color: #FFFF1d;} .cython.score-77 {background-color: #FFFF1d;} .cython.score-78 {background-color: #FFFF1c;} .cython.score-79 {background-color: #FFFF1c;} .cython.score-80 {background-color: #FFFF1c;} .cython.score-81 {background-color: #FFFF1c;} .cython.score-82 {background-color: #FFFF1b;} .cython.score-83 {background-color: #FFFF1b;} .cython.score-84 {background-color: #FFFF1b;} .cython.score-85 {background-color: #FFFF1a;} .cython.score-86 {background-color: #FFFF1a;} .cython.score-87 {background-color: #FFFF1a;} .cython.score-88 {background-color: #FFFF1a;} .cython.score-89 {background-color: #FFFF19;} .cython.score-90 {background-color: #FFFF19;} .cython.score-91 {background-color: #FFFF19;} .cython.score-92 {background-color: #FFFF19;} .cython.score-93 {background-color: #FFFF18;} .cython.score-94 {background-color: #FFFF18;} .cython.score-95 {background-color: #FFFF18;} .cython.score-96 {background-color: #FFFF18;} .cython.score-97 {background-color: #FFFF17;} .cython.score-98 {background-color: #FFFF17;} .cython.score-99 {background-color: #FFFF17;} .cython.score-100 {background-color: #FFFF17;} .cython.score-101 {background-color: #FFFF16;} .cython.score-102 {background-color: #FFFF16;} .cython.score-103 {background-color: #FFFF16;} .cython.score-104 {background-color: #FFFF16;} .cython.score-105 {background-color: #FFFF16;} .cython.score-106 {background-color: #FFFF15;} .cython.score-107 {background-color: #FFFF15;} .cython.score-108 {background-color: #FFFF15;} .cython.score-109 {background-color: #FFFF15;} .cython.score-110 {background-color: #FFFF15;} .cython.score-111 {background-color: #FFFF15;} .cython.score-112 {background-color: #FFFF14;} .cython.score-113 {background-color: #FFFF14;} .cython.score-114 {background-color: #FFFF14;} .cython.score-115 {background-color: #FFFF14;} .cython.score-116 {background-color: #FFFF14;} .cython.score-117 {background-color: #FFFF14;} .cython.score-118 {background-color: #FFFF13;} .cython.score-119 {background-color: #FFFF13;} .cython.score-120 {background-color: #FFFF13;} .cython.score-121 {background-color: #FFFF13;} .cython.score-122 {background-color: #FFFF13;} .cython.score-123 {background-color: #FFFF13;} .cython.score-124 {background-color: #FFFF13;} .cython.score-125 {background-color: #FFFF12;} .cython.score-126 {background-color: #FFFF12;} .cython.score-127 {background-color: #FFFF12;} .cython.score-128 {background-color: #FFFF12;} .cython.score-129 {background-color: #FFFF12;} .cython.score-130 {background-color: #FFFF12;} .cython.score-131 {background-color: #FFFF12;} .cython.score-132 {background-color: #FFFF11;} .cython.score-133 {background-color: #FFFF11;} .cython.score-134 {background-color: #FFFF11;} .cython.score-135 {background-color: #FFFF11;} .cython.score-136 {background-color: #FFFF11;} .cython.score-137 {background-color: #FFFF11;} .cython.score-138 {background-color: #FFFF11;} .cython.score-139 {background-color: #FFFF11;} .cython.score-140 {background-color: #FFFF11;} .cython.score-141 {background-color: #FFFF10;} .cython.score-142 {background-color: #FFFF10;} .cython.score-143 {background-color: #FFFF10;} .cython.score-144 {background-color: #FFFF10;} .cython.score-145 {background-color: #FFFF10;} .cython.score-146 {background-color: #FFFF10;} .cython.score-147 {background-color: #FFFF10;} .cython.score-148 {background-color: #FFFF10;} .cython.score-149 {background-color: #FFFF10;} .cython.score-150 {background-color: #FFFF0f;} .cython.score-151 {background-color: #FFFF0f;} .cython.score-152 {background-color: #FFFF0f;} .cython.score-153 {background-color: #FFFF0f;} .cython.score-154 {background-color: #FFFF0f;} .cython.score-155 {background-color: #FFFF0f;} .cython.score-156 {background-color: #FFFF0f;} .cython.score-157 {background-color: #FFFF0f;} .cython.score-158 {background-color: #FFFF0f;} .cython.score-159 {background-color: #FFFF0f;} .cython.score-160 {background-color: #FFFF0f;} .cython.score-161 {background-color: #FFFF0e;} .cython.score-162 {background-color: #FFFF0e;} .cython.score-163 {background-color: #FFFF0e;} .cython.score-164 {background-color: #FFFF0e;} .cython.score-165 {background-color: #FFFF0e;} .cython.score-166 {background-color: #FFFF0e;} .cython.score-167 {background-color: #FFFF0e;} .cython.score-168 {background-color: #FFFF0e;} .cython.score-169 {background-color: #FFFF0e;} .cython.score-170 {background-color: #FFFF0e;} .cython.score-171 {background-color: #FFFF0e;} .cython.score-172 {background-color: #FFFF0e;} .cython.score-173 {background-color: #FFFF0d;} .cython.score-174 {background-color: #FFFF0d;} .cython.score-175 {background-color: #FFFF0d;} .cython.score-176 {background-color: #FFFF0d;} .cython.score-177 {background-color: #FFFF0d;} .cython.score-178 {background-color: #FFFF0d;} .cython.score-179 {background-color: #FFFF0d;} .cython.score-180 {background-color: #FFFF0d;} .cython.score-181 {background-color: #FFFF0d;} .cython.score-182 {background-color: #FFFF0d;} .cython.score-183 {background-color: #FFFF0d;} .cython.score-184 {background-color: #FFFF0d;} .cython.score-185 {background-color: #FFFF0d;} .cython.score-186 {background-color: #FFFF0d;} .cython.score-187 {background-color: #FFFF0c;} .cython.score-188 {background-color: #FFFF0c;} .cython.score-189 {background-color: #FFFF0c;} .cython.score-190 {background-color: #FFFF0c;} .cython.score-191 {background-color: #FFFF0c;} .cython.score-192 {background-color: #FFFF0c;} .cython.score-193 {background-color: #FFFF0c;} .cython.score-194 {background-color: #FFFF0c;} .cython.score-195 {background-color: #FFFF0c;} .cython.score-196 {background-color: #FFFF0c;} .cython.score-197 {background-color: #FFFF0c;} .cython.score-198 {background-color: #FFFF0c;} .cython.score-199 {background-color: #FFFF0c;} .cython.score-200 {background-color: #FFFF0c;} .cython.score-201 {background-color: #FFFF0c;} .cython.score-202 {background-color: #FFFF0c;} .cython.score-203 {background-color: #FFFF0b;} .cython.score-204 {background-color: #FFFF0b;} .cython.score-205 {background-color: #FFFF0b;} .cython.score-206 {background-color: #FFFF0b;} .cython.score-207 {background-color: #FFFF0b;} .cython.score-208 {background-color: #FFFF0b;} .cython.score-209 {background-color: #FFFF0b;} .cython.score-210 {background-color: #FFFF0b;} .cython.score-211 {background-color: #FFFF0b;} .cython.score-212 {background-color: #FFFF0b;} .cython.score-213 {background-color: #FFFF0b;} .cython.score-214 {background-color: #FFFF0b;} .cython.score-215 {background-color: #FFFF0b;} .cython.score-216 {background-color: #FFFF0b;} .cython.score-217 {background-color: #FFFF0b;} .cython.score-218 {background-color: #FFFF0b;} .cython.score-219 {background-color: #FFFF0b;} .cython.score-220 {background-color: #FFFF0b;} .cython.score-221 {background-color: #FFFF0b;} .cython.score-222 {background-color: #FFFF0a;} .cython.score-223 {background-color: #FFFF0a;} .cython.score-224 {background-color: #FFFF0a;} .cython.score-225 {background-color: #FFFF0a;} .cython.score-226 {background-color: #FFFF0a;} .cython.score-227 {background-color: #FFFF0a;} .cython.score-228 {background-color: #FFFF0a;} .cython.score-229 {background-color: #FFFF0a;} .cython.score-230 {background-color: #FFFF0a;} .cython.score-231 {background-color: #FFFF0a;} .cython.score-232 {background-color: #FFFF0a;} .cython.score-233 {background-color: #FFFF0a;} .cython.score-234 {background-color: #FFFF0a;} .cython.score-235 {background-color: #FFFF0a;} .cython.score-236 {background-color: #FFFF0a;} .cython.score-237 {background-color: #FFFF0a;} .cython.score-238 {background-color: #FFFF0a;} .cython.score-239 {background-color: #FFFF0a;} .cython.score-240 {background-color: #FFFF0a;} .cython.score-241 {background-color: #FFFF0a;} .cython.score-242 {background-color: #FFFF0a;} .cython.score-243 {background-color: #FFFF0a;} .cython.score-244 {background-color: #FFFF0a;} .cython.score-245 {background-color: #FFFF0a;} .cython.score-246 {background-color: #FFFF09;} .cython.score-247 {background-color: #FFFF09;} .cython.score-248 {background-color: #FFFF09;} .cython.score-249 {background-color: #FFFF09;} .cython.score-250 {background-color: #FFFF09;} .cython.score-251 {background-color: #FFFF09;} .cython.score-252 {background-color: #FFFF09;} .cython.score-253 {background-color: #FFFF09;} .cython.score-254 {background-color: #FFFF09;} .cython .hll { background-color: #ffffcc } .cython { background: #f8f8f8; } .cython .c { color: #408080; font-style: italic } /* Comment */ .cython .err { border: 1px solid #FF0000 } /* Error */ .cython .k { color: #008000; font-weight: bold } /* Keyword */ .cython .o { color: #666666 } /* Operator */ .cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cython .cp { color: #BC7A00 } /* Comment.Preproc */ .cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .cython .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cython .cs { color: #408080; font-style: italic } /* Comment.Special */ .cython .gd { color: #A00000 } /* Generic.Deleted */ .cython .ge { font-style: italic } /* Generic.Emph */ .cython .gr { color: #FF0000 } /* Generic.Error */ .cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .cython .gi { color: #00A000 } /* Generic.Inserted */ .cython .go { color: #888888 } /* Generic.Output */ .cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .cython .gs { font-weight: bold } /* Generic.Strong */ .cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .cython .gt { color: #0044DD } /* Generic.Traceback */ .cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .cython .kp { color: #008000 } /* Keyword.Pseudo */ .cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .cython .kt { color: #B00040 } /* Keyword.Type */ .cython .m { color: #666666 } /* Literal.Number */ .cython .s { color: #BA2121 } /* Literal.String */ .cython .na { color: #7D9029 } /* Name.Attribute */ .cython .nb { color: #008000 } /* Name.Builtin */ .cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .cython .no { color: #880000 } /* Name.Constant */ .cython .nd { color: #AA22FF } /* Name.Decorator */ .cython .ni { color: #999999; font-weight: bold } /* Name.Entity */ .cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .cython .nf { color: #0000FF } /* Name.Function */ .cython .nl { color: #A0A000 } /* Name.Label */ .cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .cython .nt { color: #008000; font-weight: bold } /* Name.Tag */ .cython .nv { color: #19177C } /* Name.Variable */ .cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .cython .w { color: #bbbbbb } /* Text.Whitespace */ .cython .mb { color: #666666 } /* Literal.Number.Bin */ .cython .mf { color: #666666 } /* Literal.Number.Float */ .cython .mh { color: #666666 } /* Literal.Number.Hex */ .cython .mi { color: #666666 } /* Literal.Number.Integer */ .cython .mo { color: #666666 } /* Literal.Number.Oct */ .cython .sa { color: #BA2121 } /* Literal.String.Affix */ .cython .sb { color: #BA2121 } /* Literal.String.Backtick */ .cython .sc { color: #BA2121 } /* Literal.String.Char */ .cython .dl { color: #BA2121 } /* Literal.String.Delimiter */ .cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .cython .s2 { color: #BA2121 } /* Literal.String.Double */ .cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .cython .sh { color: #BA2121 } /* Literal.String.Heredoc */ .cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .cython .sx { color: #008000 } /* Literal.String.Other */ .cython .sr { color: #BB6688 } /* Literal.String.Regex */ .cython .s1 { color: #BA2121 } /* Literal.String.Single */ .cython .ss { color: #19177C } /* Literal.String.Symbol */ .cython .bp { color: #008000 } /* Name.Builtin.Pseudo */ .cython .fm { color: #0000FF } /* Name.Function.Magic */ .cython .vc { color: #19177C } /* Name.Variable.Class */ .cython .vg { color: #19177C } /* Name.Variable.Global */ .cython .vi { color: #19177C } /* Name.Variable.Instance */ .cython .vm { color: #19177C } /* Name.Variable.Magic */ .cython .il { color: #666666 } /* Literal.Number.Integer.Long */

/* 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_AddTraceback("_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6.__pyx_fused_cpdef”, __pyx_clineno, __pyx_lineno, __pyx_filename); __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_PyThreadState_declare __Pyx_PyThreadState_assign __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb); __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer); __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);} __Pyx_AddTraceback("_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6.cy_fused_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __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_PyThreadState_declare __Pyx_PyThreadState_assign __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb); __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_data.rcbuffer->pybuffer); __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);} __Pyx_AddTraceback("_cython_magic_b4146ff13fa5f55c0c7eec9d2783ecb6.cy_fused_sum", __pyx_clineno, __pyx_lineno, __pyx_filename); __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

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

15.0

cy_fused_sum(np.array([1, 2, 3, 4, 5]))

15


## Julia fractal

%%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


body.cython { font-family: courier; font-size: 12; }

.cython.tag { } .cython.line { margin: 0em } .cython.code { font-size: 9; color: #444444; display: none; margin: 0px 0px 0px 8px; border-left: 8px none; }

.cython.line .run { background-color: #B0FFB0; } .cython.line .mis { background-color: #FFB0B0; } .cython.code.run { border-left: 8px solid #B0FFB0; } .cython.code.mis { border-left: 8px solid #FFB0B0; }

.cython.code .py_c_api { color: red; } .cython.code .py_macro_api { color: #FF7000; } .cython.code .pyx_c_api { color: #FF3000; } .cython.code .pyx_macro_api { color: #FF7000; } .cython.code .refnanny { color: #FFA000; } .cython.code .trace { color: #FFA000; } .cython.code .error_goto { color: #FFA000; }

.cython.code .coerce { color: #008000; border: 1px dotted #008000 } .cython.code .py_attr { color: #FF0000; font-weight: bold; } .cython.code .c_attr { color: #0000FF; } .cython.code .py_call { color: #FF0000; font-weight: bold; } .cython.code .c_call { color: #0000FF; }

.cython.score-0 {background-color: #FFFFff;} .cython.score-1 {background-color: #FFFFe7;} .cython.score-2 {background-color: #FFFFd4;} .cython.score-3 {background-color: #FFFFc4;} .cython.score-4 {background-color: #FFFFb6;} .cython.score-5 {background-color: #FFFFaa;} .cython.score-6 {background-color: #FFFF9f;} .cython.score-7 {background-color: #FFFF96;} .cython.score-8 {background-color: #FFFF8d;} .cython.score-9 {background-color: #FFFF86;} .cython.score-10 {background-color: #FFFF7f;} .cython.score-11 {background-color: #FFFF79;} .cython.score-12 {background-color: #FFFF73;} .cython.score-13 {background-color: #FFFF6e;} .cython.score-14 {background-color: #FFFF6a;} .cython.score-15 {background-color: #FFFF66;} .cython.score-16 {background-color: #FFFF62;} .cython.score-17 {background-color: #FFFF5e;} .cython.score-18 {background-color: #FFFF5b;} .cython.score-19 {background-color: #FFFF57;} .cython.score-20 {background-color: #FFFF55;} .cython.score-21 {background-color: #FFFF52;} .cython.score-22 {background-color: #FFFF4f;} .cython.score-23 {background-color: #FFFF4d;} .cython.score-24 {background-color: #FFFF4b;} .cython.score-25 {background-color: #FFFF48;} .cython.score-26 {background-color: #FFFF46;} .cython.score-27 {background-color: #FFFF44;} .cython.score-28 {background-color: #FFFF43;} .cython.score-29 {background-color: #FFFF41;} .cython.score-30 {background-color: #FFFF3f;} .cython.score-31 {background-color: #FFFF3e;} .cython.score-32 {background-color: #FFFF3c;} .cython.score-33 {background-color: #FFFF3b;} .cython.score-34 {background-color: #FFFF39;} .cython.score-35 {background-color: #FFFF38;} .cython.score-36 {background-color: #FFFF37;} .cython.score-37 {background-color: #FFFF36;} .cython.score-38 {background-color: #FFFF35;} .cython.score-39 {background-color: #FFFF34;} .cython.score-40 {background-color: #FFFF33;} .cython.score-41 {background-color: #FFFF32;} .cython.score-42 {background-color: #FFFF31;} .cython.score-43 {background-color: #FFFF30;} .cython.score-44 {background-color: #FFFF2f;} .cython.score-45 {background-color: #FFFF2e;} .cython.score-46 {background-color: #FFFF2d;} .cython.score-47 {background-color: #FFFF2c;} .cython.score-48 {background-color: #FFFF2b;} .cython.score-49 {background-color: #FFFF2b;} .cython.score-50 {background-color: #FFFF2a;} .cython.score-51 {background-color: #FFFF29;} .cython.score-52 {background-color: #FFFF29;} .cython.score-53 {background-color: #FFFF28;} .cython.score-54 {background-color: #FFFF27;} .cython.score-55 {background-color: #FFFF27;} .cython.score-56 {background-color: #FFFF26;} .cython.score-57 {background-color: #FFFF26;} .cython.score-58 {background-color: #FFFF25;} .cython.score-59 {background-color: #FFFF24;} .cython.score-60 {background-color: #FFFF24;} .cython.score-61 {background-color: #FFFF23;} .cython.score-62 {background-color: #FFFF23;} .cython.score-63 {background-color: #FFFF22;} .cython.score-64 {background-color: #FFFF22;} .cython.score-65 {background-color: #FFFF22;} .cython.score-66 {background-color: #FFFF21;} .cython.score-67 {background-color: #FFFF21;} .cython.score-68 {background-color: #FFFF20;} .cython.score-69 {background-color: #FFFF20;} .cython.score-70 {background-color: #FFFF1f;} .cython.score-71 {background-color: #FFFF1f;} .cython.score-72 {background-color: #FFFF1f;} .cython.score-73 {background-color: #FFFF1e;} .cython.score-74 {background-color: #FFFF1e;} .cython.score-75 {background-color: #FFFF1e;} .cython.score-76 {background-color: #FFFF1d;} .cython.score-77 {background-color: #FFFF1d;} .cython.score-78 {background-color: #FFFF1c;} .cython.score-79 {background-color: #FFFF1c;} .cython.score-80 {background-color: #FFFF1c;} .cython.score-81 {background-color: #FFFF1c;} .cython.score-82 {background-color: #FFFF1b;} .cython.score-83 {background-color: #FFFF1b;} .cython.score-84 {background-color: #FFFF1b;} .cython.score-85 {background-color: #FFFF1a;} .cython.score-86 {background-color: #FFFF1a;} .cython.score-87 {background-color: #FFFF1a;} .cython.score-88 {background-color: #FFFF1a;} .cython.score-89 {background-color: #FFFF19;} .cython.score-90 {background-color: #FFFF19;} .cython.score-91 {background-color: #FFFF19;} .cython.score-92 {background-color: #FFFF19;} .cython.score-93 {background-color: #FFFF18;} .cython.score-94 {background-color: #FFFF18;} .cython.score-95 {background-color: #FFFF18;} .cython.score-96 {background-color: #FFFF18;} .cython.score-97 {background-color: #FFFF17;} .cython.score-98 {background-color: #FFFF17;} .cython.score-99 {background-color: #FFFF17;} .cython.score-100 {background-color: #FFFF17;} .cython.score-101 {background-color: #FFFF16;} .cython.score-102 {background-color: #FFFF16;} .cython.score-103 {background-color: #FFFF16;} .cython.score-104 {background-color: #FFFF16;} .cython.score-105 {background-color: #FFFF16;} .cython.score-106 {background-color: #FFFF15;} .cython.score-107 {background-color: #FFFF15;} .cython.score-108 {background-color: #FFFF15;} .cython.score-109 {background-color: #FFFF15;} .cython.score-110 {background-color: #FFFF15;} .cython.score-111 {background-color: #FFFF15;} .cython.score-112 {background-color: #FFFF14;} .cython.score-113 {background-color: #FFFF14;} .cython.score-114 {background-color: #FFFF14;} .cython.score-115 {background-color: #FFFF14;} .cython.score-116 {background-color: #FFFF14;} .cython.score-117 {background-color: #FFFF14;} .cython.score-118 {background-color: #FFFF13;} .cython.score-119 {background-color: #FFFF13;} .cython.score-120 {background-color: #FFFF13;} .cython.score-121 {background-color: #FFFF13;} .cython.score-122 {background-color: #FFFF13;} .cython.score-123 {background-color: #FFFF13;} .cython.score-124 {background-color: #FFFF13;} .cython.score-125 {background-color: #FFFF12;} .cython.score-126 {background-color: #FFFF12;} .cython.score-127 {background-color: #FFFF12;} .cython.score-128 {background-color: #FFFF12;} .cython.score-129 {background-color: #FFFF12;} .cython.score-130 {background-color: #FFFF12;} .cython.score-131 {background-color: #FFFF12;} .cython.score-132 {background-color: #FFFF11;} .cython.score-133 {background-color: #FFFF11;} .cython.score-134 {background-color: #FFFF11;} .cython.score-135 {background-color: #FFFF11;} .cython.score-136 {background-color: #FFFF11;} .cython.score-137 {background-color: #FFFF11;} .cython.score-138 {background-color: #FFFF11;} .cython.score-139 {background-color: #FFFF11;} .cython.score-140 {background-color: #FFFF11;} .cython.score-141 {background-color: #FFFF10;} .cython.score-142 {background-color: #FFFF10;} .cython.score-143 {background-color: #FFFF10;} .cython.score-144 {background-color: #FFFF10;} .cython.score-145 {background-color: #FFFF10;} .cython.score-146 {background-color: #FFFF10;} .cython.score-147 {background-color: #FFFF10;} .cython.score-148 {background-color: #FFFF10;} .cython.score-149 {background-color: #FFFF10;} .cython.score-150 {background-color: #FFFF0f;} .cython.score-151 {background-color: #FFFF0f;} .cython.score-152 {background-color: #FFFF0f;} .cython.score-153 {background-color: #FFFF0f;} .cython.score-154 {background-color: #FFFF0f;} .cython.score-155 {background-color: #FFFF0f;} .cython.score-156 {background-color: #FFFF0f;} .cython.score-157 {background-color: #FFFF0f;} .cython.score-158 {background-color: #FFFF0f;} .cython.score-159 {background-color: #FFFF0f;} .cython.score-160 {background-color: #FFFF0f;} .cython.score-161 {background-color: #FFFF0e;} .cython.score-162 {background-color: #FFFF0e;} .cython.score-163 {background-color: #FFFF0e;} .cython.score-164 {background-color: #FFFF0e;} .cython.score-165 {background-color: #FFFF0e;} .cython.score-166 {background-color: #FFFF0e;} .cython.score-167 {background-color: #FFFF0e;} .cython.score-168 {background-color: #FFFF0e;} .cython.score-169 {background-color: #FFFF0e;} .cython.score-170 {background-color: #FFFF0e;} .cython.score-171 {background-color: #FFFF0e;} .cython.score-172 {background-color: #FFFF0e;} .cython.score-173 {background-color: #FFFF0d;} .cython.score-174 {background-color: #FFFF0d;} .cython.score-175 {background-color: #FFFF0d;} .cython.score-176 {background-color: #FFFF0d;} .cython.score-177 {background-color: #FFFF0d;} .cython.score-178 {background-color: #FFFF0d;} .cython.score-179 {background-color: #FFFF0d;} .cython.score-180 {background-color: #FFFF0d;} .cython.score-181 {background-color: #FFFF0d;} .cython.score-182 {background-color: #FFFF0d;} .cython.score-183 {background-color: #FFFF0d;} .cython.score-184 {background-color: #FFFF0d;} .cython.score-185 {background-color: #FFFF0d;} .cython.score-186 {background-color: #FFFF0d;} .cython.score-187 {background-color: #FFFF0c;} .cython.score-188 {background-color: #FFFF0c;} .cython.score-189 {background-color: #FFFF0c;} .cython.score-190 {background-color: #FFFF0c;} .cython.score-191 {background-color: #FFFF0c;} .cython.score-192 {background-color: #FFFF0c;} .cython.score-193 {background-color: #FFFF0c;} .cython.score-194 {background-color: #FFFF0c;} .cython.score-195 {background-color: #FFFF0c;} .cython.score-196 {background-color: #FFFF0c;} .cython.score-197 {background-color: #FFFF0c;} .cython.score-198 {background-color: #FFFF0c;} .cython.score-199 {background-color: #FFFF0c;} .cython.score-200 {background-color: #FFFF0c;} .cython.score-201 {background-color: #FFFF0c;} .cython.score-202 {background-color: #FFFF0c;} .cython.score-203 {background-color: #FFFF0b;} .cython.score-204 {background-color: #FFFF0b;} .cython.score-205 {background-color: #FFFF0b;} .cython.score-206 {background-color: #FFFF0b;} .cython.score-207 {background-color: #FFFF0b;} .cython.score-208 {background-color: #FFFF0b;} .cython.score-209 {background-color: #FFFF0b;} .cython.score-210 {background-color: #FFFF0b;} .cython.score-211 {background-color: #FFFF0b;} .cython.score-212 {background-color: #FFFF0b;} .cython.score-213 {background-color: #FFFF0b;} .cython.score-214 {background-color: #FFFF0b;} .cython.score-215 {background-color: #FFFF0b;} .cython.score-216 {background-color: #FFFF0b;} .cython.score-217 {background-color: #FFFF0b;} .cython.score-218 {background-color: #FFFF0b;} .cython.score-219 {background-color: #FFFF0b;} .cython.score-220 {background-color: #FFFF0b;} .cython.score-221 {background-color: #FFFF0b;} .cython.score-222 {background-color: #FFFF0a;} .cython.score-223 {background-color: #FFFF0a;} .cython.score-224 {background-color: #FFFF0a;} .cython.score-225 {background-color: #FFFF0a;} .cython.score-226 {background-color: #FFFF0a;} .cython.score-227 {background-color: #FFFF0a;} .cython.score-228 {background-color: #FFFF0a;} .cython.score-229 {background-color: #FFFF0a;} .cython.score-230 {background-color: #FFFF0a;} .cython.score-231 {background-color: #FFFF0a;} .cython.score-232 {background-color: #FFFF0a;} .cython.score-233 {background-color: #FFFF0a;} .cython.score-234 {background-color: #FFFF0a;} .cython.score-235 {background-color: #FFFF0a;} .cython.score-236 {background-color: #FFFF0a;} .cython.score-237 {background-color: #FFFF0a;} .cython.score-238 {background-color: #FFFF0a;} .cython.score-239 {background-color: #FFFF0a;} .cython.score-240 {background-color: #FFFF0a;} .cython.score-241 {background-color: #FFFF0a;} .cython.score-242 {background-color: #FFFF0a;} .cython.score-243 {background-color: #FFFF0a;} .cython.score-244 {background-color: #FFFF0a;} .cython.score-245 {background-color: #FFFF0a;} .cython.score-246 {background-color: #FFFF09;} .cython.score-247 {background-color: #FFFF09;} .cython.score-248 {background-color: #FFFF09;} .cython.score-249 {background-color: #FFFF09;} .cython.score-250 {background-color: #FFFF09;} .cython.score-251 {background-color: #FFFF09;} .cython.score-252 {background-color: #FFFF09;} .cython.score-253 {background-color: #FFFF09;} .cython.score-254 {background-color: #FFFF09;} .cython .hll { background-color: #ffffcc } .cython { background: #f8f8f8; } .cython .c { color: #408080; font-style: italic } /* Comment */ .cython .err { border: 1px solid #FF0000 } /* Error */ .cython .k { color: #008000; font-weight: bold } /* Keyword */ .cython .o { color: #666666 } /* Operator */ .cython .ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .cython .cm { color: #408080; font-style: italic } /* Comment.Multiline */ .cython .cp { color: #BC7A00 } /* Comment.Preproc */ .cython .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .cython .c1 { color: #408080; font-style: italic } /* Comment.Single */ .cython .cs { color: #408080; font-style: italic } /* Comment.Special */ .cython .gd { color: #A00000 } /* Generic.Deleted */ .cython .ge { font-style: italic } /* Generic.Emph */ .cython .gr { color: #FF0000 } /* Generic.Error */ .cython .gh { color: #000080; font-weight: bold } /* Generic.Heading */ .cython .gi { color: #00A000 } /* Generic.Inserted */ .cython .go { color: #888888 } /* Generic.Output */ .cython .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ .cython .gs { font-weight: bold } /* Generic.Strong */ .cython .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ .cython .gt { color: #0044DD } /* Generic.Traceback */ .cython .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ .cython .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ .cython .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ .cython .kp { color: #008000 } /* Keyword.Pseudo */ .cython .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ .cython .kt { color: #B00040 } /* Keyword.Type */ .cython .m { color: #666666 } /* Literal.Number */ .cython .s { color: #BA2121 } /* Literal.String */ .cython .na { color: #7D9029 } /* Name.Attribute */ .cython .nb { color: #008000 } /* Name.Builtin */ .cython .nc { color: #0000FF; font-weight: bold } /* Name.Class */ .cython .no { color: #880000 } /* Name.Constant */ .cython .nd { color: #AA22FF } /* Name.Decorator */ .cython .ni { color: #999999; font-weight: bold } /* Name.Entity */ .cython .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ .cython .nf { color: #0000FF } /* Name.Function */ .cython .nl { color: #A0A000 } /* Name.Label */ .cython .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ .cython .nt { color: #008000; font-weight: bold } /* Name.Tag */ .cython .nv { color: #19177C } /* Name.Variable */ .cython .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ .cython .w { color: #bbbbbb } /* Text.Whitespace */ .cython .mb { color: #666666 } /* Literal.Number.Bin */ .cython .mf { color: #666666 } /* Literal.Number.Float */ .cython .mh { color: #666666 } /* Literal.Number.Hex */ .cython .mi { color: #666666 } /* Literal.Number.Integer */ .cython .mo { color: #666666 } /* Literal.Number.Oct */ .cython .sa { color: #BA2121 } /* Literal.String.Affix */ .cython .sb { color: #BA2121 } /* Literal.String.Backtick */ .cython .sc { color: #BA2121 } /* Literal.String.Char */ .cython .dl { color: #BA2121 } /* Literal.String.Delimiter */ .cython .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ .cython .s2 { color: #BA2121 } /* Literal.String.Double */ .cython .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ .cython .sh { color: #BA2121 } /* Literal.String.Heredoc */ .cython .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ .cython .sx { color: #008000 } /* Literal.String.Other */ .cython .sr { color: #BB6688 } /* Literal.String.Regex */ .cython .s1 { color: #BA2121 } /* Literal.String.Single */ .cython .ss { color: #19177C } /* Literal.String.Symbol */ .cython .bp { color: #008000 } /* Name.Builtin.Pseudo */ .cython .fm { color: #0000FF } /* Name.Function.Magic */ .cython .vc { color: #19177C } /* Name.Variable.Class */ .cython .vg { color: #19177C } /* Name.Variable.Global */ .cython .vi { color: #19177C } /* Name.Variable.Instance */ .cython .vm { color: #19177C } /* Name.Variable.Magic */ .cython .il { color: #666666 } /* Literal.Number.Integer.Long */

/* 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_AddTraceback("_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6.abs2", __pyx_clineno, __pyx_lineno, __pyx_filename); __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_AddTraceback("_cython_magic_2d6248e2a8b34a7fee22dd6092c7b4a6.abs2", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } +08: return z.real * z.real + z.imag * z.imag

N = 1024

j = np.zeros((N, N), dtype=np.int64)

z_real = np.linspace(-1.5, 1.5, N)

z_imag = np.linspace(-1.5, 1.5, N)

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

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

j1 = np.zeros((N, N), dtype=np.int64)

cy_julia_fractal(z_real, z_imag, j1)

j2 = np.zeros((N, N), dtype=np.int64)

jit_julia_fractal(z_real, z_imag, j2)

assert np.allclose(j1, j2)


## Calling C function

%%cython

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

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

%timeit cy_acos1(0.5)

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

%%cython

from libc.math cimport acos

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

%timeit cy_acos2(0.5)

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

from numpy import arccos

%timeit arccos(0.5)

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

from math import acos

%timeit acos(0.5)

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

assert cy_acos1(0.5) == acos(0.5)

assert cy_acos2(0.5) == acos(0.5)


# Versions

%reload_ext version_information

%version_information numpy, cython, numba, matplotlib