aGrUM 3.2.0
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:
[legend]

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.
FormulaPart & operator= (const FormulaPart &source)
FormulaPart & operator= (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 111 of file formula.cpp.

111 : type(token_type::NIL), number(NAN), character('\0'), function(nil) {
112 GUM_CONSTRUCTOR(FormulaPart);
113 }
token_function function
The value stored by this gum::FormulaPart.
Definition formula.h:99
FormulaPart()
Class constructor.
Definition formula.cpp:111
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(), gum::Formula::_push_comma_(), gum::Formula::_push_rightParenthesis_(), 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 115 of file formula.cpp.

115 :
116 type(t), number(n), character('\0'), function(nil) {
117 GUM_CONSTRUCTOR(FormulaPart);
118 }

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

120 :
121 type(t), number(NAN), character(c), function(nil) {
122 GUM_CONSTRUCTOR(FormulaPart);
123 }

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

125 :
126 type(t), number(NAN), character('\0'), function(func) {
127 GUM_CONSTRUCTOR(FormulaPart);
128 }

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

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

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

136 :
137 type(std::move(source.type)), number(std::move(source.number)),
138 character(std::move(source.character)), function(std::move(source.function)) {
139 GUM_CONS_MOV(FormulaPart);
140 }

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

Here is the call graph for this function:

◆ ~FormulaPart()

gum::FormulaPart::~FormulaPart ( )

Class destuctor.

Definition at line 142 of file formula.cpp.

142 {
143 GUM_DESTRUCTOR(FormulaPart);
144 ;
145 }

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

427 {
428 switch (function) {
430 return 1;
431 }
433 return 1;
434 }
436 return 1;
437 }
439 return 2;
440 }
442 return 1;
443 }
444 // case FormulaPart::token_function::nil: { return "nil"; }
445 default : {
446 GUM_ERROR(OperationNotAllowed, "unknown function")
447 }
448 }
449 }
#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 483 of file formula.cpp.

483 {
484 switch (function) {
486 return std::exp(args[0].number);
487 }
489 return std::log(args[0].number);
490 }
492 return std::log(args[0].number);
493 }
495 return std::pow(args[1].number, args[0].number);
496 }
498 return std::sqrt(args[0].number);
499 }
500 // case FormulaPart::token_function::nil: { return "nil"; }
501 default : {
502 GUM_ERROR(OperationNotAllowed, "unknown function")
503 }
504 }
505 }

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

408 {
409 switch (character) {
410 case '_' : {
411 return (size_t)1;
412 }
413 case '+' :
414 case '-' :
415 case '*' :
416 case '/' :
417 case '^' : {
418 return (size_t)2;
419 }
420
421 default : {
422 GUM_ERROR(OperationNotAllowed, "C - not an operator")
423 }
424 }
425 }

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

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

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

392 {
393 switch (type) {
394 case OPERATOR : {
395 return _operator_argc_();
396 }
397
398 case FUNCTION : {
399 return _function_argc_();
400 }
401
402 default : {
403 GUM_ERROR(OperationNotAllowed, "expecting a function or an operator")
404 }
405 }
406 }
size_t _function_argc_() const
Returns the number of arguments expected by the function stored in this gum::FormulaPart.
Definition formula.cpp:427
size_t _operator_argc_() const
Returns the number of arguments expected by the operator stored in this gum::FormulaPart.
Definition formula.cpp:408

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

507 {
508 switch (type) {
509 case OPERATOR : {
510 return {token_type::NUMBER, _operator_eval_(args)};
511 }
512
513 case FUNCTION : {
514 return {token_type::NUMBER, _function_eval_(args)};
515 }
516
517 default : {
518 GUM_ERROR(OperationNotAllowed, "cannot evaluate expression")
519 }
520 }
521 }
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:451
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:483

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

344 {
345 switch (character) {
346 case '+' :
347 case '-' :
348 case '*' :
349 case '/' : {
350 return true;
351 }
352
353 case '_' : {
354 return false;
355 }
356 case '^' : {
357 return false;
358 }
359
360 default : {
361 GUM_ERROR(OperationNotAllowed, "A - not an operator")
362 }
363 }
364 }

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:344

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

147 {
148 if (this == &source) { return *this; }
149
150 type = source.type;
151 number = source.number;
152 character = source.character;
153 function = source.function;
154
155 return *this;
156 }

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

158 {
159 if (this == &source) { return *this; }
160
161 type = std::move(source.type);
162 number = std::move(source.number);
163 character = std::move(source.character);
164 function = std::move(source.function);
165
166 return *this;
167 }

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

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

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

169 {
170 switch (type) {
171 case token_type::NUMBER : {
172 return std::format("{}", number);
173 }
174
176 case token_type::OPERATOR : {
177 return (character == '\0') ? "\\0" : std::string(1, character);
178 }
179
180 case token_type::FUNCTION : {
181 return func2str(function);
182 }
183
184 default : {
185 GUM_ERROR(OperationNotAllowed, "unknown type")
186 }
187 }
188 }
std::string func2str(FormulaPart::token_function func)
Definition formula.cpp:85

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(), gum::Formula::_isUnaryOperator_(), _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


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