aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::FormulaPart Class Reference

Represents part of a formula. More...

#include <agrum/tools/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 59 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 73 of file formula.h.

◆ token_type

The tokens constituting a formula.

Enumerator
NUMBER 
OPERATOR 
PARENTHESIS 
NIL 
FUNCTION 
ARG_SEP 

Definition at line 62 of file formula.h.

Constructor & Destructor Documentation

◆ FormulaPart() [1/6]

gum::FormulaPart::FormulaPart ( )

Class constructor.

Definition at line 85 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

85  : type(token_type::NIL), number(NAN), character('\0'), function(nil) {
86  GUM_CONSTRUCTOR(FormulaPart);
87  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call 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 89 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

89  :
90  type(t), number(n), character('\0'), function(nil) {
91  GUM_CONSTRUCTOR(FormulaPart);
92  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

94  :
95  type(t), number(NAN), character(c), function(nil) {
96  GUM_CONSTRUCTOR(FormulaPart);
97  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

99  :
100  type(t), number(NAN), character('\0'), function(func) {
101  GUM_CONSTRUCTOR(FormulaPart);
102  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

◆ FormulaPart() [5/6]

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

Copy constructor.

Parameters
sourceThe gum::FormulaPart to copy.

Definition at line 104 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

104  :
105  type(source.type), number(source.number), character(source.character),
106  function(source.function) {
107  GUM_CONS_CPY(FormulaPart);
108  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

◆ FormulaPart() [6/6]

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

Move constructor.

Parameters
sourceThe gum::FormulaPart to move.

Definition at line 110 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

110  :
111  type(std::move(source.type)), number(std::move(source.number)),
112  character(std::move(source.character)), function(std::move(source.function)) {
113  GUM_CONS_MOV(FormulaPart);
114  }
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

◆ ~FormulaPart()

gum::FormulaPart::~FormulaPart ( )

Class destuctor.

Definition at line 116 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

116  {
117  GUM_DESTRUCTOR(FormulaPart);
118  ;
119  }
FormulaPart()
Class constructor.
Definition: formula.cpp:85
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

131  {
132  switch (function) {
133  case FormulaPart::token_function::exp: {
134  return 1;
135  }
136  case FormulaPart::token_function::log: {
137  return 1;
138  }
139  case FormulaPart::token_function::ln: {
140  return 1;
141  }
142  case FormulaPart::token_function::pow: {
143  return 2;
144  }
145  case FormulaPart::token_function::sqrt: {
146  return 1;
147  }
148  // case FormulaPart::token_function::nil: { return "nil"; }
149  default: {
150  GUM_ERROR(OperationNotAllowed, "unknown function")
151  }
152  }
153  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 191 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

191  {
192  switch (function) {
193  case FormulaPart::token_function::exp: {
194  return std::exp(args[0].number);
195  }
196  case FormulaPart::token_function::log: {
197  return std::log(args[0].number);
198  }
199  case FormulaPart::token_function::ln: {
200  return std::log2(args[0].number);
201  }
202  case FormulaPart::token_function::pow: {
203  return std::pow(args[1].number, args[0].number);
204  }
205  case FormulaPart::token_function::sqrt: {
206  return std::sqrt(args[0].number);
207  }
208  // case FormulaPart::token_function::nil: { return "nil"; }
209  default: {
210  GUM_ERROR(OperationNotAllowed, "unknown function")
211  }
212  }
213  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:590
+ Here is the call 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 111 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

111  {
112  switch (character) {
113  case '_': {
114  return (size_t)1;
115  }
116  case '+':
117  case '-':
118  case '*':
119  case '/':
120  case '^': {
121  return (size_t)2;
122  }
123 
124  default: {
125  GUM_ERROR(OperationNotAllowed, "C - not an operator")
126  }
127  }
128  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 157 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

157  {
158  switch (character) {
159  case '+': {
160  return args[1].number + args[0].number;
161  }
162 
163  case '-': {
164  return args[1].number - args[0].number;
165  }
166 
167  case '*': {
168  return args[1].number * args[0].number;
169  }
170 
171  case '/': {
172  return args[1].number / args[0].number;
173  }
174 
175  case '^': {
176  return std::pow(args[1].number, args[0].number);
177  }
178 
179  case '_': {
180  return 0 - args[0].number;
181  }
182 
183  default: {
184  GUM_ERROR(OperationNotAllowed, "D - not an operator")
185  }
186  }
187  }
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 94 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

94  {
95  switch (type) {
96  case OPERATOR: {
97  return _operator_argc_();
98  }
99 
100  case FUNCTION: {
101  return _function_argc_();
102  }
103 
104  default: {
105  GUM_ERROR(OperationNotAllowed, "expecting a function or an operator")
106  }
107  }
108  }
size_t _operator_argc_() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
Definition: formula_inl.h:111
size_t _function_argc_() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition: formula_inl.h:131
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 217 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

217  {
218  switch (type) {
219  case OPERATOR: {
220  return FormulaPart(token_type::NUMBER, _operator_eval_(args));
221  }
222 
223  case FUNCTION: {
224  return FormulaPart(token_type::NUMBER, _function_eval_(args));
225  }
226 
227  default: {
228  GUM_ERROR(OperationNotAllowed, "cannot evaluate expression")
229  }
230  }
231  }
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:191
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:157
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
FormulaPart()
Class constructor.
Definition: formula.cpp:85
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 32 of file formula_inl.h.

References gum::Set< Key, Alloc >::emplace().

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

References gum::Set< Key, Alloc >::emplace().

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

◆ operator=() [1/2]

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

Definition at line 121 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

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:84
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

◆ operator=() [2/2]

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

Definition at line 132 of file formula.cpp.

References gum::Set< Key, Alloc >::emplace().

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:84
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
+ Here is the call graph for this function:

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

References gum::Set< Key, Alloc >::emplace().

67  {
68  switch (character) {
69  case '+':
70  case '-': {
71  return 2;
72  }
73 
74  case '*':
75  case '/': {
76  return 3;
77  }
78 
79  case '^': {
80  return 4;
81  }
82 
83  case '_': {
84  return 5;
85  }
86 
87  default: {
88  GUM_ERROR(OperationNotAllowed, "B - not an operator")
89  }
90  }
91  }
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 gum::Set< Key, Alloc >::emplace().

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:59
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
double number
The value stored by this gum::FormulaPart.
Definition: formula.h:93
char character
The value stored by this gum::FormulaPart.
Definition: formula.h:94
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 94 of file formula.h.

◆ 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 95 of file formula.h.

◆ 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 93 of file formula.h.

◆ type

token_type gum::FormulaPart::type

The token_type stored by this gum::FormulaPart.

Definition at line 84 of file formula.h.


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