Skip to content

Commit 06be3bd

Browse files
committed
Copy matrices in triu/tril if no zero exists
1 parent 95703b5 commit 06be3bd

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/dense.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ haszero(::Type) = false
124124
haszero(::Type{T}) where {T<:Number} = isconcretetype(T)
125125
haszero(::Type{Union{Missing,T}}) where {T<:Number} = haszero(T)
126126
@propagate_inbounds _zero(M::AbstractArray{T}, inds...) where {T} = haszero(T) ? zero(T) : zero(M[inds...])
127+
function zero!(M::AbstractArray{T}) where {T}
128+
if haszero(T)
129+
fill!(M, zero(T))
130+
else
131+
for i in eachindex(M)
132+
v = @inbounds M[i]
133+
z = zero(v)
134+
@inbounds M[i] = z
135+
end
136+
end
137+
end
127138

128139
"""
129140
triu!(M, k::Integer)

src/diagonal.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function triu!(D::Diagonal{T}, k::Integer=0) where T
279279
throw(ArgumentError(string("the requested diagonal, $k, must be at least ",
280280
"$(-n + 1) and at most $(n + 1) in an $n-by-$n matrix")))
281281
elseif k > 0
282-
fill!(D.diag, zero(T))
282+
zero!(D.diag)
283283
end
284284
return D
285285
end
@@ -290,7 +290,7 @@ function tril!(D::Diagonal{T}, k::Integer=0) where T
290290
throw(ArgumentError(LazyString(lazy"the requested diagonal, $k, must be at least ",
291291
lazy"$(-n - 1) and at most $(n - 1) in an $n-by-$n matrix")))
292292
elseif k < 0
293-
fill!(D.diag, zero(T))
293+
zero!(D.diag)
294294
end
295295
return D
296296
end

src/generic.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ julia> triu(a,-3)
466466
"""
467467
function triu(M::AbstractMatrix, k::Integer = 0)
468468
d = similar(M)
469+
if !haszero(eltype(M))
470+
# since the zero would need to be evaluated from the elements,
471+
# we copy the array to avoid undefined references in triu!
472+
copy!(d, M)
473+
end
469474
A = triu!(d,k)
470475
if iszero(k)
471476
copytrito!(A, M, 'U')
@@ -509,6 +514,11 @@ julia> tril(a,-3)
509514
"""
510515
function tril(M::AbstractMatrix,k::Integer=0)
511516
d = similar(M)
517+
if !haszero(eltype(M))
518+
# since the zero would need to be evaluated from the elements,
519+
# we copy the array to avoid undefined references in tril!
520+
copy!(d, M)
521+
end
512522
A = tril!(d,k)
513523
if iszero(k)
514524
copytrito!(A, M, 'L')

test/diagonal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,13 @@ end
819819
@test all(x -> size(x) == (2,2), D)
820820
@test D == D1 * D2
821821
end
822+
823+
@testset "triu/tril" begin
824+
D = Diagonal(fill(ones(2,2), 3))
825+
M = Matrix{eltype(D)}(D)
826+
@test triu(D,1) == triu(M,1)
827+
@test tril(D,-1) == tril(M,-1)
828+
end
822829
end
823830

824831
@testset "Eigensystem for block diagonal (issue #30681)" begin

0 commit comments

Comments
 (0)