Skip to content

Commit

Permalink
Fix errors for LinearAlgebra operators of JuMP arrays (#3476)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 4, 2023
1 parent 58ff877 commit 7b98ffc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,3 +1111,30 @@ 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(::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(::AbstractMatrix{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:qr))
end
16 changes: 16 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,18 @@ 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)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.qr(x)
y = 2.0 .* x[:, 2] .+ 1.0
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.norm(y)
@test_throws MOI.UnsupportedNonlinearOperator LinearAlgebra.nullspace(y)
return
end

end # module

0 comments on commit 7b98ffc

Please sign in to comment.