Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "MatrixCovers"
uuid = "727e6139-ff52-4636-a344-ed1d23e73ffc"
version = "2.0.1"
version = "2.0.2"
authors = ["Tim Holy <tim.holy@gmail.com> and contributors"]

[deps]
Expand Down Expand Up @@ -33,6 +33,7 @@ PrecompileTools = "1"
Random = "1"
SparseArrays = "1.10.0"
StableRNGs = "1"
StaticArrays = "1"
Statistics = "1"
Test = "1"
Unitful = "1"
Expand All @@ -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"]
46 changes: 23 additions & 23 deletions src/heuristic_covers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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
Expand All @@ -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])
Expand Down
5 changes: 5 additions & 0 deletions src/support.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/storage_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading