aGrUM 3.1.1
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_()

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

421 {
422 switch (function) {
424 return 1;
425 }
427 return 1;
428 }
430 return 1;
431 }
433 return 2;
434 }
436 return 1;
437 }
438 // case FormulaPart::token_function::nil: { return "nil"; }
439 default : {
440 GUM_ERROR(OperationNotAllowed, "unknown function")
441 }
442 }
443 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

Referenced by argc().

Here is the caller graph for this function:

◆ _function_eval_()

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.

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

477 {
478 switch (function) {
480 return std::exp(args[0].number);
481 }
483 return std::log(args[0].number);
484 }
486 return std::log(args[0].number);
487 }
489 return std::pow(args[1].number, args[0].number);
490 }
492 return std::sqrt(args[0].number);
493 }
494 // case FormulaPart::token_function::nil: { return "nil"; }
495 default : {
496 GUM_ERROR(OperationNotAllowed, "unknown function")
497 }
498 }
499 }

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

Referenced by eval().

Here is the caller graph for this function:

◆ _operator_argc_()

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

402 {
403 switch (character) {
404 case '_' : {
405 return (size_t)1;
406 }
407 case '+' :
408 case '-' :
409 case '*' :
410 case '/' :
411 case '^' : {
412 return (size_t)2;
413 }
414
415 default : {
416 GUM_ERROR(OperationNotAllowed, "C - not an operator")
417 }
418 }
419 }

References character, and GUM_ERROR.

Referenced by argc().

Here is the caller graph for this function:

◆ _operator_eval_()

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.

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

445 {
446 switch (character) {
447 case '+' : {
448 return args[1].number + args[0].number;
449 }
450
451 case '-' : {
452 return args[1].number - args[0].number;
453 }
454
455 case '*' : {
456 return args[1].number * args[0].number;
457 }
458
459 case '/' : {
460 return args[1].number / args[0].number;
461 }
462
463 case '^' : {
464 return std::pow(args[1].number, args[0].number);
465 }
466
467 case '_' : {
468 return 0 - args[0].number;
469 }
470
471 default : {
472 GUM_ERROR(OperationNotAllowed, "D - not an operator")
473 }
474 }
475 }

References character, GUM_ERROR, and number.

Referenced by eval().

Here is the caller graph for this function:

◆ argc()

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

386 {
387 switch (type) {
388 case OPERATOR : {
389 return _operator_argc_();
390 }
391
392 case FUNCTION : {
393 return _function_argc_();
394 }
395
396 default : {
397 GUM_ERROR(OperationNotAllowed, "expecting a function or an operator")
398 }
399 }
400 }
size_t _function_argc_() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition formula.cpp:421
size_t _operator_argc_() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
Definition formula.cpp:402

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()

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.

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

501 {
502 switch (type) {
503 case OPERATOR : {
504 return {token_type::NUMBER, _operator_eval_(args)};
505 }
506
507 case FUNCTION : {
508 return {token_type::NUMBER, _function_eval_(args)};
509 }
510
511 default : {
512 GUM_ERROR(OperationNotAllowed, "cannot evaluate expression")
513 }
514 }
515 }
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.cpp:445
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.cpp:477

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()

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

338 {
339 switch (character) {
340 case '+' :
341 case '-' :
342 case '*' :
343 case '/' : {
344 return true;
345 }
346
347 case '_' : {
348 return false;
349 }
350 case '^' : {
351 return false;
352 }
353
354 default : {
355 GUM_ERROR(OperationNotAllowed, "A - not an operator")
356 }
357 }
358 }

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

54 {
55 switch (character) {
56 case '_' : {
57 return false;
58 }
59 default : {
60 return !isLeftAssociative();
61 }
62 }
63 }
bool isLeftAssociative() const
Returns true if this gum::FormulaPart is left associative.
Definition formula.cpp:338

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()

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

360 {
361 switch (character) {
362 case '+' :
363 case '-' : {
364 return 2;
365 }
366
367 case '*' :
368 case '/' : {
369 return 3;
370 }
371
372 case '^' : {
373 return 4;
374 }
375
376 case '_' : {
377 return 5;
378 }
379
380 default : {
381 GUM_ERROR(OperationNotAllowed, "B - not an operator")
382 }
383 }
384 }

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 switch (type) {
165 case token_type::NUMBER : {
166 return std::format("{}", number);
167 }
168
170 case token_type::OPERATOR : {
171 return (character == '\0') ? "\\0" : std::string(1, character);
172 }
173
174 case token_type::FUNCTION : {
175 return func2str(function);
176 }
177
178 default : {
179 GUM_ERROR(OperationNotAllowed, "unknown type")
180 }
181 }
182 }
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: