Boolean operators and C++ code generation

9 views
Skip to first unread message

Dan Goodman

unread,
Feb 14, 2010, 9:14:44 PM2/14/10
to sympy
Hi all,

I just found a bug in code generation with Boolean operators. For
example, the sympy expression x&y ought to generate the C++ code x&&y
but it actually generates And(x,y). I have a fix for this to add to
sympy.printing.ccode.CCodePrinter, the extra methods:

def _print_And(self, expr):
PREC = precedence(expr)
return '&&'.join(self.parenthesize(a, PREC) for a in
expr.args)
def _print_Or(self, expr):
PREC = precedence(expr)
return '||'.join(self.parenthesize(a, PREC) for a in
expr.args)
def _print_Not(self, expr):
PREC = precedence(expr)
return '!'+self.parenthesize(expr.args[0], PREC)

I don't mess around with sympy internals much, so I might be using
self.parenthesize and precedence wrong here (it certainly seems to
create too many parentheses like this), but it does at least seem to
work.

Dan Goodman

Ondrej Certik

unread,
Feb 15, 2010, 1:24:53 AM2/15/10
to sy...@googlegroups.com
Hi Dan!

Thanks for the fix. If possible, could you please send it as a git
patch? We'll review it and push it in.

Ondrej

Aaron S. Meurer

unread,
Feb 15, 2010, 11:02:38 AM2/15/10
to sy...@googlegroups.com
You'll also need to add tests for the changed behavior. Just add assertions to the relevant place in a test_*.py file that would fail before the patch but passes now. You can verify it by running $ setup.py test

Thanks,
Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Dan Goodman

unread,
Feb 15, 2010, 11:04:26 PM2/15/10
to sympy
Hi,

Unfortunately I don't have the time for the git patch/test writing at
the moment. Indeed, I've never even used git! :) This was more like a
slightly-more-helpful-than-usual bug report than a proper patch. It's
probably not even 100% correct (for example x&y generates (x)&&(y)
rather than x&&y and I have no idea why or what I should have
written). Sorry, hope it was of some use anyway!

Dan

Aaron S. Meurer

unread,
Feb 15, 2010, 11:09:28 PM2/15/10
to sy...@googlegroups.com
Well, thanks anyway. I created an issue for it so it won't get lost:
http://code.google.com/p/sympy/issues/detail?id=1834

By the way, Ondrej has some good git patch tutorials here if you are ever interested:
http://code.google.com/p/sympy/wiki/GitTutorials

Aaron Meurer

Toon Verstraelen

unread,
Feb 16, 2010, 5:06:02 AM2/16/10
to sy...@googlegroups.com
I've posted patches on the issue tracker. (also for precedence of boolean operators). Dan,
can you try these? It comes down to these commands:

cd somedir
git clone git://git.sympy.org/sympy.git
cd sympy

(download the patches in this directory)

git apply 0001-Precedence-for-boolean-operators-And-Or-and-Not.patch
git apply 0002-Fix-boolean-operatures-in-ccode.patch

(run python, from sympy import *, and try it out)

cheers,

Toon


--
Dr. ir. Toon Verstraelen
Center for Molecular Modeling
Ghent University
Technologiepark 903,
B9052 Zwijnaarde
Belgium
Tel: +32 9 264 65 56
E-mail: Toon.Ver...@UGent.be
http://molmod.UGent.be/
http://molmod.UGent.be/code/

Dan Goodman

unread,
Feb 16, 2010, 9:49:23 AM2/16/10
to sympy
Toon, that works perfectly for me.

Aaron - yeah, I really should learn to use git some time soon, it
seems to be what all the cool kids are using these days. Not now
though, 3 papers to write by the end of March! :/

Thanks everyone for this fantastic package by the way!

Dan

Ondrej Certik

unread,
Feb 16, 2010, 4:49:36 PM2/16/10
to sy...@googlegroups.com
On Tue, Feb 16, 2010 at 6:49 AM, Dan Goodman
<dg.googl...@thesamovar.net> wrote:
> Toon, that works perfectly for me.
>
> Aaron - yeah, I really should learn to use git some time soon, it
> seems to be what all the cool kids are using these days. Not now
> though, 3 papers to write by the end of March! :/

Write your papers in git then --- that's what I do. :)

>
> Thanks everyone for this fantastic package by the way!

Thanks and thanks for your report!

Ondrej

Reply all
Reply to author
Forward
0 new messages