OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
MathExpr.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2//
3// Copyright 2026 Caleb Buahin
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
37
38#ifndef OPENSWMM_MATHEXPR_HPP
39#define OPENSWMM_MATHEXPR_HPP
40
41#include <string>
42#include <vector>
43#include <functional>
44
45namespace openswmm {
46namespace mathexpr {
47
48// ============================================================================
49// Token types
50// ============================================================================
51
52enum class TokenType : int {
53 NUMBER = 0,
55 ADD = 2,
56 SUB = 3,
57 MUL = 4,
58 DIV = 5,
59 POW = 6,
60 NEG = 7,
61 LPAREN = 8,
62 RPAREN = 9,
77 COMMA = 24,
78 // Legacy mathexpr.c functions not in original refactor (Gap 49)
79 FUNC_COT = 25,
80 FUNC_SINH = 26,
81 FUNC_COSH = 27,
82 FUNC_TANH = 28,
83 FUNC_COTH = 29,
86};
87
88struct Token {
90 double value = 0.0;
91 int var_idx = -1;
92 std::string var_name;
93};
94
95// ============================================================================
96// Compiled expression (postfix token list)
97// ============================================================================
98
99struct Expression {
100 std::vector<Token> postfix;
101 bool valid = false;
102};
103
104// ============================================================================
105// Parser (Shunting-yard)
106// ============================================================================
107
115int parse(const std::string& expr_str, Expression& result);
116
126const std::vector<std::string>& function_names();
127
128// ============================================================================
129// Variable binding (Tier 1 optimization)
130// ============================================================================
131
144int bind_variables(Expression& expr,
145 const char* const* name_table, int n_vars);
146
152int compute_max_stack_depth(const Expression& expr);
153
154// ============================================================================
155// Evaluator
156// ============================================================================
157
165double evaluate(const Expression& expr,
166 const std::function<double(const std::string&)>& var_lookup);
167
176double evaluate(const Expression& expr, const double* vars, int n_vars);
177
189double evaluate_fast(const Expression& expr, const double* vars) noexcept;
190
191} // namespace mathexpr
192} // namespace openswmm
193
194#endif // OPENSWMM_MATHEXPR_HPP
Definition MathExpr.cpp:35
int compute_max_stack_depth(const Expression &expr)
Compute the maximum stack depth needed to evaluate an expression.
Definition MathExpr.cpp:344
double evaluate_fast(const Expression &expr, const double *vars) noexcept
Fast evaluate using pre-bound variable indices and stack-free evaluation.
Definition MathExpr.cpp:370
int parse(const std::string &expr_str, Expression &result)
Parse an infix expression string into a postfix Expression.
Definition MathExpr.cpp:163
const std::vector< std::string > & function_names()
Names of the built-in functions the tokenizer recognises.
Definition MathExpr.cpp:83
TokenType
Definition MathExpr.hpp:52
@ FUNC_ATAN
Definition MathExpr.hpp:73
@ POW
Definition MathExpr.hpp:59
@ FUNC_SIN
Definition MathExpr.hpp:68
@ SUB
Definition MathExpr.hpp:56
@ DIV
Definition MathExpr.hpp:58
@ MUL
Definition MathExpr.hpp:57
@ FUNC_SINH
sinh(x)
Definition MathExpr.hpp:80
@ NUMBER
Definition MathExpr.hpp:53
@ FUNC_COTH
coth(x) = 1/tanh(x)
Definition MathExpr.hpp:83
@ FUNC_TAN
Definition MathExpr.hpp:70
@ LPAREN
Definition MathExpr.hpp:61
@ FUNC_SQRT
Definition MathExpr.hpp:65
@ COMMA
Definition MathExpr.hpp:77
@ FUNC_LOG10
log10(x)
Definition MathExpr.hpp:84
@ NEG
Definition MathExpr.hpp:60
@ FUNC_MIN
Definition MathExpr.hpp:75
@ FUNC_TANH
tanh(x)
Definition MathExpr.hpp:82
@ FUNC_SGN
Definition MathExpr.hpp:64
@ FUNC_ASIN
Definition MathExpr.hpp:71
@ FUNC_MAX
Definition MathExpr.hpp:76
@ FUNC_LOG
Definition MathExpr.hpp:66
@ FUNC_ACOT
acot(x) = atan(1/x)
Definition MathExpr.hpp:85
@ FUNC_STEP
Definition MathExpr.hpp:74
@ ADD
Definition MathExpr.hpp:55
@ FUNC_EXP
Definition MathExpr.hpp:67
@ FUNC_COS
Definition MathExpr.hpp:69
@ VARIABLE
Definition MathExpr.hpp:54
@ FUNC_COT
cot(x) = 1/tan(x)
Definition MathExpr.hpp:79
@ FUNC_ACOS
Definition MathExpr.hpp:72
@ FUNC_ABS
Definition MathExpr.hpp:63
@ RPAREN
Definition MathExpr.hpp:62
@ FUNC_COSH
cosh(x)
Definition MathExpr.hpp:81
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:315
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:218
Definition NodeCoupling.cpp:16
Definition MathExpr.hpp:99
std::vector< Token > postfix
Postfix (RPN) token sequence.
Definition MathExpr.hpp:100
bool valid
Definition MathExpr.hpp:101
Definition MathExpr.hpp:88
TokenType type
Definition MathExpr.hpp:89
std::string var_name
For VARIABLE tokens (name for lookup)
Definition MathExpr.hpp:92
int var_idx
For VARIABLE tokens (index into variable table)
Definition MathExpr.hpp:91
double value
For NUMBER tokens.
Definition MathExpr.hpp:90