# Expressions

More information can be found in the Expressions section of the manual.

## Macros

JuMP.@expressionMacro
@expression(args...)

Efficiently builds a linear or quadratic expression but does not add to model immediately. Instead, returns the expression which can then be inserted in other constraints. For example:

@expression(m, shared, sum(i*x[i] for i=1:5))
@constraint(m, shared + y >= 5)
@constraint(m, shared + z <= 10)

The ref accepts index sets in the same way as @variable, and those indices can be used in the construction of the expressions:

@expression(m, expr[i=1:3], i*sum(x[j] for j=1:3))

Anonymous syntax is also supported:

expr = @expression(m, [i=1:3], i*sum(x[j] for j=1:3))
source
JuMP.@expressionsMacro
@expressions(model, args...)

Adds multiple expressions to model at once, in the same fashion as the @expression macro.

The model must be the first argument, and multiple variables can be added on multiple lines wrapped in a begin ... end block.

Examples

@expressions(model, begin
my_expr, x^2 + y^2
my_expr_1[i = 1:2], a[i] - z[i]
end)
source

## Affine expressions

JuMP.GenericAffExprType
mutable struct GenericAffExpr{CoefType,VarType} <: AbstractJuMPScalar
constant::CoefType
terms::OrderedDict{VarType,CoefType}
end

An expression type representing an affine expression of the form: $\sum a_i x_i + c$.

Fields

• .constant: the constant c in the expression.
• .terms: an OrderedDict, with keys of VarType and values of CoefType describing the sparse vector a.
source
JuMP.linear_termsFunction
linear_terms(aff::GenericAffExpr{C, V})

Provides an iterator over coefficient-variable tuples (a_i::C, x_i::V) in the linear part of the affine expression.

source
linear_terms(quad::GenericQuadExpr{C, V})

Provides an iterator over tuples (coefficient::C, variable::V) in the linear part of the quadratic expression.

source

JuMP.GenericQuadExprType
mutable struct GenericQuadExpr{CoefType,VarType} <: AbstractJuMPScalar
aff::GenericAffExpr{CoefType,VarType}
terms::OrderedDict{UnorderedPair{VarType}, CoefType}
end

An expression type representing an quadratic expression of the form: $\sum q_{i,j} x_i x_j + \sum a_i x_i + c$.

Fields

• .aff: an GenericAffExpr representing the affine portion of the expression.
• .terms: an OrderedDict, with keys of UnorderedPair{VarType} and values of CoefType, describing the sparse list of terms q.
source
JuMP.quad_termsFunction
quad_terms(quad::GenericQuadExpr{C, V})

Provides an iterator over tuples (coefficient::C, var_1::V, var_2::V) in the quadratic part of the quadratic expression.

source

## Utilities and modifications

JuMP.constantFunction
constant(aff::GenericAffExpr{C, V})::C

Return the constant of the affine expression.

source
constant(aff::GenericQuadExpr{C, V})::C

Return the constant of the quadratic expression.

source
JuMP.coefficientFunction
coefficient(v1::VariableRef, v2::VariableRef)

Return 1.0 if v1 == v2, and 0.0 otherwise.

This is a fallback for other coefficient methods to simplify code in which the expression may be a single variable.

source
coefficient(a::GenericAffExpr{C,V}, v::V) where {C,V}

Return the coefficient associated with variable v in the affine expression a.

source
coefficient(a::GenericAffExpr{C,V}, v1::V, v2::V) where {C,V}

Return the coefficient associated with the term v1 * v2 in the quadratic expression a.

Note that coefficient(a, v1, v2) is the same as coefficient(a, v2, v1).

source
coefficient(a::GenericQuadExpr{C,V}, v::V) where {C,V}

Return the coefficient associated with variable v in the affine component of a.

source
JuMP.isequal_canonicalFunction
isequal_canonical(
aff::GenericAffExpr{C,V},
other::GenericAffExpr{C,V}
) where {C,V}

Return true if aff is equal to other after dropping zeros and disregarding the order. Mainly useful for testing.

source
JuMP.add_to_expression!Function
add_to_expression!(expression, terms...)

Updates expression in place to expression + (*)(terms...). This is typically much more efficient than expression += (*)(terms...). For example, add_to_expression!(expression, a, b) produces the same result as expression += a*b, and add_to_expression!(expression, a) produces the same result as expression += a.

Only a few methods are defined, mostly for internal use, and only for the cases when (1) they can be implemented efficiently and (2) expression is capable of storing the result. For example, add_to_expression!(::AffExpr, ::VariableRef, ::VariableRef) is not defined because a GenericAffExpr cannot store the product of two variables.

source
JuMP.drop_zeros!Function
drop_zeros!(expr::GenericAffExpr)

Remove terms in the affine expression with 0 coefficients.

source
drop_zeros!(expr::GenericQuadExpr)

Remove terms in the quadratic expression with 0 coefficients.

source
JuMP.map_coefficientsFunction
map_coefficients(f::Function, a::GenericAffExpr)

Apply f to the coefficients and constant term of an GenericAffExpr a and return a new expression.

See also: map_coefficients_inplace!

Example

julia> a = GenericAffExpr(1.0, x => 1.0)
x + 1

julia> map_coefficients(c -> 2 * c, a)
2 x + 2

julia> a
x + 1
source
map_coefficients(f::Function, a::GenericQuadExpr)

Apply f to the coefficients and constant term of an GenericQuadExpr a and return a new expression.

See also: map_coefficients_inplace!

Example

julia> a = @expression(model, x^2 + x + 1)
x² + x + 1

julia> map_coefficients(c -> 2 * c, a)
2 x² + 2 x + 2

julia> a
x² + x + 1
source
JuMP.map_coefficients_inplace!Function
map_coefficients_inplace!(f::Function, a::GenericAffExpr)

Apply f to the coefficients and constant term of an GenericAffExpr a and update them in-place.

See also: map_coefficients

Example

julia> a = GenericAffExpr(1.0, x => 1.0)
x + 1

julia> map_coefficients_inplace!(c -> 2 * c, a)
2 x + 2

julia> a
2 x + 2
source
map_coefficients_inplace!(f::Function, a::GenericQuadExpr)

Apply f to the coefficients and constant term of an GenericQuadExpr a and update them in-place.

See also: map_coefficients

Example

julia> a = @expression(model, x^2 + x + 1)
x² + x + 1

julia> map_coefficients_inplace!(c -> 2 * c, a)
2 x² + 2 x + 2

julia> a
2 x² + 2 x + 2
source