<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> import string <EOL> from sympy . core import S , Add , N <EOL> from sympy . core . compatibility import string_types , range <EOL> from sympy . core . function import Function <EOL> from sympy . printing . codeprinter import CodePrinter , Assignment <EOL> from sympy . printing . precedence import precedence <EOL> known_functions = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> class FCodePrinter ( CodePrinter ) : <EOL> """<STR_LIT>""" <EOL> printmethod = "<STR_LIT>" <EOL> language = "<STR_LIT>" <EOL> _default_settings = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } <EOL> _operators = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _relationals = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , settings = { } ) : <EOL> CodePrinter . __init__ ( self , settings ) <EOL> self . known_functions = dict ( known_functions ) <EOL> userfuncs = settings . get ( '<STR_LIT>' , { } ) <EOL> self . known_functions . update ( userfuncs ) <EOL> if self . _settings [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . _lead_code = "<STR_LIT:U+0020>" <EOL> self . _lead_cont = "<STR_LIT>" <EOL> self . _lead_comment = "<STR_LIT>" <EOL> elif self . _settings [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . _lead_code = "<STR_LIT>" <EOL> self . _lead_cont = "<STR_LIT:U+0020>" <EOL> self . _lead_comment = "<STR_LIT>" <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . _settings [ <EOL> '<STR_LIT>' ] ) <EOL> standards = { <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> } <EOL> if self . _settings [ '<STR_LIT>' ] not in standards : <EOL> raise ValueError ( "<STR_LIT>" % self . _settings [ <EOL> '<STR_LIT>' ] ) <EOL> def _rate_index_position ( self , p ) : <EOL> return - p * <NUM_LIT:5> <EOL> def _get_statement ( self , codestring ) : <EOL> return codestring <EOL> def _get_comment ( self , text ) : <EOL> return "<STR_LIT>" . format ( text ) <EOL> def _declare_number_const ( self , name , value ) : <EOL> return "<STR_LIT>" . format ( name , value ) <EOL> def _format_code ( self , lines ) : <EOL> return self . _wrap_fortran ( self . indent_code ( lines ) ) <EOL> def _traverse_matrix_indices ( self , mat ) : <EOL> rows , cols = mat . shape <EOL> return ( ( i , j ) for j in range ( cols ) for i in range ( rows ) ) <EOL> def _get_loop_opening_ending ( self , indices ) : <EOL> open_lines = [ ] <EOL> close_lines = [ ] <EOL> for i in indices : <EOL> var , start , stop = map ( self . _print , <EOL> [ i . label , i . lower + <NUM_LIT:1> , i . upper + <NUM_LIT:1> ] ) <EOL> open_lines . append ( "<STR_LIT>" % ( var , start , stop ) ) <EOL> close_lines . append ( "<STR_LIT>" ) <EOL> return open_lines , close_lines <EOL> def _print_Piecewise ( self , expr ) : <EOL> if expr . args [ - <NUM_LIT:1> ] . cond != True : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> lines = [ ] <EOL> if expr . has ( Assignment ) : <EOL> for i , ( e , c ) in enumerate ( expr . args ) : <EOL> if i == <NUM_LIT:0> : <EOL> lines . append ( "<STR_LIT>" % self . _print ( c ) ) <EOL> elif i == len ( expr . args ) - <NUM_LIT:1> and c == True : <EOL> lines . append ( "<STR_LIT>" ) <EOL> else : <EOL> lines . append ( "<STR_LIT>" % self . _print ( c ) ) <EOL> lines . append ( self . _print ( e ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> elif self . _settings [ "<STR_LIT>" ] >= <NUM_LIT> : <EOL> pattern = "<STR_LIT>" <EOL> code = self . _print ( expr . args [ - <NUM_LIT:1> ] . expr ) <EOL> terms = list ( expr . args [ : - <NUM_LIT:1> ] ) <EOL> while terms : <EOL> e , c = terms . pop ( ) <EOL> expr = self . _print ( e ) <EOL> cond = self . _print ( c ) <EOL> code = pattern . format ( T = expr , F = code , COND = cond ) <EOL> return code <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _print_MatrixElement ( self , expr ) : <EOL> return "<STR_LIT>" . format ( expr . parent , expr . i + <NUM_LIT:1> , expr . j + <NUM_LIT:1> ) <EOL> def _print_Add ( self , expr ) : <EOL> pure_real = [ ] <EOL> pure_imaginary = [ ] <EOL> mixed = [ ] <EOL> for arg in expr . args : <EOL> if arg . is_number and arg . is_real : <EOL> pure_real . append ( arg ) <EOL> elif arg . is_number and arg . is_imaginary : <EOL> pure_imaginary . append ( arg ) <EOL> else : <EOL> mixed . append ( arg ) <EOL> if len ( pure_imaginary ) > <NUM_LIT:0> : <EOL> if len ( mixed ) > <NUM_LIT:0> : <EOL> PREC = precedence ( expr ) <EOL> term = Add ( * mixed ) <EOL> t = self . _print ( term ) <EOL> if t . startswith ( '<STR_LIT:->' ) : <EOL> sign = "<STR_LIT:->" <EOL> t = t [ <NUM_LIT:1> : ] <EOL> else : <EOL> sign = "<STR_LIT:+>" <EOL> if precedence ( term ) < PREC : <EOL> t = "<STR_LIT>" % t <EOL> return "<STR_LIT>" % ( <EOL> self . _print ( Add ( * pure_real ) ) , <EOL> self . _print ( - S . ImaginaryUnit * Add ( * pure_imaginary ) ) , <EOL> sign , t , <EOL> ) <EOL> else : <EOL> return "<STR_LIT>" % ( <EOL> self . _print ( Add ( * pure_real ) ) , <EOL> self . _print ( - S . ImaginaryUnit * Add ( * pure_imaginary ) ) , <EOL> ) <EOL> else : <EOL> return CodePrinter . _print_Add ( self , expr ) <EOL> def _print_Function ( self , expr ) : <EOL> prec = self . _settings [ '<STR_LIT>' ] <EOL> args = [ N ( a , prec ) for a in expr . args ] <EOL> eval_expr = expr . func ( * args ) <EOL> if not isinstance ( eval_expr , Function ) : <EOL> return self . _print ( eval_expr ) <EOL> else : <EOL> return CodePrinter . _print_Function ( self , expr . func ( * args ) ) <EOL> def _print_ImaginaryUnit ( self , expr ) : <EOL> return "<STR_LIT>" <EOL> def _print_int ( self , expr ) : <EOL> return str ( expr ) <EOL> def _print_Mul ( self , expr ) : <EOL> if expr . is_number and expr . is_imaginary : <EOL> return "<STR_LIT>" % ( <EOL> self . _print ( - S . ImaginaryUnit * expr ) <EOL> ) <EOL> else : <EOL> return CodePrinter . _print_Mul ( self , expr ) <EOL> def _print_Pow ( self , expr ) : <EOL> PREC = precedence ( expr ) <EOL> if expr . exp == - <NUM_LIT:1> : <EOL> return '<STR_LIT>' % ( self . parenthesize ( expr . base , PREC ) ) <EOL> elif expr . exp == <NUM_LIT:0.5> : <EOL> if expr . base . is_integer : <EOL> if expr . base . is_Number : <EOL> return '<STR_LIT>' % self . _print ( expr . base ) <EOL> else : <EOL> return '<STR_LIT>' % self . _print ( expr . base ) <EOL> else : <EOL> return '<STR_LIT>' % self . _print ( expr . base ) <EOL> else : <EOL> return CodePrinter . _print_Pow ( self , expr ) <EOL> def _print_Rational ( self , expr ) : <EOL> p , q = int ( expr . p ) , int ( expr . q ) <EOL> return "<STR_LIT>" % ( p , q ) <EOL> def _print_Float ( self , expr ) : <EOL> printed = CodePrinter . _print_Float ( self , expr ) <EOL> e = printed . find ( '<STR_LIT:e>' ) <EOL> if e > - <NUM_LIT:1> : <EOL> return "<STR_LIT>" % ( printed [ : e ] , printed [ e + <NUM_LIT:1> : ] ) <EOL> return "<STR_LIT>" % printed <EOL> def _print_Indexed ( self , expr ) : <EOL> inds = [ self . _print ( i ) for i in expr . indices ] <EOL> return "<STR_LIT>" % ( self . _print ( expr . base . label ) , "<STR_LIT:U+002CU+0020>" . join ( inds ) ) <EOL> def _print_Idx ( self , expr ) : <EOL> return self . _print ( expr . label ) <EOL> def _pad_leading_columns ( self , lines ) : <EOL> result = [ ] <EOL> for line in lines : <EOL> if line . startswith ( '<STR_LIT:!>' ) : <EOL> result . append ( self . _lead_comment + line [ <NUM_LIT:1> : ] . lstrip ( ) ) <EOL> else : <EOL> result . append ( self . _lead_code + line ) <EOL> return result <EOL> def _wrap_fortran ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> my_alnum = set ( "<STR_LIT>" + string . digits + string . ascii_letters ) <EOL> my_white = set ( "<STR_LIT>" ) <EOL> def split_pos_code ( line , endpos ) : <EOL> if len ( line ) <= endpos : <EOL> return len ( line ) <EOL> pos = endpos <EOL> split = lambda pos : ( line [ pos ] in my_alnum and line [ pos - <NUM_LIT:1> ] not in my_alnum ) or ( line [ pos ] not in my_alnum and line [ pos - <NUM_LIT:1> ] in my_alnum ) or ( line [ pos ] in my_white and line [ pos - <NUM_LIT:1> ] not in my_white ) or ( line [ pos ] not in my_white and line [ pos - <NUM_LIT:1> ] in my_white ) <EOL> while not split ( pos ) : <EOL> pos -= <NUM_LIT:1> <EOL> if pos == <NUM_LIT:0> : <EOL> return endpos <EOL> return pos <EOL> result = [ ] <EOL> if self . _settings [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> trailing = '<STR_LIT>' <EOL> else : <EOL> trailing = '<STR_LIT>' <EOL> for line in lines : <EOL> if line . startswith ( self . _lead_comment ) : <EOL> if len ( line ) > <NUM_LIT> : <EOL> pos = line . rfind ( "<STR_LIT:U+0020>" , <NUM_LIT:6> , <NUM_LIT> ) <EOL> if pos == - <NUM_LIT:1> : <EOL> pos = <NUM_LIT> <EOL> hunk = line [ : pos ] <EOL> line = line [ pos : ] . lstrip ( ) <EOL> result . append ( hunk ) <EOL> while len ( line ) > <NUM_LIT:0> : <EOL> pos = line . rfind ( "<STR_LIT:U+0020>" , <NUM_LIT:0> , <NUM_LIT> ) <EOL> if pos == - <NUM_LIT:1> or len ( line ) < <NUM_LIT> : <EOL> pos = <NUM_LIT> <EOL> hunk = line [ : pos ] <EOL> line = line [ pos : ] . lstrip ( ) <EOL> result . append ( "<STR_LIT>" % ( self . _lead_comment , hunk ) ) <EOL> else : <EOL> result . append ( line ) <EOL> elif line . startswith ( self . _lead_code ) : <EOL> pos = split_pos_code ( line , <NUM_LIT> ) <EOL> hunk = line [ : pos ] . rstrip ( ) <EOL> line = line [ pos : ] . lstrip ( ) <EOL> if line : <EOL> hunk += trailing <EOL> result . append ( hunk ) <EOL> while len ( line ) > <NUM_LIT:0> : <EOL> pos = split_pos_code ( line , <NUM_LIT> ) <EOL> hunk = line [ : pos ] . rstrip ( ) <EOL> line = line [ pos : ] . lstrip ( ) <EOL> if line : <EOL> hunk += trailing <EOL> result . append ( "<STR_LIT>" % ( self . _lead_cont , hunk ) ) <EOL> else : <EOL> result . append ( line ) <EOL> return result <EOL> def indent_code ( self , code ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( code , string_types ) : <EOL> code_lines = self . indent_code ( code . splitlines ( True ) ) <EOL> return '<STR_LIT>' . join ( code_lines ) <EOL> free = self . _settings [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> code = [ line . lstrip ( '<STR_LIT>' ) for line in code ] <EOL> inc_keyword = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> dec_keyword = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> increase = [ int ( any ( map ( line . startswith , inc_keyword ) ) ) <EOL> for line in code ] <EOL> decrease = [ int ( any ( map ( line . startswith , dec_keyword ) ) ) <EOL> for line in code ] <EOL> continuation = [ int ( any ( map ( line . endswith , [ '<STR_LIT:&>' , '<STR_LIT>' ] ) ) ) <EOL> for line in code ] <EOL> level = <NUM_LIT:0> <EOL> cont_padding = <NUM_LIT:0> <EOL> tabwidth = <NUM_LIT:3> <EOL> new_code = [ ] <EOL> for i , line in enumerate ( code ) : <EOL> if line == '<STR_LIT>' or line == '<STR_LIT:\n>' : <EOL> new_code . append ( line ) <EOL> continue <EOL> level -= decrease [ i ] <EOL> if free : <EOL> padding = "<STR_LIT:U+0020>" * ( level * tabwidth + cont_padding ) <EOL> else : <EOL> padding = "<STR_LIT:U+0020>" * level * tabwidth <EOL> line = "<STR_LIT>" % ( padding , line ) <EOL> if not free : <EOL> line = self . _pad_leading_columns ( [ line ] ) [ <NUM_LIT:0> ] <EOL> new_code . append ( line ) <EOL> if continuation [ i ] : <EOL> cont_padding = <NUM_LIT:2> * tabwidth <EOL> else : <EOL> cont_padding = <NUM_LIT:0> <EOL> level += increase [ i ] <EOL> if not free : <EOL> return self . _wrap_fortran ( new_code ) <EOL> return new_code <EOL> def fcode ( expr , assign_to = None , ** settings ) : <EOL> """<STR_LIT>""" <EOL> return FCodePrinter ( settings ) . doprint ( expr , assign_to ) <EOL> def print_fcode ( expr , ** settings ) : <EOL> """<STR_LIT>""" <EOL> print ( fcode ( expr , ** settings ) ) </s>
