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

Use zero in istriu, istril methods #19399

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ end
transpose(M::Bidiagonal) = Bidiagonal(M.dv, M.ev, !M.isupper)
ctranspose(M::Bidiagonal) = Bidiagonal(conj(M.dv), conj(M.ev), !M.isupper)

istriu(M::Bidiagonal) = M.isupper || all(M.ev .== 0)
istril(M::Bidiagonal) = !M.isupper || all(M.ev .== 0)
istriu(M::Bidiagonal) = M.isupper || all(M.ev .== zero(eltype(M)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this one

all(t -> t == zero(t), M.ev)

because then you use the instance instead of the type to create the zero. Not all types support zero, e.g. Matrix.

istril(M::Bidiagonal) = !M.isupper || all(M.ev .== zero(eltype(M)))

function tril!(M::Bidiagonal, k::Integer=0)
n = length(M.dv)
Expand Down
6 changes: 4 additions & 2 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,9 @@ true
"""
function istriu(A::AbstractMatrix)
m, n = size(A)
z = zero(eltype(A))
for j = 1:min(n,m-1), i = j+1:m
if A[i,j] != 0
if A[i,j] != z
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use A[i,j] != zeros(A[i,j]) for the same reason as before and since the element type could potentially change over the array. This would also fail if the array was a Vector{Any}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably a typo but I guess zero(A[i,j]) instead of zeros.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed

return false
end
end
Expand Down Expand Up @@ -905,8 +906,9 @@ true
"""
function istril(A::AbstractMatrix)
m, n = size(A)
z = zero(eltype(A))
for j = 2:n, i = 1:min(j-1,m)
if A[i,j] != 0
if A[i,j] != z
return false
end
end
Expand Down
8 changes: 4 additions & 4 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ eigvecs{T<:BlasFloat,Eigenvalue<:Real}(A::SymTridiagonal{T}, eigvals::Vector{Eig

#tril and triu

istriu(M::SymTridiagonal) = all(M.ev .== 0)
istril(M::SymTridiagonal) = all(M.ev .== 0)
istriu(M::SymTridiagonal) = all(M.ev .== zero(eltype(M)))
istril(M::SymTridiagonal) = all(M.ev .== zero(eltype(M)))

function tril!(M::SymTridiagonal, k::Integer=0)
n = length(M.dv)
Expand Down Expand Up @@ -526,8 +526,8 @@ end

#tril and triu

istriu(M::Tridiagonal) = all(M.dl .== 0)
istril(M::Tridiagonal) = all(M.du .== 0)
istriu(M::Tridiagonal) = all(M.dl .== zero(eltype(M)))
istril(M::Tridiagonal) = all(M.du .== zero(eltype(M)))

function tril!(M::Tridiagonal, k::Integer=0)
n = length(M.d)
Expand Down