OpenSWMM Engine  6.0.0-alpha.3
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.3)
Loading...
Searching...
No Matches
MathExpr.hpp
Go to the documentation of this file.
1
21
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,
39 ADD = 2,
40 SUB = 3,
41 MUL = 4,
42 DIV = 5,
43 POW = 6,
44 NEG = 7,
45 LPAREN = 8,
46 RPAREN = 9,
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,
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
Definition MathExpr.cpp:19
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_ATAN
Definition MathExpr.hpp:57
@ POW
Definition MathExpr.hpp:43
@ FUNC_SIN
Definition MathExpr.hpp:52
@ SUB
Definition MathExpr.hpp:40
@ DIV
Definition MathExpr.hpp:42
@ MUL
Definition MathExpr.hpp:41
@ FUNC_SINH
sinh(x)
Definition MathExpr.hpp:64
@ NUMBER
Definition MathExpr.hpp:37
@ FUNC_COTH
coth(x) = 1/tanh(x)
Definition MathExpr.hpp:67
@ FUNC_TAN
Definition MathExpr.hpp:54
@ LPAREN
Definition MathExpr.hpp:45
@ FUNC_SQRT
Definition MathExpr.hpp:49
@ COMMA
Definition MathExpr.hpp:61
@ FUNC_LOG10
log10(x)
Definition MathExpr.hpp:68
@ NEG
Definition MathExpr.hpp:44
@ FUNC_MIN
Definition MathExpr.hpp:59
@ FUNC_TANH
tanh(x)
Definition MathExpr.hpp:66
@ FUNC_SGN
Definition MathExpr.hpp:48
@ FUNC_ASIN
Definition MathExpr.hpp:55
@ FUNC_MAX
Definition MathExpr.hpp:60
@ FUNC_LOG
Definition MathExpr.hpp:50
@ FUNC_ACOT
acot(x) = atan(1/x)
Definition MathExpr.hpp:69
@ FUNC_STEP
Definition MathExpr.hpp:58
@ ADD
Definition MathExpr.hpp:39
@ FUNC_EXP
Definition MathExpr.hpp:51
@ FUNC_COS
Definition MathExpr.hpp:53
@ VARIABLE
Definition MathExpr.hpp:38
@ FUNC_COT
cot(x) = 1/tan(x)
Definition MathExpr.hpp:63
@ FUNC_ACOS
Definition MathExpr.hpp:56
@ FUNC_ABS
Definition MathExpr.hpp:47
@ RPAREN
Definition MathExpr.hpp:46
@ FUNC_COSH
cosh(x)
Definition MathExpr.hpp:65
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