codegen.h
1 #ifndef SYMENGINE_CODEGEN_H
2 #define SYMENGINE_CODEGEN_H
3 
4 #include <symengine/printers.h>
5 #include <symengine/visitor.h>
6 #include <symengine/printers/strprinter.h>
7 #include <symengine/symengine_exception.h>
8 
9 namespace SymEngine
10 {
11 
12 class SYMENGINE_EXPORT CodePrinter
13  : public RewriteTrigVisitor<CodePrinter, StrPrinter>
14 {
15 public:
16  explicit CodePrinter(CodePrinterPrecision precision
17  = CodePrinterPrecision::Double);
18  using StrPrinter::apply;
19  using StrPrinter::bvisit;
20  using StrPrinter::str_;
21  void bvisit(const Basic &x);
22  void bvisit(const Complex &x);
23  void bvisit(const Dummy &x);
24  void bvisit(const Interval &x);
25  void bvisit(const Contains &x);
26  void bvisit(const Piecewise &x);
27  void bvisit(const BooleanAtom &x);
28  void bvisit(const And &x);
29  void bvisit(const Or &x);
30  void bvisit(const Xor &x);
31  void bvisit(const Not &x);
32  void bvisit(const Integer &x);
33  void bvisit(const Rational &x);
34  void bvisit(const EmptySet &x);
35  void bvisit(const FiniteSet &x);
36  void bvisit(const Reals &x);
37  void bvisit(const Rationals &x);
38  void bvisit(const Integers &x);
39  void bvisit(const UniversalSet &x);
40  void bvisit(const Abs &x);
41  void bvisit(const Ceiling &x);
42  void bvisit(const Truncate &x);
43  void bvisit(const Max &x);
44  void bvisit(const Min &x);
45  void bvisit(const Constant &x);
46  void bvisit(const NaN &x);
47  void bvisit(const Equality &x);
48  void bvisit(const Unequality &x);
49  void bvisit(const LessThan &x);
50  void bvisit(const StrictLessThan &x);
51  void bvisit(const Sign &x);
52  void bvisit(const UnevaluatedExpr &x);
53  void bvisit(const UnivariateSeries &x);
54  void bvisit(const Derivative &x);
55  void bvisit(const Subs &x);
56  void bvisit(const GaloisField &x);
57  void bvisit(const Function &x);
58  void bvisit(const RealDouble &x);
59 #ifdef HAVE_SYMENGINE_MPFR
60  void bvisit(const RealMPFR &x);
61 #endif
62 
63 protected:
64  CodePrinterPrecision precision_;
65  std::string print_scalar_literal(double d) const;
66  std::string print_math_function(const std::string &name) const;
67  virtual std::string
68  format_codegen_function_name(const std::string &name) const;
69  std::string print_binary_reduction(const vec_basic &args,
70  const std::string &func_name);
71  std::string print_binary_reduction_impl(vec_basic::const_iterator begin,
72  vec_basic::const_iterator end,
73  const std::string &func_name);
74 };
75 
76 class SYMENGINE_EXPORT C89CodePrinter
77  : public RewriteTrigVisitor<C89CodePrinter, CodePrinter>
78 {
79 public:
80  explicit C89CodePrinter(CodePrinterPrecision precision
81  = CodePrinterPrecision::Double);
82  using CodePrinter::apply;
83  using CodePrinter::bvisit;
84  using CodePrinter::str_;
85  void bvisit(const Infty &x);
86  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
87  const RCP<const Basic> &b) override;
88 };
89 
90 class SYMENGINE_EXPORT C99CodePrinter
91  : public RewriteTrigVisitor<C99CodePrinter, C89CodePrinter>
92 {
93 public:
94  explicit C99CodePrinter(CodePrinterPrecision precision
95  = CodePrinterPrecision::Double);
96  using C89CodePrinter::apply;
97  using C89CodePrinter::bvisit;
98  using C89CodePrinter::str_;
99  void bvisit(const Infty &x);
100  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
101  const RCP<const Basic> &b) override;
102  void bvisit(const Gamma &x);
103  void bvisit(const LogGamma &x);
104 };
105 
106 class SYMENGINE_EXPORT CudaCodePrinter
107  : public RewriteTrigVisitor<CudaCodePrinter, C99CodePrinter>
108 {
109 public:
110  explicit CudaCodePrinter(CodePrinterPrecision precision
111  = CodePrinterPrecision::Double);
112  using C99CodePrinter::apply;
113  using C99CodePrinter::bvisit;
114  using C99CodePrinter::str_;
115  void bvisit(const Integer &x);
116  void bvisit(const Constant &x);
117  void bvisit(const NaN &x);
118  void bvisit(const Infty &x);
119 };
120 
121 class SYMENGINE_EXPORT MetalCodePrinter
122  : public RewriteTrigVisitor<MetalCodePrinter, CodePrinter>
123 {
124 public:
125  explicit MetalCodePrinter(CodePrinterPrecision precision
126  = CodePrinterPrecision::Float);
127  using CodePrinter::apply;
128  using CodePrinter::bvisit;
129  using CodePrinter::str_;
130  void bvisit(const Constant &x);
131  void bvisit(const NaN &x);
132  void bvisit(const Infty &x);
133  void bvisit(const Abs &x);
134  void bvisit(const Ceiling &x);
135  void bvisit(const Truncate &x);
136  void bvisit(const Max &x);
137  void bvisit(const Min &x);
138  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
139  const RCP<const Basic> &b) override;
140 
141 protected:
142  std::string
143  format_codegen_function_name(const std::string &name) const override;
144 };
145 
146 class SYMENGINE_EXPORT JSCodePrinter
147  : public RewriteTrigVisitor<JSCodePrinter, CodePrinter>
148 {
149 public:
150  using CodePrinter::apply;
151  using CodePrinter::bvisit;
152  using CodePrinter::str_;
153  void bvisit(const Constant &x);
154  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
155  const RCP<const Basic> &b) override;
156  void bvisit(const Abs &x);
157  void bvisit(const Sin &x);
158  void bvisit(const Cos &x);
159  void bvisit(const Max &x);
160  void bvisit(const Min &x);
161 
162 protected:
163  std::string
164  format_codegen_function_name(const std::string &name) const override;
165 };
166 } // namespace SymEngine
167 
168 #endif // SYMENGINE_CODEGEN_H
The lowest unit of symbolic representation.
Definition: basic.h:97
Complex Class.
Definition: complex.h:33
Integer Class.
Definition: integer.h:19
Rational Class.
Definition: rational.h:16
RealDouble Class to hold double values.
Definition: real_double.h:20
UnivariateSeries Class.
Main namespace for SymEngine package.
Definition: add.cpp:19