forked from JuliaIntervals/IntervalLinearAlgebra.jl
/
utils.jl
179 lines (137 loc) · 3.95 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
mid(x::Real) = x
mid(x::Complex) = x
"""
interval_isapprox(a::Interval, b::Interval; kwargs)
Checks whether the intervals ``a`` and ``b`` are approximate equal, that is both their
lower and upper bound are approximately equal.
### Keywords
Same of `Base.isapprox`
### Example
```jldoctest
julia> a = 1..2
[1, 2]
julia> b = a + 1e-10
[1, 2.00001]
julia> interval_isapprox(a, b)
true
julia> interval_isapprox(a, b; atol=1e-15)
false
```
"""
interval_isapprox(a::Interval, b::Interval; kwargs...) = isapprox(a.lo, b.lo; kwargs...) && isapprox(a.hi, b.hi; kwargs...)
"""
interval_norm(A::AbstractMatrix{T}) where {T<:Interval}
computes the infinity norm of interval matrix ``A``.
### Examples
```jldoctest
julia> A = [2..4 -1..1; -1..1 2..4]
2×2 Matrix{Interval{Float64}}:
[2, 4] [-1, 1]
[-1, 1] [2, 4]
julia> interval_norm(A)
5.0
```
"""
interval_norm(A::AbstractMatrix{T}) where {T<:Interval} = opnorm(mag.(A), Inf) # TODO: proper expand norm function and add 1-norm
"""
interval_norm(A::AbstractVector{T}) where {T<:Interval}
computes the infinity norm of interval vector ``v``.
### Examples
```jldoctest
julia> b = [-2..2, -3..2]
2-element Vector{Interval{Float64}}:
[-2, 2]
[-3, 2]
julia> interval_norm(b)
3.0
```
"""
interval_norm(v::AbstractVector{T}) where {T<:Interval} = maximum(mag.(v))
# ? use manual loops instead
"""
enclose(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T<:Interval}
Computes an enclosure of the solution of the interval linear system ``Ax=b`` using the
algorithm described in sec. 5.7.1 of [[HOR19]](@ref).
"""
function enclose(A::StaticMatrix{N, N, T}, b::StaticVector{N, T}) where {N, T<:Interval}
C = inv(mid.(A))
A1 = Diagonal(ones(N)) - C*A
e = interval_norm(C*b)/(1 - interval_norm(A1))
x0 = MVector{N, T}(ntuple(_ -> -e..e, Val(N)))
return x0
end
function enclose(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T<:Interval}
n = length(b)
C = inv(mid.(A))
A1 = Diagonal(ones(n)) - C*A
e = interval_norm(C*b)/(1 - interval_norm(A1))
x0 = fill(-e..e, n)
return x0
end
"""
comparison_matrix(A::AbstractMatrix{T}) where {T<:Interval}
Computes the comparison matrix ``⟨A⟩`` of the given interval matrix ``A`` according to the
definition ``⟨A⟩ᵢᵢ = mig(Aᵢᵢ)`` and ``⟨A⟩ᵢⱼ = -mag(Aᵢⱼ)`` if ``i≠j``.
### Examples
```jldoctest
julia> A = [2..4 -1..1; -1..1 2..4]
2×2 Matrix{Interval{Float64}}:
[2, 4] [-1, 1]
[-1, 1] [2, 4]
julia> comparison_matrix(A)
2×2 Matrix{Float64}:
2.0 -1.0
-1.0 2.0
```
"""
function comparison_matrix(A::SMatrix{N, N, T, M}) where {N, M, T<:Interval}
n = size(A, 1)
compA = -mag.(A)
@inbounds for (i, idx) in enumerate(diagind(A))
compA = setindex(compA, mig(A[i, i]), idx)
end
return compA
end
function comparison_matrix(A::AbstractMatrix{T}) where {T<:Interval}
n = size(A, 1)
compA = -mag.(A)
@inbounds for i in 1:n
compA[i, i] = mig(A[i, i])
end
return compA
end
"""
Orthants
Iterator to go through all the ``2ⁿ`` vectors of length ``n`` with elements ``±1``.
This is equivalento to going through the orthants of an ``n``-dimensional euclidean space.
### Fields
n::Int -- dimension of the vector space
### Example
```jldoctest
julia> for or in Orthants(2)
@show or
end
or = [1, 1]
or = [-1, 1]
or = [1, -1]
or = [-1, -1]
```
"""
struct Orthants
n::Int
end
Base.eltype(::Type{Orthants}) = Vector{Int}
Base.length(O::Orthants) = 2^(O.n)
function Base.iterate(O::Orthants, state=1)
state > 2 ^ O.n && return nothing
vec = -2*digits(state-1, base=2, pad=O.n) .+ 1
return (vec, state+1)
end
function Base.getindex(O::Orthants, i::Int)
1 <= i <= length(O) || throw(BoundsError(O, i))
return -2*digits(i-1, base=2, pad=O.n) .+ 1
end
Base.firstindex(O::Orthants) = 1
Base.lastindex(O::Orthants) = length(O)
_unchecked_interval(x::Real) = Interval(x)
_unchecked_interval(x::Complex) = Interval(real(x)) + Interval(imag(x)) * im