Skip to content

Commit 95703b5

Browse files
authored
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.
1 parent d15de4d commit 95703b5

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/generic.jl

+21-1
Original file line numberDiff line numberDiff line change
@@ -1511,14 +1511,34 @@ function _isbanded_impl(A, kl, ku)
15111511
beyond ku, where the elements should all be zero. The reason we separate this from the
15121512
third group is that we may loop over all the rows using A[:, col] instead of A[rowrange, col],
15131513
which is usually faster.
1514+
1515+
E.g., in the following 6x10 matrix with (kl,ku) = (-1,1):
1516+
1 1 0 0 0 0 0 0 0 0
1517+
1 2 2 0 0 0 0 0 0 0
1518+
0 2 3 3 0 0 0 0 0 0
1519+
0 0 3 4 4 0 0 0 0 0
1520+
0 0 0 4 5 5 0 0 0 0
1521+
0 0 0 0 5 6 6 0 0 0
1522+
1523+
last_col_nonzeroblocks: 7, as every column beyond this is entirely zero
1524+
last_col_emptytoprows: 2, as there are zeros above the stored bands beyond this column
1525+
last_col_nonemptybottomrows: 4, as there are no zeros below the stored bands beyond this column
1526+
colrange_onlybottomrows: 1:2, as these columns only have zeros below the stored bands
1527+
colrange_topbottomrows: 3:4, as these columns have zeros both above and below the stored bands
1528+
colrange_onlytoprows_nonzero: 5:7, as these columns only have zeros above the stored bands
1529+
colrange_zero_block: 8:10, as every column in this range is filled with zeros
1530+
1531+
These are used to determine which rows to check for zeros in each column.
15141532
=#
15151533

15161534
last_col_nonzeroblocks = size(A,1) + ku # fully zero rectangular block beyond this column
15171535
last_col_emptytoprows = ku + 1 # empty top rows before this column
15181536
last_col_nonemptybottomrows = size(A,1) + kl - 1 # empty bottom rows after this column
15191537

15201538
colrange_onlybottomrows = firstindex(A,2):min(last_col_nonemptybottomrows, last_col_emptytoprows)
1521-
colrange_topbottomrows = max(last_col_emptytoprows, last(colrange_onlybottomrows))+1:last_col_nonzeroblocks
1539+
col_topbotrows_start = max(last_col_emptytoprows, last(colrange_onlybottomrows))+1
1540+
col_topbotrows_end = min(last_col_nonemptybottomrows, last_col_nonzeroblocks)
1541+
colrange_topbottomrows = col_topbotrows_start:col_topbotrows_end
15221542
colrange_onlytoprows_nonzero = last(colrange_topbottomrows)+1:last_col_nonzeroblocks
15231543
colrange_zero_block = last_col_nonzeroblocks+1:lastindex(A,2)
15241544

test/generic.jl

+16-5
Original file line numberDiff line numberDiff line change
@@ -572,17 +572,17 @@ end
572572

573573
@testset "generic functions for checking whether matrices have banded structure" begin
574574
pentadiag = [1 2 3; 4 5 6; 7 8 9]
575-
tridiag = [1 2 0; 4 5 6; 0 8 9]
576-
tridiagG = GenericArray([1 2 0; 4 5 6; 0 8 9])
575+
tridiag = diagm(-1=>1:6, 1=>1:6)
576+
tridiagG = GenericArray(tridiag)
577577
Tridiag = Tridiagonal(tridiag)
578578
ubidiag = [1 2 0; 0 5 6; 0 0 9]
579-
ubidiagG = GenericArray([1 2 0; 0 5 6; 0 0 9])
579+
ubidiagG = GenericArray(ubidiag)
580580
uBidiag = Bidiagonal(ubidiag, :U)
581581
lbidiag = [1 0 0; 4 5 0; 0 8 9]
582-
lbidiagG = GenericArray([1 0 0; 4 5 0; 0 8 9])
582+
lbidiagG = GenericArray(lbidiag)
583583
lBidiag = Bidiagonal(lbidiag, :L)
584584
adiag = [1 0 0; 0 5 0; 0 0 9]
585-
adiagG = GenericArray([1 0 0; 0 5 0; 0 0 9])
585+
adiagG = GenericArray(adiag)
586586
aDiag = Diagonal(adiag)
587587
@testset "istriu" begin
588588
@test !istriu(pentadiag)
@@ -676,6 +676,17 @@ end
676676
end
677677
end
678678
end
679+
680+
tridiag = diagm(-1=>1:6, 1=>1:6)
681+
A = [tridiag zeros(size(tridiag,1), 2)]
682+
G = GenericArray(A)
683+
@testset for (kl,ku) in Iterators.product(-10:10, -10:10)
684+
@test isbanded(A, kl, ku) == isbanded(G, kl, ku)
685+
end
686+
@testset for k in -10:10
687+
@test istriu(A,k) == istriu(G,k)
688+
@test istril(A,k) == istril(G,k)
689+
end
679690
end
680691

681692
@testset "missing values" begin

0 commit comments

Comments
 (0)