gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
mathEvaluator.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "mathEvaluator.h"
7 
8 #if defined(HAVE_MATHEX)
9 
10 mathEvaluator::mathEvaluator(std::vector<std::string> &expressions,
11  const std::vector<std::string> &variables)
12 {
13  static std::string lastError;
14 
15  _expressions.resize(expressions.size());
16  _variables.resize(variables.size(), 0.);
17  bool error = false;
18  for(std::size_t i = 0; i < expressions.size(); i++) {
19  _expressions[i] = new smlib::mathex();
20  for(std::size_t j = 0; j < variables.size(); j++)
21  _expressions[i]->addvar(variables[j], &_variables[j]);
22  try {
23  _expressions[i]->expression(expressions[i]);
24  _expressions[i]->parse();
25  } catch(smlib::mathex::error &e) {
26  if(e.what() + expressions[i] != lastError) {
27  lastError = e.what() + expressions[i];
28  Msg::Error(e.what());
29  std::string pos(_expressions[i]->stopposition(), ' ');
30  pos.push_back('^');
31  Msg::Error(expressions[i].c_str());
32  Msg::Error(pos.c_str());
33  }
34  error = true;
35  }
36  }
37  if(error) {
38  for(std::size_t i = 0; i < _expressions.size(); i++)
39  delete(_expressions[i]);
40  _expressions.clear();
41  expressions.clear();
42  }
43 }
44 
46 {
47  for(std::size_t i = 0; i < _expressions.size(); i++) delete(_expressions[i]);
48 }
49 
50 bool mathEvaluator::eval(const std::vector<double> &values,
51  std::vector<double> &res)
52 {
53  if(values.size() != _variables.size()) {
54  Msg::Error("Given %d value(s) for %d variable(s)", values.size(),
55  _variables.size());
56  return false;
57  }
58 
59  if(res.size() != _expressions.size()) {
60  Msg::Error("Given %d result(s) for %d expression(s)", res.size(),
61  _expressions.size());
62  return false;
63  }
64 
65  for(std::size_t i = 0; i < values.size(); i++) _variables[i] = values[i];
66 
67  for(std::size_t i = 0; i < _expressions.size(); i++) {
68  try {
69  res[i] = _expressions[i]->eval();
70  } catch(smlib::mathex::error &e) {
71  Msg::Error(e.what());
72  double eps = 1.e-20;
73  for(std::size_t j = 0; j < values.size(); j++)
74  _variables[j] = values[j] + eps;
75  try {
76  res[i] = _expressions[i]->eval();
77  } catch(smlib::mathex::error &e2) {
78  Msg::Error(e2.what());
79  return false;
80  }
81  }
82  }
83  return true;
84 }
85 
86 #endif
mathEvaluator.h
mathEvaluator::mathEvaluator
mathEvaluator(std::vector< std::string > &expressions, const std::vector< std::string > &variables)
Definition: mathEvaluator.h:39
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
mathEvaluator::eval
bool eval(const std::vector< double > &values, std::vector< double > &res)
Definition: mathEvaluator.h:47
mathEvaluator::~mathEvaluator
~mathEvaluator()
Definition: mathEvaluator.h:46