Skip to content

Commit 3a37d96

Browse files
authored
Docstrings for colrange/rowrange and friends (#372)
1 parent 6319750 commit 3a37d96

File tree

2 files changed

+205
-29
lines changed

2 files changed

+205
-29
lines changed

src/generic/AbstractBandedMatrix.jl

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,193 @@ bandrange(A) = -bandwidth(A,1):bandwidth(A,2)
6464

6565

6666

67-
# start/stop indices of the i-th column/row, bounded by actual matrix size
67+
"""
68+
colstart(A, i::Integer)
69+
70+
Return the starting row index of the filled bands in the i-th column,
71+
bounded by the actual matrix size.
72+
73+
# Examples
74+
```jldoctest
75+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
76+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
77+
1 5 ⋅ ⋅
78+
⋅ 2 6 ⋅
79+
⋅ ⋅ 3 7
80+
⋅ ⋅ ⋅ 4
81+
82+
julia> BandedMatrices.colstart(A, 3)
83+
2
84+
85+
julia> BandedMatrices.colstart(A, 4)
86+
3
87+
```
88+
"""
6889
@inline colstart(A, i::Integer) = max(i-bandwidth(A,2), 1)
90+
91+
"""
92+
colstop(A, i::Integer)
93+
94+
Return the stopping row index of the filled bands in the i-th column,
95+
bounded by the actual matrix size.
96+
97+
# Examples
98+
```jldoctest
99+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
100+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
101+
1 5 ⋅ ⋅
102+
⋅ 2 6 ⋅
103+
⋅ ⋅ 3 7
104+
⋅ ⋅ ⋅ 4
105+
106+
julia> BandedMatrices.colstop(A, 3)
107+
3
108+
109+
julia> BandedMatrices.colstop(A, 4)
110+
4
111+
```
112+
"""
69113
@inline colstop(A, i::Integer) = max(min(i+bandwidth(A,1), size(A, 1)), 0)
114+
115+
"""
116+
rowstart(A, i::Integer)
117+
118+
Return the starting column index of the filled bands in the i-th row,
119+
bounded by the actual matrix size.
120+
121+
# Examples
122+
```jldoctest
123+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
124+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
125+
1 5 ⋅ ⋅
126+
⋅ 2 6 ⋅
127+
⋅ ⋅ 3 7
128+
⋅ ⋅ ⋅ 4
129+
130+
julia> BandedMatrices.rowstart(A, 2)
131+
2
132+
133+
julia> BandedMatrices.rowstart(A, 3)
134+
3
135+
```
136+
"""
70137
@inline rowstart(A, i::Integer) = max(i-bandwidth(A,1), 1)
138+
139+
"""
140+
rowstop(A, i::Integer)
141+
142+
Return the stopping column index of the filled bands in the i-th row,
143+
bounded by the actual matrix size.
144+
145+
# Examples
146+
```jldoctest
147+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
148+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
149+
1 5 ⋅ ⋅
150+
⋅ 2 6 ⋅
151+
⋅ ⋅ 3 7
152+
⋅ ⋅ ⋅ 4
153+
154+
julia> BandedMatrices.rowstop(A, 2)
155+
3
156+
157+
julia> BandedMatrices.rowstop(A, 4)
158+
4
159+
```
160+
"""
71161
@inline rowstop(A, i::Integer) = max(min(i+bandwidth(A,2), size(A, 2)), 0)
72162

163+
"""
164+
colrange(A, i::Integer)
165+
166+
Return the range of rows in the `i`-th column that correspond to filled bands.
167+
168+
# Examples
169+
```jldoctest
170+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
171+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
172+
1 5 ⋅ ⋅
173+
⋅ 2 6 ⋅
174+
⋅ ⋅ 3 7
175+
⋅ ⋅ ⋅ 4
73176
177+
julia> colrange(A, 1)
178+
1:1
179+
180+
julia> colrange(A, 3)
181+
2:3
182+
```
183+
"""
74184
@inline colrange(A, i::Integer) = colstart(A,i):colstop(A,i)
185+
186+
"""
187+
rowrange(A, i::Integer)
188+
189+
Return the range of columns in the `i`-th row that correspond to filled bands.
190+
191+
# Examples
192+
```jldoctest
193+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
194+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
195+
1 5 ⋅ ⋅
196+
⋅ 2 6 ⋅
197+
⋅ ⋅ 3 7
198+
⋅ ⋅ ⋅ 4
199+
200+
julia> rowrange(A, 1)
201+
1:2
202+
203+
julia> rowrange(A, 4)
204+
4:4
205+
```
206+
"""
75207
@inline rowrange(A, i::Integer) = rowstart(A,i):rowstop(A,i)
76208

77209

78-
# length of i-the column/row
210+
"""
211+
collength(A, i::Integer)
212+
213+
Return the number of filled bands in the `i`-th column.
214+
215+
# Examples
216+
```jldoctest
217+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
218+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
219+
1 5 ⋅ ⋅
220+
⋅ 2 6 ⋅
221+
⋅ ⋅ 3 7
222+
⋅ ⋅ ⋅ 4
223+
224+
julia> BandedMatrices.collength(A, 1)
225+
1
226+
227+
julia> BandedMatrices.collength(A, 2)
228+
2
229+
```
230+
"""
79231
@inline collength(A, i::Integer) = max(colstop(A, i) - colstart(A, i) + 1, 0)
232+
233+
"""
234+
rowlength(A, i::Integer)
235+
236+
Return the number of filled bands in the `i`-th row.
237+
238+
# Examples
239+
```jldoctest
240+
julia> A = BandedMatrix(0=>1:4, 1=>5:7)
241+
4×4 BandedMatrix{Int64} with bandwidths (0, 1):
242+
1 5 ⋅ ⋅
243+
⋅ 2 6 ⋅
244+
⋅ ⋅ 3 7
245+
⋅ ⋅ ⋅ 4
246+
247+
julia> BandedMatrices.rowlength(A, 1)
248+
2
249+
250+
julia> BandedMatrices.rowlength(A, 4)
251+
1
252+
```
253+
"""
80254
@inline rowlength(A, i::Integer) = max(rowstop(A, i) - rowstart(A, i) + 1, 0)
81255

82256
@inline banded_colsupport(A, j::Integer) = colrange(A, j)

test/test_symbanded.jl

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -310,33 +310,35 @@ end
310310
end
311311
end
312312

313-
@testset "Generalized eigenvalues $W{$T}($Ua,$Ub)($n,$wa-$wb)" for (T,W) in (
314-
(Float32, Symmetric),
315-
(Float64, Symmetric),
316-
(Float32, Hermitian),
317-
(Float64, Hermitian),
318-
(ComplexF32, Hermitian),
319-
(ComplexF64, Hermitian),
320-
),
321-
(Ua, Ub) in ((:L,:L), (:U,:U)),
322-
(wa, wb) in ((2, 3), (3, 2)), n in (4,)
323-
#
324-
function sbmatrix(W, T, U, w, n)
325-
r = U == :L ? (0:-1:-w+1) : (0:w-1)
326-
band(k) = k => ones(T, n - abs(k)) * 2.0^-abs(k)
327-
W(BandedMatrix(band.(r)...), U)
328-
end
329-
A = sbmatrix(W, T, Ua, wa, n)
330-
B = sbmatrix(W, T, Ub, wb, n)
331-
AM, BM = Matrix.((A,B))
332-
@test eigvals(A, B) eigvals(AM, BM)
333-
if VERSION >= v"1.9"
334-
Λ, V = eigen(A, B)
335-
VM = Matrix(V)
336-
Λ2, V2 = eigen(AM, BM)
337-
@test Λ Λ2
338-
@test VM' * AM * VM V2' * AM * V2
339-
@test VM' * AM * VM VM' * BM * VM * Diagonal(Λ)
313+
@testset "Generalized eigenvalues" begin
314+
@testset "$W{$T}($Ua,$Ub)($n,$wa-$wb)" for (T,W) in (
315+
(Float32, Symmetric),
316+
(Float64, Symmetric),
317+
(Float32, Hermitian),
318+
(Float64, Hermitian),
319+
(ComplexF32, Hermitian),
320+
(ComplexF64, Hermitian),
321+
),
322+
(Ua, Ub) in ((:L,:L), (:U,:U)),
323+
(wa, wb) in ((2, 3), (3, 2)), n in (4,)
324+
#
325+
function sbmatrix(W, T, U, w, n)
326+
r = U == :L ? (0:-1:-w+1) : (0:w-1)
327+
band(k) = k => ones(T, n - abs(k)) * 2.0^-abs(k)
328+
W(BandedMatrix(band.(r)...), U)
329+
end
330+
A = sbmatrix(W, T, Ua, wa, n)
331+
B = sbmatrix(W, T, Ub, wb, n)
332+
AM, BM = Matrix.((A,B))
333+
@test eigvals(A, B) eigvals(AM, BM)
334+
if VERSION >= v"1.9"
335+
Λ, V = eigen(A, B)
336+
VM = Matrix(V)
337+
Λ2, V2 = eigen(AM, BM)
338+
@test Λ Λ2
339+
@test VM' * AM * VM V2' * AM * V2
340+
@test VM' * AM * VM VM' * BM * VM * Diagonal(Λ)
341+
end
340342
end
341343
end
342344

0 commit comments

Comments
 (0)