Skip to content

Commit

Permalink
added missing functions (radius, issubset) and fixes in real-valued f…
Browse files Browse the repository at this point in the history
…unctions (#170)

* added missing functions (radius, issubset) and fixes in real-valued functions

* added hull

* fix workflow

* removed strict subset and bumped patch version
  • Loading branch information
lucaferranti committed Sep 18, 2021
1 parent 466bd64 commit 8fb9d46
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@

docs/build/
docs/site/

Manifest.toml
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalMatrices"
uuid = "5c1f47dc-42dd-5697-8aaa-4d102d140ba9"
version = "0.6.5"
version = "0.6.6"

[deps]
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
Expand Down
77 changes: 66 additions & 11 deletions src/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Base: similar, split, , ,
import Base: similar, split, , , ,
import Random: rand
import IntervalArithmetic: inf, sup, mid, diam
import IntervalArithmetic: inf, sup, mid, diam, radius, hull

"""
AbstractIntervalMatrix{IT} <: AbstractMatrix{IT}
Expand Down Expand Up @@ -227,7 +227,7 @@ element-wise infimum of `A`.
A scalar matrix whose coefficients are the infima of each element in `A`.
"""
function inf(A::IntervalMatrix{T}) where {T}
return map(inf, A)
return map(inf, A.mat)
end

"""
Expand All @@ -245,7 +245,7 @@ element-wise supremum of `A`.
A scalar matrix whose coefficients are the suprema of each element in `A`.
"""
function sup(A::IntervalMatrix{T}) where {T}
return map(sup, A)
return map(sup, A.mat)
end

"""
Expand All @@ -263,7 +263,25 @@ element-wise midpoint of `A`.
A scalar matrix whose coefficients are the midpoints of each element in `A`.
"""
function mid(A::IntervalMatrix{T}) where {T}
return map(mid, A)
return map(mid, A.mat)
end

"""
radius(A::IntervalMatrix{T}) where {T}
Return the radius of an interval matrix `A`, which corresponds to taking the
element-wise radius of `A`.
### Input
- `A` -- interval matrix
### Output
A scalar matrix whose coefficients are the radii of each element in `A`.
"""
function radius(A::IntervalMatrix{T}) where {T}
return map(radius, A.mat)
end

"""
Expand Down Expand Up @@ -450,7 +468,7 @@ A matrix `B` of the same shape as `A` such that `B[i, j] == diam(A[i, j])` for
each `i` and `j`.
"""
function diam(A::IntervalMatrix{T}) where {T}
return map(diam, A)
return map(diam, A.mat)
end

"""
Expand Down Expand Up @@ -519,13 +537,50 @@ function ∩(A::IntervalMatrix, B::IntervalMatrix)
@assert size(A) == size(B) "incompatible matrix sizes (A: $(size(A)), B: " *
"$(size(B)))"

C = similar(A)
@inbounds for j in 1:n, i in 1:m
C[i, j] = A[i, j] B[i, j]
end
return C
return IntervalMatrix(map((x, y) -> x y, A, B))
end


"""
hull(A::IntervalMatrix, B::IntervalMatrix)
Finds the interval hull of two interval matrices. This is equivalent to [`∪`](@ref).
### Input
- `A` -- interval matrix
- `B` -- interval matrix (of the same shape as `A`)
### Output
A new matrix `C` of the same shape as `A` such that
`C[i, j] = hull(A[i, j], B[i, j])` for each `i` and `j`.
"""
function hull(A::IntervalMatrix, B::IntervalMatrix)
@assert size(A) == size(B) "incompatible matrix sizes (A: $(size(A)), B: " *
"$(size(B)))"

return IntervalMatrix(map((x, y) -> hull(x, y), A, B))
end

"""
∪(A::IntervalMatrix, B::IntervalMatrix)
Finds the interval union (hull) of two interval matrices.
This is equivalent to [`hull`](@ref).
### Input
- `A` -- interval matrix
- `B` -- interval matrix (of the same shape as `A`)
### Output
A new matrix `C` of the same shape as `A` such that
`C[i, j] = A[i, j] ∪ B[i, j]` for each `i` and `j`.
"""
(A::IntervalMatrix, B::IntervalMatrix) = hull(A, B)

"""
diam_norm(A::IntervalMatrix, p=Inf)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ end
r = sup(m)
c = mid(m)
d = diam(m)
rad = radius(m)
m2 = copy(m)
@test m2 isa IntervalMatrix && m.mat == m2.mat
@test l == inf.(m) && r == sup.(m) && c == mid.(m)
@test d r - l
@test rad d/2
sm = scale(m, 2.0)
@test sm == 2.0 .* m
@test sm m
Expand All @@ -81,6 +83,7 @@ end
m3 = IntervalMatrix([-2.0..2.0 -2.0..0.0; 0.0..2.0 -1.0..1.0])
m4 = IntervalMatrix([-1.0..1.0 -1.0..1.0; -1.0..1.0 -2.0..2.0])
@test m3 m4 == IntervalMatrix([-1.0..1.0 -1.0..0.0; 0.0..1.0 -1.0..1.0])
@test m3 m4 == IntervalMatrix([-2.0..2.0 -2.0..1.0; -1.0..2.0 -2.0..2.0])
@test diam_norm(m3) 6.0 # default diameter p-norm is Inf
@test diam_norm(m3, 1) 6.0
end
Expand Down Expand Up @@ -122,6 +125,7 @@ end
m1 = IntervalMatrix([-1.1..0.9 -4.1.. -3.9; 3.9..4.1 -1.1..0.9])
m2 = IntervalMatrix([-1.2..1.0 -4.1.. -3.9; 3.9..4.2 -1.2..0.9])
@test m1 m2 && !(m2 m1)

end

@testset "Interval matrix rand" begin
Expand Down

0 comments on commit 8fb9d46

Please sign in to comment.