Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider changing x^1 to return x #3454

Closed
odow opened this issue Aug 23, 2023 · 0 comments · Fixed by #3474
Closed

Consider changing x^1 to return x #3454

odow opened this issue Aug 23, 2023 · 0 comments · Fixed by #3474
Labels
Category: Nonlinear Related to nonlinear programming Type: Feature request

Comments

@odow
Copy link
Member

odow commented Aug 23, 2023

We currently have these methods:

JuMP.jl/src/operators.jl

Lines 201 to 232 in 8a856bb

function Base.:^(lhs::AbstractVariableRef, rhs::Integer)
T = value_type(typeof(lhs))
if rhs == 2
return lhs * lhs
elseif rhs == 1
return convert(GenericQuadExpr{T,variable_ref_type(lhs)}, lhs)
elseif rhs == 0
return one(GenericQuadExpr{T,variable_ref_type(lhs)})
else
error(
"Only exponents of 0, 1, or 2 are currently supported. Are you " *
"trying to build a nonlinear problem? Make sure you use " *
"@NLconstraint/@NLobjective.",
)
end
end
function Base.:^(lhs::GenericAffExpr{T}, rhs::Integer) where {T}
if rhs == 2
return lhs * lhs
elseif rhs == 1
return convert(GenericQuadExpr{T,variable_ref_type(lhs)}, lhs)
elseif rhs == 0
return one(GenericQuadExpr{T,variable_ref_type(lhs)})
else
error(
"Only exponents of 0, 1, or 2 are currently supported. Are you " *
"trying to build a nonlinear problem? Make sure you use " *
"@NLconstraint/@NLobjective.",
)
end
end

#3106 will make them somewhat more general by supporting 3+ exponents. In doing so, it makes the return type unstable.

Once merged, we should consider changing x^0 to return one(T) and x^1 to return x.

See also https://github.com/jump-dev/JuMP.jl/pull/3106/files#r1295483658

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Nonlinear Related to nonlinear programming Type: Feature request
Development

Successfully merging a pull request may close this issue.

1 participant