Expressions and Functions

๐Ÿงฎ Expressions & Functions

In Qaekwy, an expression is a mathematical or logical formula that involves variables, constants, and operators. Expressions are the building blocks of constraints and objectives.

Building Expressions

You can build expressions using standard Python operators:

# A simple expression
expr1 = x + y

# A more complex expression
expr2 = (x * 2) + (y / 3)

# A boolean expression
expr3 = x > y

Expression Variables

You can also create a variable from an expression. This is useful for breaking down complex calculations.

from qaekwy.model.variable.integer import IntegerExpressionVariable

# z is a variable that is always equal to x + y
z = IntegerExpressionVariable("z", x + y)

Mathematical Functions

Qaekwy also provides a set of built-in mathematical functions that you can use in your expressions. You can find them in the qaekwy.model.function module.

Here’s what’s in the calculator:

  • maximum(expr): Find the biggest value in a list of expressions.
  • minimum(expr): Find the smallest value in a list of expressions.
  • sum_of(expr): Add up all the values in a list of expressions.
  • absolute(expr): Get the absolute value of an expression (e.g., absolute(-5) enforces 5).
  • power(expr, val): Raise an expression to a power (e.g., power(x, 2) is x*x).
  • nroot(expr, val): Find the nth root of an expression.
  • sqr(expr): Square an expression (same as power(expr, 2)).
  • sqrt(expr): Enforces the square root of an expression.
  • sin(expr): The sine of an expression.
  • cos(expr): The cosine of an expression.
  • tan(expr): The tangent of an expression.
  • asin(expr): The arcsine of an expression.
  • acos(expr): The arccosine of an expression.
  • atan(expr): The arctangent of an expression.
  • log(expr): The natural logarithm of an expression.
  • exp(expr): The exponential of an expression.

Check out Mathematical Functions.

Example

from qaekwy.model.function import sin, cos, power

# A fun math fact: sin(x)^2 + cos(x)^2 is always 1!
# Let's add it as a constraint (absolutely useless here, but you get the idea):
modeller.add_constraint(power(sin(x), 2) + power(cos(x), 2) == 1)