示例#1
0
文件: fcode.py 项目: tovrstra/sympy
    def doprint(self, expr):
        """Returns Fortran code for expr (as a string)"""
        # keep a set of expressions that are not strictly translatable to
        # Fortran.
        self.not_fortran = set([])

        lines = []
        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))
                if self._settings["assign_to"] is None:
                    lines.append("        %s" % self._print(e))
                else:
                    lines.append("        %s = %s" % (self._settings["assign_to"], self._print(e)))
            lines.append("      end if")
            return "\n".join(lines)
        else:
            line = StrPrinter.doprint(self, expr)
            if self._settings["assign_to"] is None:
                return "      %s" % line
            else:
                return "      %s = %s" % (self._settings["assign_to"], line)
示例#2
0
    def doprint(self, expr):
        """Returns Fortran code for expr (as a string)"""
        # find all number symbols
        number_symbols = set([])
        for sub in postorder_traversal(expr):
            if isinstance(sub, NumberSymbol):
                number_symbols.add(sub)
        number_symbols = [(str(ns), ns.evalf(self._settings["precision"]))
                          for ns in sorted(number_symbols)]

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

        lines = []
        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))
                if self._settings["assign_to"] is None:
                    lines.append("        %s" % self._print(e))
                else:
                    lines.append("        %s = %s" %
                                 (self._settings["assign_to"], self._print(e)))
            lines.append("      end if")
            text = "\n".join(lines)
        else:
            line = StrPrinter.doprint(self, expr)
            if self._settings["assign_to"] is None:
                text = "      %s" % line
            else:
                text = "      %s = %s" % (self._settings["assign_to"], line)

        # format the output
        if self._settings["human"]:
            lines = []
            if len(self._not_fortran) > 0:
                lines.append("C     Not Fortran 77:")
                for expr in sorted(self._not_fortran):
                    lines.append("C     %s" % expr)
            for name, value in number_symbols:
                lines.append("      parameter (%s = %s)" % (name, value))
            lines.extend(text.split("\n"))
            lines = wrap_fortran(lines)
            result = "\n".join(lines)
        else:
            result = number_symbols, self._not_fortran, text

        del self._not_fortran
        return result
示例#3
0
    def doprint(self, expr):
        """Returns Fortran code for expr (as a string)"""
        # find all number symbols
        number_symbols = set([])
        for sub in postorder_traversal(expr):
            if isinstance(sub, NumberSymbol):
                number_symbols.add(sub)
        number_symbols = [(str(ns), ns.evalf(self._settings["precision"]))
                          for ns in sorted(number_symbols)]

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

        lines = []
        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))
                if self._settings["assign_to"] is None:
                    lines.append("        %s" % self._print(e))
                else:
                    lines.append("        %s = %s" % (self._settings["assign_to"], self._print(e)))
            lines.append("      end if")
            text = "\n".join(lines)
        else:
            line = StrPrinter.doprint(self, expr)
            if self._settings["assign_to"] is None:
                text = "      %s" % line
            else:
                text = "      %s = %s" % (self._settings["assign_to"], line)

        # format the output
        if self._settings["human"]:
            lines = []
            if len(self._not_fortran) > 0:
                lines.append("C     Not Fortran 77:")
                for expr in sorted(self._not_fortran):
                    lines.append("C     %s" % expr)
            for name, value in number_symbols:
                lines.append("      parameter (%s = %s)" % (name, value))
            lines.extend(text.split("\n"))
            lines = wrap_fortran(lines)
            result = "\n".join(lines)
        else:
            result = number_symbols, self._not_fortran, text

        del self._not_fortran
        return result
示例#4
0
文件: fcode.py 项目: goriccardo/sympy
    def doprint(self, expr):
        """Returns Fortran code for expr (as a string)"""
        # find all number symbols
        number_symbols = set([])
        for sub in postorder_traversal(expr):
            if isinstance(sub, NumberSymbol):
                number_symbols.add(sub)
        number_symbols = [(str(ns), ns.evalf(self._settings["precision"]))
                          for ns in sorted(number_symbols)]

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


        # Setup loops if expression contain Indexed objects
        openloop, closeloop, local_ints = self._get_loop_opening_ending_ints(expr)

        self._not_fortran |= set(local_ints)

        # the lhs may contain loops that are not in the rhs
        lhs = self._settings['assign_to']
        if lhs:
            open_lhs, close_lhs, lhs_ints = self._get_loop_opening_ending_ints(lhs)
            for n,ind in enumerate(lhs_ints):
                if ind not in self._not_fortran:
                    self._not_fortran.add(ind)
                    openloop.insert(0,open_lhs[n])
                    closeloop.append(close_lhs[n])
            lhs_printed = self._print(lhs)

        lines = []
        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))
                if self._settings["assign_to"] is None:
                    lines.extend(openloop)
                    lines.append("  %s" % self._print(e))
                    lines.extend(closeloop)
                else:
                    lines.extend(openloop)
                    lines.append("  %s = %s" % (lhs_printed, self._print(e)))
                    lines.extend(closeloop)
            lines.append("end if")
        else:
            lines.extend(openloop)
            line = StrPrinter.doprint(self, expr)
            if self._settings["assign_to"] is None:
                text = "%s" % line
            else:
                text = "%s = %s" % (lhs_printed, line)
            lines.append(text)
            lines.extend(closeloop)

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

        del self._not_fortran
        return result