From a0f4b0c528e5341a0849268885ec89403549b2d2 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Wed, 18 Nov 2026 15:30:47 -0600 Subject: [PATCH] Support StaticArrays inputs in the heuristic covers With static axes (`SOneTo`), `zeros(T, ax)` and `fill(x, ax)` return immutable `SVector`s, so `symcover`, `initialize_symcover`, `cover` on non-square or `:geomean`/`:unconstrained` starts, and the `AbsLinear` paths failed with a `setindex!` error on `SMatrix` input. Scratch buffers indexed by the input's axes are now allocated with `similar(Array{T}, ax)` via a `_fill` helper. Bump version to 2.0.2. Assisted-by: Claude Opus 5.5 --- Project.toml | 6 ++++-- src/heuristic_covers.jl | 46 ++++++++++++++++++++--------------------- src/support.jl | 5 +++++ test/runtests.jl | 1 + test/storage_types.jl | 21 +++++++++++++++++++ 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Project.toml b/Project.toml index fdecf60..e937c10 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "MatrixCovers" uuid = "727e6139-ff52-4636-a344-ed1d23e73ffc" -version = "2.0.1" +version = "2.0.2" authors = ["Tim Holy and contributors"] [deps] @@ -33,6 +33,7 @@ PrecompileTools = "1" Random = "1" SparseArrays = "1.10.0" StableRNGs = "1" +StaticArrays = "1" Statistics = "1" Test = "1" Unitful = "1" @@ -47,9 +48,10 @@ Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" +StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [targets] -test = ["Aqua", "DoubleFloats", "ExplicitImports", "HiGHS", "Ipopt", "JuMP", "OffsetArrays", "StableRNGs", "Statistics", "Test", "Unitful"] +test = ["Aqua", "DoubleFloats", "ExplicitImports", "HiGHS", "Ipopt", "JuMP", "OffsetArrays", "StableRNGs", "StaticArrays", "Statistics", "Test", "Unitful"] diff --git a/src/heuristic_covers.jl b/src/heuristic_covers.jl index 96fedf9..6186084 100644 --- a/src/heuristic_covers.jl +++ b/src/heuristic_covers.jl @@ -418,7 +418,7 @@ end # Returns the row support counts. function _sym_unconstrained!(a::AbstractVector{T}, foreach_entries::F) where {T,F} ax = eachindex(a) - nza = zeros(Int, ax) + nza = _fill(0, ax) logref = fill!(similar(a, T), T(NaN)) # NaN marks a row without a reference foreach_entries() do i, j, lv nza[i] += 1 @@ -557,8 +557,8 @@ function unconstrained_min!(::AbsLog{2}, a::AbstractVector, b::AbstractVector, A axes(A, 2) == eachindex(b) || throw(DimensionMismatch("`unconstrained_min!(ρ, a, b, A)` requires column indices of `A` to match `b`, got axes(A, 2)=$(string(axes(A, 2))), axes(b)=$(string(axes(b)))")) loga = fill!(similar(a, T), zero(T)) logb = fill!(similar(b, T), zero(T)) - nza = zeros(Int, axes(A, 1)) - nzb = zeros(Int, axes(A, 2)) + nza = _fill(0, axes(A, 1)) + nzb = _fill(0, axes(A, 2)) foreach_support(A) do i, j, v lAij = log(T(v)) loga[i] += lAij @@ -590,8 +590,8 @@ function unconstrained_min!(::AbsLog{2}, a::AbstractVector, b::AbstractVector, s is, js, lv = sup.is, sup.js, sup.lv loga = fill!(similar(a, T), zero(T)) logb = fill!(similar(b, T), zero(T)) - nza = zeros(Int, eachindex(a)) - nzb = zeros(Int, eachindex(b)) + nza = _fill(0, eachindex(a)) + nzb = _fill(0, eachindex(b)) for k in _eachindex(is, js, lv) i, j, lAij = is[k], js[k], lv[k] loga[i] += lAij @@ -844,8 +844,8 @@ end function _cg_refine!(lα::AbstractVector{T}, lβ::AbstractVector{T}, foreach_entries::F, maxiter::Int) where {T,F} maxiter > 0 || return lα, lβ axa, axb = eachindex(lα), eachindex(lβ) - na = zeros(Int, axa) - nb = zeros(Int, axb) + na = _fill(0, axa) + nb = _fill(0, axb) ra = fill!(similar(lα, T), zero(T)) rb = fill!(similar(lβ, T), zero(T)) # Accumulate support counts and off-diagonal residuals. @@ -1036,8 +1036,8 @@ function tighten_cover!(a::AbstractVector, b::AbstractVector, A::AbstractMatrix; T = float(promote_type(eltype(a), eltype(b))) eachindex(a) == axes(A, 1) || throw(DimensionMismatch("indices of a must match row-indexing of A")) eachindex(b) == axes(A, 2) || throw(DimensionMismatch("indices of b must match column-indexing of A")) - lratioa = fill(T(Inf), eachindex(a)) - lratiob = fill(T(Inf), eachindex(b)) + lratioa = _fill(T(Inf), eachindex(a)) + lratiob = _fill(T(Inf), eachindex(b)) la, lb = similar(a, T), similar(b, T) for _ in 1:maxiter map!(log, la, a) # log(0) = -Inf marks zero scales; see below @@ -1074,8 +1074,8 @@ end function tighten_cover!(a::AbstractVector, b::AbstractVector, sup::FlatSupport; maxiter::Int=3) T = float(promote_type(eltype(a), eltype(b))) is, js, lv = sup.is, sup.js, sup.lv - lratioa = fill(T(Inf), eachindex(a)) - lratiob = fill(T(Inf), eachindex(b)) + lratioa = _fill(T(Inf), eachindex(a)) + lratiob = _fill(T(Inf), eachindex(b)) la, lb = similar(a, T), similar(b, T) for _ in 1:maxiter map!(log, la, a) # log(0) = -Inf marks zero scales; see the matrix method @@ -1134,7 +1134,7 @@ function boost_feasible!(a::AbstractVector{T}, A::AbstractMatrix) where T # Growing log-scales directly (rather than multiplying by exp(z)) stays # finite even when exp(z) alone would overflow. la = map(log, a) - s = fill(zero(T), eachindex(a)) + s = _fill(zero(T), eachindex(a)) foreach_support_sym(A) do i, j, v z = _pos_deficit(log(T(v)), la[i], la[j]) s[i] += z @@ -1145,7 +1145,7 @@ function boost_feasible!(a::AbstractVector{T}, A::AbstractMatrix) where T all(isfinite, s) || throw(ArgumentError("boost_feasible! requires a start with positive scale on every supported row")) map!(sqrt, s, s) - r = fill(zero(T), eachindex(a)) + r = _fill(zero(T), eachindex(a)) foreach_support_sym(A) do i, j, v z = _pos_deficit(log(T(v)), la[i], la[j]) zi, zj = _split_deficit(z, s[i], s[j]) @@ -1164,7 +1164,7 @@ end function boost_feasible!(a::AbstractVector{T}, sup::FlatSupport) where T is, js, lv = sup.is, sup.js, sup.lv la = map(log, a) - s = fill(zero(T), eachindex(a)) + s = _fill(zero(T), eachindex(a)) for k in _eachindex(is, js, lv) i, j = is[k], js[k] z = _pos_deficit(lv[k], la[i], la[j]) @@ -1174,7 +1174,7 @@ function boost_feasible!(a::AbstractVector{T}, sup::FlatSupport) where T all(isfinite, s) || throw(ArgumentError("boost_feasible! requires a start with positive scale on every supported row")) map!(sqrt, s, s) - r = fill(zero(T), eachindex(a)) + r = _fill(zero(T), eachindex(a)) for k in _eachindex(is, js, lv) i, j = is[k], js[k] z = _pos_deficit(lv[k], la[i], la[j]) @@ -1196,8 +1196,8 @@ end function boost_feasible!(a::AbstractVector, b::AbstractVector, A::AbstractMatrix) T = float(promote_type(eltype(a), eltype(b))) la, lb = map(log, a), map(log, b) - sa = fill(zero(T), eachindex(a)) - sb = fill(zero(T), eachindex(b)) + sa = _fill(zero(T), eachindex(a)) + sb = _fill(zero(T), eachindex(b)) foreach_support(A) do i, j, v z = _pos_deficit(log(T(v)), la[i], lb[j]) sa[i] += z @@ -1207,8 +1207,8 @@ function boost_feasible!(a::AbstractVector, b::AbstractVector, A::AbstractMatrix throw(ArgumentError("boost_feasible! requires a start with positive scale on every supported row/column")) map!(sqrt, sa, sa) map!(sqrt, sb, sb) - ra = fill(zero(T), eachindex(a)) - rb = fill(zero(T), eachindex(b)) + ra = _fill(zero(T), eachindex(a)) + rb = _fill(zero(T), eachindex(b)) foreach_support(A) do i, j, v z = _pos_deficit(log(T(v)), la[i], lb[j]) zi, zj = _split_deficit(z, sa[i], sb[j]) @@ -1225,8 +1225,8 @@ function boost_feasible!(a::AbstractVector, b::AbstractVector, sup::FlatSupport) T = float(promote_type(eltype(a), eltype(b))) is, js, lv = sup.is, sup.js, sup.lv la, lb = map(log, a), map(log, b) - sa = fill(zero(T), eachindex(a)) - sb = fill(zero(T), eachindex(b)) + sa = _fill(zero(T), eachindex(a)) + sb = _fill(zero(T), eachindex(b)) for k in _eachindex(is, js, lv) z = _pos_deficit(lv[k], la[is[k]], lb[js[k]]) sa[is[k]] += z @@ -1236,8 +1236,8 @@ function boost_feasible!(a::AbstractVector, b::AbstractVector, sup::FlatSupport) throw(ArgumentError("boost_feasible! requires a start with positive scale on every supported row/column")) map!(sqrt, sa, sa) map!(sqrt, sb, sb) - ra = fill(zero(T), eachindex(a)) - rb = fill(zero(T), eachindex(b)) + ra = _fill(zero(T), eachindex(a)) + rb = _fill(zero(T), eachindex(b)) for k in _eachindex(is, js, lv) i, j = is[k], js[k] z = _pos_deficit(lv[k], la[i], lb[j]) diff --git a/src/support.jl b/src/support.jl index d100dcd..79f6ddb 100644 --- a/src/support.jl +++ b/src/support.jl @@ -27,6 +27,11 @@ end @noinline _throw_eachindex_mismatch(inds, other) = throw(DimensionMismatch(LazyString("all inputs to eachindex must have the same indices, got ", inds, " and ", other))) +# Mutable buffer indexed by `ax` with every entry `x`. Not `fill(x, ax)` or +# `zeros(T, ax)`: for some axis types (StaticArrays' `SOneTo`) those return an +# immutable array. +_fill(x, ax) = fill!(similar(Array{typeof(x)}, ax), x) + """ foreach_support(f, A) diff --git a/test/runtests.jl b/test/runtests.jl index ba22d04..795ee77 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,7 @@ using SparseArrays using Unitful # triggers MatrixCoversUnitfulExt extension using LinearAlgebra using OffsetArrays +using StaticArrays: SMatrix, MVector using DoubleFloats: Double64 using Statistics: median using Random: MersenneTwister, randperm diff --git a/test/storage_types.jl b/test/storage_types.jl index 3163d7f..2c59759 100644 --- a/test/storage_types.jl +++ b/test/storage_types.jl @@ -271,3 +271,24 @@ end @test length(emitted) == length(unique(e -> (e[1], e[2]), emitted)) end end + +# Static axes (`SOneTo`) make `zeros`/`fill` return immutable arrays, so the +# solvers' scratch buffers must be allocated with `similar`. +@testset "StaticArrays" begin + Ssym = SMatrix{3,3}([4.0 1 0.5; 1 9 2; 0.5 2 16]) + Ssq = SMatrix{3,3}([1.0 2 3; 6 5 4; 0.1 7 2]) + Srect = SMatrix{2,3}([1.0 2 3; 6 5 4]) + @test symcover(Ssym) ≈ symcover(Matrix(Ssym)) + @test symcover!(MVector{3,Float64}(undef), Ssym) ≈ symcover(Matrix(Ssym)) + @test initialize_symcover(Ssym) ≈ initialize_symcover(Matrix(Ssym)) + @test soft_symcover(Ssym)[1] ≈ soft_symcover(Matrix(Ssym))[1] + @test symcover_min(AbsLog{2}(), Ssym)[1] ≈ symcover_min(AbsLog{2}(), Matrix(Ssym))[1] + for A in (Ssq, Srect) + @test all(map(≈, cover(A), cover(Matrix(A)))) + @test all(map(≈, cover(A; start=:geomean), cover(Matrix(A); start=:geomean))) + @test all(map(≈, initialize_cover(A), initialize_cover(Matrix(A)))) + @test soft_cover(A)[1] ≈ soft_cover(Matrix(A))[1] + @test cover_min(AbsLog{2}(), A)[1] ≈ cover_min(AbsLog{2}(), Matrix(A))[1] + end + @test cover_transversal(Ssq)[1] ≈ cover_transversal(Matrix(Ssq))[1] +end