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

tril/triu for ZerosMatrix and OneElementMatrix #334

Merged
merged 2 commits into from
Jul 8, 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
3 changes: 3 additions & 0 deletions src/fillalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,6 @@ end
function LinearAlgebra.istril(A::AbstractFillMatrix, k::Integer = 0)
iszero(A) || k >= size(A,2)-1
end

triu(A::AbstractZerosMatrix, k::Integer=0) = A
tril(A::AbstractZerosMatrix, k::Integer=0) = A
13 changes: 13 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,20 @@ 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))

# tril/triu

function tril(A::OneElementMatrix, k::Integer=0)
nzband = A.ind[2] - A.ind[1]
OneElement(nzband > k ? zero(A.val) : A.val, A.ind, axes(A))
end

function triu(A::OneElementMatrix, k::Integer=0)
nzband = A.ind[2] - A.ind[1]
OneElement(nzband < k ? zero(A.val) : A.val, A.ind, axes(A))
end

# broadcast

function broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::OneElement{<:Any,N}) where {N}
OneElement(conj(r.val), r.ind, axes(r))
end
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,17 @@ end
@test A / (2 + 3.0im) === OneElement(val / (2 + 3.0im), (2,2), (3,4)) == B / (2 + 3.0im)
end


@testset "tril/triu" begin
for A in (OneElement(3, (4,2), (4,5)), OneElement(3, (2,3), (4,5)), OneElement(3, (3,3), (4,5)))
B = Array(A)
for k in -5:5
@test tril(A, k) == tril(B, k)
@test triu(A, k) == triu(B, k)
end
end
end

@testset "broadcasting" begin
for v in (OneElement(2, 3, 4), OneElement(2im, (1,2), (3,4)))
w = Array(v)
Expand Down Expand Up @@ -2634,3 +2645,11 @@ end
end
end
end

@testset "triu/tril for Zeros" begin
Z = Zeros(3, 4)
@test triu(Z) === Z
@test tril(Z) === Z
@test triu(Z, 2) === Z
@test tril(Z, 2) === Z
end
Loading