From 944e5c5d3d6a035380fcd6d40c330af30675125b Mon Sep 17 00:00:00 2001 From: James Foster <38274066+jd-foster@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:24:41 +1100 Subject: [PATCH] =?UTF-8?q?Add=20error=20message=20for=20Matrix=20=C2=B1?= =?UTF-8?q?=20AbstractJuMPScalar=20(#3557)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/operators.jl | 20 ++++++++++++++++++++ test/test_operator.jl | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/operators.jl b/src/operators.jl index 5310234ddc0..608dacc5028 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -451,3 +451,23 @@ function LinearAlgebra.issymmetric(x::Matrix{T}) where {T<:_JuMPTypes} end return true end + +function Base.:+(A::AbstractMatrix, x::AbstractJuMPScalar) + return error( + "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * + "do `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "do `A + x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ) +end + +Base.:+(x::AbstractJuMPScalar, A::AbstractMatrix) = A + x + +function Base.:-(A::AbstractMatrix, x::AbstractJuMPScalar) + return error( + "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * + "do `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "do `A - x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ) +end + +Base.:-(x::AbstractJuMPScalar, A::AbstractMatrix) = A - x diff --git a/test/test_operator.jl b/test/test_operator.jl index 967fb8b666d..4f43a05891a 100644 --- a/test/test_operator.jl +++ b/test/test_operator.jl @@ -625,4 +625,43 @@ function test_complex_pow() return end +function test_matrix_abstractscalar_add() + model = Model() + @variable(model, x) + A = rand(Float64, 2, 2) + @test_throws( + ErrorException( + "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * + "do `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "do `A + x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ), + A + x + ), + @test_throws( + ErrorException( + "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * + "do `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "do `A + x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ), + x + A + ), + @test_throws( + ErrorException( + "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * + "do `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "do `A - x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ), + A - x + ), + @test_throws( + ErrorException( + "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * + "do `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "do `A - x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", + ), + x - A + ), + return +end + end