I'm getting a compiler error using "gcc -c integrand.c". integrand.c: In function ‘integrand’: integrand.c:13:11: error: expected expression before ‘if’
The generated C-code is : /****************************************************************************** * Code generated with sympy 0.7.2 * * * * See http://www.sympy.org/ for more information. * * * * This file is part of 'project' * ******************************************************************************/ #include "integrand.h" #include <math.h> double integrand(double t, double x, double y) { return if (x < 0.5) { t*x*y + pow(x, 2) + pow(y, 2) + cos(t) - 1 } else if (x >= 0.5) { t*x*y + pow(x, 2) + pow(y, 2) + cos(t) }; } On Tue, Oct 8, 2013 at 2:25 PM, Nathan Woods <charlesnwo...@gmail.com>wrote: > That will actually work fine for my application, and it also more closely > approximates how piecewise functions are used in mathematics, which is a > plus. Let me test it out. > > > On Tue, Oct 8, 2013 at 2:24 PM, Ondřej Čertík <ondrej.cer...@gmail.com>wrote: > >> 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 <mrock...@gmail.com> >> 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 <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. >> >> -- >> 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.