From 4e43811ea3385a3d7abaab8ca4796bd03fe50dec Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Fri, 17 Mar 2023 15:53:36 +0100 Subject: [PATCH] fix a nasty bug with negatives in ConstraintMatrix --- src/constraint_matrix.jl | 13 +++++++++---- test/constratint_matrices.jl | 14 +++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/constraint_matrix.jl b/src/constraint_matrix.jl index 8fbd36a..5734837 100644 --- a/src/constraint_matrix.jl +++ b/src/constraint_matrix.jl @@ -50,10 +50,15 @@ ConstraintMatrix(nzeros::AbstractArray{<:Integer}, n, m, val::T) where {T} = Base.size(cm::ConstraintMatrix) = cm.size -__get_positive(cm::ConstraintMatrix, idx::Integer) = - convert(eltype(cm), cm.val * length(searchsorted(cm.pos, idx))) -__get_negative(cm::ConstraintMatrix, idx::Integer) = - convert(eltype(cm), cm.val * length(searchsorted(cm.neg, idx))) +function __get_positive(cm::ConstraintMatrix, idx::Integer) + return convert(eltype(cm), cm.val * length(searchsorted(cm.pos, idx))) +end +function __get_negative(cm::ConstraintMatrix, idx::Integer) + return convert( + eltype(cm), + cm.val * length(searchsorted(cm.neg, idx; rev = true)), + ) +end Base.@propagate_inbounds function Base.getindex( cm::ConstraintMatrix, diff --git a/test/constratint_matrices.jl b/test/constratint_matrices.jl index afdee61..1d04876 100644 --- a/test/constratint_matrices.jl +++ b/test/constratint_matrices.jl @@ -1,12 +1,17 @@ @testset "ConstraintMatrix" begin - @test PropertyT.ConstraintMatrix{Float64}([-1, 2, -1, 1, 4, 2, 6], 3, 2, π) isa AbstractMatrix + @test PropertyT.ConstraintMatrix{Float64}( + [-1, 2, -1, 1, 4, 2, 6], + 3, + 2, + π, + ) isa AbstractMatrix cm = PropertyT.ConstraintMatrix{Float64}([-1, 2, -1, 1, 4, 2, 6], 3, 2, π) @test cm == Float64[ -π π - 2π 0.0 - 0.0 π + 2π 0 + 0 π ] @test collect(PropertyT.nzpairs(cm)) == [ @@ -18,4 +23,7 @@ 1 => -3.141592653589793 1 => -3.141592653589793 ] + + @test PropertyT.ConstraintMatrix{Float64}([-9:-1; 1:9], 3, 3, 1.0) == + zeros(3, 3) end