From 82a7adc042cdc162999750c6b9610aac9aaf7e67 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 8 Oct 2025 16:50:57 +0100 Subject: [PATCH 1/4] Overload array constructors for BunchKaufman (#1461) --- src/bunchkaufman.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/bunchkaufman.jl b/src/bunchkaufman.jl index 69811fde..b4430451 100644 --- a/src/bunchkaufman.jl +++ b/src/bunchkaufman.jl @@ -217,6 +217,12 @@ BunchKaufman{T}(B::BunchKaufman) where {T} = BunchKaufman(convert(Matrix{T}, B.LD), B.ipiv, B.uplo, B.symmetric, B.rook, B.info) Factorization{T}(B::BunchKaufman) where {T} = BunchKaufman{T}(B) +AbstractMatrix(B::BunchKaufman) = B.uplo == 'U' ? B.P'B.U*B.D*B.U'B.P : B.P'B.L*B.D*B.L'B.P +AbstractArray(B::BunchKaufman) = AbstractMatrix(B) +Matrix(B::BunchKaufman) = Array(AbstractArray(B)) +Array(B::BunchKaufman) = Matrix(B) + + size(B::BunchKaufman) = size(getfield(B, :LD)) size(B::BunchKaufman, d::Integer) = size(getfield(B, :LD), d) issymmetric(B::BunchKaufman) = B.symmetric From 23ceb451ab3814c045af2be286f59fab68fa9170 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 8 Oct 2025 16:55:03 +0100 Subject: [PATCH 2/4] add tests --- test/bunchkaufman.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/bunchkaufman.jl b/test/bunchkaufman.jl index cf4fa488..4eebc4c7 100644 --- a/test/bunchkaufman.jl +++ b/test/bunchkaufman.jl @@ -259,4 +259,13 @@ end @test B.U * B.D * B.U' ≈ S end +@testset "BunchKaufman array constructors #1461" begin + a = randn(5,5) + A = a'a + for ul in (:U, :L) + B = bunchkaufman(A, ul) + @test A ≈ Array(B) ≈ Matrix(B) ≈ AbstractArray(B) ≈ AbstractMatrix(B) + end +end + end # module TestBunchKaufman From dfb133492d2d75a5ebee88b1aa69b8e14e3a7d7b Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Wed, 8 Oct 2025 17:19:11 +0100 Subject: [PATCH 3/4] Update bunchkaufman.jl --- test/bunchkaufman.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bunchkaufman.jl b/test/bunchkaufman.jl index 4eebc4c7..3e3e0d71 100644 --- a/test/bunchkaufman.jl +++ b/test/bunchkaufman.jl @@ -263,7 +263,7 @@ end a = randn(5,5) A = a'a for ul in (:U, :L) - B = bunchkaufman(A, ul) + B = bunchkaufman(Symmetric(A, ul)) @test A ≈ Array(B) ≈ Matrix(B) ≈ AbstractArray(B) ≈ AbstractMatrix(B) end end From 23e122f578433f7468d822882dae07dcc72d6092 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Thu, 9 Oct 2025 10:36:45 +0100 Subject: [PATCH 4/4] avoid extra copy --- src/bunchkaufman.jl | 2 +- src/cholesky.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bunchkaufman.jl b/src/bunchkaufman.jl index b4430451..d962f9bd 100644 --- a/src/bunchkaufman.jl +++ b/src/bunchkaufman.jl @@ -219,7 +219,7 @@ Factorization{T}(B::BunchKaufman) where {T} = BunchKaufman{T}(B) AbstractMatrix(B::BunchKaufman) = B.uplo == 'U' ? B.P'B.U*B.D*B.U'B.P : B.P'B.L*B.D*B.L'B.P AbstractArray(B::BunchKaufman) = AbstractMatrix(B) -Matrix(B::BunchKaufman) = Array(AbstractArray(B)) +Matrix(B::BunchKaufman) = convert(Array, AbstractArray(B)) Array(B::BunchKaufman) = Matrix(B) diff --git a/src/cholesky.jl b/src/cholesky.jl index d960ce57..675c93a6 100644 --- a/src/cholesky.jl +++ b/src/cholesky.jl @@ -646,7 +646,7 @@ Factorization{T}(C::CholeskyPivoted) where {T} = CholeskyPivoted{T}(C) AbstractMatrix(C::Cholesky) = C.uplo == 'U' ? C.U'C.U : C.L*C.L' AbstractArray(C::Cholesky) = AbstractMatrix(C) -Matrix(C::Cholesky) = Array(AbstractArray(C)) +Matrix(C::Cholesky) = convert(Array, AbstractArray(C)) Array(C::Cholesky) = Matrix(C) function AbstractMatrix(F::CholeskyPivoted) @@ -655,7 +655,7 @@ function AbstractMatrix(F::CholeskyPivoted) U'U end AbstractArray(F::CholeskyPivoted) = AbstractMatrix(F) -Matrix(F::CholeskyPivoted) = Array(AbstractArray(F)) +Matrix(F::CholeskyPivoted) = convert(Array, AbstractArray(F)) Array(F::CholeskyPivoted) = Matrix(F) copy(C::Cholesky) = Cholesky(copy(C.factors), C.uplo, C.info)