File: mathEvaluator.cpp

package info (click to toggle)
gmsh 4.8.4%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,812 kB
  • sloc: cpp: 378,014; ansic: 99,669; yacc: 7,216; python: 6,680; java: 3,486; lisp: 659; lex: 621; perl: 571; makefile: 470; sh: 440; xml: 415; javascript: 113; pascal: 35; modula3: 32
file content (86 lines) | stat: -rw-r--r-- 2,508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.

#include "mathEvaluator.h"

#if defined(HAVE_MATHEX)

mathEvaluator::mathEvaluator(std::vector<std::string> &expressions,
                             const std::vector<std::string> &variables)
{
  static std::string lastError;

  _expressions.resize(expressions.size());
  _variables.resize(variables.size(), 0.);
  bool error = false;
  for(std::size_t i = 0; i < expressions.size(); i++) {
    _expressions[i] = new smlib::mathex();
    for(std::size_t j = 0; j < variables.size(); j++)
      _expressions[i]->addvar(variables[j], &_variables[j]);
    try {
      _expressions[i]->expression(expressions[i]);
      _expressions[i]->parse();
    } catch(smlib::mathex::error &e) {
      if(e.what() + expressions[i] != lastError) {
        lastError = e.what() + expressions[i];
        Msg::Error(e.what());
        std::string pos(_expressions[i]->stopposition(), ' ');
        pos.push_back('^');
        Msg::Error(expressions[i].c_str());
        Msg::Error(pos.c_str());
      }
      error = true;
    }
  }
  if(error) {
    for(std::size_t i = 0; i < _expressions.size(); i++)
      delete(_expressions[i]);
    _expressions.clear();
    expressions.clear();
  }
}

mathEvaluator::~mathEvaluator()
{
  for(std::size_t i = 0; i < _expressions.size(); i++) delete(_expressions[i]);
}

bool mathEvaluator::eval(const std::vector<double> &values,
                         std::vector<double> &res)
{
  if(values.size() != _variables.size()) {
    Msg::Error("Given %d value(s) for %d variable(s)", values.size(),
               _variables.size());
    return false;
  }

  if(res.size() != _expressions.size()) {
    Msg::Error("Given %d result(s) for %d expression(s)", res.size(),
               _expressions.size());
    return false;
  }

  for(std::size_t i = 0; i < values.size(); i++) _variables[i] = values[i];

  for(std::size_t i = 0; i < _expressions.size(); i++) {
    try {
      res[i] = _expressions[i]->eval();
    } catch(smlib::mathex::error &e) {
      Msg::Error(e.what());
      double eps = 1.e-20;
      for(std::size_t j = 0; j < values.size(); j++)
        _variables[j] = values[j] + eps;
      try {
        res[i] = _expressions[i]->eval();
      } catch(smlib::mathex::error &e2) {
        Msg::Error(e2.what());
        return false;
      }
    }
  }
  return true;
}

#endif