<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy . core import Mul , Pow , S , Rational <EOL> from sympy . core . compatibility import string_types , range <EOL> from sympy . core . mul import _keep_coeff <EOL> from sympy . printing . codeprinter import CodePrinter , Assignment <EOL> from sympy . printing . precedence import precedence <EOL> from re import search <EOL> known_fcns_src1 = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> known_fcns_src2 = { <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 OctaveCodePrinter ( CodePrinter ) : <EOL> """<STR_LIT>""" <EOL> printmethod = "<STR_LIT>" <EOL> language = "<STR_LIT>" <EOL> _operators = { <EOL> '<STR_LIT>' : '<STR_LIT:&>' , <EOL> '<STR_LIT>' : '<STR_LIT:|>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _default_settings = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:16> , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> def __init__ ( self , settings = { } ) : <EOL> super ( OctaveCodePrinter , self ) . __init__ ( settings ) <EOL> self . known_functions = dict ( zip ( known_fcns_src1 , known_fcns_src1 ) ) <EOL> self . known_functions . update ( dict ( known_fcns_src2 ) ) <EOL> userfuncs = settings . get ( '<STR_LIT>' , { } ) <EOL> self . known_functions . update ( userfuncs ) <EOL> def _rate_index_position ( self , p ) : <EOL> return p * <NUM_LIT:5> <EOL> def _get_statement ( self , codestring ) : <EOL> return "<STR_LIT>" % 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 . 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:end>" ) <EOL> return open_lines , close_lines <EOL> def _print_Mul ( self , expr ) : <EOL> if ( expr . is_number and expr . is_imaginary and <EOL> expr . as_coeff_Mul ( ) [ <NUM_LIT:0> ] . is_integer ) : <EOL> return "<STR_LIT>" % self . _print ( - S . ImaginaryUnit * expr ) <EOL> prec = precedence ( expr ) <EOL> c , e = expr . as_coeff_Mul ( ) <EOL> if c < <NUM_LIT:0> : <EOL> expr = _keep_coeff ( - c , e ) <EOL> sign = "<STR_LIT:->" <EOL> else : <EOL> sign = "<STR_LIT>" <EOL> a = [ ] <EOL> b = [ ] <EOL> if self . order not in ( '<STR_LIT>' , '<STR_LIT:none>' ) : <EOL> args = expr . as_ordered_factors ( ) <EOL> else : <EOL> args = Mul . make_args ( expr ) <EOL> for item in args : <EOL> if ( item . is_commutative and item . is_Pow and item . exp . is_Rational <EOL> and item . exp . is_negative ) : <EOL> if item . exp != - <NUM_LIT:1> : <EOL> b . append ( Pow ( item . base , - item . exp , evaluate = False ) ) <EOL> else : <EOL> b . append ( Pow ( item . base , - item . exp ) ) <EOL> elif item . is_Rational and item is not S . Infinity : <EOL> if item . p != <NUM_LIT:1> : <EOL> a . append ( Rational ( item . p ) ) <EOL> if item . q != <NUM_LIT:1> : <EOL> b . append ( Rational ( item . q ) ) <EOL> else : <EOL> a . append ( item ) <EOL> a = a or [ S . One ] <EOL> a_str = [ self . parenthesize ( x , prec ) for x in a ] <EOL> b_str = [ self . parenthesize ( x , prec ) for x in b ] <EOL> def multjoin ( a , a_str ) : <EOL> r = a_str [ <NUM_LIT:0> ] <EOL> for i in range ( <NUM_LIT:1> , len ( a ) ) : <EOL> mulsym = '<STR_LIT:*>' if a [ i - <NUM_LIT:1> ] . is_number else '<STR_LIT>' <EOL> r = r + mulsym + a_str [ i ] <EOL> return r <EOL> if len ( b ) == <NUM_LIT:0> : <EOL> return sign + multjoin ( a , a_str ) <EOL> elif len ( b ) == <NUM_LIT:1> : <EOL> divsym = '<STR_LIT:/>' if b [ <NUM_LIT:0> ] . is_number else '<STR_LIT>' <EOL> return sign + multjoin ( a , a_str ) + divsym + b_str [ <NUM_LIT:0> ] <EOL> else : <EOL> divsym = '<STR_LIT:/>' if all ( [ bi . is_number for bi in b ] ) else '<STR_LIT>' <EOL> return ( sign + multjoin ( a , a_str ) + <EOL> divsym + "<STR_LIT>" % multjoin ( b , b_str ) ) <EOL> def _print_Pow ( self , expr ) : <EOL> powsymbol = '<STR_LIT>' if all ( [ x . is_number for x in expr . args ] ) else '<STR_LIT>' <EOL> PREC = precedence ( expr ) <EOL> if expr . exp == S . Half : <EOL> return "<STR_LIT>" % self . _print ( expr . base ) <EOL> if expr . is_commutative : <EOL> if expr . exp == - S . Half : <EOL> sym = '<STR_LIT:/>' if expr . base . is_number else '<STR_LIT>' <EOL> return "<STR_LIT:1>" + sym + "<STR_LIT>" % self . _print ( expr . base ) <EOL> if expr . exp == - S . One : <EOL> sym = '<STR_LIT:/>' if expr . base . is_number else '<STR_LIT>' <EOL> return "<STR_LIT:1>" + sym + "<STR_LIT:%s>" % self . parenthesize ( expr . base , PREC ) <EOL> return '<STR_LIT>' % ( self . parenthesize ( expr . base , PREC ) , powsymbol , <EOL> self . parenthesize ( expr . exp , PREC ) ) <EOL> def _print_MatPow ( self , expr ) : <EOL> PREC = precedence ( expr ) <EOL> return '<STR_LIT>' % ( self . parenthesize ( expr . base , PREC ) , <EOL> self . parenthesize ( expr . exp , PREC ) ) <EOL> def _print_Pi ( self , expr ) : <EOL> return '<STR_LIT>' <EOL> def _print_ImaginaryUnit ( self , expr ) : <EOL> return "<STR_LIT>" <EOL> def _print_Exp1 ( self , expr ) : <EOL> return "<STR_LIT>" <EOL> def _print_GoldenRatio ( self , expr ) : <EOL> return "<STR_LIT>" <EOL> def _print_NumberSymbol ( self , expr ) : <EOL> if self . _settings [ "<STR_LIT>" ] : <EOL> return self . _print ( expr . evalf ( self . _settings [ "<STR_LIT>" ] ) ) <EOL> else : <EOL> return super ( OctaveCodePrinter , self ) . _print_NumberSymbol ( expr ) <EOL> def _print_Assignment ( self , expr ) : <EOL> from sympy . functions . elementary . piecewise import Piecewise <EOL> from sympy . tensor . indexed import IndexedBase <EOL> lhs = expr . lhs <EOL> rhs = expr . rhs <EOL> if not self . _settings [ "<STR_LIT>" ] and isinstance ( expr . rhs , Piecewise ) : <EOL> expressions = [ ] <EOL> conditions = [ ] <EOL> for ( e , c ) in rhs . args : <EOL> expressions . append ( Assignment ( lhs , e ) ) <EOL> conditions . append ( c ) <EOL> temp = Piecewise ( * zip ( expressions , conditions ) ) <EOL> return self . _print ( temp ) <EOL> if self . _settings [ "<STR_LIT>" ] and ( lhs . has ( IndexedBase ) or <EOL> rhs . has ( IndexedBase ) ) : <EOL> return self . _doprint_loops ( rhs , lhs ) <EOL> else : <EOL> lhs_code = self . _print ( lhs ) <EOL> rhs_code = self . _print ( rhs ) <EOL> return self . _get_statement ( "<STR_LIT>" % ( lhs_code , rhs_code ) ) <EOL> def _print_Infinity ( self , expr ) : <EOL> return '<STR_LIT>' <EOL> def _print_NegativeInfinity ( self , expr ) : <EOL> return '<STR_LIT>' <EOL> def _print_NaN ( self , expr ) : <EOL> return '<STR_LIT>' <EOL> def _print_list ( self , expr ) : <EOL> return '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( self . _print ( a ) for a in expr ) + '<STR_LIT:}>' <EOL> _print_tuple = _print_list <EOL> _print_Tuple = _print_list <EOL> def _print_BooleanTrue ( self , expr ) : <EOL> return "<STR_LIT:true>" <EOL> def _print_BooleanFalse ( self , expr ) : <EOL> return "<STR_LIT:false>" <EOL> def _print_bool ( self , expr ) : <EOL> return str ( expr ) . lower ( ) <EOL> def _print_MatrixBase ( self , A ) : <EOL> if ( A . rows , A . cols ) == ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> return '<STR_LIT>' <EOL> elif A . rows == <NUM_LIT:0> or A . cols == <NUM_LIT:0> : <EOL> return '<STR_LIT>' % ( A . rows , A . cols ) <EOL> elif ( A . rows , A . cols ) == ( <NUM_LIT:1> , <NUM_LIT:1> ) : <EOL> return self . _print ( A [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> elif A . rows == <NUM_LIT:1> : <EOL> return "<STR_LIT>" % A . table ( self , rowstart = '<STR_LIT>' , rowend = '<STR_LIT>' , colsep = '<STR_LIT:U+0020>' ) <EOL> elif A . cols == <NUM_LIT:1> : <EOL> return "<STR_LIT>" % "<STR_LIT>" . join ( [ self . _print ( a ) for a in A ] ) <EOL> return "<STR_LIT>" % A . table ( self , rowstart = '<STR_LIT>' , rowend = '<STR_LIT>' , <EOL> rowsep = '<STR_LIT>' , colsep = '<STR_LIT:U+0020>' ) <EOL> def _print_SparseMatrix ( self , A ) : <EOL> from sympy . matrices import Matrix <EOL> L = A . col_list ( ) ; <EOL> I = Matrix ( [ [ k [ <NUM_LIT:0> ] + <NUM_LIT:1> for k in L ] ] ) <EOL> J = Matrix ( [ [ k [ <NUM_LIT:1> ] + <NUM_LIT:1> for k in L ] ] ) <EOL> AIJ = Matrix ( [ [ k [ <NUM_LIT:2> ] for k in L ] ] ) <EOL> return "<STR_LIT>" % ( self . _print ( I ) , self . _print ( J ) , <EOL> self . _print ( AIJ ) , A . rows , A . cols ) <EOL> _print_Matrix = _print_DenseMatrix = _print_MutableDenseMatrix = _print_ImmutableMatrix = _print_ImmutableDenseMatrix = _print_MatrixBase <EOL> _print_MutableSparseMatrix = _print_ImmutableSparseMatrix = _print_SparseMatrix <EOL> def _print_MatrixElement ( self , expr ) : <EOL> return self . _print ( expr . parent ) + '<STR_LIT>' % ( expr . i + <NUM_LIT:1> , expr . j + <NUM_LIT:1> ) <EOL> def _print_MatrixSlice ( self , expr ) : <EOL> def strslice ( x , lim ) : <EOL> l = x [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> h = x [ <NUM_LIT:1> ] <EOL> step = x [ <NUM_LIT:2> ] <EOL> lstr = self . _print ( l ) <EOL> hstr = '<STR_LIT:end>' if h == lim else self . _print ( h ) <EOL> if step == <NUM_LIT:1> : <EOL> if l == <NUM_LIT:1> and h == lim : <EOL> return '<STR_LIT::>' <EOL> if l == h : <EOL> return lstr <EOL> else : <EOL> return lstr + '<STR_LIT::>' + hstr <EOL> else : <EOL> return '<STR_LIT::>' . join ( ( lstr , self . _print ( step ) , hstr ) ) <EOL> return ( self . _print ( expr . parent ) + '<STR_LIT:(>' + <EOL> strslice ( expr . rowslice , expr . parent . shape [ <NUM_LIT:0> ] ) + '<STR_LIT:U+002CU+0020>' + <EOL> strslice ( expr . colslice , expr . parent . shape [ <NUM_LIT:1> ] ) + '<STR_LIT:)>' ) <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 _print_Identity ( self , expr ) : <EOL> return "<STR_LIT>" % self . _print ( expr . shape [ <NUM_LIT:0> ] ) <EOL> def _print_hankel1 ( self , expr ) : <EOL> return "<STR_LIT>" % ( self . _print ( expr . order ) , <EOL> self . _print ( expr . argument ) ) <EOL> def _print_hankel2 ( self , expr ) : <EOL> return "<STR_LIT>" % ( self . _print ( expr . order ) , <EOL> self . _print ( expr . argument ) ) <EOL> def _print_jn ( self , expr ) : <EOL> from sympy . functions import sqrt , besselj <EOL> x = expr . argument <EOL> expr2 = sqrt ( S . Pi / ( <NUM_LIT:2> * x ) ) * besselj ( expr . order + S . Half , x ) <EOL> return self . _print ( expr2 ) <EOL> def _print_yn ( self , expr ) : <EOL> from sympy . functions import sqrt , bessely <EOL> x = expr . argument <EOL> expr2 = sqrt ( S . Pi / ( <NUM_LIT:2> * x ) ) * bessely ( expr . order + S . Half , x ) <EOL> return self . _print ( expr2 ) <EOL> def _print_airyai ( self , expr ) : <EOL> return "<STR_LIT>" % self . _print ( expr . args [ <NUM_LIT:0> ] ) <EOL> def _print_airyaiprime ( self , expr ) : <EOL> return "<STR_LIT>" % self . _print ( expr . args [ <NUM_LIT:0> ] ) <EOL> def _print_airybi ( self , expr ) : <EOL> return "<STR_LIT>" % self . _print ( expr . args [ <NUM_LIT:0> ] ) <EOL> def _print_airybiprime ( self , expr ) : <EOL> return "<STR_LIT>" % self . _print ( expr . args [ <NUM_LIT:0> ] ) <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 self . _settings [ "<STR_LIT>" ] : <EOL> ecpairs = [ "<STR_LIT>" . format <EOL> ( self . _print ( c ) , self . _print ( e ) ) <EOL> for e , c in expr . args [ : - <NUM_LIT:1> ] ] <EOL> elast = "<STR_LIT:%s>" % self . _print ( expr . args [ - <NUM_LIT:1> ] . expr ) <EOL> pw = "<STR_LIT>" . join ( ecpairs ) + elast + "<STR_LIT:)>" * len ( ecpairs ) <EOL> return "<STR_LIT:(>" + pw + "<STR_LIT:)>" <EOL> else : <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> code0 = self . _print ( e ) <EOL> lines . append ( code0 ) <EOL> if i == len ( expr . args ) - <NUM_LIT:1> : <EOL> lines . append ( "<STR_LIT:end>" ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <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> tab = "<STR_LIT:U+0020>" <EOL> inc_regex = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> dec_regex = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> code = [ line . lstrip ( '<STR_LIT>' ) for line in code ] <EOL> increase = [ int ( any ( [ search ( re , line ) for re in inc_regex ] ) ) <EOL> for line in code ] <EOL> decrease = [ int ( any ( [ search ( re , line ) for re in dec_regex ] ) ) <EOL> for line in code ] <EOL> pretty = [ ] <EOL> level = <NUM_LIT:0> <EOL> for n , line in enumerate ( code ) : <EOL> if line == '<STR_LIT>' or line == '<STR_LIT:\n>' : <EOL> pretty . append ( line ) <EOL> continue <EOL> level -= decrease [ n ] <EOL> pretty . append ( "<STR_LIT>" % ( tab * level , line ) ) <EOL> level += increase [ n ] <EOL> return pretty <EOL> def octave_code ( expr , assign_to = None , ** settings ) : <EOL> r"""<STR_LIT>""" <EOL> return OctaveCodePrinter ( settings ) . doprint ( expr , assign_to ) <EOL> def print_octave_code ( expr , ** settings ) : <EOL> """<STR_LIT>""" <EOL> print ( octave_code ( expr , ** settings ) ) </s>
