Skip to content

Commit

Permalink
Merge pull request #128 from JuliaReach/schillic/similar
Browse files Browse the repository at this point in the history
Add 'similar' constructor
  • Loading branch information
schillic authored Feb 11, 2020
2 parents f901e7e + 927ad75 commit 16edd54
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ We follow [1, Section 6].
matrix is NP-hard. SAC 2005.
"""
function square(A::IntervalMatrix)
B = similar(A.mat)
B = similar(A)
n = checksquare(A)

# case i == j
Expand All @@ -83,5 +83,5 @@ function square(A::IntervalMatrix)
end
end
end
return IntervalMatrix(B)
return B
end
6 changes: 3 additions & 3 deletions src/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function quadratic_expansion(A::IntervalMatrix{T}, t) where {T}
end
end

W = IntervalMatrix(similar(A.mat))
W = similar(A)

@inbounds for j in 1:n
for i in 1:n
Expand Down Expand Up @@ -95,7 +95,7 @@ bᵢⱼ = β\\sum_\\{k, k ≠ i, k ≠ j} a_{ik} a_{kj} + (α + βa_{ii} + βa_{
matrix is NP-hard. SAC 2005.
"""
function quadratic_expansion(A::IntervalMatrix, α::Real, β::Real)
B = similar(A.mat)
B = similar(A)
n = checksquare(A)

# case i = j
Expand All @@ -118,7 +118,7 @@ function quadratic_expansion(A::IntervalMatrix, α::Real, β::Real)
end
end
end
return IntervalMatrix(B)
return B
end

function _truncated_exponential_series(A::IntervalMatrix{T}, t, p::Integer;
Expand Down
7 changes: 6 additions & 1 deletion src/matrix.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Base: split, ,
import Base: similar, split, ,
import Random: rand
import IntervalArithmetic: inf, sup, mid, diam

Expand Down Expand Up @@ -90,6 +90,11 @@ function IntervalMatrix{T}(u::UndefInitializer, m::Integer, n::Integer=m) where
return IntervalMatrix(mat)
end

# similar initializer
function similar(M::IntervalMatrix)
return IntervalMatrix(similar(M.mat))
end

"""
IntervalMatrix(C::MT, S::MT) where {T, MT<:AbstractMatrix{T}}
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ using IntervalMatrices: _truncated_exponential_series, scale_and_square
@test a * (b + 1) == -4.0..0.0
end

@testset "Interval matrix construction" begin
m1 = IntervalMatrix([-1.1..0.9 -4.1.. -3.9; 3.8..4.2 0.0..0.9])
m2 = IntervalMatrix{Float64}(undef, 2, 2)
@test m2 isa IntervalMatrix{Float64} && size(m2) == (2, 2)
m3 = similar(m1)
@test m3 isa IntervalMatrix{Float64} && size(m3) == size(m1)
end

@testset "Interval matrix arithmetic" begin
a = 1.0..1.3; b = 2.0..3.5; c = -0.5 ± 0.5; d = 0.0 ± 0.1
a₊ = 2.0..2.6; b₊ = 4.0..7.0; c₊ = -2.0..0.0; d₊ = -0.2..0.2
Expand Down

0 comments on commit 16edd54

Please sign in to comment.