(lambdastr, use_cse=False)
return code
Python lambda string -> C function code

    Optionally cse() is used to eliminate common subexpressions.
Python lambda string -> C function code
Python lambda string - > C function code
def genfcode(lambdastr, use_cse=False):
    """
    Python lambda string -> C function code

    Optionally cse() is used to eliminate common subexpressions.
    """
    # TODO: verify lambda string
    # interpret lambda string
    varstr, fstr = lambdastr.split(': ')
    varstr = varstr.lstrip('lambda ')
    # generate C variable string
    cvars = varstr.split(',')
    cvarstr = ''
    for v in cvars:
        cvarstr += 'double %s, ' % v
    cvarstr = cvarstr.rstrip(', ')
    # convert function string to C syntax
    if not use_cse:
        cfstr = ''
        finalexpr = cexpr(fstr)
    else:
        # eliminate common subexpressions
        subs, finalexpr = cse(sympify(fstr), _gentmpvars())
        if len(finalexpr) != 1:
            raise ValueError("Length should be 1")
        vardec = ''
        cfstr = ''
        for symbol, expr in subs:
            vardec += '    double %s;\n' % symbol.name
            cfstr += '    %s = %s;\n' % (
                symbol.name, cexpr(str(expr.evalf(dps))))
        cfstr = vardec + cfstr
        finalexpr = cexpr(str(finalexpr[0].evalf(dps)))
    # generate C code
    code = """
inline double f(%s)
    {
%s
    return %s;
    }
""" % (cvarstr, cfstr, finalexpr)
    return code
def genfcode ( lambdastr , use_cse = False ) : # TODO: verify lambda string # interpret lambda string varstr , fstr = lambdastr . split ( ': ' ) varstr = varstr . lstrip ( 'lambda ' ) # generate C variable string cvars = varstr . split ( ',' ) cvarstr = '' for v in cvars : cvarstr += 'double %s, ' % v cvarstr = cvarstr . rstrip ( ', ' ) # convert function string to C syntax if not use_cse : cfstr = '' finalexpr = cexpr ( fstr ) else : # eliminate common subexpressions subs , finalexpr = cse ( sympify ( fstr ) , _gentmpvars ( ) ) if len ( finalexpr ) != 1 : raise ValueError ( "Length should be 1" ) vardec = '' cfstr = '' for symbol , expr in subs : vardec += '    double %s;\n' % symbol . name cfstr += '    %s = %s;\n' % ( symbol . name , cexpr ( str ( expr . evalf ( dps ) ) ) ) cfstr = vardec + cfstr finalexpr = cexpr ( str ( finalexpr [ 0 ] . evalf ( dps ) ) ) # generate C code code = """
inline double f(%s)
    {
%s
    return %s;
    }
""" % ( cvarstr , cfstr , finalexpr ) return code