From 88740928a3cef626feacf0417366323ae3026890 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 30 Oct 2023 13:08:01 +1300 Subject: [PATCH] Fix docs --- docs/src/manual/variables.md | 19 ------------------- src/aff_expr.jl | 6 ++++-- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/docs/src/manual/variables.md b/docs/src/manual/variables.md index aa69b829841..ce8d6cf11a5 100644 --- a/docs/src/manual/variables.md +++ b/docs/src/manual/variables.md @@ -1214,25 +1214,6 @@ julia> @variable(model, x[1:2, 1:2] in SkewSymmetricMatrixSpace()) `model`; the remaining elements in `x` are linear transformations of the single variable. -Because the returned matrix `x` is `Matrix{AffExpr}`, you cannot use -variable-related functions on its elements: -```jldoctest skewsymmetric -julia> set_lower_bound(x[1, 2], 0.0) -ERROR: MethodError: no method matching set_lower_bound(::AffExpr, ::Float64) -[...] -``` - -Instead, you can convert an upper-triangular elements to a variable as follows: -```jldoctest skewsymmetric -julia> to_variable(x::AffExpr) = first(keys(x.terms)) -to_variable (generic function with 1 method) - -julia> to_variable(x[1, 2]) -x[1,2] - -julia> set_lower_bound(to_variable(x[1, 2]), 0.0) -``` - ### Example: Hermitian positive semidefinite variables Declare a matrix of JuMP variables to be Hermitian positive semidefinite using diff --git a/src/aff_expr.jl b/src/aff_expr.jl index cfb54aaef51..faa7a5c0f15 100644 --- a/src/aff_expr.jl +++ b/src/aff_expr.jl @@ -824,13 +824,15 @@ function _eval_as_variable(f::F, x::GenericAffExpr, args...) where {F} "Cannot call $f with $x because it is not a real-valued affine " * "expression of one variable.", ) - elseif !isone(first(values(x.terms))) + end + variable, coefficient = first(x.terms) + if !isone(coefficient) error( "Cannot call $f with $x because it is not a real-valued affine " * "expression of one variable with a coefficient of `+1`.", ) end - return f(first(keys(x.terms)), args...) + return f(variable, args...) end function _eval_as_variable(