From bcac4310317cd8f7008895a993f2274833167efa Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 2 Nov 2023 22:27:40 +1100 Subject: [PATCH 1/7] Add errors for Matrix +- AbstractJuMPScalar --- src/operators.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/operators.jl b/src/operators.jl index 5310234ddc0..2478a89fb36 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::Matrix, x::AbstractJuMPScalar) + return error( + "Addition between a Matrix and a JuMP variable is not supported: instead of A + x, " * + "prefer 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::Matrix) = A + x + +function Base.:-(A::Matrix, x::AbstractJuMPScalar) + return error( + "Subtraction between a Matrix and a JuMP variable is not supported: instead of A - x, " * + "prefer 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::Matrix) = A - x \ No newline at end of file From b15f2cce5688bb3110e1c4ea951f3732d3b67005 Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 2 Nov 2023 22:37:44 +1100 Subject: [PATCH 2/7] Add code escaping --- src/operators.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index 2478a89fb36..38d1da77db1 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -454,9 +454,9 @@ end function Base.:+(A::Matrix, x::AbstractJuMPScalar) return error( - "Addition between a Matrix and a JuMP variable is not supported: instead of A + x, " * - "prefer 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." + "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * + "prefer `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 @@ -464,9 +464,9 @@ Base.:+(x::AbstractJuMPScalar, A::Matrix) = A + x function Base.:-(A::Matrix, x::AbstractJuMPScalar) return error( - "Subtraction between a Matrix and a JuMP variable is not supported: instead of A - x, " * - "prefer 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." + "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * + "prefer `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 From 74cc3f0f93a7ff79c9e0b78fb0ad15a271dba177 Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 2 Nov 2023 23:12:17 +1100 Subject: [PATCH 3/7] Format file. --- src/operators.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index 38d1da77db1..7be20f13fed 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -456,7 +456,7 @@ function Base.:+(A::Matrix, x::AbstractJuMPScalar) return error( "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * "prefer `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." + "do `A + x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", ) end @@ -466,8 +466,8 @@ function Base.:-(A::Matrix, x::AbstractJuMPScalar) return error( "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * "prefer `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." + "do `A - x * LinearAlgebra.I(n)`, where `n` is the diagonal length.", ) end -Base.:-(x::AbstractJuMPScalar, A::Matrix) = A - x \ No newline at end of file +Base.:-(x::AbstractJuMPScalar, A::Matrix) = A - x From ca48481fc785a6d155a180af21fc9f2a47d149db Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 2 Nov 2023 23:51:19 +1100 Subject: [PATCH 4/7] Add test --- test/test_operator.jl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/test_operator.jl b/test/test_operator.jl index 967fb8b666d..f097cf5c72a 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`, " * + "prefer `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`, " * + "prefer `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`, " * + "prefer `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`, " * + "prefer `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 From fb4b15186c3734dd61742547354fbe6976674485 Mon Sep 17 00:00:00 2001 From: James Foster Date: Thu, 2 Nov 2023 23:58:33 +1100 Subject: [PATCH 5/7] "prefer" -> "do" --- src/operators.jl | 4 ++-- test/test_operator.jl | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index 7be20f13fed..f110a990997 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -455,7 +455,7 @@ end function Base.:+(A::Matrix, x::AbstractJuMPScalar) return error( "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * - "prefer `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "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 @@ -465,7 +465,7 @@ Base.:+(x::AbstractJuMPScalar, A::Matrix) = A + x function Base.:-(A::Matrix, x::AbstractJuMPScalar) return error( "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * - "prefer `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "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 diff --git a/test/test_operator.jl b/test/test_operator.jl index f097cf5c72a..4f43a05891a 100644 --- a/test/test_operator.jl +++ b/test/test_operator.jl @@ -632,7 +632,7 @@ function test_matrix_abstractscalar_add() @test_throws( ErrorException( "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * - "prefer `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "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 @@ -640,7 +640,7 @@ function test_matrix_abstractscalar_add() @test_throws( ErrorException( "Addition between a Matrix and a JuMP variable is not supported: instead of `A + x`, " * - "prefer `A .+ x` for element-wise addition, or if you are modifying the diagonal entries of the matrix " * + "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 @@ -648,7 +648,7 @@ function test_matrix_abstractscalar_add() @test_throws( ErrorException( "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * - "prefer `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "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 @@ -656,7 +656,7 @@ function test_matrix_abstractscalar_add() @test_throws( ErrorException( "Subtraction between a Matrix and a JuMP variable is not supported: instead of `A - x`, " * - "prefer `A .- x` for element-wise subtraction, or if you are modifying the diagonal entries of the matrix " * + "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 From a7cc3b906137d7fa5eb9f976b8f3d2a23c29b2cf Mon Sep 17 00:00:00 2001 From: James Foster <38274066+jd-foster@users.noreply.github.com> Date: Fri, 3 Nov 2023 07:19:37 +1100 Subject: [PATCH 6/7] Update src/operators.jl Co-authored-by: Oscar Dowson --- src/operators.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/operators.jl b/src/operators.jl index f110a990997..bd65f43a5cf 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -452,7 +452,7 @@ function LinearAlgebra.issymmetric(x::Matrix{T}) where {T<:_JuMPTypes} return true end -function Base.:+(A::Matrix, x::AbstractJuMPScalar) +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 " * From ff19d49b3b42321cc42e4bc7ecd4a912ee419690 Mon Sep 17 00:00:00 2001 From: James Foster <38274066+jd-foster@users.noreply.github.com> Date: Fri, 3 Nov 2023 07:20:33 +1100 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Oscar Dowson --- src/operators.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/operators.jl b/src/operators.jl index bd65f43a5cf..608dacc5028 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -460,9 +460,9 @@ function Base.:+(A::AbstractMatrix, x::AbstractJuMPScalar) ) end -Base.:+(x::AbstractJuMPScalar, A::Matrix) = A + x +Base.:+(x::AbstractJuMPScalar, A::AbstractMatrix) = A + x -function Base.:-(A::Matrix, x::AbstractJuMPScalar) +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 " * @@ -470,4 +470,4 @@ function Base.:-(A::Matrix, x::AbstractJuMPScalar) ) end -Base.:-(x::AbstractJuMPScalar, A::Matrix) = A - x +Base.:-(x::AbstractJuMPScalar, A::AbstractMatrix) = A - x