(*args, **kwargs)
return a
frange(lambdastr, [start,] stop[, step]) -> ctypes double array

    Evaluates function on range using machine code.
    Currently only one-dimensional functions are supported.

    For simple functions it's somewhat slower than NumPy.
    For big functions it can be several times faster.

    lambdastr has the same restrictions as in clambdify.

    >>> frange('lambda x: sqrt(x)', 1, 4) # doctest: +ELLIPSIS
    <__main__.c_double_Array_3 object at ...>
    >>> for i in _:
    ...     print(i)
    ...
    1.0
    1.41421356237
    1.73205080757
frange(lambdastr, [start,] stop[, step]) -> ctypes double array
frange ( lambdastr [ start ] stop [ step ] ) - > ctypes double array
def frange(*args, **kwargs):
    """
    frange(lambdastr, [start,] stop[, step]) -> ctypes double array

    Evaluates function on range using machine code.
    Currently only one-dimensional functions are supported.

    For simple functions it's somewhat slower than NumPy.
    For big functions it can be several times faster.

    lambdastr has the same restrictions as in clambdify.

    >>> frange('lambda x: sqrt(x)', 1, 4) # doctest: +ELLIPSIS
    <__main__.c_double_Array_3 object at ...>
    >>> for i in _:
    ...     print(i)
    ...
    1.0
    1.41421356237
    1.73205080757

    """
    if len(args) > 4:
        raise TypeError('expected at most 4 arguments, got %i' % len(args))
    if len(args) < 2:
        raise TypeError('expected at least 2 argument, got %i' % len(args))
    # interpret arguments
    lambdastr = args[0]
    start = 0
    step = 1
    if len(args) == 2:
        stop = args[1]
    elif len(args) >= 3:
        start = args[1]
        stop = args[2]
    if len(args) == 4:
        step = args[3]
    if start + step == start:
        raise ValueError("step is too small and would cause an infinite loop")
    # determine length of resulting array
    # TODO: do this better
    length = stop - start
    if length % step == 0:
        length = length/step - 1  # exclude last one
    else:
        length = length//step
    if step > 0:
        if start < stop:
            length += 1  # include first one
    else:
        if start > stop:
            length += 1  # include first one
    if length < 0:
        length = 0
    if length != int(length):
        raise ValueError("length should be an integer")
    length = int(length)
    # create array
    a = (ctypes.c_double * length)()
    # generate code
    vardef = 'double* MAX; double x = %f;' % start
    loopbody = '*result = f(x); x += %f;' % step
    code = """
# include <math.h>

# define pi M_PI
# define e M_E

%s

void evalonrange(double *result, int n)
    {
    %s
    for (MAX = result + n; result < MAX; result++)
        {
        %s
        }
    }

""" % (genfcode(lambdastr, **kwargs), vardef, loopbody)
    # compile and run
    evalonrange = _compile(code, fname='evalonrange',
                           fprototype=[None, ctypes.c_void_p, ctypes.c_int])
    evalonrange(ctypes.byref(a), ctypes.c_int(length))
    # return ctypes array with results
    return a
def frange ( * args , * * kwargs ) : if len ( args ) > 4 : raise TypeError ( 'expected at most 4 arguments, got %i' % len ( args ) ) if len ( args ) < 2 : raise TypeError ( 'expected at least 2 argument, got %i' % len ( args ) ) # interpret arguments lambdastr = args [ 0 ] start = 0 step = 1 if len ( args ) == 2 : stop = args [ 1 ] elif len ( args ) >= 3 : start = args [ 1 ] stop = args [ 2 ] if len ( args ) == 4 : step = args [ 3 ] if start + step == start : raise ValueError ( "step is too small and would cause an infinite loop" ) # determine length of resulting array # TODO: do this better length = stop - start if length % step == 0 : length = length / step - 1 # exclude last one else : length = length // step if step > 0 : if start < stop : length += 1 # include first one else : if start > stop : length += 1 # include first one if length < 0 : length = 0 if length != int ( length ) : raise ValueError ( "length should be an integer" ) length = int ( length ) # create array a = ( ctypes . c_double * length ) ( ) # generate code vardef = 'double* MAX; double x = %f;' % start loopbody = '*result = f(x); x += %f;' % step code = """
# include <math.h>

# define pi M_PI
# define e M_E

%s

void evalonrange(double *result, int n)
    {
    %s
    for (MAX = result + n; result < MAX; result++)
        {
        %s
        }
    }

""" % ( genfcode ( lambdastr , * * kwargs ) , vardef , loopbody ) # compile and run evalonrange = _compile ( code , fname = 'evalonrange' , fprototype = [ None , ctypes . c_void_p , ctypes . c_int ] ) evalonrange ( ctypes . byref ( a ) , ctypes . c_int ( length ) ) # return ctypes array with results return a