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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstraintCommons"
uuid = "e37357d9-0691-492f-a822-e5ea6a920954"
authors = ["azzaare (jean-François BAFFIER)"]
version = "0.2.3"
version = "0.3.0"

[deps]
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Expand Down
58 changes: 38 additions & 20 deletions src/extrema.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
"""
δ_extrema(X...)
Compute both the difference between the maximum and the minimum of over all the collections of `X`.
δ_extrema(X)

Compute the difference between the overall maximum and overall minimum value found
within all elements of the collection(s) passed in `X`.
"""
function δ_extrema(X)
mn, mx = extrema(Iterators.flatten(X))
return mx - mn
end

function δ_extrema(X, Y)
minx, maxx = extrema(Iterators.flatten(X))
miny, maxy = extrema(Iterators.flatten(Y))
return max(maxx, maxy) - min(minx, miny)
end

function δ_extrema(X...)
M = map(extrema ∘ Iterators.flatten, X)
_minmax(t1, t2) = min(t1[1], t2[1]), max(t1[2], t2[2])
m = reduce(_minmax, M; init = (Inf, -Inf))
m = extrema(Iterators.flatten(X); init = (Inf, -Inf))
return m[2] - m[1]
end

# SECTION - Test Items for δ_extrema
@testitem "δ_extrema" tags=[:δ_extrema] begin
X = map(_ -> rand(1:100, 100), 1:3)
# Test case 1: Single non-nested collection
@test δ_extrema([1, 5, 2, 10]) == 10 - 1

# Test case 2: Single nested collection
@test δ_extrema([[1, 5], [2, 10]]) == 10 - 1

# Test case 5: Multiple collections from the original example
X = map(_ -> rand(1:100, 100), 1:3) # Creates a Vector of Vectors
expected_min = minimum(minimum(v) for v in X)
expected_max = maximum(maximum(v) for v in X)
expected_delta = expected_max - expected_min

@test δ_extrema(X[1]) == maximum(X[1]) - minimum(X[1])
@test δ_extrema(X[1:2]) ==
max(maximum(X[1]), maximum(X[2])) - min(minimum(X[1]), minimum(X[2]))
@test δ_extrema(X) == expected_delta

# Test range constraints from original example
@test 0 ≤ δ_extrema(X[1]) ≤ 99 # Max diff in 1:100 is 99
@test 0 ≤ δ_extrema(X[1:2]) ≤ 99
@test 0 ≤ δ_extrema(X) ≤ 99

# Test case 7: Different data types (crucial for testing stability)
@test δ_extrema([[1.0, 5.5], [2, 10.1]]) ≈ 10.1 - 1.0 # Use ≈ for float results
@test δ_extrema([[-5, -1], [-10, -2]]) == -1 - (-10)
@test δ_extrema([[1, 2], [3.0f0, 4.0f0], [-1.0, 0.0]]) ≈ 4.0 - (-1.0)

@test 0 ≤ δ_extrema(X[1]) ≤ 100
@test 0 ≤ δ_extrema(X[1:2]...) ≤ 100
@test 0 ≤ δ_extrema(X...) ≤ 100
# Test case 8: Larger mix
vec1 = rand(Int8, 50)
vec2 = rand(Float32, 50) .* 100
vec3 = [999.0]
all_elements = vcat(vec1, vec2, vec3)
expected = maximum(all_elements) - minimum(all_elements)
@test δ_extrema([vec1, vec2, vec3]) ≈ expected
end
Loading