Skip to content

Commit 8154f8e

Browse files
committed
dispatch on haszero
1 parent b9a0dfb commit 8154f8e

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/generic.jl

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,9 @@ julia> triu(a,-3)
464464
1.0 1.0 1.0 1.0
465465
```
466466
"""
467-
function triu(M::AbstractMatrix, k::Integer = 0)
467+
triu(M::AbstractMatrix, k::Integer = 0) = _triu(M, Val(haszero(eltype(M))), k)
468+
function _triu(M::AbstractMatrix, ::Val{true}, k::Integer)
468469
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
474470
A = triu!(d,k)
475471
if iszero(k)
476472
copytrito!(A, M, 'U')
@@ -482,6 +478,14 @@ function triu(M::AbstractMatrix, k::Integer = 0)
482478
end
483479
return A
484480
end
481+
function _triu(M::AbstractMatrix, ::Val{false}, k::Integer)
482+
d = similar(M)
483+
# since the zero would need to be evaluated from the elements,
484+
# we copy the array to avoid undefined references in triu!
485+
copy!(d, M)
486+
A = triu!(d,k)
487+
return A
488+
end
485489

486490
"""
487491
tril(M, k::Integer = 0)
@@ -512,13 +516,9 @@ julia> tril(a,-3)
512516
1.0 0.0 0.0 0.0
513517
```
514518
"""
515-
function tril(M::AbstractMatrix,k::Integer=0)
519+
tril(M::AbstractMatrix,k::Integer=0) = _tril(M, Val(haszero(eltype(M))), k)
520+
function _tril(M::AbstractMatrix, ::Val{true}, k::Integer)
516521
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
522522
A = tril!(d,k)
523523
if iszero(k)
524524
copytrito!(A, M, 'L')
@@ -530,6 +530,14 @@ function tril(M::AbstractMatrix,k::Integer=0)
530530
end
531531
return A
532532
end
533+
function _tril(M::AbstractMatrix, ::Val{false}, k::Integer)
534+
d = similar(M)
535+
# since the zero would need to be evaluated from the elements,
536+
# we copy the array to avoid undefined references in tril!
537+
copy!(d, M)
538+
A = tril!(d,k)
539+
return A
540+
end
533541

534542
"""
535543
triu!(M)

0 commit comments

Comments
 (0)