OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
MathExpr.hpp
Go to the documentation of this file.
1
22#ifndef OPENSWMM_MATHEXPR_HPP
23#define OPENSWMM_MATHEXPR_HPP
24
25#include <string>
26#include <vector>
27#include <functional>
28
29namespace openswmm {
30namespace mathexpr {
31
32// ============================================================================
33// Token types
34// ============================================================================
35
36enum class TokenType : int {
37 NUMBER = 0,
38 VARIABLE = 1,
39 ADD = 2,
40 SUB = 3,
41 MUL = 4,
42 DIV = 5,
43 POW = 6,
44 NEG = 7,
45 LPAREN = 8,
46 RPAREN = 9,
47 FUNC_ABS = 10,
48 FUNC_SGN = 11,
49 FUNC_SQRT = 12,
50 FUNC_LOG = 13,
51 FUNC_EXP = 14,
52 FUNC_SIN = 15,
53 FUNC_COS = 16,
54 FUNC_TAN = 17,
55 FUNC_ASIN = 18,
56 FUNC_ACOS = 19,
57 FUNC_ATAN = 20,
58 FUNC_STEP = 21,
59 FUNC_MIN = 22,
60 FUNC_MAX = 23,
61 COMMA = 24,
62 // Legacy mathexpr.c functions not in original refactor (Gap 49)
63 FUNC_COT = 25,
64 FUNC_SINH = 26,
65 FUNC_COSH = 27,
66 FUNC_TANH = 28,
67 FUNC_COTH = 29,
68 FUNC_LOG10 = 30,
69 FUNC_ACOT = 31
70};
71
72struct Token {
74 double value = 0.0;
75 int var_idx = -1;
76 std::string var_name;
77};
78
79// ============================================================================
80// Compiled expression (postfix token list)
81// ============================================================================
82
83struct Expression {
84 std::vector<Token> postfix;
85 bool valid = false;
86};
87
88// ============================================================================
89// Parser (Shunting-yard)
90// ============================================================================
91
99int parse(const std::string& expr_str, Expression& result);
100
101// ============================================================================
102// Variable binding (Tier 1 optimization)
103// ============================================================================
104
117int bind_variables(Expression& expr,
118 const char* const* name_table, int n_vars);
119
125int compute_max_stack_depth(const Expression& expr);
126
127// ============================================================================
128// Evaluator
129// ============================================================================
130
138double evaluate(const Expression& expr,
139 const std::function<double(const std::string&)>& var_lookup);
140
149double evaluate(const Expression& expr, const double* vars, int n_vars);
150
162double evaluate_fast(const Expression& expr, const double* vars) noexcept;
163
164} // namespace mathexpr
165} // namespace openswmm
166
167#endif // OPENSWMM_MATHEXPR_HPP
int compute_max_stack_depth(const Expression &expr)
Compute the maximum stack depth needed to evaluate an expression.
Definition MathExpr.cpp:313
double evaluate_fast(const Expression &expr, const double *vars) noexcept
Fast evaluate using pre-bound variable indices and stack-free evaluation.
Definition MathExpr.cpp:339
int parse(const std::string &expr_str, Expression &result)
Parse an infix expression string into a postfix Expression.
Definition MathExpr.cpp:132
TokenType
Definition MathExpr.hpp:36
@ FUNC_COTH
coth(x) = 1/tanh(x)
@ FUNC_ACOT
acot(x) = atan(1/x)
@ FUNC_COT
cot(x) = 1/tan(x)
int bind_variables(Expression &expr, const char *const *name_table, int n_vars)
Bind variable names in a compiled expression to integer indices.
Definition MathExpr.cpp:284
double evaluate(const Expression &expr, const std::function< double(const std::string &)> &var_lookup)
Evaluate a compiled expression with named variable lookup.
Definition MathExpr.cpp:187
Definition NodeCoupling.cpp:15
Definition MathExpr.hpp:83
std::vector< Token > postfix
Postfix (RPN) token sequence.
Definition MathExpr.hpp:84
bool valid
Definition MathExpr.hpp:85
Definition MathExpr.hpp:72
TokenType type
Definition MathExpr.hpp:73
std::string var_name
For VARIABLE tokens (name for lookup)
Definition MathExpr.hpp:76
int var_idx
For VARIABLE tokens (index into variable table)
Definition MathExpr.hpp:75
double value
For NUMBER tokens.
Definition MathExpr.hpp:74