From d387ae6bd15d3450ff337f5ef6132709b1371b75 Mon Sep 17 00:00:00 2001 From: schillic Date: Tue, 11 Feb 2020 08:36:55 +0100 Subject: [PATCH 1/2] add 'similar' constructor --- src/matrix.jl | 7 ++++++- test/runtests.jl | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/matrix.jl b/src/matrix.jl index 3ec49f39..36cd70dc 100644 --- a/src/matrix.jl +++ b/src/matrix.jl @@ -1,4 +1,4 @@ -import Base: split, ∈, ⊆ +import Base: similar, split, ∈, ⊆ import Random: rand import IntervalArithmetic: inf, sup, mid, diam @@ -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}} diff --git a/test/runtests.jl b/test/runtests.jl index c3c755ab..4ac5b8e2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 From 927ad7558417478f5e0447c20f57d375bce650ea Mon Sep 17 00:00:00 2001 From: schillic Date: Tue, 11 Feb 2020 08:39:50 +0100 Subject: [PATCH 2/2] use 'similar' constructor --- src/arithmetic.jl | 4 ++-- src/exponential.jl | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/arithmetic.jl b/src/arithmetic.jl index 3a6c3401..702dbb8b 100644 --- a/src/arithmetic.jl +++ b/src/arithmetic.jl @@ -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 @@ -83,5 +83,5 @@ function square(A::IntervalMatrix) end end end - return IntervalMatrix(B) + return B end diff --git a/src/exponential.jl b/src/exponential.jl index e4ff9e6e..9f487eef 100644 --- a/src/exponential.jl +++ b/src/exponential.jl @@ -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 @@ -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 @@ -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;