Skip to content

Commit 27de9e9

Browse files
authored
Unaliasing and short-circuiting in copytrito! (#1287)
The LAPACK function `lacpy!` seems to handle aliasing correctly already, but since the LAPACK documentation doesn't expect the output arguments to be aliased, it's best if we carry out the unaliasing by ourselves. I've also added a quick return criterion if the two matrices are identical, in which case there is nothing to copy. This would be useful in copying elements between a triangular/symmetric matrix and its parent.
1 parent a32a281 commit 27de9e9

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/generic.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ julia> copytrito!(B, A, 'L')
20932093
function copytrito!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar)
20942094
require_one_based_indexing(A, B)
20952095
BLAS.chkuplo(uplo)
2096+
B === A && return B
20962097
m,n = size(A)
20972098
A = Base.unalias(B, A)
20982099
if uplo == 'U'
@@ -2114,5 +2115,10 @@ function copytrito!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar)
21142115
end
21152116
# Forward LAPACK-compatible strided matrices to lacpy
21162117
function copytrito!(B::StridedMatrixStride1{T}, A::StridedMatrixStride1{T}, uplo::AbstractChar) where {T<:BlasFloat}
2118+
require_one_based_indexing(A, B)
2119+
BLAS.chkuplo(uplo)
2120+
B === A && return B
2121+
A = Base.unalias(B, A)
21172122
LAPACK.lacpy!(B, A, uplo)
2123+
return B
21182124
end

test/generic.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,4 +915,13 @@ end
915915
@test mul!(copy!(similar(v), v), v, 2, 2, 0) == 4v
916916
end
917917

918+
@testset "aliasing in copytrito! for strided matrices" begin
919+
M = rand(4, 1)
920+
A = view(M, 1:3, 1:1)
921+
A2 = copy(A)
922+
B = view(M, 2:4, 1:1)
923+
copytrito!(B, A, 'L')
924+
@test B == A2
925+
end
926+
918927
end # module TestGeneric

0 commit comments

Comments
 (0)