From 2db81efff90f6a263e41b7d44c797c611238e419 Mon Sep 17 00:00:00 2001 From: Alexis Montoison Date: Tue, 31 Oct 2023 22:05:22 -0500 Subject: [PATCH] Update lacpy! and copytotri! --- stdlib/LinearAlgebra/src/generic.jl | 22 ++++++++++++---------- stdlib/LinearAlgebra/src/lapack.jl | 8 ++++---- stdlib/LinearAlgebra/test/generic.jl | 4 ++-- stdlib/LinearAlgebra/test/lapack.jl | 4 ++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index b717f08cd959dd..b96d8e3e0403ae 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1899,7 +1899,7 @@ normalize(x) = x / norm(x) normalize(x, p::Real) = x / norm(x, p) """ - copytotri!(uplo, A, B) -> B + copytotri!(B, A, uplo) -> B Copies a triangular part of a matrix `A` to another matrix `B`. `uplo` specifies the part of the matrix `A` to be copied to `B`. @@ -1915,26 +1915,28 @@ julia> A = [1 2 ; 3 4]; julia> B = [0 0 ; 0 0]; -julia> copytotri!('L', A, B) +julia> copytotri!(B, A, 'L') 2×2 Matrix{Int64}: 1 0 3 4 ``` """ -function copytotri!(uplo::AbstractChar, A::AbstractMatrix, B::AbstractMatrix) +function copytotri!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar) require_one_based_indexing(A, B) LinearAlgebra.BLAS.chkuplo(uplo) - m, n = size(A) + m,n = size(A) + m1,n1 = size(B) + (m1 < m || n1 < n) && throw(DimensionMismatch("B of size ($m1,$n1) should have at least the same number of rows and columns than A of size ($m,$n)")) if uplo == 'U' - @inbounds for j=1:n - @inbounds for i=1:min(j,m) - B[i,j] = A[i,j] + for j=1:n + for i=1:min(j,m) + @inbounds B[i,j] = A[i,j] end end else # uplo == 'L' - @inbounds for j=1:n - @inbounds for i=j:m - B[i,j] = A[i,j] + for j=1:n + for i=j:m + @inbounds B[i,j] = A[i,j] end end end diff --git a/stdlib/LinearAlgebra/src/lapack.jl b/stdlib/LinearAlgebra/src/lapack.jl index cbf0063be23eb3..9cab8832ee0005 100644 --- a/stdlib/LinearAlgebra/src/lapack.jl +++ b/stdlib/LinearAlgebra/src/lapack.jl @@ -7005,7 +7005,7 @@ for (fn, elty) in ((:dlacpy_, :Float64), # .. Array Arguments .. # DOUBLE PRECISION A( LDA, * ), B( LDB, * ) # .. - function lacpy!(uplo::AbstractChar, A::AbstractMatrix{$elty}, B::AbstractMatrix{$elty}) + function lacpy!(B::AbstractMatrix{$elty}, A::AbstractMatrix{$elty}, uplo::AbstractChar) require_one_based_indexing(A, B) chkstride1(A, B) chkuplo(uplo) @@ -7024,7 +7024,7 @@ for (fn, elty) in ((:dlacpy_, :Float64), end """ - lacpy!(uplo, A, B) -> B + lacpy!(B, A, uplo) -> B Copies all or part of a matrix `A` to another matrix `B`. uplo specifies the part of the matrix `A` to be copied to `B`. @@ -7038,12 +7038,12 @@ julia> A = [1. 2. ; 3. 4.]; julia> B = [0. 0. ; 0. 0.]; -julia> LAPACK.lacpy!('U', A, B) +julia> LAPACK.lacpy!(B, A, 'U') 2×2 Matrix{Float64}: 1.0 2.0 0.0 4.0 ``` """ -lacpy!(uplo::AbstractChar, A::AbstractMatrix, B::AbstractMatrix) +lacpy!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar) end # module diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index dab24b0e514021..59c634746fa528 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -641,9 +641,9 @@ end A = rand(n, n) for uplo in ('L', 'U') B = zeros(n, n) - copytotri!(uplo, A, B) + copytotri!(B, A, uplo) C = uplo == 'L' ? tril(A) : triu(A) - @test A == C + @test A ≈ C end end diff --git a/stdlib/LinearAlgebra/test/lapack.jl b/stdlib/LinearAlgebra/test/lapack.jl index c8f5f4482c64e8..93455a3e68ec2d 100644 --- a/stdlib/LinearAlgebra/test/lapack.jl +++ b/stdlib/LinearAlgebra/test/lapack.jl @@ -691,9 +691,9 @@ end A = rand(elty, n, n) for uplo in ('L', 'U', 'N') B = zeros(elty, n, n) - LinearAlgebra.LAPACK.lacpy!(uplo, A, B) + LinearAlgebra.LAPACK.lacpy!(B, A, uplo) C = uplo == 'L' ? tril(A) : (uplo == 'U' ? triu(A) : A) - @test A == C + @test A ≈ C end end end