diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index 6fe42fcb5ee..ed43347a54a 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -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 diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index 53b6704073e..9fc10703621 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -8,6 +8,8 @@ module TestNLPExpr using JuMP using Test +import LinearAlgebra + function test_extension_univariate_operators( ModelType = Model, VariableRefType = VariableRef, @@ -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