sbml.cpp
1 #include <symengine/printers/sbml.h>
2 #include <symengine/printers.h>
3 
4 namespace SymEngine
5 {
6 
7 static 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 
26 void 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 
40 void SbmlPrinter::bvisit(const BooleanAtom &x)
41 {
42  if (x.get_val()) {
43  str_ = "true";
44  } else {
45  str_ = "false";
46  }
47 }
48 
49 void 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 
62 void 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 
75 void 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 
88 void SbmlPrinter::bvisit(const Not &x)
89 {
91  s << "not(" << *x.get_arg() << ")";
92  str_ = s.str();
93 }
94 
95 void 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  if (!(it + 1 == vec.end() && eq(*(*it).second, *boolTrue))) {
104  s << ", ";
105  s << apply((*it).second);
106  }
107  ++it;
108  if (it != vec.end()) {
109  s << ", ";
110  }
111  }
112  s << ")";
113  str_ = s.str();
114 }
115 
116 void SbmlPrinter::bvisit(const Infty &x)
117 {
118  str_ = "inf";
119 }
120 
121 void SbmlPrinter::bvisit(const Constant &x)
122 {
123  if (eq(x, *E)) {
124  str_ = "exponentiale";
125  } else {
126  str_ = x.get_name();
127  std::transform(str_.begin(), str_.end(), str_.begin(), ::tolower);
128  }
129 }
130 
131 void SbmlPrinter::bvisit(const Function &x)
132 {
133  static const std::vector<std::string> names_ = init_sbml_printer_names();
135  vec_basic vec = x.get_args();
136  if (x.get_type_code() == SYMENGINE_GAMMA) {
137  // sbml only has factorial, no gamma function
138  o << "factorial(" << apply(vec) << " - 1)";
139  } else if (x.get_type_code() == SYMENGINE_LOG && vec.size() == 2) {
140  // sbml log has order of arguments inverted
141  o << "log(" << apply(vec[1]) << ", " << apply(vec[0]) << ")";
142  } else {
143  o << names_[x.get_type_code()];
144  o << parenthesize(apply(vec));
145  }
146  str_ = o.str();
147 }
148 
149 std::string sbml(const Basic &x)
150 {
151  SbmlPrinter m;
152  return m.apply(x);
153 }
154 
155 } // 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)