Expressions API

Variable, Constant, and expression building
Published

February 8, 2026

1 Variable

Decision variables that the optimizer will determine.

TipVector Variables

For creating arrays of variables (e.g., x[0], x[1]), use VectorVariable instead of creating individual Variable objects in a loop.

from optyx import Variable

x = Variable(name, lb=None, ub=None, domain="continuous")

1.1 Parameters

Parameter Type Description Default
name str Unique identifier for the variable Required
lb float | None Lower bound None (unbounded)
ub float | None Upper bound None (unbounded)
domain str One of "continuous", "integer", "binary" "continuous"
WarningInteger/Binary Domains Relaxed

The "integer" and "binary" domains are accepted but relaxed to continuous values when using the SciPy solver backend. A runtime warning is emitted when this occurs. For true mixed-integer programming, consider using PuLP or Pyomo.

1.2 Properties

Property Type Description
.name str Variable name
.lb float | None Lower bound
.ub float | None Upper bound
.domain str Variable domain

1.3 Examples

from optyx import Variable

# Unbounded variable
x = Variable("x")

# Non-negative variable
y = Variable("y", lb=0)

# Bounded variable
z = Variable("z", lb=-10, ub=10)

# Binary variable (relaxed to [0, 1])
b = Variable("b", domain="binary")
print(f"Binary: lb={b.lb}, ub={b.ub}")
Binary: lb=0.0, ub=1.0

2 Constant

A fixed numerical value in an expression.

from optyx import Constant

c = Constant(value)

2.1 Parameters

Parameter Type Description
value float | int | np.ndarray The constant value

2.2 Examples

from optyx import Constant, Variable

# Explicit constant
c = Constant(3.14)
print(f"Value: {c.evaluate({})}")

# Constants are created automatically from numbers
x = Variable("x")
expr = 2 * x + 5  # 2 and 5 become Constant nodes
Value: 3.14

3 Mathematical Functions

Transcendental and special functions for building expressions.

from optyx import sin, cos, tan, exp, log, sqrt, abs_, sinh, cosh, tanh
from optyx import asin, acos, atan, asinh, acosh, atanh, log2, log10

3.1 Available Functions

3.1.1 Trigonometric

Function Description Domain
sin(x) Sine All real
cos(x) Cosine All real
tan(x) Tangent \(x \neq \frac{\pi}{2} + n\pi\)

3.1.2 Inverse Trigonometric

Function Description Domain Range
asin(x) Arcsine \([-1, 1]\) \([-\frac{\pi}{2}, \frac{\pi}{2}]\)
acos(x) Arccosine \([-1, 1]\) \([0, \pi]\)
atan(x) Arctangent All real \((-\frac{\pi}{2}, \frac{\pi}{2})\)

3.1.3 Exponential and Logarithmic

Function Description Domain
exp(x) Exponential \(e^x\) All real
log(x) Natural logarithm \(\ln(x)\) \(x > 0\)
log2(x) Base-2 logarithm \(x > 0\)
log10(x) Base-10 logarithm \(x > 0\)
sqrt(x) Square root \(x \geq 0\)

3.1.4 Hyperbolic

Function Description Domain
sinh(x) Hyperbolic sine All real
cosh(x) Hyperbolic cosine All real
tanh(x) Hyperbolic tangent All real

3.1.5 Inverse Hyperbolic

Function Description Domain
asinh(x) Inverse hyperbolic sine All real
acosh(x) Inverse hyperbolic cosine \(x \geq 1\)
atanh(x) Inverse hyperbolic tangent \((-1, 1)\)

3.1.6 Other

Function Description Domain
abs_(x) Absolute value All real

3.2 Examples

from optyx import Variable, sin, cos, exp, log, sqrt

x = Variable("x")
y = Variable("y")

# Trigonometric
f1 = sin(x)**2 + cos(x)**2  # Should equal 1

# Exponential and log
f2 = exp(log(x))  # Should equal x for x > 0

# Composition
f3 = sqrt(x**2 + y**2)  # Euclidean norm

# Evaluate
print(f"sin²(π/4) + cos²(π/4) = {f1.evaluate({'x': 0.785}):.4f}")
sin²(π/4) + cos²(π/4) = 1.0000

4 Expression Operators

Expressions support standard Python operators.

4.1 Arithmetic Operators

Operator Description Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
** Power x ** 2
-x Negation -x

4.2 Comparison Operators (for constraints)

Operator Description Returns
>= Greater or equal Constraint
<= Less or equal Constraint
.eq() Equality Constraint

4.3 Examples

from optyx import Variable

x = Variable("x")
y = Variable("y")

# Build complex expressions
quadratic = x**2 + 2*x*y + y**2
rational = (x + 1) / (y + 1)
polynomial = x**3 - 3*x**2 + 3*x - 1  # (x-1)³

# Create constraints
c1 = x + y >= 10
c2 = x - y <= 5
c3 = (x + y).eq(15)

5 Expression Methods

5.1 .evaluate(values)

Compute the numerical value of an expression.

result = expr.evaluate({"x": 1.0, "y": 2.0})
Parameter Type Description
values dict[str, float] Variable name to value mapping

Returns: float or np.ndarray

5.2 .get_variables()

Get all variables the expression depends on.

variables = expr.get_variables()

Returns: set[Variable]

5.3 Examples

from optyx import Variable

x = Variable("x")
y = Variable("y")
z = Variable("z")

expr = x**2 + y*z

# Evaluate
val = expr.evaluate({"x": 2, "y": 3, "z": 4})
print(f"f(2,3,4) = {val}")

# Get variables
vars = expr.get_variables()
print(f"Variables: {[v.name for v in vars]}")
f(2,3,4) = 16
Variables: ['z', 'x', 'y']