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

Fix errors for LinearAlgebra operators of JuMP arrays #3476

Merged
merged 5 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,3 +1111,34 @@ end
function MOI.VectorNonlinearFunction(f::Vector{<:AbstractJuMPScalar})
return MOI.VectorNonlinearFunction(map(moi_function, f))
end

# LinearAlgebra overloads to throw nicer error messages. These may be changed to
# return expressions in the future.

function LinearAlgebra.det(::AbstractMatrix{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:det))
end

function LinearAlgebra.logdet(::AbstractVector{<:AbstractJuMPScalar})
odow marked this conversation as resolved.
Show resolved Hide resolved
return throw(MOI.UnsupportedNonlinearOperator(:logdet))
end

function LinearAlgebra.logdet(::AbstractMatrix{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:logdet))
end

function LinearAlgebra.norm(::AbstractArray{<:AbstractJuMPScalar}, ::Real)
return throw(MOI.UnsupportedNonlinearOperator(:norm))
end

function LinearAlgebra.nullspace(::AbstractVector{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:nullspace))
end

function LinearAlgebra.nullspace(::AbstractMatrix{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:nullspace))
end

function LinearAlgebra.qr(::AbstractVector{<:AbstractJuMPScalar})
odow marked this conversation as resolved.
Show resolved Hide resolved
return throw(MOI.UnsupportedNonlinearOperator(:qr))
end
17 changes: 17 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module TestNLPExpr
using JuMP
using Test

import LinearAlgebra

function test_extension_univariate_operators(
ModelType = Model,
VariableRefType = VariableRef,
Expand Down Expand Up @@ -828,4 +830,19 @@ function test_redefinition_of_function()
return
end

function test_linear_algebra_errors()
model = Model()
@variable(model, x[1:2, 1:2])
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.det(x)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.logdet(x)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.norm(x)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.nullspace(x)
y = 2.0 .* x[:, 2] .+ 1.0
odow marked this conversation as resolved.
Show resolved Hide resolved
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.logdet(y)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.norm(y)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.nullspace(y)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.qr(y)
return
end

end # module