Thanks Matthew!

That's right. Nathan, let me know if this works for you:

In [1]: e = t*x*y + x**2 + y**2 + Piecewise((0, x < 0.5), (1, x >=
0.5)) + cos(t) - 1

In [2]: ccode(piecewise_fold(e))
Out[2]: 'if (x < 0.5) {\n   t*x*y + pow(x, 2) + pow(y, 2) + cos(t) -
1\n}\nelse if (x >= 0.5) {\n   t*x*y + pow(x, 2) + pow(y, 2) +
cos(t)\n}'


Ondrej

On Tue, Oct 8, 2013 at 2:20 PM, Matthew Rocklin <[email protected]> wrote:
>  A simple fix would be to put the entire expression within the Piecewise.
> E.g.
>
> Instead of
>
> x + Piecewise((0, x < 0), (1, x > 0))
>
> Try
>
> Piecewise((x, x < 0), (x + 1, x > 0))
>
> You shouldn't have to do this (ccodegen should be smart enough to handle
> this), but I suspect it will work in the short term.
>
>
> On Tue, Oct 8, 2013 at 1:07 PM, Nathan Woods <[email protected]>
> wrote:
>>
>> I would be happy with either of the following implementations, one or the
>> other of which might be preferred for other reasons. The immediate intended
>> use is to wrap the resulting function in ctypes so that I can feed it to
>> some existing code.
>>
>> - An if/then construct, like what you mentioned. I don't quite understand
>> why that doesn't work, though.
>> - 0 + (1-0)*(x>=0.5) (essentially a Heaviside implementation. C interprets
>> a "false" as 0, and "true" as 1, correct?)
>>
>>
>> On Tue, Oct 8, 2013 at 2:00 PM, Ondřej Čertík <[email protected]>
>> wrote:
>>>
>>> On Tue, Oct 8, 2013 at 1:58 PM, Ondřej Čertík <[email protected]>
>>> wrote:
>>> > Nathan,
>>> >
>>> > I am working on a fix.
>>> >
>>> > Can you provide the exact C expression that you want Piecewise((0, x <
>>> > 0.5), (1, x >= 0.5)) to generate?
>>> > I.e. can you fill in the right hand side here:
>>> >
>>> >     assert ccode(Piecewise((0, x < 0.5), (1, x >= 0.5))) ==
>>> > "piecewise(...)"
>>>
>>> Ok, it actually works... It produces:
>>>
>>> if (x < 0.5) {
>>>    0
>>> }
>>> else if (x >= 0.5) {
>>>    1
>>> }
>>>
>>> But obviously this will not work inside an expression. Can you provide
>>> us the exact C code that you expect
>>> for your expression?
>>>
>>> Ondrej
>>>
>>> --
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/sympy.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/sympy.
>> For more options, visit https://groups.google.com/groups/opt_out.
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to