(self, expr)
return result
Returns Fortran code for expr (as a string)
Returns Fortran code for expr (as a string)
Returns Fortran code for expr ( as a string )
def doprint(self, expr):
        """Returns Fortran code for expr (as a string)"""
        # find all number symbols
        self._number_symbols = set()

        # keep a set of expressions that are not strictly translatable to
        # Fortran.
        self._not_supported = set()

        lines = []
        from sympy.functions import Piecewise
        if isinstance(expr, Piecewise):
            # support for top-level Piecewise function
            for i, (e, c) in enumerate(expr.args):
                if i == 0:
                    lines.append("if (%s) then" % self._print(c))
                elif i == len(expr.args) - 1 and c == True:
                    lines.append("else")
                else:
                    lines.append("else if (%s) then" % self._print(c))
                lines.extend(
                    self._doprint_a_piece(e, self._settings['assign_to']))
            lines.append("end if")
        else:
            lines.extend(
                self._doprint_a_piece(expr, self._settings['assign_to']))

        # format the output
        if self._settings["human"]:
            frontlines = []
            if len(self._not_supported) > 0:
                frontlines.append("! Not Fortran:")
                for expr in sorted(self._not_supported, key=self._print):
                    frontlines.append("! %s" % repr(expr))
            for name, value in sorted(self._number_symbols, key=str):
                frontlines.append("parameter (%s = %s)" % (str(name), value))
            frontlines.extend(lines)
            lines = frontlines
            lines = self.indent_code(lines)
            lines = self._wrap_fortran(lines)
            result = "\n".join(lines)
        else:
            lines = self.indent_code(lines)
            lines = self._wrap_fortran(lines)
            result = self._number_symbols, self._not_supported, "\n".join(
                lines)

        del self._not_supported
        del self._number_symbols
        return result
def doprint ( self , expr ) : # find all number symbols self . _number_symbols = set ( ) # keep a set of expressions that are not strictly translatable to # Fortran. self . _not_supported = set ( ) lines = [ ] from sympy . functions import Piecewise if isinstance ( expr , Piecewise ) : # support for top-level Piecewise function for i , ( e , c ) in enumerate ( expr . args ) : if i == 0 : lines . append ( "if (%s) then" % self . _print ( c ) ) elif i == len ( expr . args ) - 1 and c == True : lines . append ( "else" ) else : lines . append ( "else if (%s) then" % self . _print ( c ) ) lines . extend ( self . _doprint_a_piece ( e , self . _settings [ 'assign_to' ] ) ) lines . append ( "end if" ) else : lines . extend ( self . _doprint_a_piece ( expr , self . _settings [ 'assign_to' ] ) ) # format the output if self . _settings [ "human" ] : frontlines = [ ] if len ( self . _not_supported ) > 0 : frontlines . append ( "! Not Fortran:" ) for expr in sorted ( self . _not_supported , key = self . _print ) : frontlines . append ( "! %s" % repr ( expr ) ) for name , value in sorted ( self . _number_symbols , key = str ) : frontlines . append ( "parameter (%s = %s)" % ( str ( name ) , value ) ) frontlines . extend ( lines ) lines = frontlines lines = self . indent_code ( lines ) lines = self . _wrap_fortran ( lines ) result = "\n" . join ( lines ) else : lines = self . indent_code ( lines ) lines = self . _wrap_fortran ( lines ) result = self . _number_symbols , self . _not_supported , "\n" . join ( lines ) del self . _not_supported del self . _number_symbols return result