aGrUM  0.15.0
gum::FormulaPart Class Reference

Represents part of a formula. More...

#include <agrum/core/math/formula.h>

+ Collaboration diagram for gum::FormulaPart:

Public Attributes

token_type type
 The token_type stored by this gum::FormulaPart. More...
 
double number
 The value stored by this gum::FormulaPart. More...
 
char character
 The value stored by this gum::FormulaPart. More...
 
token_function function
 The value stored by this gum::FormulaPart. More...
 

Public Member Functions

Constructors and destructor
 FormulaPart ()
 Class constructor. More...
 
 FormulaPart (token_type t, double n)
 Constructor for doubles. More...
 
 FormulaPart (token_type t, char c)
 Constructor for chars. More...
 
 FormulaPart (token_type t, token_function func)
 Constructor for functions. More...
 
 FormulaPart (const FormulaPart &source)
 Copy constructor. More...
 
 FormulaPart (FormulaPart &&source)
 Move constructor. More...
 
 ~FormulaPart ()
 Class destuctor. More...
 
source The gum::FormulaPart to copy.

Move operator.

Returns
Returns this gum::FormulaPart.
FormulaPartoperator= (const FormulaPart &source)
 
FormulaPartoperator= (FormulaPart &&source)
 
Getters and setters
std::string str () const
 Returns a string representation of this gum::FormulaPart value. More...
 
bool isLeftAssociative () const
 Returns true if this gum::FormulaPart is left associative. More...
 
bool isRightAssociative () const
 Returns true if this gum::FormulaPart is right associative. More...
 
int precedence () const
 Returns the precedence priority of the value stored in this gum::FormulaPart. More...
 
size_t argc () const
 Returns the number of argument of the function stored in this gum::FormulaPart. More...
 
FormulaPart eval (const std::vector< FormulaPart > &args) const
 Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart. More...
 

Public Types

enum  token_type {
  NUMBER, OPERATOR, PARENTHESIS, NIL,
  FUNCTION, ARG_SEP
}
 The tokens constituting a formula. More...
 
enum  token_function {
  exp, log, ln, pow,
  sqrt, nil
}
 The functions allowed in a formula. More...
 

Detailed Description

Represents part of a formula.

This class is used by the gum::Formula class to store intermediate results when solving the formula using the Shuntin-yard algorithm.

Definition at line 60 of file formula.h.

Member Enumeration Documentation

◆ token_function

The functions allowed in a formula.

Enumerator
exp 
log 
ln 
pow 
sqrt 
nil 

Definition at line 66 of file formula.h.

◆ token_type

The tokens constituting a formula.

Enumerator
NUMBER 
OPERATOR 
PARENTHESIS 
NIL 
FUNCTION 
ARG_SEP 

Definition at line 63 of file formula.h.

Constructor & Destructor Documentation

◆ FormulaPart() [1/6]

gum::FormulaPart::FormulaPart ( )

Class constructor.

Definition at line 86 of file formula.cpp.

Referenced by gum::Formula::__push_comma(), gum::Formula::__push_rightParenthesis(), and eval().

