generate code for Boolean expressions

33 views
Skip to first unread message

Nico

unread,
Aug 24, 2015, 5:09:11 PM8/24/15
to sympy
With `codegen`, one can generate code from a sympy expression. Typically, it is assumed that the return value is a scalar.

In one of my use cases, the return value is Boolean. (The answer to "Is x inside a particular domain?")
The hack I employed so far is to generate the code as usual
```
from sympy import *
from sympy.utilities.codegen import codegen

x = Symbol('x')
expr = x**2 - x + sin(x) < x**3 - 2*x - cos(5*x)  # ok
[(c_name, c_code), (h_name, c_header)] = codegen(("f", expr), "C")
print(c_code)
```
and then regex the relevant part out of `c_code`. (If you know of a better way to handle this, I'll be happy to hear about it.)
This works alright, but fails in the most simple cases, e.g.,
```
expr = True
```
with
```
TypeError: The first argument must be a sympy expression.
```
Quite understandable.

Is there a better way/hack to support this kind of code generation?

Cheers,
Nico

Sudhanshu Mishra

unread,
Aug 24, 2015, 6:49:47 PM8/24/15
to sy...@googlegroups.com
Hi

I can't comment on the first part but for the boolean expression, it's a design issue. Booleans have not been derived from Expr class in sympy which is why it fails.

Sudhanshu Mishra

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/21baed7f-e7e6-4d1e-979c-6d30dee206e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Björn Dahlgren

unread,
Aug 26, 2015, 6:33:20 PM8/26/15
to sympy


On Monday, 24 August 2015 23:09:11 UTC+2, Nico wrote:

Is there a better way/hack to support this kind of code generation?

Not that I know of, we should teach codegen how to handle Boolean and Relational (and make it emit the
apropriate boolean data type).
Reply all
Reply to author
Forward
0 new messages