details: http://hg.sympy.org/sympy/rev/3082b769dbfa changeset: 1855:3082b769dbfa user: Andy R. Terrel <[EMAIL PROTECTED]> date: Wed Nov 05 09:46:55 2008 +0100 description: Implements printings for Piecewise and ExprCondPair objects.
diffs (209 lines): diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/ccode.py --- a/sympy/printing/ccode.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/ccode.py Wed Nov 05 09:46:55 2008 +0100 @@ -20,6 +20,20 @@ def _print_Exp1(self, expr): return "exp(1)" + def _print_Piecewise(self, expr): + ecpairs = ["(%s) {\n%s\n}\n" % (self._print(c), self._print(e)) \ + for e, c in expr.args[:-1]] + last_line = "" + if expr.args[-1].cond is S.One: + last_line = "else {\n%s\n}" % self._print(expr.args[-1].expr) + else: + ecpairs.append("(%s) {\n%s\n" % \ + (self._print(expr.args[-1].cond), + self._print(expr.args[-1].expr))) + code = "if %s" + last_line + return code % "else if ".join(ecpairs) + + def ccode(expr): r"""Converts an expr to a string of c code diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/latex.py --- a/sympy/printing/latex.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/latex.py Wed Nov 05 09:46:55 2008 +0100 @@ -389,6 +389,19 @@ return "%s %s %s" % (self._print(expr.lhs), charmap[expr.rel_op], self._print(expr.rhs)) + def _print_Piecewise(self, expr): + ecpairs = [r"%s & for %s" % (self._print(e), self._print(c)) \ + for e, c in expr.args[:-1]] + if expr.args[-1].cond is S.One: + ecpairs.append(r"%s & \textrm{otherwise}" % \ + self._print(expr.args[-1].expr)) + else: + ecpairs.append(r"%s & for %s" % \ + (self._print(expr.args[-1].cond), + self._print(expr.args[-1].expr))) + tex = r"\left\{\begin{array}{cl} %s \end{array}\right." + return tex % r" \\".join(ecpairs) + def _print_Matrix(self, expr): lines = [] diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/pretty/pretty.py --- a/sympy/printing/pretty/pretty.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/pretty/pretty.py Wed Nov 05 09:46:55 2008 +0100 @@ -295,6 +295,58 @@ D = prettyForm(*D.parens('[',']')) return D + def _print_Piecewise(self, pexpr): + + P = {} + for n, ec in enumerate(pexpr.args): + P[n,0] = self._print(ec.expr) + if ec.cond is S.One: + P[n,1] = prettyForm('otherwise') + else: + P[n,1] = prettyForm(*prettyForm('for ').right(self._print(ec.cond))) + hsep = 2 + vsep = 1 + len_args = len(pexpr.args) + + # max widths + maxw = [max([P[i,j].width() for i in xrange(len_args)]) \ + for j in xrange(2)] + + # FIXME: Refactor this code and matrix into some tabular environment. + # drawing result + D = None + + for i in xrange(len_args): + D_row = None + for j in xrange(2): + p = P[i,j] + assert p.width() <= maxw[j] + + wdelta = maxw[j] - p.width() + wleft = wdelta // 2 + wright = wdelta - wleft + + p = prettyForm(*p.right(' '*wright)) + p = prettyForm(*p.left (' '*wleft)) + + if D_row is None: + D_row = p + continue + + D_row = prettyForm(*D_row.right(' '*hsep)) # h-spacer + D_row = prettyForm(*D_row.right(p)) + if D is None: + D = D_row # first row in a picture + continue + + # v-spacer + for _ in range(vsep): + D = prettyForm(*D.below(' ')) + + D = prettyForm(*D.below(D_row)) + + D = prettyForm(*D.parens('{','')) + return D def _print_exp(self, e): base = prettyForm(pretty_atom('Exp1', 'e')) diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/pretty/tests/test_pretty.py --- a/sympy/printing/pretty/tests/test_pretty.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/pretty/tests/test_pretty.py Wed Nov 05 09:46:55 2008 +0100 @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from sympy import Symbol, Matrix, Integral, log, Rational, Derivative, exp, \ sqrt, pi, Function, sin, cos, pprint_use_unicode, oo, Eq, Le, \ - Gt, Ne, Limit, factorial, gamma, conjugate, I + Gt, Ne, Limit, factorial, gamma, conjugate, I, Piecewise from sympy.printing.pretty import pretty as xpretty x = Symbol('x') @@ -223,6 +223,18 @@ """ assert p in [s1, s2] +def test_pretty_Piecewise(): + p = pretty(Piecewise((x,x<1),(x**2,True))) + s = \ +"""\ +/x for x < 1 +| \n\ +< 2 \n\ +|x otherwise\n\ +\ \ +""" + assert p == s + def test_pretty_seq(): assert pretty([]) == '[]' assert pretty(()) == '()' diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/str.py --- a/sympy/printing/str.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/str.py Wed Nov 05 09:46:55 2008 +0100 @@ -95,6 +95,9 @@ def _print_Exp1(self, expr): return 'E' + + def _print_ExprCondPair(self, expr): + return '(%s, %s)' % (expr.expr, expr.cond) def _print_Factorial(self, expr): return "%s!" % self.parenthesize(expr.args[0], PRECEDENCE["Pow"]) diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/tests/test_ccode.py --- a/sympy/printing/tests/test_ccode.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/tests/test_ccode.py Wed Nov 05 09:46:55 2008 +0100 @@ -1,4 +1,4 @@ -from sympy import abs, exp, Function, symbols +from sympy import abs, exp, Function, Piecewise, symbols from sympy.printing import ccode from sympy.utilities.pytest import XFAIL @@ -6,11 +6,24 @@ x, y = symbols('xy') g = Function('g') -def test_Pow(): +def test_ccode_Pow(): assert ccode(x**3) == "pow(x,3)" assert ccode(x**(y**3)) == "pow(x,(pow(y,3)))" assert ccode(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \ "1/(y + pow(x,2))*pow((3.50000000000000*g(x)),(-x + pow(y,x)))" -def test_Exp1(): +def test_ccode_Exp1(): assert ccode(exp(1)) == "exp(1)" + +def test_ccode_Piecewise(): + p = ccode(Piecewise((x,x<1),(x**2,True))) + s = \ +"""\ +if (x < 1) { +x +} +else { +pow(x,2) +}\ +""" + assert p == s diff -r 31c75caad38d -r 3082b769dbfa sympy/printing/tests/test_latex.py --- a/sympy/printing/tests/test_latex.py Wed Nov 05 09:46:55 2008 +0100 +++ b/sympy/printing/tests/test_latex.py Wed Nov 05 09:46:55 2008 +0100 @@ -1,6 +1,6 @@ from sympy import symbols, Rational, Symbol, Integral, log, diff, sin, exp, \ Function, factorial, floor, ceiling, abs, re, im, conjugate, gamma, \ - Order + Order, Piecewise from sympy.abc import mu, tau from sympy.printing.latex import latex from sympy.utilities.pytest import XFAIL @@ -117,3 +117,8 @@ assert latex(DiracDelta(x)) == "$\\delta\\left(x\\right)$" assert latex(DiracDelta(x,0)) == "$\\delta\\left(x\\right)$" assert latex(DiracDelta(x,5)) == "$\\delta^{\\left( 5 \\right)}\\left( x \\right)$" + +def test_latex_Piecewise(): + p = Piecewise((x,x<1),(x**2,True)) + assert latex(p) == "$\\left\\{\\begin{array}{cl} x & for x < 1 \\\\{x}^{2} &" \ + " \\textrm{otherwise} \\end{array}\\right.$" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-commits" group. To post to this group, send email to sympy-commits@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy-commits?hl=en -~----------~----~----~----~------~----~------~--~---