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 CodePrinter : public BaseVisitor<CodePrinter, StrPrinter>
13 {
14 public:
15  explicit CodePrinter(CodePrinterPrecision precision
16  = CodePrinterPrecision::Double);
17  using StrPrinter::apply;
18  using StrPrinter::bvisit;
19  using StrPrinter::str_;
20  void bvisit(const Basic &x);
21  void bvisit(const Complex &x);
22  void bvisit(const Dummy &x);
23  void bvisit(const Interval &x);
24  void bvisit(const Contains &x);
25  void bvisit(const Piecewise &x);
26  void bvisit(const BooleanAtom &x);
27  void bvisit(const And &x);
28  void bvisit(const Or &x);
29  void bvisit(const Xor &x);
30  void bvisit(const Not &x);
31  void bvisit(const Integer &x);
32  void bvisit(const Rational &x);
33  void bvisit(const EmptySet &x);
34  void bvisit(const FiniteSet &x);
35  void bvisit(const Reals &x);
36  void bvisit(const Rationals &x);
37  void bvisit(const Integers &x);
38  void bvisit(const UniversalSet &x);
39  void bvisit(const Abs &x);
40  void bvisit(const Ceiling &x);
41  void bvisit(const Truncate &x);
42  void bvisit(const Max &x);
43  void bvisit(const Min &x);
44  void bvisit(const Constant &x);
45  void bvisit(const NaN &x);
46  void bvisit(const Equality &x);
47  void bvisit(const Unequality &x);
48  void bvisit(const LessThan &x);
49  void bvisit(const StrictLessThan &x);
50  void bvisit(const Sign &x);
51  void bvisit(const UnevaluatedExpr &x);
52  void bvisit(const UnivariateSeries &x);
53  void bvisit(const Derivative &x);
54  void bvisit(const Subs &x);
55  void bvisit(const GaloisField &x);
56  void bvisit(const Function &x);
57  void bvisit(const RealDouble &x);
58 #ifdef HAVE_SYMENGINE_MPFR
59  void bvisit(const RealMPFR &x);
60 #endif
61 
62 protected:
63  CodePrinterPrecision precision_;
64  std::string print_scalar_literal(double d) const;
65  std::string print_math_function(const std::string &name) const;
66  std::string print_binary_reduction(const vec_basic &args,
67  const std::string &func_name);
68  std::string print_binary_reduction_impl(vec_basic::const_iterator begin,
69  vec_basic::const_iterator end,
70  const std::string &func_name);
71 };
72 
73 class C89CodePrinter : public BaseVisitor<C89CodePrinter, CodePrinter>
74 {
75 public:
76  explicit C89CodePrinter(CodePrinterPrecision precision
77  = CodePrinterPrecision::Double);
78  using CodePrinter::apply;
79  using CodePrinter::bvisit;
80  using CodePrinter::str_;
81  void bvisit(const Infty &x);
82  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
83  const RCP<const Basic> &b) override;
84 };
85 
86 class C99CodePrinter : public BaseVisitor<C99CodePrinter, C89CodePrinter>
87 {
88 public:
89  explicit C99CodePrinter(CodePrinterPrecision precision
90  = CodePrinterPrecision::Double);
91  using C89CodePrinter::apply;
92  using C89CodePrinter::bvisit;
93  using C89CodePrinter::str_;
94  void bvisit(const Infty &x);
95  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
96  const RCP<const Basic> &b) override;
97  void bvisit(const Gamma &x);
98  void bvisit(const LogGamma &x);
99 };
100 
101 class CudaCodePrinter : public BaseVisitor<CudaCodePrinter, C99CodePrinter>
102 {
103 public:
104  explicit CudaCodePrinter(CodePrinterPrecision precision
105  = CodePrinterPrecision::Double);
106  using C99CodePrinter::apply;
107  using C99CodePrinter::bvisit;
108  using C99CodePrinter::str_;
109  void bvisit(const Integer &x);
110  void bvisit(const Constant &x);
111  void bvisit(const NaN &x);
112  void bvisit(const Infty &x);
113 };
114 
115 class MetalCodePrinter : public BaseVisitor<MetalCodePrinter, CodePrinter>
116 {
117 public:
118  explicit MetalCodePrinter(CodePrinterPrecision precision
119  = CodePrinterPrecision::Float);
120  using CodePrinter::apply;
121  using CodePrinter::bvisit;
122  using CodePrinter::str_;
123  void bvisit(const Constant &x);
124  void bvisit(const NaN &x);
125  void bvisit(const Infty &x);
126  void bvisit(const Abs &x);
127  void bvisit(const Ceiling &x);
128  void bvisit(const Truncate &x);
129  void bvisit(const Max &x);
130  void bvisit(const Min &x);
131  void bvisit(const Function &x);
132  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
133  const RCP<const Basic> &b) override;
134 };
135 
136 class JSCodePrinter : public BaseVisitor<JSCodePrinter, CodePrinter>
137 {
138 public:
139  using CodePrinter::apply;
140  using CodePrinter::bvisit;
141  using CodePrinter::str_;
142  void bvisit(const Constant &x);
143  void _print_pow(std::ostringstream &o, const RCP<const Basic> &a,
144  const RCP<const Basic> &b) override;
145  void bvisit(const Abs &x);
146  void bvisit(const Sin &x);
147  void bvisit(const Cos &x);
148  void bvisit(const Max &x);
149  void bvisit(const Min &x);
150 };
151 } // namespace SymEngine
152 
153 #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