From d4b9ee9278592c4bf193a1bba37c7b0b35ceaea4 Mon Sep 17 00:00:00 2001 From: Qingyu Qu <2283984853@qq.com> Date: Sat, 19 Sep 2026 06:24:27 +0800 Subject: [PATCH] Boundary block should use dense storage --- src/FastAlmostBandedMatrices.jl | 5 +++-- test/core_tests.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/FastAlmostBandedMatrices.jl b/src/FastAlmostBandedMatrices.jl index f01eb59..956182c 100644 --- a/src/FastAlmostBandedMatrices.jl +++ b/src/FastAlmostBandedMatrices.jl @@ -613,7 +613,7 @@ function _almostbanded_qr(_, A) # Expand the bandsize for the QR factorization ## Bypass the safety checks in `AlmostBandedMatrix` return almostbanded_qr!( - AlmostBandedMatrix{eltype(A)}(BandedMatrix(copy(B), (l, l + u)), copy(L)), Val(true) + AlmostBandedMatrix{eltype(A)}(BandedMatrix(copy(B), (l, l + u)), Matrix(L)), Val(true) ) end @@ -641,7 +641,8 @@ end @views function _almostbanded_qr!(A::AbstractMatrix, τ::AbstractVector, ncols::Int) T = eltype(A) - B, L = bandpart(A), fillpart(A) + B = bandpart(A) + L = convert(Matrix{T}, fillpart(A)) l, u = bandwidths(B) m, n = size(A) mf = size(L, 1) diff --git a/test/core_tests.jl b/test/core_tests.jl index 8fc8035..2ebb53c 100644 --- a/test/core_tests.jl +++ b/test/core_tests.jl @@ -205,3 +205,31 @@ end fill!(A1, BigFloat(0.0)) @test length(A1.fill.nzval) == 2 end + +@safetestset "QR with a sparse boundary block" begin + using FastAlmostBandedMatrices, LinearAlgebra, SparseArrays + + for T in (Float64, ComplexF64), m in (40, 48), storage in (Matrix, sparse) + n = 40 + B = BandedMatrix(randn(T, m, n), (3, 2)) + B[band(0)] .+= 10 + L = zeros(T, 3, n) + L[:, 1:3] = 10Matrix{T}(I, 3, 3) + L[:, (n - 2):n] = randn(T, 3, 3) + A = AlmostBandedMatrix(B, storage(L)) + reference = Matrix(A) + b = randn(T, m) + for factorize in (qr, qr!) + F = factorize(copy(A)) + U, V = fillpart(F.factors).A, fillpart(F.factors).B + @test U isa Matrix{T} + @test V isa Matrix{T} + @test typeof(F) === typeof(FastAlmostBandedMatrices.ArrayInterface.qr_instance(A)) + @test F \ b ≈ reference \ b + @test Matrix(A) == reference + end + F = qr(A) + @test Matrix(A) == reference # non-mutating QR must preserve its input + @test F \ b ≈ reference \ b + end +end