Permalink
Browse files

Add Cython and numba examples

  • Loading branch information...
hombit committed Dec 8, 2017
1 parent 47b8ac4 commit 4eb630757373621d69502351ff1f1834afe3cad1
@@ -11,3 +11,8 @@ dist/

### Jupyter ###
.ipynb_checkpoints/


### Cython ###
*.c
*.so
@@ -1,2 +1,4 @@
numpy>=1.13
scipy>=1.0.0
cython>=0.26
numba>=0.35
@@ -0,0 +1 @@
__all__ = ('compilers', 'contfrac')
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import numpy as np
from numpy.testing import assert_allclose
from timeit import timeit
from copy import copy


timeit_kwargs= {
'stmt' : 'contfrac(a)',
'number': 10,
'setup': """import numpy as np
from contfrac import {f}_contfrac as contfrac
a = np.ones({n})""",
}

n = 1000000
f_types = ['pure', 'numba', 'cython']
time = {}
for f in f_types:
kwargs = copy(timeit_kwargs)
kwargs['setup'] = kwargs['setup'].format(f=f, n=n)
time[f] = timeit(**kwargs)
assert time['pure'] > time['numba']
assert time['pure'] > time['cython']
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

from functools import reduce
import numpy as np
from numpy.testing import assert_allclose
import numba


if __package__:
from .contfrac import contfrac as cython_contfrac
else:
import pyximport
pyximport.install(setup_args={'include_dirs': [np.get_include()]})
from cy_contfrac import contfrac as cython_contfrac

def pure_contfrac(a):
return reduce(lambda x, y: y + 1/x, a[::-1])

@numba.jit
def numba_contfrac(a):
c = 1 / a[-1]
for i in range(len(a)-2, -1, -1):
c = a[i] + 1/c
return c

n = 1000
a = np.ones(n)
assert (pure_contfrac(a)
== numba_contfrac(a)
== cython_contfrac(a))
assert_allclose(pure_contfrac(a), 0.5*(np.sqrt(5)+1))
@@ -0,0 +1,8 @@
cimport numpy as np
cimport cython

def contfrac(np.ndarray[np.float64_t, ndim=1] a):
cdef np.float64_t c = 1.0 / a[-1]
for i in range(len(a)-2, -1, -1):
c = a[i] + 1.0/c
return c
@@ -3,18 +3,31 @@
# Read packaging and distributing tutorial:
# https://packaging.python.org/tutorials/distributing-packages/

from setuptools import find_packages, setup
from setuptools import find_packages, setup, Extension
import os
import glob

packagename='scientific_python'

try:
from Cython.Build import cythonize
import numpy as np
ext_modules = cythonize(Extension(
'*',
['**/*.pyx'],
include_dirs=[np.get_include()])
)
except ImportError:
ext_modules = []

here = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join(here, 'requirements.txt')) as f:
install_requires=f.read().splitlines()

setup(
name='scientific_python',
version='0.1.1',
name=packagename,
version='0.2.0',
url='http://homb.it/sci_py/',
license='MIT',
author='Konstantin Malanchev',
@@ -24,6 +37,7 @@
# If you have only one Python module use `py_modules` instead of `packages`:
# py_module = ['mymodule'],
scripts=['bin/sci_py_example', 'bin/sci_py_import_all'],
ext_modules=ext_modules,
data_files=[
('doc', ['doc/course_abstract.docx']),
],
@@ -17,5 +17,5 @@ if [ "${PYTHON_PATH/$ENV}" = "${PYTHON_PATH}" ]; then
echo "Activation failed"
else
pip install -r requirements.txt
python setup.py install
python setup.py develop
fi

0 comments on commit 4eb6307

Please sign in to comment.