[SciPy-User] [ANN] Bottleneck 0.2

Keith Goodman kwgoodman@gmail....
Tue Dec 28 16:56:27 CST 2010


On Tue, Dec 28, 2010 at 2:40 PM, Sebastian Haase <seb.haase@gmail.com> wrote:
> Congratulations !  What do you mean by "templated functions" -- do you
> have a way of doing cython template functions now ?

Thank you! My hope is that others find it useful.

Nothing fancy (or of general use). For example, the nanmax function template:

https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/src/template/func/nanmax.py

is used to generate the nanmax pyx file:

https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/src/func/nanmax.pyx

The templating of the axis, for example, is done like this (from the
looper function docstring):

    Make a 3d loop template:

    >>> loop = '''
    .... for iINDEX0 in range(nINDEX0):
    ....         for iINDEX1 in range(nINDEX1):
    ....             amin = MAXDTYPE
    ....         for iINDEX2 in range(nINDEX2):
    ....                 ai = a[INDEXALL]
    ....             if ai <= amin:
    ....                 amin = ai
    ....         y[INDEXPOP] = amin
    .... '''

    Import the looper function:

    >>> from bottleneck.src.template.template import looper

    Make a loop over axis=0:

    >>> print looper(loop, ndim=3, axis=0)
    for i1 in range(n1):
        for i2 in range(n2):
            amin = MAXDTYPE
            for i0 in range(n0):
                ai = a[i0, i1, i2]
                if ai <= amin:
                    amin = ai
            y[i1, i2] = amin

    Make a loop over axis=1:

    >>> print looper(loop, ndim=3, axis=1)
    for i0 in range(n0):
        for i2 in range(n2):
            amin = MAXDTYPE
            for i1 in range(n1):
                ai = a[i0, i1, i2]
                if ai <= amin:
                    amin = ai
            y[i0, i2] = amin


More information about the SciPy-User mailing list