Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add min/maximum(abs) #196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,47 @@

extrema(I::TypedEndpointsInterval) = (infimum(I), supremum(I))

function minimum(::typeof(abs), I::AbstractInterval)
a, b = abs.(endpoints(I))
z = zero(promote_type(typeof(a), typeof(b)))
ac, bc = closedendpoints(I)
if isempty(I)
throw(ArgumentError("minimum(abs, $I) is undefined for empty intervals"))

Check warning on line 345 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L345

Added line #L345 was not covered by tests
elseif z ∈ I
return z
elseif a < b
ac && return a
throw(ArgumentError("$I is open on the left, no minimum(abs)"))
elseif a > b
bc && return b
throw(ArgumentError("$I is open on the right, no minimum(abs)"))

Check warning on line 353 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L350-L353

Added lines #L350 - L353 were not covered by tests
else
throw(ArgumentError("cannot determine minimum(abs, $I)"))

Check warning on line 355 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L355

Added line #L355 was not covered by tests
end
end

function maximum(::typeof(abs), I::AbstractInterval)
a, b = abs.(endpoints(I))
ac, bc = closedendpoints(I)
if isempty(I)
throw(ArgumentError("maximum(abs, $I) is undefined for empty intervals"))

Check warning on line 363 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L363

Added line #L363 was not covered by tests
elseif a == b
ac && return a
bc && return b
throw(ArgumentError("$I is open on both sides, no maximum(abs)"))

Check warning on line 367 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L365-L367

Added lines #L365 - L367 were not covered by tests
elseif a > b
ac && return a
throw(ArgumentError("$I is open on the left, no maximum(abs)"))

Check warning on line 370 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L370

Added line #L370 was not covered by tests
elseif a < b
bc && return b
throw(ArgumentError("$I is open on the right, no maximum(abs)"))
else
throw(ArgumentError("cannot determine maximum(abs, $I)"))

Check warning on line 375 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L375

Added line #L375 was not covered by tests
end
end

extrema(::typeof(abs), I::AbstractInterval) = (minimum(abs, I), maximum(abs, I))

# Open and closed at endpoints
isleftclosed(d::TypedEndpointsInterval{:closed}) = true
isleftclosed(d::TypedEndpointsInterval{:open}) = false
Expand Down
20 changes: 20 additions & 0 deletions test/base_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ end
IntervalSets.endpoints(::RandTestUnitInterval) = (-1.0, 1.0)
@test rand(RandTestUnitInterval()) in -1.0..1.0
end

@testset "min/maximum(abs)" begin
@test maximum(abs, 1..2) == 2
@test maximum(abs, -3..2) == 3
@test minimum(abs, 1..2) == 1
@test minimum(abs, -3..2) == 0
@test_throws Exception maximum(abs, iv"[1, 2)")
@test minimum(abs, iv"[1, 2)") == 1
@test minimum(abs, iv"(-3, 2)") == 0

@test maximum(abs, 1u"m"..2u"m") == 2u"m"
@test maximum(abs, -3u"m"..2u"m") == 3u"m"
@test minimum(abs, 1u"m"..2u"m") == 1u"m"
@test minimum(abs, -3u"m"..2u"m") == 0u"m"

@test extrema(abs, 1..2) == (1, 2)
@test extrema(abs, -3..2) == (0, 3)
@test_throws Exception extrema(abs, iv"[1, 2)")
@test extrema(abs, iv"[-3, 2)") == (0, 3)
end
Loading