aGrUM  0.20.2
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  :
86  type(token_type::NIL), number(NAN), character('\0'), function(nil) {
87  GUM_CONSTRUCTOR(FormulaPart);
88  }
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 90 of file formula.cpp.

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

90  :
91  type(t), number(n), character('\0'), function(nil) {
92  GUM_CONSTRUCTOR(FormulaPart);
93  }
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 95 of file formula.cpp.

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

95  :
96  type(t), number(NAN), character(c), function(nil) {
97  GUM_CONSTRUCTOR(FormulaPart);
98  }
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 100 of file formula.cpp.

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

100  :
101  type(t), number(NAN), character('\0'), function(func) {
102  GUM_CONSTRUCTOR(FormulaPart);
103  }
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 105 of file formula.cpp.

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

105  :
106  type(source.type), number(source.number), character(source.character),
107  function(source.function) {
108  GUM_CONS_CPY(FormulaPart);
109  }
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 111 of file formula.cpp.

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

111  :
112  type(std::move(source.type)), number(std::move(source.number)),
113  character(std::move(source.character)),
114  function(std::move(source.function)) {
115  GUM_CONS_MOV(FormulaPart);
116  }
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 118 of file formula.cpp.

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

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

Member Function Documentation

◆ 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
token_type type
The token_type stored by this gum::FormulaPart.
Definition: formula.h:84
size_t function_argc__() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition: formula_inl.h:131
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 219 of file formula_inl.h.

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

219  {
220  switch (type) {
221  case OPERATOR: {
222  return FormulaPart(token_type::NUMBER, operator_eval__(args));
223  }
224 
225  case FUNCTION: {
226  return FormulaPart(token_type::NUMBER, function_eval__(args));
227  }
228 
229  default: {
230  GUM_ERROR(OperationNotAllowed, "cannot evaluate expression");
231  }
232  }
233  }
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:158
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:193
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:54
+ Here is the call graph for this function:

◆ 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:54
+ 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 193 of file formula_inl.h.

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

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

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

120  {
121  if (this == &source) { return *this; }
122 
123  type = source.type;
124  number = source.number;
125  character = source.character;
126  function = source.function;
127 
128  return *this;
129  }
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 131 of file formula.cpp.

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

131  {
132  if (this == &source) { return *this; }
133 
134  type = std::move(source.type);
135  number = std::move(source.number);
136  character = std::move(source.character);
137  function = std::move(source.function);
138 
139  return *this;
140  }
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_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:54
+ 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 158 of file formula_inl.h.

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

158  {
159  switch (character) {
160  case '+': {
161  return args[1].number + args[0].number;
162  }
163 
164  case '-': {
165  return args[1].number - args[0].number;
166  }
167 
168  case '*': {
169  return args[1].number * args[0].number;
170  }
171 
172  case '/': {
173  return args[1].number / args[0].number;
174  }
175 
176  case '^': {
177  return std::pow(args[1].number, args[0].number);
178  }
179 
180  case '_': {
181  return 0 - args[0].number;
182  }
183 
184  default: {
185  GUM_ERROR(OperationNotAllowed, "D - not an operator");
186  }
187  }
188  }
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:54
+ 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:54
+ 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 142 of file formula.cpp.

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

142  {
143  std::ostringstream s;
144  switch (type) {
145  case token_type::NUMBER: {
146  s << number;
147  break;
148  }
149 
150  case token_type::PARENTHESIS:
151  case token_type::OPERATOR: {
152  if (character == '\0') {
153  s << "\\0";
154  } else {
155  s << character;
156  }
157  break;
158  }
159 
160  case token_type::FUNCTION: {
161  s << func2str(function);
162  break;
163  }
164 
165  default: {
166  GUM_ERROR(OperationNotAllowed, "unknown type");
167  }
168  }
169  return s.str();
170  }
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:54
+ 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: