Next: Special Functions Up: Special Functions (special) Previous: Special Functions (special)

Vectorizing functions (special.general_function)

One of the features that the special sub-package provides is a class general_function to convert an ordinary Python function which accepts scalars and returns scalars into a ``vectorized-function'' with the same broadcasting rules as other Numeric functions (i.e. the Universal functions, or ufuncs). For example, suppose you have a Python function named addsubtract defined as:


>>> def addsubtract(a,b):
    if a > b:
        return a - b
    else:
        return a + b
which defines a function of two scalar variables and returns a scalar result. The class general_function can be used to ``vectorize'' this function so that
>>> vec_addsubstract = special.general_function(addsubtract)
returns a function which takes array arguments and returns an array result:


>>> vec_addsubtract([0,3,6,9],[1,3,5,7])
array([1, 6, 1, 2])



2001-07-27