Tomek B wrote: > Sure. The problem was that ccode printer was giving wrong results, > Example: > ccode((2*tau)**Rational(7,2)) -> '8*pow(2,(1/2))*pow(tau,(7/2))' > > which is wrong (because 1/2 evaluates to 0 in C).
I meant in the patch description. Please put that (git commit --amend) and I'll push in the patch. > > Cheers, > Tomek > > 2009/4/8 Fabian Seoane <fab...@fseoane.net>: >> looks good, but please put a little description on what issue 1329 was >> about, so we don't have to go to the issue tracker to understand the patch. >> >> Tomasz Buchert wrote: >>> --- >>> sympy/printing/ccode.py | 21 +++++++++++++++++---- >>> sympy/printing/tests/test_ccode.py | 27 ++++++++++++++++++++++----- >>> 2 files changed, 39 insertions(+), 9 deletions(-) >>> >>> diff --git a/sympy/printing/ccode.py b/sympy/printing/ccode.py >>> index e62786e..a1341b5 100644 >>> --- a/sympy/printing/ccode.py >>> +++ b/sympy/printing/ccode.py >>> @@ -7,19 +7,32 @@ from sympy.printing.precedence import precedence, >>> PRECEDENCE >>> from sympy.core.basic import S >>> >>> class CCodePrinter(StrPrinter): >>> - """A printer to convert python expressions to stings of c code""" >>> + """A printer to convert python expressions to strings of c code""" >>> printmethod = "_ccode_" >>> >>> def _print_Pow(self, expr): >>> PREC = precedence(expr) >>> if expr.exp is S.NegativeOne: >>> - return '1/%s'%(self.parenthesize(expr.base, PREC)) >>> + return '1.0/%s'%(self.parenthesize(expr.base, PREC)) >>> else: >>> return 'pow(%s,%s)'%(self.parenthesize(expr.base, PREC), >>> self.parenthesize(expr.exp, PREC)) >>> >>> + def _print_Rational(self, expr): >>> + p, q = int(expr.p), int(expr.q) >>> + return '%d.0/%d.0' % (p, q) >>> + >>> def _print_Exp1(self, expr): >>> - return "exp(1)" >>> + return "M_E" >>> + >>> + def _print_Pi(self, expr): >>> + return 'M_PI' >>> + >>> + def _print_Infinity(self, expr): >>> + return 'HUGE_VAL' >>> + >>> + def _print_NegativeInfinity(self, expr): >>> + return '-HUGE_VAL' >>> >>> def _print_Piecewise(self, expr): >>> ecpairs = ["(%s) {\n%s\n}\n" % (self._print(c), self._print(e)) \ >>> @@ -44,7 +57,7 @@ def ccode(expr): >>> >>> from sympy.abc import * >>> >>> >>> ccode((2*tau)**Rational(7,2)) >>> - '8*pow(2,(1/2))*pow(tau,(7/2))' >>> + '8*pow(2,(1.0/2.0))*pow(tau,(7.0/2.0))' >>> """ >>> return CCodePrinter().doprint(expr) >>> >>> diff --git a/sympy/printing/tests/test_ccode.py >>> b/sympy/printing/tests/test_ccode.py >>> index 9ff2587..9534891 100644 >>> --- a/sympy/printing/tests/test_ccode.py >>> +++ b/sympy/printing/tests/test_ccode.py >>> @@ -1,4 +1,5 @@ >>> -from sympy import abs, exp, Function, Piecewise, symbols >>> +from sympy import sin, cos, abs, exp, pi, oo, symbols >>> +from sympy import Function, Piecewise, Rational, Integer >>> >>> from sympy.printing import ccode >>> from sympy.utilities.pytest import XFAIL >>> @@ -9,8 +10,8 @@ g = Function('g') >>> def test_printmethod(): >>> class fabs(abs): >>> def _ccode_(self): >>> - return "fabs(%s);" % ccode(self.args[0]) >>> - assert ccode(fabs(x)) == "fabs(x);" >>> + return "fabs(%s)" % ccode(self.args[0]) >>> + assert ccode(fabs(x)) == "fabs(x)" >>> >>> def test_ccode_Pow(): >>> assert ccode(x**3) == "pow(x,3)" >>> @@ -18,8 +19,24 @@ def test_ccode_Pow(): >>> assert ccode(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \ >>> "pow((3.5*g(x)),(-x + pow(y,x)))/(y + pow(x,2))" >>> >>> -def test_ccode_Exp1(): >>> - assert ccode(exp(1)) == "exp(1)" >>> +def test_ccode_constants(): >>> + assert ccode(exp(1)) == "M_E" >>> + assert ccode(pi) == "M_PI" >>> + assert ccode(oo) == "HUGE_VAL" >>> + assert ccode(-oo) == "-HUGE_VAL" >>> + >>> +def test_ccode_Rational(): >>> + assert ccode(Rational(3,7)) == "3.0/7.0" >>> + assert ccode(Rational(18,9)) == "2" >>> + assert ccode(Rational(3,-7)) == "-3.0/7.0" >>> + assert ccode(Rational(-3,-7)) == "3.0/7.0" >>> + >>> +def test_ccode_Integer(): >>> + assert ccode(Integer(67)) == "67" >>> + assert ccode(Integer(-1)) == "-1" >>> + >>> +def test_ccode_functions(): >>> + assert ccode(sin(x) ** cos(x)) == "pow(sin(x),cos(x))" >>> >>> def test_ccode_Piecewise(): >>> p = ccode(Piecewise((x,x<1),(x**2,True))) >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patches@googlegroups.com To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en -~----------~----~----~----~------~----~------~--~---