<s> import copy <EOL> import sympy <EOL> import re <EOL> options = { } <EOL> options [ '<STR_LIT>' ] = True <EOL> def apply_func ( code , func , apply_to_ivs = True ) : <EOL> if apply_to_ivs : <EOL> code_ivs = [ ( func ( iv ) , func ( se ) ) for iv , se in code [ <NUM_LIT:0> ] ] <EOL> else : <EOL> code_ivs = [ ( iv , func ( se ) ) for iv , se in code [ <NUM_LIT:0> ] ] <EOL> code_exprs = [ ] <EOL> for expr in code [ <NUM_LIT:1> ] : <EOL> if isinstance ( expr , sympy . MatrixBase ) : <EOL> expr = expr . applyfunc ( func ) <EOL> else : <EOL> expr = func ( expr ) <EOL> code_exprs . append ( expr ) <EOL> return code_ivs , code_exprs <EOL> def xreplace ( code , xreplace_dict ) : <EOL> return apply_func ( code , lambda x : x . xreplace ( xreplace_dict ) ) <EOL> def code_back_to_exprs ( code ) : <EOL> ivars = copy . deepcopy ( code [ <NUM_LIT:0> ] ) <EOL> exps = copy . deepcopy ( code [ <NUM_LIT:1> ] ) <EOL> for i in range ( len ( ivars ) ) : <EOL> Dxreplace = { ivars [ i ] [ <NUM_LIT:0> ] : ivars [ i ] [ <NUM_LIT:1> ] } <EOL> for j in range ( i + <NUM_LIT:1> , len ( ivars ) ) : <EOL> if ivars [ j ] [ <NUM_LIT:1> ] . has ( ivars [ i ] [ <NUM_LIT:0> ] ) : <EOL> ivars [ j ] = ( ivars [ j ] [ <NUM_LIT:0> ] , ivars [ j ] [ <NUM_LIT:1> ] . xreplace ( Dxreplace ) ) <EOL> for j in range ( len ( exps ) ) : <EOL> if sympy . sympify ( exps [ j ] ) . has ( ivars [ i ] [ <NUM_LIT:0> ] ) : <EOL> exps [ j ] = exps [ j ] . xreplace ( Dxreplace ) <EOL> return exps <EOL> def _ccode ( expr , ) : <EOL> code = sympy . ccode ( expr ) <EOL> if options [ '<STR_LIT>' ] : <EOL> return re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , code ) <EOL> else : <EOL> return code <EOL> def _juliacode ( expr , ) : <EOL> code = sympy . printing . lambdarepr . lambdarepr ( expr ) <EOL> return code . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def code_to_string ( code , out_parms , printer , indent = '<STR_LIT>' , realtype = '<STR_LIT>' , <EOL> line_end = '<STR_LIT>' , outidxoffset = <NUM_LIT:0> ) : <EOL> codestr = '<STR_LIT>' <EOL> if realtype : <EOL> realtype += '<STR_LIT:U+0020>' <EOL> for i in range ( len ( code [ <NUM_LIT:0> ] ) ) : <EOL> codestr += indent + realtype + printer ( code [ <NUM_LIT:0> ] [ i ] [ <NUM_LIT:0> ] ) + '<STR_LIT>' + printer ( <EOL> code [ <NUM_LIT:0> ] [ i ] [ <NUM_LIT:1> ] ) + line_end + '<STR_LIT:\n>' <EOL> for c , out in enumerate ( out_parms ) : <EOL> codestr += '<STR_LIT:\n>' <EOL> for i in range ( len ( code [ <NUM_LIT:1> ] [ c ] ) ) : <EOL> codestr += indent + out + '<STR_LIT:[>' + str ( i + outidxoffset ) + '<STR_LIT>' + printer ( code [ <NUM_LIT:1> ] [ c ] [ i ] ) + line_end + '<STR_LIT:\n>' <EOL> return codestr <EOL> def codestring_count ( codestring , resume = False ) : <EOL> ops = [ ] <EOL> ops += [ ( '<STR_LIT:=>' , int ( codestring . count ( '<STR_LIT:=>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT:+>' , int ( codestring . count ( '<STR_LIT:+>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT:->' , int ( codestring . count ( '<STR_LIT:->' ) ) ) ] <EOL> ops += [ ( '<STR_LIT:*>' , int ( codestring . count ( '<STR_LIT:*>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT:/>' , int ( codestring . count ( '<STR_LIT:/>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT>' , int ( codestring . count ( '<STR_LIT>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT>' , int ( codestring . count ( '<STR_LIT>' ) ) ) ] <EOL> ops += [ ( '<STR_LIT>' , int ( codestring . count ( '<STR_LIT>' ) ) ) ] <EOL> if not resume : <EOL> return ops <EOL> else : <EOL> adds = int ( codestring . count ( '<STR_LIT:+>' ) ) + int ( codestring . count ( '<STR_LIT:->' ) ) <EOL> muls = int ( codestring . count ( '<STR_LIT:*>' ) ) + int ( <EOL> codestring . count ( '<STR_LIT:/>' ) ) + int ( codestring . count ( '<STR_LIT>' ) ) <EOL> return ops , { '<STR_LIT>' : adds , '<STR_LIT>' : muls , '<STR_LIT>' : adds + muls } <EOL> def gen_py_func ( code , out_parms , func_parms , func_name = '<STR_LIT>' ) : <EOL> indent = <NUM_LIT:4> * '<STR_LIT:U+0020>' <EOL> pycode = '<STR_LIT>' + func_name + '<STR_LIT:(>' <EOL> pycode += '<STR_LIT:U+002CU+0020>' . join ( func_parms ) <EOL> pycode += '<STR_LIT>' <EOL> for i , out in enumerate ( out_parms ) : <EOL> pycode += indent + out + '<STR_LIT>' + str ( len ( code [ <NUM_LIT:1> ] [ i ] ) ) + '<STR_LIT:\n>' <EOL> pycode += '<STR_LIT:\n>' <EOL> mainpycode = code_to_string ( code , out_parms , <EOL> sympy . printing . lambdarepr . lambdarepr , indent ) <EOL> pycode += mainpycode <EOL> pycode += '<STR_LIT:\n>' + indent + '<STR_LIT>' <EOL> pycode += '<STR_LIT:U+002CU+0020>' . join ( out_parms ) <EOL> pycode = pycode . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return pycode <EOL> def gen_c_func ( code , out_parms , func_parms , func_name = '<STR_LIT>' ) : <EOL> indent = <NUM_LIT:2> * '<STR_LIT:U+0020>' <EOL> ccode = '<STR_LIT>' + func_name + '<STR_LIT>' <EOL> ccode += '<STR_LIT>' . join ( out_parms ) <EOL> ccode += '<STR_LIT>' <EOL> ccode += '<STR_LIT>' . join ( func_parms ) <EOL> ccode += '<STR_LIT>' <EOL> mainccode = code_to_string ( code , out_parms , _ccode , indent , '<STR_LIT>' , '<STR_LIT:;>' ) <EOL> ccode += mainccode + '<STR_LIT:\n>' + indent + '<STR_LIT>' <EOL> ccode = ccode . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return ccode <EOL> def gen_julia_func ( code , out_parms , func_parms , func_name = '<STR_LIT>' ) : <EOL> indent = <NUM_LIT:4> * '<STR_LIT:U+0020>' <EOL> ccode = '<STR_LIT>' + func_name + '<STR_LIT:(>' <EOL> ccode += '<STR_LIT:U+002CU+0020>' . join ( out_parms ) <EOL> ccode += '<STR_LIT:U+002CU+0020>' <EOL> ccode += '<STR_LIT:U+002CU+0020>' . join ( func_parms ) <EOL> ccode += '<STR_LIT>' <EOL> code = code [ <NUM_LIT:0> ] , [ ( e . T if isinstance ( e , sympy . MatrixBase ) else e ) <EOL> for e in code [ <NUM_LIT:1> ] ] <EOL> mainccode = code_to_string ( code , out_parms , _juliacode , indent , '<STR_LIT>' , '<STR_LIT>' , <EOL> outidxoffset = <NUM_LIT:1> ) <EOL> ccode += mainccode + '<STR_LIT:\n>' + indent + '<STR_LIT>' <EOL> ccode += '<STR_LIT:U+002CU+0020>' . join ( out_parms ) <EOL> ccode += '<STR_LIT>' <EOL> ccode = ccode . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return ccode <EOL> def code_to_func ( lang , code , out_parms , func_name , func_parms , symb_replace ) : <EOL> if not isinstance ( code [ <NUM_LIT:1> ] , list ) : <EOL> code = ( code [ <NUM_LIT:0> ] , [ code [ <NUM_LIT:1> ] ] ) <EOL> if not isinstance ( out_parms , list ) : <EOL> out_parms = [ out_parms ] <EOL> lang = lang . lower ( ) <EOL> if lang in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> gen_func = gen_py_func <EOL> elif lang in [ '<STR_LIT:c>' , '<STR_LIT>' ] : <EOL> gen_func = gen_c_func <EOL> elif lang in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> gen_func = gen_julia_func <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if symb_replace : <EOL> sympified_replace = { } <EOL> for k , v in symb_replace . items ( ) : <EOL> if isinstance ( k , str ) : <EOL> k = sympy . Symbol ( k ) <EOL> if isinstance ( v , str ) : <EOL> v = sympy . Symbol ( v ) <EOL> sympified_replace [ k ] = v <EOL> code = xreplace ( code , sympified_replace ) <EOL> return gen_func ( code , out_parms , func_parms , func_name ) </s>
