philsupertramp/game-math
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | List of all members
Equation Class Reference

#include <Equation.h>

Public Member Functions

 Equation ()
 
 Equation (const char *val)
 
 Equation (const std::string &val)
 
int GetDegree (const std::shared_ptr< MathNode > &node)
 
void Print (std::ostream &ostr=std::cout)
 
template<typename... VArgs>
double operator() (VArgs... args)
 
double operator() ()
 
template<typename... VArgs>
void SetSymbols (const int &index, double val, VArgs... args)
 
void SetSymbols (const int &index)
 
void SetSymbols (const std::vector< double > &values)
 
std::string GetString () const
 
void PrintTree () const
 
void PrintNode (const std::shared_ptr< MathNode > &node, std::vector< std::vector< std::string > > &levels, const size_t &row, const size_t &column) const
 
size_t GetDepth (const std::shared_ptr< MathNode > &node, size_t &current_depth) const
 
void PrintTree (const std::shared_ptr< MathNode > &node, int &level, std::vector< std::string > &tree) const
 
void Simplify ()
 

Static Public Member Functions

static Equation Chain (const Equation &left, const Equation &right, const std::shared_ptr< Operator > &op)
 

Public Attributes

std::vector< std::shared_ptr< Symbolic > > symbols
 storage for containing symbols More...
 
std::shared_ptr< MathNodebaseNode = nullptr
 Holds the base node of the abstract syntax tree. More...
 
int degree = 1
 degree of equation. Linear equation = 1, quadratic = 2, ... More...
 

Private Member Functions

std::shared_ptr< MathNodeSimplifyTree (const std::shared_ptr< MathNode > &node) const
 
void resolveOP (std::shared_ptr< MathNode > &out, const std::shared_ptr< Operator > &op) const
 
std::shared_ptr< MathNodesimplifyOP (const std::shared_ptr< MathNode > &node) const
 
std::shared_ptr< MathNodeApplyOperator (const std::shared_ptr< MathNode > &node, const std::shared_ptr< Operator > &op, const double &val, bool isLeft) const
 

Detailed Description

Representation of mathematical statement

Examples
symb/TestSymbolic.cpp.

Constructor & Destructor Documentation

◆ Equation() [1/3]

Equation::Equation ( )
inline

empty default constructor

◆ Equation() [2/3]

Equation::Equation ( const char *  val)
inlineexplicit

default constructor for char* representation

Parameters
val

◆ Equation() [3/3]

Equation::Equation ( const std::string &  val)
inlineexplicit

default constructor for string representation

Parameters
val

Member Function Documentation

◆ ApplyOperator()

std::shared_ptr< MathNode > Equation::ApplyOperator ( const std::shared_ptr< MathNode > &  node,
const std::shared_ptr< Operator > &  op,
const double &  val,
bool  isLeft 
) const
inlineprivate

Applies numeric operation op like if isLeft: node (op) val else: val (op) node

Parameters
nodetarget to apply op on
opoperator to apply
valnumerical value to apply
isLeftsignalize node is left side of OP
Returns
evaluated operator node

◆ Chain()

static Equation Equation::Chain ( const Equation left,
const Equation right,
const std::shared_ptr< Operator > &  op 
)
inlinestatic

Connects left and right using op, this essentially takes both sides baseNode and connects both trees, then returns a new equation object. Dude! It even generates a unique set of symbols!

Parameters
leftleft equation
rightright equation
opoperator to connect with
Returns
chained equation object
Examples
symb/TestSymbolic.cpp.

◆ GetDegree()

int Equation::GetDegree ( const std::shared_ptr< MathNode > &  node)
inline

◆ GetDepth()

size_t Equation::GetDepth ( const std::shared_ptr< MathNode > &  node,
size_t &  current_depth 
) const
inline

Getter for max AST depth

Parameters
nodeto check depth of
current_depthcounter for current depth level
Returns
current depth if last node in branch, else max of left depth and right depth

◆ GetString()

std::string Equation::GetString ( ) const
inline

Helper to recreate string representation based on tree

