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 <charlesnwo...@gmail.com>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 <ondrej.cer...@gmail.com>wrote:
>
>> On Tue, Oct 8, 2013 at 1:58 PM, Ondřej Čertík <ondrej.cer...@gmail.com>
>> 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 sympy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sympy@googlegroups.com.
>> 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 sympy+unsubscr...@googlegroups.com.
> To post to this group, send email to sympy@googlegroups.com.
> 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 sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
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