-
Notifications
You must be signed in to change notification settings - Fork 35
/
Band.jl
233 lines (185 loc) · 5.72 KB
/
Band.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# ~~ Type to set\get data along a band
struct Band
i::Int
end
show(io::IO, r::Band) = print(io, "Band(", r.i, ")")
"""
band(i)
Represents the `i`-th band of a banded matrix.
```jldoctest
julia> using BandedMatrices
julia> A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
4×4 BandedMatrix{Int64} with bandwidths (1, 1):
1 5 ⋅ ⋅
8 2 6 ⋅
⋅ 9 3 7
⋅ ⋅ 10 4
julia> A[band(1)]
3-element Vector{Int64}:
5
6
7
julia> A[band(0)]
4-element Vector{Int64}:
1
2
3
4
julia> A[band(-1)]
3-element Vector{Int64}:
8
9
10
```
"""
band(i::Int) = Band(i)
struct BandRangeType end
"""
BandRange
Represents the entries in a row/column inside the bands.
```jldoctest
julia> using BandedMatrices
julia> A = BandedMatrix(0=>1:4, 1=>5:7, -1=>8:10)
4×4 BandedMatrix{Int64} with bandwidths (1, 1):
1 5 ⋅ ⋅
8 2 6 ⋅
⋅ 9 3 7
⋅ ⋅ 10 4
julia> A[2, BandRange]
3-element Vector{Int64}:
8
2
6
```
"""
const BandRange = BandRangeType()
to_indices(A::AbstractArray, (_, j)::Tuple{BandRangeType,Integer}) = (colrange(A, j), j)
to_indices(A::AbstractArray, (k, _)::Tuple{Integer,BandRangeType}) = (k, rowrange(A, k))
# ~~ Out of band error
struct BandError <: Exception
A::AbstractMatrix
i::Int
end
# shorthand to specify k and j without calculating band
BandError(A::AbstractMatrix, (k, j)::Tuple{Int,Int}) = BandError(A, j-k)
BandError(A::AbstractMatrix) = BandError(A, max(size(A)...)-1)
function showerror(io::IO, e::BandError)
A, i = e.A, e.i
print(io, "BandError: attempt to access $(typeof(A)) with bandwidths " *
"($(bandwidth(A, 1)), $(bandwidth(A, 2))) at band $i")
end
# length of diagonal
@inline diaglength(A::AbstractMatrix, b::Band) = diaglength(A, b.i)
@inline function diaglength(A::AbstractMatrix, i::Integer)
max(min(size(A, 2), size(A, 1)+i) - max(0, i), 0)
end
# check indices fall in the band
checkband(A::AbstractMatrix, i::Integer) =
(-bandwidth(A, 1) ≤ i ≤ bandwidth(A, 2) || throw(BandError(A, i)))
checkband(A::AbstractMatrix, b::Band) = checkband(A, b.i)
checkband(A::AbstractMatrix, k::Integer, j::Integer) = checkband(A, j-k)
checkband(A::AbstractMatrix, kr::AbstractRange, j::Integer) =
(checkband(A, first(kr), j); checkband(A, last(kr), j))
checkband(A::AbstractMatrix, k::Integer, jr::AbstractRange) =
(checkband(A, k, first(jr)); checkband(A, k, last(jr)))
checkband(A::AbstractMatrix, kr::AbstractRange, jr::AbstractRange) =
(checkband(A, kr, first(jr)); checkband(A, kr, last(jr)))
# checks if the bands match A
function checkbandmatch(A::AbstractMatrix{T}, V::AbstractVector, ::Colon, j::Integer) where {T}
for k = 1:colstart(A,j)-1
if V[k] ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
for k = colstop(A,j)+1:size(A,1)
if V[k] ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
end
function checkbandmatch(A::AbstractMatrix{T}, V::AbstractVector, kr::AbstractRange, j::Integer) where {T}
a = colstart(A, j)
b = colstop(A, j)
i = 0
for v in V
k = kr[i+=1]
if (k < a || k > b) && v ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
end
function checkbandmatch(A::AbstractMatrix{T}, V::AbstractVector, k::Integer, ::Colon) where {T}
for j = 1:rowstart(A,k)-1
if V[j] ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
for j = rowstop(A,k)+1:size(A,2)
if V[j] ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
end
function checkbandmatch(A::AbstractMatrix{T}, V::AbstractVector, k::Integer, jr::AbstractRange) where {T}
a = rowstart(A, k)
b = rowstop(A, k)
i = 0
for v in V
j = jr[i+=1]
if (j < a || j > b) && v ≠ zero(T)
throw(BandError(A, (k,j)))
end
end
end
function checkbandmatch(A::AbstractMatrix{T}, V::AbstractMatrix, kr::AbstractRange, jr::AbstractRange) where {T}
l, u = bandwidths(A)
jj = 1
for j in jr
kk = 1
for k in kr
if !(-l ≤ j - k ≤ u) && V[kk, jj] ≠ zero(T)
# we index V manually in column-major order
throw(BandError(A, (k,j)))
end
kk += 1
end
jj += 1
end
end
checkbandmatch(A::AbstractMatrix, V::AbstractMatrix, ::Colon, ::Colon) =
checkbandmatch(A, V, 1:size(A,1), 1:size(A,2))
"""
BandSlice(band::Band, indices)
Represent a range of indices corresponding to a band.
Upon calling `to_indices`, `Band`s are converted to `BandSlice` objects to represent
the indices over which the `Band` spans.
This mimics the relationship between `Colon` and `Base.Slice`.
# Example
```jldoctest
julia> B = BandedMatrix(0 => 1:4, 1=>1:3);
julia> bs = to_indices(B, (Band(1),))[1];
julia> bs isa BandedMatrices.BandSlice
true
julia> using LinearAlgebra
julia> bs == diagind(B, 1)
true
```
"""
struct BandSlice{T, R<:AbstractRange{T}} <: AbstractRange{T}
band::Band
indices::R
end
for f in (:indices, :unsafe_indices, :axes1, :first, :last, :size, :length,
:unsafe_length, :start, :step)
@eval $f(S::BandSlice) = $f(S.indices)
end
@propagate_inbounds getindex(S::BandSlice, i::Union{Int, AbstractRange{<:Integer}}) = getindex(S.indices, i)
@propagate_inbounds view(S::BandSlice, i::AbstractRange{<:Integer}) = view(S.indices, i)
show(io::IO, r::BandSlice) = print(io, BandSlice, "(", r.band, ", ", r.indices, ")")
to_index(::Band) = throw(ArgumentError("Block must be converted by to_indices(...)"))
"""
" the following is designed to supported infinite baned arrays
"""
band_to_indices(A, _, b) = (BandSlice(b, diagind(A, b.i)),)
@inline to_indices(A, I::Tuple{Band}) = band_to_indices(A, axes(A), I[1])
view(A::AbstractArray, I::Band) = view(A, to_indices(A, (I,))...)