In trying to produce C output of a piecewise function (actually it’s piecewise derivative), the output has the word piecewise instead of splitting things up into ’if’ and ’then’...is there something I am missing?
How can get C output that I can directly use? Is there a macro or lib I need to link to?
Manual editing isn’t really an option since we actually have to generate a large file with lots of equations of this nature.
This is what "prep2trans" in the codegen package is for. For example:
> f:= proc(x,y) piecewise(x<3*y,x,y) end; with(codegen): g:= prep2trans(f): C(g); double g(x,y) double x; double y; { { if( x < 3.0*y ) return(x); else return(y); } }
It also handles procedures involving "sum" or "add". However, I just noticed a bug: it doesn’t seem to like a "piecewise" inside a "sum" or "add": e.g.
> f:= proc(x,y) local i; add(piecewise(x<i*y,x,y),i=1..3) end; g:= prep2trans(f); Error, (in intrep/StatSeq) invalid terms in sum