Skip to content

Commit a7eea43

Browse files
committed
Refine column ranges in _isbanded_impl (#1267)
Fixes the column ranges, so that these correctly correspond to the ranges over which there are top and bottom rows to consider beyond the bands. The change is to the upper limit of the range. (cherry picked from commit 95703b5)
1 parent 1efe04a commit a7eea43

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/generic.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,14 +1486,34 @@ function _isbanded_impl(A, kl, ku)
14861486
beyond ku, where the elements should all be zero. The reason we separate this from the
14871487
third group is that we may loop over all the rows using A[:, col] instead of A[rowrange, col],
14881488
which is usually faster.
1489+
1490+
E.g., in the following 6x10 matrix with (kl,ku) = (-1,1):
1491+
1 1 0 0 0 0 0 0 0 0
1492+
1 2 2 0 0 0 0 0 0 0
1493+
0 2 3 3 0 0 0 0 0 0
1494+
0 0 3 4 4 0 0 0 0 0
1495+
0 0 0 4 5 5 0 0 0 0
1496+
0 0 0 0 5 6 6 0 0 0
1497+
1498+
last_col_nonzeroblocks: 7, as every column beyond this is entirely zero
1499+
last_col_emptytoprows: 2, as there are zeros above the stored bands beyond this column
1500+
last_col_nonemptybottomrows: 4, as there are no zeros below the stored bands beyond this column
1501+
colrange_onlybottomrows: 1:2, as these columns only have zeros below the stored bands
1502+
colrange_topbottomrows: 3:4, as these columns have zeros both above and below the stored bands
1503+
colrange_onlytoprows_nonzero: 5:7, as these columns only have zeros above the stored bands
1504+
colrange_zero_block: 8:10, as every column in this range is filled with zeros
1505+
1506+
These are used to determine which rows to check for zeros in each column.
14891507
=#
14901508

14911509
last_col_nonzeroblocks = size(A,1) + ku # fully zero rectangular block beyond this column
14921510
last_col_emptytoprows = ku + 1 # empty top rows before this column
14931511
last_col_nonemptybottomrows = size(A,1) + kl - 1 # empty bottom rows after this column
14941512

14951513
colrange_onlybottomrows = firstindex(A,2):min(last_col_nonemptybottomrows, last_col_emptytoprows)
1496-
colrange_topbottomrows = max(last_col_emptytoprows, last(colrange_onlybottomrows))+1:last_col_nonzeroblocks
1514+
col_topbotrows_start = max(last_col_emptytoprows, last(colrange_onlybottomrows))+1
1515+
col_topbotrows_end = min(last_col_nonemptybottomrows, last_col_nonzeroblocks)
1516+
colrange_topbottomrows = col_topbotrows_start:col_topbotrows_end
14971517
colrange_onlytoprows_nonzero = last(colrange_topbottomrows)+1:last_col_nonzeroblocks
14981518
colrange_zero_block = last_col_nonzeroblocks+1:lastindex(A,2)
14991519

test/generic.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,17 @@ end
514514

515515
@testset "generic functions for checking whether matrices have banded structure" begin
516516
pentadiag = [1 2 3; 4 5 6; 7 8 9]
517-
tridiag = [1 2 0; 4 5 6; 0 8 9]
518-
tridiagG = GenericArray([1 2 0; 4 5 6; 0 8 9])
517+
tridiag = diagm(-1=>1:6, 1=>1:6)
518+
tridiagG = GenericArray(tridiag)
519519
Tridiag = Tridiagonal(tridiag)
520520
ubidiag = [1 2 0; 0 5 6; 0 0 9]
521-
ubidiagG = GenericArray([1 2 0; 0 5 6; 0 0 9])
521+
ubidiagG = GenericArray(ubidiag)
522522
uBidiag = Bidiagonal(ubidiag, :U)
523523
lbidiag = [1 0 0; 4 5 0; 0 8 9]
524-
lbidiagG = GenericArray([1 0 0; 4 5 0; 0 8 9])
524+
lbidiagG = GenericArray(lbidiag)
525525
lBidiag = Bidiagonal(lbidiag, :L)
526526
adiag = [1 0 0; 0 5 0; 0 0 9]
527-
adiagG = GenericArray([1 0 0; 0 5 0; 0 0 9])
527+
adiagG = GenericArray(adiag)
528528
aDiag = Diagonal(adiag)
529529
@testset "istriu" begin
530530
@test !istriu(pentadiag)
@@ -618,6 +618,17 @@ end
618618
end
619619
end
620620
end
621+
622+
tridiag = diagm(-1=>1:6, 1=>1:6)
623+
A = [tridiag zeros(size(tridiag,1), 2)]
624+
G = GenericArray(A)
625+
@testset for (kl,ku) in Iterators.product(-10:10, -10:10)
626+
@test isbanded(A, kl, ku) == isbanded(G, kl, ku)
627+
end
628+
@testset for k in -10:10
629+
@test istriu(A,k) == istriu(G,k)
630+
@test istril(A,k) == istril(G,k)
631+
end
621632
end
622633

623634
@testset "missing values" begin

0 commit comments

Comments
 (0)