qat.core.variables
Represents arithmetic expressions |
|
Describes a given label in the AST on an |
This framework provides a set of symbols:
Absolute value of a (complex) number |
|
Angle in the complex plane of a complex number |
|
The atan2 function from the math library |
|
Cosinus function |
|
Natural exponential function |
|
A function extracting a number by its position in an array |
|
Heaviside function |
|
Imaginary part extraction function |
|
Natural logarithm function |
|
Real part extraction function |
|
Sinus function |
|
Square root function |
|
Maximum of two numbers |
User defined symbols can be created using the class Symbol
.
For instance, the following piece of code adds a new symbol that represents the min between two numeric values.
from qat.core.variables import Variable, Symbol
# token will "min"
# underlying function is the standard `min`
# it has arity 2
# it is not an infix operator
my_min = Symbol("min", min, 2, infix=False)
a, b = Variable("a"), Variable("b")
expr = my_min(a, b)
print(expr)
print(expr(a=3))
print(expr(a=3, b=4))
min(a,b)
min(3,b)
3
Warning
If you create a Symbol in your environment, but choose to send it for evaluation to a remote Qaptiva appliance, the remote environment will not be aware of this new Symbol and the expression evaluation might fail.
Listing defined symbols and getting their arity
Defined symbols can be accessed via the ALL_SYMBOLS module variable. The only constraint is that the new symbols should have a fixed arity that is known in advance.
from qat.core.variables import ALL_SYMBOLS
for token, symbol in ALL_SYMBOLS.items():
print(f"Symbol {token!r} takes {symbol.arity} argument(s)")
Symbol '+' takes 2 argument(s)
Symbol '*' takes 2 argument(s)
Symbol '-' takes 2 argument(s)
Symbol 'UMINUS' takes 1 argument(s)
Symbol '/' takes 2 argument(s)
Symbol '**' takes 2 argument(s)
Symbol 'cos' takes 1 argument(s)
Symbol 'sin' takes 1 argument(s)
Symbol 'exp' takes 1 argument(s)
Symbol 'sqrt' takes 1 argument(s)
Symbol 'heaviside' takes 3 argument(s)
Symbol 'max' takes 2 argument(s)
Symbol 'ln' takes 1 argument(s)
Symbol 'real' takes 1 argument(s)
Symbol 'imag' takes 1 argument(s)
Symbol 'angle' takes 1 argument(s)
Symbol 'abs' takes 1 argument(s)
Symbol 'atan2' takes 2 argument(s)
Symbol 'get_item' takes 2 argument(s)