Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MathExpr.cpp
1 #include <Teuchos_MathExpr.hpp>
2 
3 namespace Teuchos {
4 
5 namespace MathExpr {
6 
7 Language make_language() {
8  Language out;
9  Language::Productions& prods = out.productions;
10  prods.resize(NPRODS);
11  prods[PROD_PROGRAM]("program") >> "statements", "expr?";
12  prods[PROD_NO_STATEMENTS]("statements");
13  prods[PROD_NEXT_STATEMENT]("statements") >> "statements", "statement", ";", "S?";
14  prods[PROD_ASSIGN]("statement") >> "name", "S?", "=", "S?", "expr";
15  prods[PROD_NO_EXPR]("expr?");
16  prods[PROD_YES_EXPR]("expr?") >> "expr";
17  prods[PROD_EXPR]("expr") >> "ternary";
18  prods[PROD_TERNARY_DECAY]("ternary") >> "add_sub";
19  prods[PROD_OR_DECAY]("or") >> "and";
20  prods[PROD_AND_DECAY]("and") >> "comp";
21  prods[PROD_ADD_SUB_DECAY]("add_sub") >> "mul_div";
22  prods[PROD_MUL_DIV_DECAY]("mul_div") >> "neg";
23  prods[PROD_NEG_DECAY]("neg") >> "pow";
24  prods[PROD_POW_DECAY]("pow") >> "scalar";
25  prods[PROD_TERNARY]("ternary")
26  >> "or", "?", "S?", "add_sub", ":", "S?", "add_sub";
27  prods[PROD_OR]("or") >> "or", "||", "S?", "and";
28  prods[PROD_AND]("and") >> "and", "&&", "S?", "comp";
29  prods[PROD_GT]("comp") >> "add_sub", ">", "S?", "add_sub";
30  prods[PROD_LT]("comp") >> "add_sub", "<", "S?", "add_sub";
31  prods[PROD_GEQ]("comp") >> "add_sub", ">=", "S?", "add_sub";
32  prods[PROD_LEQ]("comp") >> "add_sub", "<=", "S?", "add_sub";
33  prods[PROD_EQ]("comp") >> "add_sub", "==", "S?", "add_sub";
34  prods[PROD_BOOL_PARENS]("comp") >> "(", "S?", "or", ")", "S?";
35  prods[PROD_ADD]("add_sub") >> "add_sub", "+", "S?", "mul_div";
36  prods[PROD_SUB]("add_sub") >> "add_sub", "-", "S?", "mul_div";
37  prods[PROD_MUL]("mul_div") >> "mul_div", "*", "S?", "pow";
38  prods[PROD_DIV]("mul_div") >> "mul_div", "/", "S?", "pow";
39  prods[PROD_POW]("pow") >> "scalar", "^", "S?", "pow";
40  prods[PROD_CALL]("scalar")
41  >> "name", "S?", "(", "S?", "args?", ")", "S?";
42  prods[PROD_NO_ARGS]("args?");
43  prods[PROD_SOME_ARGS]("args?") >> "args";
44  prods[PROD_FIRST_ARG]("args") >> "ternary";
45  prods[PROD_NEXT_ARG]("args") >> "args", ",", "S?", "ternary";
46  prods[PROD_NEG]("neg") >> "-", "S?", "neg";
47  prods[PROD_VAL_PARENS]("scalar") >> "(", "S?", "ternary", ")", "S?";
48  prods[PROD_CONST]("scalar") >> "constant", "S?";
49  prods[PROD_VAR]("scalar") >> "name", "S?";
50  prods[PROD_NO_SPACES]("S?");
51  prods[PROD_SPACES]("S?") >> "spaces";
52  out.tokens.resize(NTOKS);
53  out.tokens[TOK_SPACE]("spaces", "[ \t\n\r]+");
54  out.tokens[TOK_NAME]("name", "[_a-zA-Z][_a-zA-Z0-9]*");
55  out.tokens[TOK_ADD]("+", "\\+");
56  out.tokens[TOK_SUB]("-", "\\-");
57  out.tokens[TOK_MUL]("*", "\\*");
58  out.tokens[TOK_DIV]("/", "\\/");
59  out.tokens[TOK_POW]("^", "\\^");
60  out.tokens[TOK_LPAREN]("(", "\\(");
61  out.tokens[TOK_RPAREN](")", "\\)");
62  out.tokens[TOK_COMMA](",", ",");
63  out.tokens[TOK_CHECK]("?", "\\?");
64  out.tokens[TOK_CHOOSE](":", ":");
65  out.tokens[TOK_GT](">", ">");
66  out.tokens[TOK_LT]("<", "<");
67  out.tokens[TOK_GEQ](">=", ">=");
68  out.tokens[TOK_LEQ]("<=", "<=");
69  out.tokens[TOK_EQ]("==", "==");
70  out.tokens[TOK_AND]("&&", "&&");
71  out.tokens[TOK_OR]("||", "\\|\\|");
72  out.tokens[TOK_CONST]("constant",
73  "(0|([1-9][0-9]*))(\\.[0-9]*)?([eE]\\-?[1-9][0-9]*)?");
74  out.tokens[TOK_SEMICOLON](";", ";");
75  out.tokens[TOK_ASSIGN]("=", "=");
76  return out;
77 }
78 
79 LanguagePtr ask_language() {
80  static LanguagePtr ptr;
81  if (ptr.strong_count() == 0) {
82  ptr.reset(new Language(make_language()));
83  }
84  return ptr;
85 }
86 
87 ReaderTablesPtr ask_reader_tables() {
88  static ReaderTablesPtr ptr;
89  if (ptr.strong_count() == 0) {
90  LanguagePtr lang = ask_language();
91  ptr = make_reader_tables(*lang);
92  }
93  return ptr;
94 }
95 
96 SymbolSetReader::SymbolSetReader():
97  Reader(ask_reader_tables())
98 {
99 }
100 
101 SymbolSetReader::~SymbolSetReader()
102 {
103 }
104 
105 void SymbolSetReader::at_shift(any& result, int token, std::string& text) {
106  if (token == TOK_NAME) result = text;
107 }
108 
109 void SymbolSetReader::at_reduce(any& /* result */, int prod, std::vector<any>& rhs) {
110  if (prod == PROD_VAR) {
111  std::string& name = any_ref_cast<std::string>(rhs.at(0));
112  variable_names.insert(name);
113  } else if (prod == PROD_CALL) {
114  std::string& name = any_ref_cast<std::string>(rhs.at(0));
115  function_names.insert(name);
116  }
117 }
118 
119 std::set<std::string> get_variables_used(std::string const& expr) {
120  SymbolSetReader reader;
121  any result;
122  reader.read_string(result, expr, "get_variables_used");
123  return reader.variable_names;
124 }
125 
126 std::set<std::string> get_symbols_used(std::string const& expr) {
127  SymbolSetReader reader;
128  any result;
129  reader.read_string(result, expr, "get_symbols_used");
130  auto set = std::move(reader.variable_names);
131  set.insert(reader.function_names.begin(), reader.function_names.end());
132  return set;
133 }
134 
135 class CalcReader : public Reader {
136  public:
137  CalcReader():Reader(MathExpr::ask_reader_tables()) {
138  unary_function_map["sqrt"] = &std::sqrt;
139  unary_function_map["sin"] = &std::sin;
140  unary_function_map["cos"] = &std::cos;
141  unary_function_map["tan"] = &std::tan;
142  unary_function_map["asin"] = &std::asin;
143  unary_function_map["acos"] = &std::acos;
144  unary_function_map["atan"] = &std::atan;
145  unary_function_map["exp"] = &std::exp;
146  unary_function_map["log"] = &std::log;
147  unary_function_map["log10"] = &std::log10;
148  binary_function_map["atan2"] = &std::atan2;
149  }
150  virtual ~CalcReader() = default;
151  protected:
152  struct CallArgs {
153  double a0;
154  double a1;
155  int n;
156  };
157  virtual void at_shift(any& result_any, int token, std::string& text) {
158  using std::swap;
159  switch (token) {
160  case MathExpr::TOK_NAME: {
161  std::string& result = make_any_ref<std::string>(result_any);
162  swap(result, text);
163  return;
164  }
165  case MathExpr::TOK_CONST: {
166  result_any = std::atof(text.c_str());
167  return;
168  }
169  }
170  }
171  virtual void at_reduce(any& result, int prod, std::vector<any>& rhs) {
172  using std::swap;
173  switch (prod) {
174  case MathExpr::PROD_PROGRAM: {
175  TEUCHOS_TEST_FOR_EXCEPTION(!rhs.at(1).has_value(), ParserFail,
176  "Calculator needs an expression to evaluate!");
177  swap(result, rhs.at(1));
178  break;
179  }
180  case MathExpr::PROD_NO_STATEMENTS:
181  case MathExpr::PROD_NO_EXPR:
182  case MathExpr::PROD_NEXT_STATEMENT: {
183  break;
184  }
185  case MathExpr::PROD_ASSIGN: {
186  std::string const& name = any_ref_cast<std::string>(rhs.at(0));
187  double value = any_cast<double>(rhs.at(4));
188  variable_map[name] = value;
189  break;
190  }
191  case MathExpr::PROD_YES_EXPR:
192  case MathExpr::PROD_EXPR:
193  case MathExpr::PROD_TERNARY_DECAY:
194  case MathExpr::PROD_OR_DECAY:
195  case MathExpr::PROD_AND_DECAY:
196  case MathExpr::PROD_ADD_SUB_DECAY:
197  case MathExpr::PROD_MUL_DIV_DECAY:
198  case MathExpr::PROD_POW_DECAY:
199  case MathExpr::PROD_NEG_DECAY:
200  case MathExpr::PROD_SOME_ARGS:
201  swap(result, rhs.at(0));
202  break;
203  case MathExpr::PROD_TERNARY:
204  result = any_cast<bool>(rhs.at(0)) ?
205  any_cast<double>(rhs.at(3)) :
206  any_cast<double>(rhs.at(6));
207  break;
208  case MathExpr::PROD_OR:
209  result = any_cast<bool>(rhs.at(0)) || any_cast<bool>(rhs.at(3));
210  break;
211  case MathExpr::PROD_AND:
212  result = any_cast<bool>(rhs.at(0)) && any_cast<bool>(rhs.at(3));
213  break;
214  case MathExpr::PROD_GT:
215  result = any_cast<double>(rhs.at(0)) > any_cast<double>(rhs.at(3));
216  break;
217  case MathExpr::PROD_LT:
218  result = any_cast<double>(rhs.at(0)) < any_cast<double>(rhs.at(3));
219  break;
220  case MathExpr::PROD_GEQ:
221  result = any_cast<double>(rhs.at(0)) >= any_cast<double>(rhs.at(3));
222  break;
223  case MathExpr::PROD_LEQ:
224  result = any_cast<double>(rhs.at(0)) <= any_cast<double>(rhs.at(3));
225  break;
226  case MathExpr::PROD_EQ:
227  result = any_cast<double>(rhs.at(0)) == any_cast<double>(rhs.at(3));
228  break;
229  case MathExpr::PROD_BOOL_PARENS:
230  result = any_cast<bool>(rhs.at(2));
231  break;
232  case MathExpr::PROD_ADD:
233  result = any_cast<double>(rhs.at(0)) + any_cast<double>(rhs.at(3));
234  break;
235  case MathExpr::PROD_SUB:
236  result = any_cast<double>(rhs.at(0)) - any_cast<double>(rhs.at(3));
237  break;
238  case MathExpr::PROD_MUL:
239  result = any_cast<double>(rhs.at(0)) * any_cast<double>(rhs.at(3));
240  break;
241  case MathExpr::PROD_DIV:
242  result = any_cast<double>(rhs.at(0)) / any_cast<double>(rhs.at(3));
243  break;
244  case MathExpr::PROD_POW:
245  result = std::pow(any_cast<double>(rhs.at(0)), any_cast<double>(rhs.at(3)));
246  break;
247  case MathExpr::PROD_CALL: {
248  std::string& name = any_ref_cast<std::string>(rhs.at(0));
249  CallArgs& args = any_ref_cast<CallArgs>(rhs.at(4));
250  TEUCHOS_TEST_FOR_EXCEPTION(args.n < 1 || args.n > 2, ParserFail,
251  "Only unary and binary functions supported!\n");
252  if (args.n == 1) {
253  TEUCHOS_TEST_FOR_EXCEPTION(!unary_function_map.count(name), ParserFail,
254  "Unknown unary function name \"" << name << "\"\n");
255  Unary fptr = unary_function_map[name];
256  result = (*fptr)(args.a0);
257  } else {
258  TEUCHOS_TEST_FOR_EXCEPTION(!binary_function_map.count(name), ParserFail,
259  "Unknown binary function name \"" << name << "\"\n");
260  Binary fptr = binary_function_map[name];
261  result = (*fptr)(args.a0, args.a1);
262  }
263  break;
264  }
265  case MathExpr::PROD_NO_ARGS: {
266  CallArgs& args = make_any_ref<CallArgs>(result);
267  args.n = 0;
268  break;
269  }
270  case MathExpr::PROD_FIRST_ARG: {
271  CallArgs& args = make_any_ref<CallArgs>(result);
272  args.a0 = any_cast<double>(rhs.at(0));
273  args.n = 1;
274  break;
275  }
276  case MathExpr::PROD_NEXT_ARG: {
277  CallArgs& args = any_ref_cast<CallArgs>(rhs.at(0));
278  args.a1 = any_cast<double>(rhs.at(3));
279  args.n = 2;
280  swap(result, rhs.at(0));
281  break;
282  }
283  case MathExpr::PROD_NEG:
284  result = - any_cast<double>(rhs.at(2));
285  break;
286  case MathExpr::PROD_VAL_PARENS:
287  result = any_cast<double>(rhs.at(2));
288  break;
289  case MathExpr::PROD_CONST:
290  result = any_cast<double>(rhs.at(0));
291  break;
292  case MathExpr::PROD_VAR:
293  std::string const& name = any_ref_cast<std::string>(rhs.at(0));
294  auto it = variable_map.find(name);
295  TEUCHOS_TEST_FOR_EXCEPTION(it == variable_map.end(), ParserFail,
296  "variable " << name << " not defined!");
297  double value = it->second;
298  result = value;
299  break;
300  }
301  }
302  private:
303  typedef double (*Unary)(double);
304  typedef double (*Binary)(double, double);
305  std::map<std::string, Unary> unary_function_map;
306  std::map<std::string, Binary> binary_function_map;
307  std::map<std::string, double> variable_map;
308 };
309 
310 Reader* new_calc_reader() {
311  return new CalcReader();
312 }
313 
314 } // end namespace MathExpr
315 
316 } // end namespace Teuchos
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Productions productions
vector of productions
ValueType & any_ref_cast(any &operand)
Keep the convenient behavior of Teuchos::any_cast w.r.t. references, but don&#39;t confuse it with the be...
RCP< const ReaderTables > ReaderTablesPtr
an RCP to a const ReaderTables
ReaderTablesPtr make_reader_tables(Language const &language)
constructs ReaderTables for the given Language.
RCP< const Language > LanguagePtr
an RCP to a const Language