Loading...
Searching...
No Matches
sbml.cpp
1#include <symengine/printers/sbml.h>
2#include <symengine/printers.h>
3
4namespace SymEngine
5{
6
7static std::vector<std::string> init_sbml_printer_names()
8{
9 std::vector<std::string> names = init_str_printer_names();
10 names[SYMENGINE_LOG] = "ln";
11 names[SYMENGINE_ASIN] = "arcsin";
12 names[SYMENGINE_ACOS] = "arccos";
13 names[SYMENGINE_ASEC] = "arcsec";
14 names[SYMENGINE_ACSC] = "arccsc";
15 names[SYMENGINE_ATAN] = "arctan";
16 names[SYMENGINE_ACOT] = "arccot";
17 names[SYMENGINE_ASINH] = "arcsinh";
18 names[SYMENGINE_ACSCH] = "arccsch";
19 names[SYMENGINE_ACOSH] = "arccosh";
20 names[SYMENGINE_ATANH] = "arctanh";
21 names[SYMENGINE_ACOTH] = "arccoth";
22 names[SYMENGINE_ASECH] = "arcsech";
23 return names;
24}
25
26void SbmlPrinter::_print_pow(std::ostringstream &o, const RCP<const Basic> &a,
27 const RCP<const Basic> &b)
28{
29 if (eq(*a, *E)) {
30 o << "exp(" << apply(b) << ")";
31 } else if (eq(*b, *rational(1, 2))) {
32 o << "sqrt(" << apply(a) << ")";
33 } else {
34 o << parenthesizeLE(a, PrecedenceEnum::Pow);
35 o << "^";
36 o << parenthesizeLE(b, PrecedenceEnum::Pow);
37 }
38}
39
40void SbmlPrinter::bvisit(const BooleanAtom &x)
41{
42 if (x.get_val()) {
43 str_ = "true";
44 } else {
45 str_ = "false";
46 }
47}
48
49void SbmlPrinter::bvisit(const And &x)
50{
52 const auto &container = x.get_container();
53 s << "and(";
54 s << apply(*container.begin());
55 for (auto it = ++(container.begin()); it != container.end(); ++it) {
56 s << ", " << apply(*it);
57 }
58 s << ")";
59 str_ = s.str();
60}
61
62void SbmlPrinter::bvisit(const Or &x)
63{
65 const auto &container = x.get_container();
66 s << "or(";
67 s << apply(*container.begin());
68 for (auto it = ++(container.begin()); it != container.end(); ++it) {
69 s << ", " << apply(*it);
70 }
71 s << ")";
72 str_ = s.str();
73}
74
75void SbmlPrinter::bvisit(const Xor &x)
76{
78 const auto &container = x.get_container();
79 s << "xor(";
80 s << apply(*container.begin());
81 for (auto it = ++(container.begin()); it != container.end(); ++it) {
82 s << ", " << apply(*it);
83 }
84 s << ")";
85 str_ = s.str();
86}
87
88void SbmlPrinter::bvisit(const Not &x)
89{
91 s << "not(" << *x.get_arg() << ")";
92 str_ = s.str();
93}
94
95void SbmlPrinter::bvisit(const Piecewise &x)
96{
98 auto vec = x.get_vec();
99 auto it = vec.begin();
100 s << "piecewise(";
101 while (it != vec.end()) {
102 s << apply((*it).first);
103 s << ", ";
104 s << apply((*it).second);
105 ++it;
106 if (it != vec.end()) {
107 s << ", ";
108 }
109 }
110 s << ")";
111 str_ = s.str();
112}
113
114void SbmlPrinter::bvisit(const Infty &x)
115{
116 str_ = "inf";
117}
118
119void SbmlPrinter::bvisit(const Constant &x)
120{
121 if (eq(x, *E)) {
122 str_ = "exponentiale";
123 } else {
124 str_ = x.get_name();
125 std::transform(str_.begin(), str_.end(), str_.begin(), ::tolower);
126 }
127}
128
129void SbmlPrinter::bvisit(const Function &x)
130{
131 static const std::vector<std::string> names_ = init_sbml_printer_names();
133 vec_basic vec = x.get_args();
134 if (x.get_type_code() == SYMENGINE_GAMMA) {
135 // sbml only has factorial, no gamma function
136 o << "factorial(" << apply(vec) << " - 1)";
137 } else if (x.get_type_code() == SYMENGINE_LOG && vec.size() == 2) {
138 // sbml log has order of arguments inverted
139 o << "log(" << apply(vec[1]) << ", " << apply(vec[0]) << ")";
140 } else {
141 o << names_[x.get_type_code()];
142 o << parenthesize(apply(vec));
143 }
144 str_ = o.str();
145}
146
147std::string sbml(const Basic &x)
148{
149 SbmlPrinter m;
150 return m.apply(x);
151}
152
153} // namespace SymEngine
T begin(T... args)
T end(T... args)
Main namespace for SymEngine package.
Definition: add.cpp:19
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21
RCP< const Number > rational(long n, long d)
convenience creator from two longs
Definition: rational.h:328
T str(T... args)
T transform(T... args)