Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize istriu/istril to accept a band index #590

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4141,7 +4141,7 @@ function is_hermsym(A::AbstractSparseMatrixCSC, check::Function)
return true
end

function istriu(A::AbstractSparseMatrixCSC)
function istriu(A::AbstractSparseMatrixCSC, k::Integer=0)
m, n = size(A)
colptr = getcolptr(A)
rowval = rowvals(A)
Expand All @@ -4150,7 +4150,8 @@ function istriu(A::AbstractSparseMatrixCSC)
for col = 1:min(n, m-1)
l1 = colptr[col+1]-1
for i = 0 : (l1 - colptr[col])
if rowval[l1-i] <= col
if rowval[l1-i] <= col - k
# rows preceeding the index would also lie above the band
break
end
if _isnotzero(nzval[l1-i])
Expand All @@ -4161,15 +4162,16 @@ function istriu(A::AbstractSparseMatrixCSC)
return true
end

function istril(A::AbstractSparseMatrixCSC)
function istril(A::AbstractSparseMatrixCSC, k::Integer=0)
m, n = size(A)
colptr = getcolptr(A)
rowval = rowvals(A)
nzval = nonzeros(A)

for col = 2:n
for i = colptr[col] : (colptr[col+1]-1)
if rowval[i] >= col
if rowval[i] >= col - k
# subsequent rows would also lie below the band
break
end
if _isnotzero(nzval[i])
Expand Down
13 changes: 13 additions & 0 deletions test/sparsematrix_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,17 @@ end
@test_throws ArgumentError copytrito!(M, S, 'M')
end

@testset "istriu/istril" begin
for T in Any[Tridiagonal(1:3, 1:4, 1:3),
Bidiagonal(1:4, 1:3, :U), Bidiagonal(1:4, 1:3, :L),
Diagonal(1:4),
diagm(-2=>1:2, 2=>1:2)]
S = sparse(T)
for k in -5:5
@test istriu(S, k) == istriu(T, k)
@test istril(S, k) == istril(T, k)
end
end
end

end # module
Loading