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

Represents part of a formula. More...

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

Collaboration diagram for gum::FormulaPart:

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...

Public Member Functions

Constructors and destructor
 FormulaPart ()
 Class constructor.
 FormulaPart (token_type t, double n)
 Constructor for doubles.
 FormulaPart (token_type t, char c)
 Constructor for chars.
 FormulaPart (token_type t, token_function func)
 Constructor for functions.
 FormulaPart (const FormulaPart &source)
 Copy constructor.
 FormulaPart (FormulaPart &&source)
 Move constructor.
 ~FormulaPart ()
 Class destuctor.
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.
bool isLeftAssociative () const
 Returns true if this gum::FormulaPart is left associative.
bool isRightAssociative () const
 Returns true if this gum::FormulaPart is right associative.
int precedence () const
 Returns the precedence priority of the value stored in this gum::FormulaPart.
size_t argc () const
 Returns the number of argument of the function stored in this gum::FormulaPart.
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.

Public Attributes

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

Private Member Functions

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::FormulaPart.
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::FormulaPart.
size_t _operator_argc_ () const
 Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
size_t _function_argc_ () const
 Returns the number of arguments expected by the function stored in this gum::FormulaPart.

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 79 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 85 of file formula.h.

◆ token_type

The tokens constituting a formula.

Enumerator
NUMBER 
OPERATOR 
PARENTHESIS 
NIL 
FUNCTION 
ARG_SEP 

Definition at line 82 of file formula.h.

Constructor & Destructor Documentation

◆ FormulaPart() [1/6]

gum::FormulaPart::FormulaPart ( )

Class constructor.

Definition at line 105 of file formula.cpp.

105 : type(token_type::NIL), number(NAN), character('\0'), function(nil) {
106 GUM_CONSTRUCTOR(FormulaPart);
107 }
token_function function
The value stored by this gum::FormulaPart.
Definition formula.h:99
FormulaPart()
Class constructor.
Definition formula.cpp:105
double number
The value stored by this gum::FormulaPart.
Definition formula.h:97
char character
The value stored by this gum::FormulaPart.
Definition formula.h:98
token_type type
The token_type stored by this gum::FormulaPart.
Definition formula.h:88

References FormulaPart(), character, function, NIL, nil, number, and type.

Referenced by FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), ~FormulaPart(), eval(), operator=(), and operator=().

Here is the call graph for this function:
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 109 of file formula.cpp.

109 :
110 type(t), number(n), character('\0'), function(nil) {
111 GUM_CONSTRUCTOR(FormulaPart);
112 }

References FormulaPart(), character, function, nil, number, and type.

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

114 :
115 type(t), number(NAN), character(c), function(nil) {
116 GUM_CONSTRUCTOR(FormulaPart);
117 }

References FormulaPart(), character, function, nil, number, and type.

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

119 :
120 type(t), number(NAN), character('\0'), function(func) {
121 GUM_CONSTRUCTOR(FormulaPart);
122 }

References FormulaPart(), character, function, number, and type.

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

124 :
125 type(source.type), number(source.number), character(source.character),
126 function(source.function) {
127 GUM_CONS_CPY(FormulaPart);
128 }

References FormulaPart(), character, function, number, and type.

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

130 :
131 type(std::move(source.type)), number(std::move(source.number)),
132 character(std::move(source.character)), function(std::move(source.function)) {
133 GUM_CONS_MOV(FormulaPart);
134 }

References FormulaPart(), character, function, number, and type.

Here is the call graph for this function:

◆ ~FormulaPart()

gum::FormulaPart::~FormulaPart ( )

Class destuctor.

Definition at line 136 of file formula.cpp.

136 {
137 GUM_DESTRUCTOR(FormulaPart);
138 ;
139 }

References FormulaPart().

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

152 {
153 switch (function) {
155 return 1;
156 }
158 return 1;
159 }
161 return 1;
162 }
164 return 2;
165 }
167 return 1;
168 }
169 // case FormulaPart::token_function::nil: { return "nil"; }
170 default : {
171 GUM_ERROR(OperationNotAllowed, "unknown function")
172 }
173 }
174 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References exp, function, GUM_ERROR, ln, log, pow, and sqrt.

Referenced by argc().

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

212 {
213 switch (function) {
215 return std::exp(args[0].number);
216 }
218 return std::log(args[0].number);
219 }
221 return std::log2(args[0].number);
222 }
224 return std::pow(args[1].number, args[0].number);
225 }
227 return std::sqrt(args[0].number);
228 }
229 // case FormulaPart::token_function::nil: { return "nil"; }
230 default : {
231 GUM_ERROR(OperationNotAllowed, "unknown function")
232 }
233 }
234 }

References exp, function, GUM_ERROR, ln, log, number, pow, and sqrt.

Referenced by eval().

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

