Skip to content

Example: binary search using GSL

Syrtis Major edited this page Jun 2, 2022 · 1 revision
import cyper
import cython_gsl

code = """
import numpy as np
from numpy cimport ndarray
from cython_gsl cimport *


def search(double[:] a, double v):
    cdef:
        int i, n = len(a)
        gsl_interp_accel *spl_acc

    spl_acc = gsl_interp_accel_alloc()  # workspace for interp lookups
    i = gsl_interp_accel_find(spl_acc, &a[0], n, v)
    gsl_interp_accel_free(spl_acc)

    return i
"""

pyx = cyper.inline(code,
                   include_dirs=[cython_gsl.get_include(), np.get_include()],
                   library_dirs=[cython_gsl.get_library_dir()],
                   cimport_dirs=[cython_gsl.get_cython_include_dir()],
                   cythonize_args={'language_level': '3'},
                   libraries=['gsl', 'gslcblas'],
                   fast_indexing=True,
                   )
pyx.search(np.arange(10, dtype='f8'), 2.5)
# 2
Clone this wiki locally