86  :
87  type(token_type::NIL), number(NAN), character('\0'), function(nil) {
88  GUM_CONSTRUCTOR(FormulaPart);
89  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
+ Here is the caller graph for this function:

◆ FormulaPart() [2/6]

gum::FormulaPart::FormulaPart ( token_type  t,
double  n 
)

Constructor for doubles.

Parameters
tThe token_type of this gum::FormulaPart.
nThe value of this gum::FormulaPart.

Definition at line 91 of file formula.cpp.

91  :
92  type(t), number(n), character('\0'), function(nil) {
93  GUM_CONSTRUCTOR(FormulaPart);
94  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ FormulaPart() [3/6]

gum::FormulaPart::FormulaPart ( token_type  t,
char  c 
)

Constructor for chars.

Parameters
tThe token_type of this gum::FormulaPart.
cThe value of this gum::FormulaPart.

Definition at line 96 of file formula.cpp.

96  :
97  type(t), number(NAN), character(c), function(nil) {
98  GUM_CONSTRUCTOR(FormulaPart);
99  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ FormulaPart() [4/6]

gum::FormulaPart::FormulaPart ( token_type  t,
token_function  func 
)

Constructor for functions.

Parameters
tThe token_type of this gum::FormulaPart.
funcThe value of this gum::FormulaPart.

Definition at line 101 of file formula.cpp.

101  :
102  type(t), number(NAN), character('\0'), function(func) {
103  GUM_CONSTRUCTOR(FormulaPart);
104  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ FormulaPart() [5/6]

gum::FormulaPart::FormulaPart ( const FormulaPart source)

Copy constructor.

Parameters
sourceThe gum::FormulaPart to copy.

Definition at line 106 of file formula.cpp.

106  :
107  type(source.type), number(source.number), character(source.character),
108  function(source.function) {
109  GUM_CONS_CPY(FormulaPart);
110  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ FormulaPart() [6/6]

gum::FormulaPart::FormulaPart ( FormulaPart &&  source)

Move constructor.

Parameters
sourceThe gum::FormulaPart to move.

Definition at line 112 of file formula.cpp.

112  :
113  type(std::move(source.type)), number(std::move(source.number)),
114  character(std::move(source.character)),
115  function(std::move(source.function)) {
116  GUM_CONS_MOV(FormulaPart);
117  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
FormulaPart()
Class constructor.
Definition: formula.cpp:86
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ ~FormulaPart()

gum::FormulaPart::~FormulaPart ( )

Class destuctor.

Definition at line 119 of file formula.cpp.

119 { GUM_DESTRUCTOR(FormulaPart); }
FormulaPart()
Class constructor.
Definition: formula.cpp:86

Member Function Documentation

◆ __function_argc()

INLINE size_t gum::FormulaPart::__function_argc ( ) const
private

Returns the number of arguments expected by the function stored in this gum::FormulaPart.

Returns
Returns the number of arguments expected by the function stored in this gum::FormulaPart.

Definition at line 132 of file formula_inl.h.

References GUM_ERROR.

Referenced by argc().

132  {
133  switch (function) {
134  case FormulaPart::token_function::exp: {
135  return 1;
136  }
137  case FormulaPart::token_function::log: {
138  return 1;
139  }
140  case FormulaPart::token_function::ln: {
141  return 1;
142  }
143  case FormulaPart::token_function::pow: {
144  return 2;
145  }
146  case FormulaPart::token_function::sqrt: {
147  return 1;
148  }
149  // case FormulaPart::token_function::nil: { return "nil"; }
150  default: {
151  GUM_ERROR(OperationNotAllowed, "unknown function");
152  }
153  }
154  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ __function_eval()

INLINE double gum::FormulaPart::__function_eval ( const std::vector< FormulaPart > &  args) const
private

Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.

Args are backwards !

Warning
Args must be backwards !
Parameters
argsThe arguments, in backards, passed to the value stored in this gum::FormulaPart.
Returns
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.
Exceptions
OperationNotAllowedRaised if the value stored is not a function.

Definition at line 194 of file formula_inl.h.

References GUM_ERROR, and number.

Referenced by eval().

194  {
195  switch (function) {
196  case FormulaPart::token_function::exp: {
197  return std::exp(args[0].number);
198  }
199  case FormulaPart::token_function::log: {
200  return std::log(args[0].number);
201  }
202  case FormulaPart::token_function::ln: {
203  return std::log2(args[0].number);
204  }
205  case FormulaPart::token_function::pow: {
206  return std::pow(args[1].number, args[0].number);
207  }
208  case FormulaPart::token_function::sqrt: {
209  return std::sqrt(args[0].number);
210  }
211  // case FormulaPart::token_function::nil: { return "nil"; }
212  default: {
213  GUM_ERROR(OperationNotAllowed, "unknown function");
214  }
215  }
216  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ __operator_argc()

INLINE size_t gum::FormulaPart::__operator_argc ( ) const
private

Returns the number of arguments expected by the operator stored in this gum::FormulaPart.

Returns
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.

Definition at line 112 of file formula_inl.h.

References character, and GUM_ERROR.

Referenced by argc().

112  {
113  switch (character) {
114  case '_': {
115  return (size_t)1;
116  }
117  case '+':
118  case '-':
119  case '*':
120  case '/':
121  case '^': {
122  return (size_t)2;
123  }
124 
125  default: {
126  GUM_ERROR(OperationNotAllowed, "C - not an operator");
127  }
128  }
129  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ __operator_eval()

INLINE double gum::FormulaPart::__operator_eval ( const std::vector< FormulaPart > &  args) const
private

Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.

Args are backwards !

Warning
Args must be backwards !
Parameters
argsThe arguments, in backards, passed to the value stored in this gum::FormulaPart.
Returns
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.
Exceptions
OperationNotAllowedRaised if the value stored is not an operator.

Definition at line 159 of file formula_inl.h.

References character, GUM_ERROR, and number.

Referenced by eval().

159  {
160  switch (character) {
161  case '+': {
162  return args[1].number + args[0].number;
163  }
164 
165  case '-': {
166  return args[1].number - args[0].number;
167  }
168 
169  case '*': {
170  return args[1].number * args[0].number;
171  }
172 
173  case '/': {
174  return args[1].number / args[0].number;
175  }
176 
177  case '^': {
178  return std::pow(args[1].number, args[0].number);
179  }
180 
181  case '_': {
182  return 0 - args[0].number;
183  }
184 
185  default: {
186  GUM_ERROR(OperationNotAllowed, "D - not an operator");
187  }
188  }
189  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ argc()

INLINE size_t gum::FormulaPart::argc ( ) const

Returns the number of argument of the function stored in this gum::FormulaPart.

Returns
Returns the number of argument of the function stored in this gum::FormulaPart.
Exceptions
OperationNotAllowedRaised if the value stored is not a function.

Definition at line 95 of file formula_inl.h.

References __function_argc(), __operator_argc(), FUNCTION, GUM_ERROR, OPERATOR, and type.

Referenced by gum::Formula::__reduceOperatorOrFunction().

95  {
96  switch (type) {
97  case OPERATOR: {
98  return __operator_argc();
99  }
100 
101  case FUNCTION: {
102  return __function_argc();
103  }
104 
105  default: {
106  GUM_ERROR(OperationNotAllowed, "expecting a function or an operator");
107  }
108  }
109  }
size_t __function_argc() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition: formula_inl.h:132
size_t __operator_argc() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
Definition: formula_inl.h:112
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eval()

INLINE FormulaPart gum::FormulaPart::eval ( const std::vector< FormulaPart > &  args) const

Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.

Args are backwards !

Warning
Args must be backwards !
Parameters
argsThe arguments, in backards, passed to the value stored in this gum::FormulaPart.
Returns
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum::FormulaPart.
Exceptions
OperationNotAllowedRaised if the value stored is neither a function nor an operator.

Definition at line 220 of file formula_inl.h.

References __function_eval(), __operator_eval(), FormulaPart(), FUNCTION, GUM_ERROR, OPERATOR, and type.

Referenced by gum::Formula::__reduceOperatorOrFunction().

220  {
221  switch (type) {
222  case OPERATOR: {
223  return FormulaPart(token_type::NUMBER, __operator_eval(args));
224  }
225 
226  case FUNCTION: {
227  return FormulaPart(token_type::NUMBER, __function_eval(args));
228  }
229 
230  default: {
231  GUM_ERROR(OperationNotAllowed, "cannot evaluate expression");
232  }
233  }
234  }
double __operator_eval(const std::vector< FormulaPart > &args) const
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum...
Definition: formula_inl.h:159
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
double __function_eval(const std::vector< FormulaPart > &args) const
Returns the evaluation of the vector of gum::FormulaPart as arguments of the value stored in this gum...
Definition: formula_inl.h:194
FormulaPart()
Class constructor.
Definition: formula.cpp:86
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isLeftAssociative()

INLINE bool gum::FormulaPart::isLeftAssociative ( ) const

Returns true if this gum::FormulaPart is left associative.

Returns
Returns true if this gum::FormulaPart is left associative.
Exceptions
OperationNotAllowedRaised if the value stored is not an operator.

Definition at line 33 of file formula_inl.h.

References character, and GUM_ERROR.

Referenced by gum::Formula::__popOperator(), and isRightAssociative().

33  {
34  switch (character) {
35  case '+':
36  case '-':
37  case '*':
38  case '/': {
39  return true;
40  }
41 
42  case '_': {
43  return false;
44  }
45  case '^': {
46  return false;
47  }
48 
49  default: {
50  GUM_ERROR(OperationNotAllowed, "A - not an operator");
51  }
52  }
53  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ isRightAssociative()

INLINE bool gum::FormulaPart::isRightAssociative ( ) const

Returns true if this gum::FormulaPart is right associative.

Returns
Returns true if this gum::FormulaPart is right associative.
Exceptions
OperationNotAllowedRaised if the value stored is not an operator.

Definition at line 56 of file formula_inl.h.

References character, and isLeftAssociative().

Referenced by gum::Formula::__popOperator().

56  {
57  switch (character) {
58  case '_': {
59  return false;
60  }
61  default: {
62  return !isLeftAssociative();
63  }
64  }
65  }
bool isLeftAssociative() const
Returns true if this gum::FormulaPart is left associative.
Definition: formula_inl.h:33
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=() [1/2]

FormulaPart & gum::FormulaPart::operator= ( const FormulaPart source)

Definition at line 121 of file formula.cpp.

References character, function, number, and type.

121  {
122  if (this == &source) { return *this; }
123 
124  type = source.type;
125  number = source.number;
126  character = source.character;
127  function = source.function;
128 
129  return *this;
130  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ operator=() [2/2]

FormulaPart & gum::FormulaPart::operator= ( FormulaPart &&  source)

Definition at line 132 of file formula.cpp.

References character, number, and type.

132  {
133  if (this == &source) { return *this; }
134 
135  type = std::move(source.type);
136  number = std::move(source.number);
137  character = std::move(source.character);
138  function = std::move(source.function);
139 
140  return *this;
141  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79

◆ precedence()

INLINE int gum::FormulaPart::precedence ( ) const

Returns the precedence priority of the value stored in this gum::FormulaPart.

Returns
Returns the precedence priority of the value stored in this gum::FormulaPart.
Exceptions
OperationNotAllowedRaised if the value stored is not an operator.

Definition at line 68 of file formula_inl.h.

References character, and GUM_ERROR.

Referenced by gum::Formula::__popOperator().

68  {
69  switch (character) {
70  case '+':
71  case '-': {
72  return 2;
73  }
74 
75  case '*':
76  case '/': {
77  return 3;
78  }
79 
80  case '^': {
81  return 4;
82  }
83 
84  case '_': {
85  return 5;
86  }
87 
88  default: {
89  GUM_ERROR(OperationNotAllowed, "B - not an operator");
90  }
91  }
92  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ str()

std::string gum::FormulaPart::str ( ) const

Returns a string representation of this gum::FormulaPart value.

Returns
Returns a string representation of this gum::FormulaPart value.

Definition at line 143 of file formula.cpp.

References character, gum::func2str(), GUM_ERROR, number, and type.

143  {
144  std::ostringstream s;
145  switch (type) {
146  case token_type::NUMBER: {
147  s << number;
148  break;
149  }
150 
151  case token_type::PARENTHESIS:
152  case token_type::OPERATOR: {
153  if (character == '\0') {
154  s << "\\0";
155  } else {
156  s << character;
157  }
158  break;
159  }
160 
161  case token_type::FUNCTION: {
162  s << func2str(function);
163  break;
164  }
165 
166  default: {
167  GUM_ERROR(OperationNotAllowed, "unknown type");
168  }
169  }
170  return s.str();
171  }
std::string func2str(FormulaPart::token_function func)
Definition: formula.cpp:60
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:69
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:78
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:79
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

Member Data Documentation

◆ character

char gum::FormulaPart::character

The value stored by this gum::FormulaPart.

Warning
Only one of these three members will hold the value, given the type of this gum::FormulaPart.

Definition at line 79 of file formula.h.

Referenced by __operator_argc(), __operator_eval(), isLeftAssociative(), isRightAssociative(), operator=(), precedence(), and str().

◆ function

token_function gum::FormulaPart::function

The value stored by this gum::FormulaPart.

Warning
Only one of these three members will hold the value, given the type of this gum::FormulaPart.

Definition at line 80 of file formula.h.

Referenced by operator=().

◆ number

double gum::FormulaPart::number

The value stored by this gum::FormulaPart.

Warning
Only one of these three members will hold the value, given the type of this gum::FormulaPart.

Definition at line 78 of file formula.h.

Referenced by __function_eval(), __operator_eval(), operator=(), and str().

◆ type

token_type gum::FormulaPart::type

The token_type stored by this gum::FormulaPart.

Definition at line 69 of file formula.h.

Referenced by argc(), eval(), operator=(), and str().


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