132 {
133 switch (character) {
134 case '_' : {
135 return (size_t)1;
136 }
137 case '+' :
138 case '-' :
139 case '*' :
140 case '/' :
141 case '^' : {
142 return (size_t)2;
143 }
144
145 default : {
146 GUM_ERROR(OperationNotAllowed, "C - not an operator")
147 }
148 }
149 }

References character, and GUM_ERROR.

Referenced by argc().

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

178 {
179 switch (character) {
180 case '+' : {
181 return args[1].number + args[0].number;
182 }
183
184 case '-' : {
185 return args[1].number - args[0].number;
186 }
187
188 case '*' : {
189 return args[1].number * args[0].number;
190 }
191
192 case '/' : {
193 return args[1].number / args[0].number;
194 }
195
196 case '^' : {
197 return std::pow(args[1].number, args[0].number);
198 }
199
200 case '_' : {
201 return 0 - args[0].number;
202 }
203
204 default : {
205 GUM_ERROR(OperationNotAllowed, "D - not an operator")
206 }
207 }
208 }

References character, GUM_ERROR, and number.

Referenced by eval().

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

115 {
116 switch (type) {
117 case OPERATOR : {
118 return _operator_argc_();
119 }
120
121 case FUNCTION : {
122 return _function_argc_();
123 }
124
125 default : {
126 GUM_ERROR(OperationNotAllowed, "expecting a function or an operator")
127 }
128 }
129 }
size_t _function_argc_() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
size_t _operator_argc_() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.

References _function_argc_(), _operator_argc_(), FUNCTION, GUM_ERROR, OPERATOR, and type.

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

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

238 {
239 switch (type) {
240 case OPERATOR : {
242 }
243
244 case FUNCTION : {
246 }
247
248 default : {
249 GUM_ERROR(OperationNotAllowed, "cannot evaluate expression")
250 }
251 }
252 }
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...
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...

References FormulaPart(), _function_eval_(), _operator_eval_(), FUNCTION, GUM_ERROR, NUMBER, OPERATOR, and type.

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

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

53 {
54 switch (character) {
55 case '+' :
56 case '-' :
57 case '*' :
58 case '/' : {
59 return true;
60 }
61
62 case '_' : {
63 return false;
64 }
65 case '^' : {
66 return false;
67 }
68
69 default : {
70 GUM_ERROR(OperationNotAllowed, "A - not an operator")
71 }
72 }
73 }

References character, and GUM_ERROR.

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

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

76 {
77 switch (character) {
78 case '_' : {
79 return false;
80 }
81 default : {
82 return !isLeftAssociative();
83 }
84 }
85 }
bool isLeftAssociative() const
Returns true if this gum::FormulaPart is left associative.
Definition formula_inl.h:53

References character, and isLeftAssociative().

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

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

141 {
142 if (this == &source) { return *this; }
143
144 type = source.type;
145 number = source.number;
146 character = source.character;
147 function = source.function;
148
149 return *this;
150 }

References FormulaPart(), character, function, number, and type.

Here is the call graph for this function:

◆ operator=() [2/2]

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

Definition at line 152 of file formula.cpp.

152 {
153 if (this == &source) { return *this; }
154
155 type = std::move(source.type);
156 number = std::move(source.number);
157 character = std::move(source.character);
158 function = std::move(source.function);
159
160 return *this;
161 }

References FormulaPart(), character, function, number, and type.

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

88 {
89 switch (character) {
90 case '+' :
91 case '-' : {
92 return 2;
93 }
94
95 case '*' :
96 case '/' : {
97 return 3;
98 }
99
100 case '^' : {
101 return 4;
102 }
103
104 case '_' : {
105 return 5;
106 }
107
108 default : {
109 GUM_ERROR(OperationNotAllowed, "B - not an operator")
110 }
111 }
112 }

References character, and GUM_ERROR.

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

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

163 {
164 std::ostringstream s;
165 switch (type) {
166 case token_type::NUMBER : {
167 s << number;
168 break;
169 }
170
172 case token_type::OPERATOR : {
173 if (character == '\0') {
174 s << "\\0";
175 } else {
176 s << character;
177 }
178 break;
179 }
180
181 case token_type::FUNCTION : {
182 s << func2str(function);
183 break;
184 }
185
186 default : {
187 GUM_ERROR(OperationNotAllowed, "unknown type")
188 }
189 }
190 return s.str();
191 }
std::string func2str(FormulaPart::token_function func)
Definition formula.cpp:79

References character, gum::func2str(), FUNCTION, function, GUM_ERROR, NUMBER, number, OPERATOR, PARENTHESIS, and type.

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

Referenced by FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), _operator_argc_(), _operator_eval_(), isLeftAssociative(), isRightAssociative(), operator=(), 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 99 of file formula.h.

Referenced by FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), _function_argc_(), _function_eval_(), operator=(), operator=(), and str().

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

Referenced by FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), FormulaPart(), _function_eval_(), _operator_eval_(), operator=(), operator=(), and str().

◆ type

token_type gum::FormulaPart::type

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