1 #include <symengine/printers/codegen.h>
2 #include <symengine/printers.h>
3 #include <symengine/symengine_exception.h>
8 void CodePrinter::bvisit(
const Basic &x)
10 throw SymEngineException(
"Not supported");
12 void CodePrinter::bvisit(
const Complex &x)
14 throw NotImplementedError(
"Not implemented");
16 void CodePrinter::bvisit(
const Interval &x)
20 bool is_inf =
eq(*x.get_start(), *NegInf);
23 if (x.get_left_open()) {
28 s << apply(x.get_start());
30 if (
neq(*x.get_end(), *Inf)) {
35 if (x.get_right_open()) {
40 s << apply(x.get_end());
44 void CodePrinter::bvisit(
const Contains &x)
46 x.get_expr()->accept(*
this);
47 x.get_set()->accept(*
this);
49 void CodePrinter::bvisit(
const Piecewise &x)
52 auto vec = x.get_vec();
53 for (
size_t i = 0;; ++i) {
54 if (i == vec.size() - 1) {
55 if (
neq(*vec[i].second, *boolTrue)) {
56 throw SymEngineException(
57 "Code generation requires a (Expr, True) at the end");
59 s <<
"(\n " << apply(vec[i].first) <<
"\n";
63 s << apply(vec[i].second);
65 s << apply(vec[i].first);
69 for (
size_t i = 0; i < vec.size(); i++) {
74 void CodePrinter::bvisit(
const Rational &x)
77 double n = mp_get_d(get_num(x.as_rational_class()));
78 double d = mp_get_d(get_den(x.as_rational_class()));
79 o << print_double(n) <<
"/" << print_double(d);
82 void CodePrinter::bvisit(
const Reals &x)
84 throw SymEngineException(
"Not supported");
86 void CodePrinter::bvisit(
const Rationals &x)
88 throw SymEngineException(
"Not supported");
90 void CodePrinter::bvisit(
const Integers &x)
92 throw SymEngineException(
"Not supported");
94 void CodePrinter::bvisit(
const EmptySet &x)
96 throw SymEngineException(
"Not supported");
98 void CodePrinter::bvisit(
const FiniteSet &x)
100 throw SymEngineException(
"Not supported");
102 void CodePrinter::bvisit(
const UniversalSet &x)
104 throw SymEngineException(
"Not supported");
106 void CodePrinter::bvisit(
const Abs &x)
109 s <<
"fabs(" << apply(x.get_arg()) <<
")";
112 void CodePrinter::bvisit(
const Ceiling &x)
115 s <<
"ceil(" << apply(x.get_arg()) <<
")";
118 void CodePrinter::bvisit(
const Truncate &x)
121 s <<
"trunc(" << apply(x.get_arg()) <<
")";
124 void CodePrinter::bvisit(
const Max &x)
127 const auto &args = x.get_args();
128 switch (args.size()) {
131 throw SymEngineException(
"Impossible");
133 s <<
"fmax(" << apply(args[0]) <<
", " << apply(args[1]) <<
")";
136 vec_basic inner_args(args.begin() + 1, args.end());
137 auto inner =
max(inner_args);
138 s <<
"fmax(" << apply(args[0]) <<
", " << apply(inner) <<
")";
144 void CodePrinter::bvisit(
const Min &x)
147 const auto &args = x.get_args();
148 switch (args.size()) {
151 throw SymEngineException(
"Impossible");
153 s <<
"fmin(" << apply(args[0]) <<
", " << apply(args[1]) <<
")";
156 vec_basic inner_args(args.begin() + 1, args.end());
157 auto inner =
min(inner_args);
158 s <<
"fmin(" << apply(args[0]) <<
", " << apply(inner) <<
")";
164 void CodePrinter::bvisit(
const Constant &x)
168 }
else if (
eq(x, *pi)) {
174 void CodePrinter::bvisit(
const NaN &x)
180 void CodePrinter::bvisit(
const Equality &x)
183 s << apply(x.get_arg1()) <<
" == " << apply(x.get_arg2());
186 void CodePrinter::bvisit(
const Unequality &x)
189 s << apply(x.get_arg1()) <<
" != " << apply(x.get_arg2());
192 void CodePrinter::bvisit(
const LessThan &x)
195 s << apply(x.get_arg1()) <<
" <= " << apply(x.get_arg2());
198 void CodePrinter::bvisit(
const StrictLessThan &x)
201 s << apply(x.get_arg1()) <<
" < " << apply(x.get_arg2());
204 void CodePrinter::bvisit(
const UnivariateSeries &x)
206 throw SymEngineException(
"Not supported");
208 void CodePrinter::bvisit(
const Derivative &x)
210 throw SymEngineException(
"Not supported");
212 void CodePrinter::bvisit(
const Subs &x)
214 throw SymEngineException(
"Not supported");
216 void CodePrinter::bvisit(
const GaloisField &x)
218 throw SymEngineException(
"Not supported");
221 void C89CodePrinter::bvisit(
const Infty &x)
224 if (x.is_negative_infinity())
226 else if (x.is_positive_infinity())
229 throw SymEngineException(
"Not supported");
233 const RCP<const Basic> &a,
234 const RCP<const Basic> &b)
237 o <<
"exp(" << apply(b) <<
")";
239 o <<
"sqrt(" << apply(a) <<
")";
241 o <<
"pow(" << apply(a) <<
", " << apply(b) <<
")";
245 void C99CodePrinter::bvisit(
const Infty &x)
248 if (x.is_negative_infinity())
250 else if (x.is_positive_infinity())
253 throw SymEngineException(
"Not supported");
257 const RCP<const Basic> &a,
258 const RCP<const Basic> &b)
261 o <<
"exp(" << apply(b) <<
")";
263 o <<
"sqrt(" << apply(a) <<
")";
265 o <<
"cbrt(" << apply(a) <<
")";
267 o <<
"pow(" << apply(a) <<
", " << apply(b) <<
")";
270 void C99CodePrinter::bvisit(
const Gamma &x)
273 s <<
"tgamma(" << apply(x.get_arg()) <<
")";
276 void C99CodePrinter::bvisit(
const LogGamma &x)
279 s <<
"lgamma(" << apply(x.get_arg()) <<
")";
283 void JSCodePrinter::bvisit(
const Constant &x)
287 }
else if (
eq(x, *pi)) {
294 const RCP<const Basic> &b)
297 o <<
"Math.exp(" << apply(b) <<
")";
299 o <<
"Math.sqrt(" << apply(a) <<
")";
301 o <<
"Math.cbrt(" << apply(a) <<
")";
303 o <<
"Math.pow(" << apply(a) <<
", " << apply(b) <<
")";
306 void JSCodePrinter::bvisit(
const Abs &x)
309 s <<
"Math.abs(" << apply(x.get_arg()) <<
")";
312 void JSCodePrinter::bvisit(
const Sin &x)
315 s <<
"Math.sin(" << apply(x.get_arg()) <<
")";
318 void JSCodePrinter::bvisit(
const Cos &x)
321 s <<
"Math.cos(" << apply(x.get_arg()) <<
")";
324 void JSCodePrinter::bvisit(
const Max &x)
326 const auto &args = x.get_args();
329 for (
size_t i = 0; i < args.size(); ++i) {
331 s << ((i == args.size() - 1) ?
")" :
", ");
335 void JSCodePrinter::bvisit(
const Min &x)
337 const auto &args = x.get_args();
340 for (
size_t i = 0; i < args.size(); ++i) {
342 s << ((i == args.size() - 1) ?
")" :
", ");
Main namespace for SymEngine package.
RCP< const Basic > max(const vec_basic &arg)
Canonicalize Max:
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
RCP< const Basic > min(const vec_basic &arg)
Canonicalize Min:
RCP< const Number > rational(long n, long d)
convenience creator from two longs