16#include <unordered_set>
33 std::vector<std::shared_ptr<Symbolic>>
symbols;
68 int GetDegree(
const std::shared_ptr<MathNode>& node) {
69 if(node ==
nullptr)
return 0;
72 if(node->value[0] ==
'^' && node->left->type ==
NodeType_Symbolic) {
return (
int)node->right->Evaluate(); }
75 return left > right ? left : right;
83 void Print(std::ostream& ostr = std::cout) {
98 template<
typename... VArgs>
100 assert(
sizeof...(args) ==
symbols.size());
117 template<
typename... VArgs>
118 void SetSymbols(
const int& index,
double val, VArgs... args) {
119 symbols[index]->evaluationValue = val;
135 assert(values.size() ==
symbols.size());
137 for(
size_t i = 0; i <
symbols.size(); ++i) {
symbols[i]->evaluationValue = values[i]; }
152 std::vector<std::vector<std::string>> levels(elems, std::vector<std::string>(elems));
156 std::vector<std::vector<std::string>> realLevels(elems, std::vector<std::string>(elems * 2));
161 std::vector<size_t> padding(elems);
163 for(
const auto& line : levels) {
164 for(
const auto& elem : line) {
165 if(elem ==
" " || elem.empty()) {
166 if(eol) { padding[index]++; }
175 for(
size_t paddIndex = 0; paddIndex < elems; ++paddIndex) {
176 for(index = 0; index < elems + padding[paddIndex]; ++index) {
177 if(index < padding[paddIndex]) {
180 std::cout << levels[paddIndex][index - padding[paddIndex]];
183 std::cout << std::endl;
192 const std::shared_ptr<MathNode>& node,
193 std::vector<std::vector<std::string>>& levels,
195 const size_t& column)
const {
196 switch(node->connectionType) {
197 case NodeConnectionType::ConnectionType_Dual:
203 levels[row][column] +=
" ";
204 levels[row][column + 1] += node->value;
205 levels[row][column + 2] +=
" ";
206 levels[row + 1][column] +=
" /";
207 levels[row + 1][column + 2] +=
" \\";
208 auto childIndex = row + 2;
209 levels[childIndex][column + 1] +=
" ";
210 PrintNode(node->left, levels, childIndex, column);
211 PrintNode(node->right, levels, childIndex, column + 2);
212 levels[childIndex][column + 2] +=
" ";
215 case NodeConnectionType::ConnectionType_Left:
221 levels[row][column] +=
" ";
222 levels[row][column + 1] += node->value;
223 levels[row][column + 2] +=
" ";
224 levels[row + 1][column] +=
" /";
225 levels[row + 1][column + 2] +=
" ";
226 auto childIndex = row + 2;
227 levels[childIndex][column + 1] +=
" ";
228 levels[childIndex][column + 2] +=
" ";
229 PrintNode(node->left, levels, childIndex, column);
232 case NodeConnectionType::ConnectionType_Right:
238 levels[row][column] +=
" ";
239 levels[row][column + 1] += node->value;
240 levels[row][column + 2] +=
" ";
241 levels[row + 1][column] +=
" ";
242 levels[row + 1][column + 2] +=
" \\";
243 auto childIndex = row + 2;
244 levels[childIndex][column] +=
" ";
245 levels[childIndex][column + 1] +=
" ";
246 PrintNode(node->right, levels, childIndex, column + 2);
247 levels[childIndex][column + 2] +=
" ";
250 case NodeConnectionType::ConnectionType_None:
253 levels[row][column] += std::string(
" ") + node->value;
256 case NodeConnectionType::ConnectionType_Unknown:
267 size_t GetDepth(
const std::shared_ptr<MathNode>& node,
size_t& current_depth)
const {
269 auto leftDep = std::numeric_limits<size_t>::min();
270 auto rightDep = std::numeric_limits<size_t>::min();
271 if(node->left) { leftDep =
GetDepth(node->left, current_depth); }
272 if(node->right) { rightDep =
GetDepth(node->right, current_depth); }
274 return std::max(std::max(leftDep, current_depth), rightDep);
283 void PrintTree(
const std::shared_ptr<MathNode>& node,
int& level, std::vector<std::string>& tree)
const {
285 tree[level] += std::string(node->value);
337 [[nodiscard]] std::shared_ptr<MathNode>
SimplifyTree(
const std::shared_ptr<MathNode>& node)
const {
362 void resolveOP(std::shared_ptr<MathNode>& out,
const std::shared_ptr<Operator>& op)
const {
365 auto rightNode = out->right;
366 while(rightNode !=
nullptr) {
368 out =
ApplyOperator(rightNode, op, out->left->Evaluate(),
true);
371 rightNode = rightNode->left;
375 auto leftNode = out->left;
377 while(leftNode !=
nullptr) {
379 parent->right =
ApplyOperator(leftNode, op, out->right->Evaluate(),
false);
384 leftNode = leftNode->right;
403 [[nodiscard]] std::shared_ptr<MathNode>
simplifyOP(
const std::shared_ptr<MathNode>& node)
const {
404 std::shared_ptr<MathNode> nodeOut = node;
407 return std::make_shared<Number>(std::to_string(nodeOut->Evaluate()));
419 assert(nodeOut->right !=
nullptr);
423 return std::make_shared<Number>(std::to_string(nodeOut->Evaluate()));
448 const std::shared_ptr<MathNode>& node,
const std::shared_ptr<Operator>& op,
const double& val,
bool isLeft)
const {
450 switch(out->connectionType) {
452 if(isLeft) out->left =
ApplyOperator(out->left, op, val,
true);
457 if(isLeft) out->left =
ApplyOperator(out->left, op, val,
true);
460 if(!isLeft) out->right =
ApplyOperator(out->right, op, val,
false);
465 if(isLeft)
return std::make_shared<Number>(std::to_string(op->op(out->Evaluate(), val)));
467 return std::make_shared<Number>(std::to_string(op->op(val, out->Evaluate())));
@ ConnectionType_Right
Definition: MathNode.h:26
@ ConnectionType_Dual
Definition: MathNode.h:24
@ ConnectionType_None
Definition: MathNode.h:27
@ ConnectionType_Unknown
Definition: MathNode.h:28
@ ConnectionType_Left
Definition: MathNode.h:25
@ NodeType_Operator
Definition: MathNode.h:13
@ NodeType_Symbolic
Definition: MathNode.h:14
@ NodeType_Any
Definition: MathNode.h:19
@ NodeType_Functional
Definition: MathNode.h:17
@ NodeType_DefaultSymbol
Definition: MathNode.h:18
@ NodeType_Operator_or_Parentheses
Definition: MathNode.h:20
@ NodeType_Parentheses
Definition: MathNode.h:16
@ NodeType_Numeric
Definition: MathNode.h:15
@ OPClassLine
Definition: Operator.h:17
@ OPClassDot
Definition: Operator.h:18
Definition: EquationParser.h:15
std::shared_ptr< MathNode > createAST()
std::vector< std::shared_ptr< Symbolic > > symbols
storage for symbols, gets cleared on EquationParser::createAST
Definition: EquationParser.h:31
static std::shared_ptr< Operator > GetOperator(const std::string &valString)
Definition: EquationParser.h:80
static std::vector< std::shared_ptr< Symbolic > > buildSymbolSuperSet(const std::vector< std::shared_ptr< Symbolic > > &a, const std::vector< std::shared_ptr< Symbolic > > &b)
Definition: EquationParser.h:45
Definition: Equation.h:30
void Simplify()
Definition: Equation.h:317
double operator()()
Definition: Equation.h:108
void SetSymbols(const int &index, double val, VArgs... args)
Definition: Equation.h:118
std::shared_ptr< MathNode > SimplifyTree(const std::shared_ptr< MathNode > &node) const
Definition: Equation.h:337
std::vector< std::shared_ptr< Symbolic > > symbols
storage for containing symbols
Definition: Equation.h:33
Equation(const char *val)
Definition: Equation.h:50
Equation(const std::string &val)
Definition: Equation.h:61
void SetSymbols(const int &index)
Definition: Equation.h:126
std::string GetString() const
Definition: Equation.h:144
void SetSymbols(const std::vector< double > &values)
Definition: Equation.h:134
Equation()
Definition: Equation.h:44
std::shared_ptr< MathNode > simplifyOP(const std::shared_ptr< MathNode > &node) const
Definition: Equation.h:403
void PrintTree(const std::shared_ptr< MathNode > &node, int &level, std::vector< std::string > &tree) const
Definition: Equation.h:283
void resolveOP(std::shared_ptr< MathNode > &out, const std::shared_ptr< Operator > &op) const
Definition: Equation.h:362
void Print(std::ostream &ostr=std::cout)
Definition: Equation.h:83
static Equation Chain(const Equation &left, const Equation &right, const std::shared_ptr< Operator > &op)
Definition: Equation.h:305
int degree
degree of equation. Linear equation = 1, quadratic = 2, ...
Definition: Equation.h:39
void PrintTree() const
Definition: Equation.h:149
std::shared_ptr< MathNode > ApplyOperator(const std::shared_ptr< MathNode > &node, const std::shared_ptr< Operator > &op, const double &val, bool isLeft) const
Definition: Equation.h:447
void PrintNode(const std::shared_ptr< MathNode > &node, std::vector< std::vector< std::string > > &levels, const size_t &row, const size_t &column) const
Definition: Equation.h:191
std::shared_ptr< MathNode > baseNode
Holds the base node of the abstract syntax tree.
Definition: Equation.h:36
size_t GetDepth(const std::shared_ptr< MathNode > &node, size_t ¤t_depth) const
Definition: Equation.h:267
double operator()(VArgs... args)
Definition: Equation.h:99
int GetDegree(const std::shared_ptr< MathNode > &node)
Definition: Equation.h:68