Returns
Examples
symb/TestSymbolic.cpp.

◆ operator()() [1/2]

double Equation::operator() ( )
inline

Evaluate equation with preset values

Returns

◆ operator()() [2/2]

template<typename... VArgs>
double Equation::operator() ( VArgs...  args)
inline

var arg method to evaluate equation using variable amount of symbols, yet if called with parameters, all parameters need to be present.

Needs to be called with number symbols as number parameters

Template Parameters
VArgsvariable amount of arguments
Parameters
argsarguments passed for symbols, with index representing index of symbol in symbols
Returns
Evaluation of equation

◆ Print()

void Equation::Print ( std::ostream &  ostr = std::cout)
inline

Prints equation into std::cout

Examples
symb/TestSymbolic.cpp.

◆ PrintNode()

void Equation::PrintNode ( const std::shared_ptr< MathNode > &  node,
std::vector< std::vector< std::string > > &  levels,
const size_t &  row,
const size_t &  column 
) const
inline

Generates left aligned string representation of node in tree leave form according to their connection types

Parameters
node

◆ PrintTree() [1/2]

void Equation::PrintTree ( ) const
inline

Helper method to display ASTree.

Add padding to levels, count empty elements on right side, then append them on the left side

◆ PrintTree() [2/2]

void Equation::PrintTree ( const std::shared_ptr< MathNode > &  node,
int &  level,
std::vector< std::string > &  tree 
) const
inline

Add representation string into tree of passed node

Parameters
nodethe current node to process
levelcurrent depth level
treetree representation as array of lines, each line represents a depth level

◆ resolveOP()

void Equation::resolveOP ( std::shared_ptr< MathNode > &  out,
const std::shared_ptr< Operator > &  op 
) const
inlineprivate

resolves operator scope as far as possible

Parameters
outoutput node
opoperator scope to resolve

◆ SetSymbols() [1/3]

void Equation::SetSymbols ( const int &  index)
inline

empty method to stop va arg recursion

Parameters
index

◆ SetSymbols() [2/3]

template<typename... VArgs>
void Equation::SetSymbols ( const int &  index,
double  val,
VArgs...  args 
)
inline

var arg method to recursively set current values of symbols

Template Parameters
VArgsvariable amount of arguments
Parameters
indexcurrent index of symbol
valvalue for symbol with index index
argsfollowing arguments

◆ SetSymbols() [3/3]

void Equation::SetSymbols ( const std::vector< double > &  values)
inline

Helper shortcut to check for existence of symbolic within equation setter for evaluation values for symbolic nodes using a vector of values

Parameters
valuesvector containing values for symbols

◆ Simplify()

void Equation::Simplify ( )
inline

Simplifies an equation in place according to mathematical rules.

Examples
symb/TestSymbolic.cpp.

◆ simplifyOP()

std::shared_ptr< MathNode > Equation::simplifyOP ( const std::shared_ptr< MathNode > &  node) const
inlineprivate

Simplifies operator scope

Within a scope the following rules are applied:

  • Functions: Functions with numeric values as left hand nodes can be simplified to numeric nodes
  • Operators: Numeric values can be combined.
Parameters
nodeoperator node (-tree) to simplify
Returns
simplified tree with [num_nodes_in >= num_nodes_out]

◆ SimplifyTree()

std::shared_ptr< MathNode > Equation::SimplifyTree ( const std::shared_ptr< MathNode > &  node) const
inlineprivate

Simplify equation tree given by node

Parameters
nodetree to simplify
Returns
simplified tree

Member Data Documentation

◆ baseNode

std::shared_ptr<MathNode> Equation::baseNode = nullptr

Holds the base node of the abstract syntax tree.

Examples
symb/TestSymbolic.cpp.

◆ degree

int Equation::degree = 1

degree of equation. Linear equation = 1, quadratic = 2, ...

Examples
symb/TestSymbolic.cpp.

◆ symbols

std::vector<std::shared_ptr<Symbolic> > Equation::symbols

storage for containing symbols


The documentation for this class was generated from the following file: