Skip to content

Commit

Permalink
Specialize isbanded for OneElement (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Jul 21, 2024
1 parent 1896e30 commit a7ddcca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ end
adjoint(A::OneElementMatrix) = OneElement(adjoint(A.val), reverse(A.ind), reverse(A.axes))
transpose(A::OneElementMatrix) = OneElement(transpose(A.val), reverse(A.ind), reverse(A.axes))

# isbanded
function LinearAlgebra.isbanded(A::OneElementMatrix, kl::Integer, ku::Integer)
iszero(getindex_value(A)) || kl <= A.ind[2] - A.ind[1] <= ku
end

# tril/triu

function tril(A::OneElementMatrix, k::Integer=0)
Expand Down
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,37 @@ end
@test A / (2 + 3.0im) === OneElement(val / (2 + 3.0im), (2,2), (3,4)) == B / (2 + 3.0im)
end


@testset "isbanded" begin
A = OneElement(3, (2,3), (4,5))
@test !isdiag(A)
@test istriu(A)
@test !istril(A)
@test LinearAlgebra.isbanded(A, 1, 2)
@test LinearAlgebra.isbanded(A, -1, 2)
@test !LinearAlgebra.isbanded(A, 2, 2)

A = OneElement(3, (4,2), (4,5))
@test !isdiag(A)
@test !istriu(A)
@test istril(A)
@test LinearAlgebra.isbanded(A, -2, -2)
@test LinearAlgebra.isbanded(A, -2, 2)
@test !LinearAlgebra.isbanded(A, 2, 2)

for A in (OneElement(3, (2,2), (4,5)), OneElement(3, (1,1), (1,2)), OneElement(3, (8,7), (2,1)))
@test isdiag(A)
@test istriu(A)
@test istril(A)
end

for A in (OneElement(0, (2,3), (4,5)), OneElement(0, (3,2), (4,5)))
@test isdiag(A)
@test istriu(A)
@test istril(A)
end
end

@testset "zero/iszero" begin
v = OneElement(10, 3, 4)
@test v + zero(v) == v
Expand Down

0 comments on commit a7ddcca

Please sign in to comment.