Nonlinear Expression Helper Functions#
This section covers functions in the gurobipy.nlfunc
module. Each
function can be called with any modeling object and returns an
NLExpr
or MNLExpr
representing the resulting
nonlinear expression. The resulting expression can be used to add
nonlinear constraints to the model. For example:
import gurobipy as gp
from gurobipy import GRB, nlfunc
with gp.Env() as env, gp.Model(env=env) as model:
x = model.addVar(lb=-GRB.INFINITY, name="x")
y = model.addVar(lb=-GRB.INFINITY, name="y")
z = model.addVar(lb=-GRB.INFINITY, name="z")
# Create a constraint specifying z = sin(x + y)
model.addConstr(z == nlfunc.sin(x + y))
- gurobipy.nlfunc.sqrt(expr)#
Return an expression representing the square root of the argument.
- Parameters:
expr – A modeling object or numeric value
- Returns:
If the argument is a term-based modeling object or numeric value, returns an
NLExpr
. If the argument is a matrix-friendly modeling object, returns anMNLExpr
.
Also note the additional information on how Gurobi handles the square root function internally.
- gurobipy.nlfunc.sin(expr)#
Return an expression representing the sine of the argument.
- gurobipy.nlfunc.cos(expr)#
Return an expression representing the cosine of the argument.
- gurobipy.nlfunc.tan(expr)#
Return an expression representing the tangent function of the argument.
- gurobipy.nlfunc.exp(expr)#
Return an expression representing the exponential function of the argument.
- gurobipy.nlfunc.log(expr)#
Return an expression representing the natural logarithm of the argument.
- Parameters:
expr – A modeling object or numeric value
- Returns:
If the argument is a term-based modeling object or numeric value, returns an
NLExpr
. If the argument is a matrix-friendly modeling object, returns anMNLExpr
.
Also note the additional information on how Gurobi handles the logarithm functions internally.
- gurobipy.nlfunc.log2(expr)#
Return an expression representing the base 2 logarithm of the argument.
- Parameters:
expr – A modeling object or numeric value
- Returns:
If the argument is a term-based modeling object or numeric value, returns an
NLExpr
. If the argument is a matrix-friendly modeling object, returns anMNLExpr
.
Also note the additional information on how Gurobi handles the logarithm functions internally.
- gurobipy.nlfunc.log10(expr)#
Return an expression representing the base 10 logarithm of the argument.
- Parameters:
expr – A modeling object or numeric value
- Returns:
If the argument is a term-based modeling object or numeric value, returns an
NLExpr
. If the argument is a matrix-friendly modeling object, returns anMNLExpr
.
Also note the additional information on how Gurobi handles the logarithm functions internally.
- gurobipy.nlfunc.logistic(expr)#
Return an expression representing the logistic function of the argument.
- gurobipy.nlfunc.square(expr)#
Return an expression representing the square of the argument.