qat.core.variables

qat.core.variables.ArithExpression

Represents arithmetic expressions

qat.core.variables.Symbol

Describes a given label in the AST on an ArithExpression

This framework provides a set of symbols:

qat.core.variables.abso()

Absolute value of a (complex) number

qat.core.variables.angle()

Angle in the complex plane of a complex number

qat.core.variables.atan2()

The atan2 function from the math library

qat.core.variables.cos()

Cosinus function

qat.core.variables.exp()

Natural exponential function

qat.core.variables.get_item()

A function extracting a number by its position in an array

qat.core.variables.heaviside()

Heaviside function

qat.core.variables.imag()

Imaginary part extraction function

qat.core.variables.ln()

Natural logarithm function

qat.core.variables.real()

Real part extraction function

qat.core.variables.sin()

Sinus function

qat.core.variables.sqrt()

Square root function

qat.core.variables.vmax()

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)