qat.core.variables.Symbol

class qat.core.variables.Symbol(token, evaluator, arity, infix=False, pretty_token=None, diff=None)

A simple structure to describe labels in the AST of an arithmetic expression.

A symbol requires:
  • a token representing the symbol

  • a proper python function that implements the symbol for numerical values

  • an arity (i.e a number of arguments)

For instance, the cos symbol of this module is declared as follows, where the diff field can be omitted if one wouldn’t need differentiation:

from qat.core.variables import Symbol
import numpy as np
cos = Symbol("cos", np.cos, 1, diff=lambda x, dx: -sin(x) * dx)
print(cos)
print(type(cos))
print(cos(np.pi))
cos
<class 'qat.core.variables.Symbol'>
-1.0

Its __call__ operator is overloaded in order to behave as the underlying function for numeric values and to build an ArithExpression when called on abstract expressions or variables.

Parameters
  • token (str) – the token corresponding to the symbol

  • evaluator (callable) – the proper function underlying the symbol (i.e the semantic)

  • arity (int) – the expected arguments of the symbol

  • infix (bool) – If set to True, the symbol will be displayed in infix format (only allowed when arity=2). Defaults to False.

  • pretty_token (optional, str) – If set, the pretty token will be used when displaying the symbol.