aGrUM  0.14.2
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 57 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 63 of file formula.h.

◆ token_type

The tokens constituting a formula.

Enumerator
NUMBER 
OPERATOR 
PARENTHESIS 
NIL 
FUNCTION 
ARG_SEP 

Definition at line 60 of file formula.h.

Constructor & Destructor Documentation

◆ FormulaPart() [1/6]

gum::FormulaPart::FormulaPart ( )

Class constructor.

Definition at line 82 of file formula.cpp.

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

82  :
83  type(token_type::NIL), number(NAN), character('\0'), function(nil) {
84  GUM_CONSTRUCTOR(FormulaPart);
85  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
+ 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 87 of file formula.cpp.

87  :
88  type(t), number(n), character('\0'), function(nil) {
89  GUM_CONSTRUCTOR(FormulaPart);
90  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ 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 92 of file formula.cpp.

92  :
93  type(t), number(NAN), character(c), function(nil) {
94  GUM_CONSTRUCTOR(FormulaPart);
95  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ 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 97 of file formula.cpp.

97  :
98  type(t), number(NAN), character('\0'), function(func) {
99  GUM_CONSTRUCTOR(FormulaPart);
100  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ FormulaPart() [5/6]

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

Copy constructor.

Parameters
sourceThe gum::FormulaPart to copy.

Definition at line 102 of file formula.cpp.

102  :
103  type(source.type), number(source.number), character(source.character),
104  function(source.function) {
105  GUM_CONS_CPY(FormulaPart);
106  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ FormulaPart() [6/6]

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

Move constructor.

Parameters
sourceThe gum::FormulaPart to move.

Definition at line 108 of file formula.cpp.

108  :
109  type(std::move(source.type)), number(std::move(source.number)),
110  character(std::move(source.character)),
111  function(std::move(source.function)) {
112  GUM_CONS_MOV(FormulaPart);
113  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
FormulaPart()
Class constructor.
Definition: formula.cpp:82
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ ~FormulaPart()

gum::FormulaPart::~FormulaPart ( )

Class destuctor.

Definition at line 115 of file formula.cpp.

115 { GUM_DESTRUCTOR(FormulaPart); }
FormulaPart()
Class constructor.
Definition: formula.cpp:82

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 122 of file formula_inl.h.

References GUM_ERROR.

Referenced by argc().

122  {
123  switch (function) {
124  case FormulaPart::token_function::exp: {
125  return 1;
126  }
127  case FormulaPart::token_function::log: {
128  return 1;
129  }
130  case FormulaPart::token_function::ln: {
131  return 1;
132  }
133  case FormulaPart::token_function::pow: {
134  return 2;
135  }
136  case FormulaPart::token_function::sqrt: {
137  return 1;
138  }
139  // case FormulaPart::token_function::nil: { return "nil"; }
140  default: { GUM_ERROR(OperationNotAllowed, "unknown function"); }
141  }
142  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 180 of file formula_inl.h.

References GUM_ERROR, and number.

Referenced by eval().

180  {
181  switch (function) {
182  case FormulaPart::token_function::exp: {
183  return std::exp(args[0].number);
184  }
185  case FormulaPart::token_function::log: {
186  return std::log(args[0].number);
187  }
188  case FormulaPart::token_function::ln: {
189  return std::log2(args[0].number);
190  }
191  case FormulaPart::token_function::pow: {
192  return std::pow(args[1].number, args[0].number);
193  }
194  case FormulaPart::token_function::sqrt: {
195  return std::sqrt(args[0].number);
196  }
197  // case FormulaPart::token_function::nil: { return "nil"; }
198  default: { GUM_ERROR(OperationNotAllowed, "unknown function"); }
199  }
200  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 104 of file formula_inl.h.

References character, and GUM_ERROR.

Referenced by argc().

104  {
105  switch (character) {
106  case '_': {
107  return (size_t)1;
108  }
109  case '+':
110  case '-':
111  case '*':
112  case '/':
113  case '^': {
114  return (size_t)2;
115  }
116 
117  default: { GUM_ERROR(OperationNotAllowed, "C - not an operator"); }
118  }
119  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 147 of file formula_inl.h.

References character, GUM_ERROR, and number.

Referenced by eval().

147  {
148  switch (character) {
149  case '+': {
150  return args[1].number + args[0].number;
151  }
152 
153  case '-': {
154  return args[1].number - args[0].number;
155  }
156 
157  case '*': {
158  return args[1].number * args[0].number;
159  }
160 
161  case '/': {
162  return args[1].number / args[0].number;
163  }
164 
165  case '^': {
166  return std::pow(args[1].number, args[0].number);
167  }
168 
169  case '_': {
170  return 0 - args[0].number;
171  }
172 
173  default: { GUM_ERROR(OperationNotAllowed, "D - not an operator"); }
174  }
175  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 87 of file formula_inl.h.

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

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

87  {
88  switch (type) {
89  case OPERATOR: {
90  return __operator_argc();
91  }
92 
93  case FUNCTION: {
94  return __function_argc();
95  }
96 
97  default: {
98  GUM_ERROR(OperationNotAllowed, "expecting a function or an operator");
99  }
100  }
101  }
size_t __function_argc() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition: formula_inl.h:122
size_t __operator_argc() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
Definition: formula_inl.h:104
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 204 of file formula_inl.h.

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

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

204  {
205  switch (type) {
206  case OPERATOR: {
207  return FormulaPart(token_type::NUMBER, __operator_eval(args));
208  }
209 
210  case FUNCTION: {
211  return FormulaPart(token_type::NUMBER, __function_eval(args));
212  }
213 
214  default: { GUM_ERROR(OperationNotAllowed, "cannot evaluate expression"); }
215  }
216  }
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:147
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
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:180
FormulaPart()
Class constructor.
Definition: formula.cpp:82
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 31 of file formula_inl.h.

References character, and GUM_ERROR.

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

31  {
32  switch (character) {
33  case '+':
34  case '-':
35  case '*':
36  case '/': {
37  return true;
38  }
39 
40  case '_': {
41  return false;
42  }
43  case '^': {
44  return false;
45  }
46 
47  default: { GUM_ERROR(OperationNotAllowed, "A - not an operator"); }
48  }
49  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 52 of file formula_inl.h.

References character, and isLeftAssociative().

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

52  {
53  switch (character) {
54  case '_': {
55  return false;
56  }
57  default: { return !isLeftAssociative(); }
58  }
59  }
bool isLeftAssociative() const
Returns true if this gum::FormulaPart is left associative.
Definition: formula_inl.h:31
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
+ 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 117 of file formula.cpp.

References character, function, number, and type.

117  {
118  if (this == &source) { return *this; }
119 
120  type = source.type;
121  number = source.number;
122  character = source.character;
123  function = source.function;
124 
125  return *this;
126  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ operator=() [2/2]

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

Definition at line 128 of file formula.cpp.

References character, number, and type.

128  {
129  if (this == &source) { return *this; }
130 
131  type = std::move(source.type);
132  number = std::move(source.number);
133  character = std::move(source.character);
134  function = std::move(source.function);
135 
136  return *this;
137  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76

◆ 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 62 of file formula_inl.h.

References character, and GUM_ERROR.

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

62  {
63  switch (character) {
64  case '+':
65  case '-': {
66  return 2;
67  }
68 
69  case '*':
70  case '/': {
71  return 3;
72  }
73 
74  case '^': {
75  return 4;
76  }
77 
78  case '_': {
79  return 5;
80  }
81 
82  default: { GUM_ERROR(OperationNotAllowed, "B - not an operator"); }
83  }
84  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 139 of file formula.cpp.

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

139  {
140  std::ostringstream s;
141  switch (type) {
142  case token_type::NUMBER: {
143  s << number;
144  break;
145  }
146 
147  case token_type::PARENTHESIS:
148  case token_type::OPERATOR: {
149  if (character == '\0') {
150  s << "\\0";
151  } else {
152  s << character;
153  }
154  break;
155  }
156 
157  case token_type::FUNCTION: {
158  s << func2str(function);
159  break;
160  }
161 
162  default: { GUM_ERROR(OperationNotAllowed, "unknown type"); }
163  }
164  return s.str();
165  }
std::string func2str(FormulaPart::token_function func)
Definition: formula.cpp:58
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:66
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:75
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:76
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 76 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 77 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 75 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 66 of file formula.h.

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


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