From 6e8982c0d8dbe7faa562af09cc3c349d5e91e5f4 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Sat, 27 Apr 2024 09:08:24 +0100 Subject: [PATCH] materialize! for banded QRPackedQLayout (#440) * materialize! for banded QRPackedQLayout * Update Project.toml * Update test_bandedqr.jl * Add resize --- Project.toml | 2 +- src/BandedMatrices.jl | 3 ++- src/banded/BandedMatrix.jl | 14 ++++++++++++++ src/banded/bandedqr.jl | 8 ++++++++ test/test_banded.jl | 15 ++++++++++++++- test/test_bandedqr.jl | 16 +++++++++++++--- 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 84b1faae..5bf621cd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BandedMatrices" uuid = "aae01518-5342-5314-be14-df237901396f" -version = "1.6.1" +version = "1.7" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" diff --git a/src/BandedMatrices.jl b/src/BandedMatrices.jl index a0fac6cd..ff9b760a 100644 --- a/src/BandedMatrices.jl +++ b/src/BandedMatrices.jl @@ -31,7 +31,8 @@ import ArrayLayouts: AbstractTridiagonalLayout, BidiagonalLayout, BlasMatLdivVec colsupport, conjlayout, copymutable_oftype_layout, diagonaldata, dualadjoint, hermitiandata, hermitianlayout, materialize, materialize!, reflector!, reflectorApply!, rowsupport, sub_materialize, subdiagonaldata, sublayout, supdiagonaldata, symmetricdata, symmetriclayout, - symmetricuplo, transposelayout, triangulardata, triangularlayout, zero! + symmetricuplo, transposelayout, triangulardata, triangularlayout, zero!, + QRPackedQLayout, AdjQRPackedQLayout import FillArrays: AbstractFill, getindex_value, _broadcasted_zeros, unique_value, OneElement, RectDiagonal diff --git a/src/banded/BandedMatrix.jl b/src/banded/BandedMatrix.jl index 9e43ee33..7872c586 100644 --- a/src/banded/BandedMatrix.jl +++ b/src/banded/BandedMatrix.jl @@ -992,3 +992,17 @@ function show(io::IO, B::BandedMatrix) end print(io, ")") end + + +### +# resize +### + +function resize(A::BandedMatrix, n::Integer, m::Integer) + l,u = bandwidths(A) + _BandedMatrix(reshape(resize!(vec(bandeddata(A)), (l+u+1)*m), l+u+1, m), n, l,u) +end +function resize(A::BandedSubBandedMatrix, n::Integer, m::Integer) + l,u = bandwidths(A) + _BandedMatrix(reshape(resize!(vec(copy(bandeddata(A))), (l+u+1)*m), l+u+1, m), n, l,u) +end diff --git a/src/banded/bandedqr.jl b/src/banded/bandedqr.jl index 9904988b..5df0d494 100644 --- a/src/banded/bandedqr.jl +++ b/src/banded/bandedqr.jl @@ -205,3 +205,11 @@ for BTyp in (:AbstractBandedMatrix, :BandedSubBandedMatrix), Typ in (:StridedVe end end end + + + +materialize!(M::Lmul{<:QRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_lmul!(M.A,M.B) +materialize!(M::Lmul{<:AdjQRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_lmul!(M.A,M.B) + +materialize!(M::Rmul{<:Any,<:QRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_rmul!(M.A,M.B) +materialize!(M::Rmul{<:Any,<:AdjQRPackedQLayout{<:AbstractBandedLayout}}) = banded_qr_rmul!(M.A,M.B) diff --git a/test/test_banded.jl b/test/test_banded.jl index 2560aea9..b41143f1 100644 --- a/test/test_banded.jl +++ b/test/test_banded.jl @@ -2,7 +2,7 @@ module TestBanded using ArrayLayouts using BandedMatrices -import BandedMatrices: _BandedMatrix +using BandedMatrices: _BandedMatrix, resize using FillArrays using LinearAlgebra using SparseArrays @@ -545,6 +545,19 @@ include("mymatrix.jl") @test BandedMatrices.colrange(A, 3) == 3:3 @test BandedMatrices.colrange(A, 4) == 4:4 end + + @testset "resize" begin + B = brand(5,6,2,1) + B̃ = resize(B, 10,7) + @test size(B̃) == (10,7) + @test bandwidths(B̃) == (2,1) + @test B̃[1:5,1:6] == B + + C = resize(view(B,1:4,1:5), 10, 7) + @test size(C) == (10,7) + @test bandwidths(C) == (2,1) + @test C[1:4,1:5] == B[1:4,1:5] + end end end # module diff --git a/test/test_bandedqr.jl b/test/test_bandedqr.jl index 74e34119..67696cde 100644 --- a/test/test_bandedqr.jl +++ b/test/test_bandedqr.jl @@ -1,6 +1,6 @@ module TestBandedQR -using BandedMatrices, LinearAlgebra, Test, Random +using BandedMatrices, ArrayLayouts, LinearAlgebra, Test, Random import BandedMatrices: banded_qr! Random.seed!(0) @@ -123,13 +123,23 @@ Random.seed!(0) @test lmul!(Q', view(BandedMatrix(B,(size(B,1),2)),:,4:10)) ≈ Q'*B[:,4:10] end + @testset "lmul!" begin + for T in (Float64,ComplexF64,Float32,ComplexF32) + A = brand(T,10,10,3,2) + Q,R = qr(A) + B = randn(T, 10, 2) + @test ArrayLayouts.lmul!(Q', copy(B)) == lmul!(Q', copy(B)) ≈ Q'B + @test ArrayLayouts.lmul!(Q, copy(B)) == lmul!(Q, copy(B)) ≈ Q*B + end + end + @testset "rmul!" begin for T in (Float64,ComplexF64,Float32,ComplexF32) A = brand(T,10,10,3,2) Q,R = qr(A) B = randn(T, 2, 10) - @test rmul!(copy(B), Q') ≈ B*Q' - @test rmul!(copy(B), Q) ≈ B*Q + @test ArrayLayouts.rmul!(copy(B), Q') == rmul!(copy(B), Q') ≈ B*Q' + @test ArrayLayouts.rmul!(copy(B), Q) == rmul!(copy(B), Q) ≈ B*Q end end end