Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/lapack.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using SemiseparableMatrices

# A benefit of our approach to QR is that it is in an LAPACK-compatible format. Thus for moderate n our
# O(n) factorization can either be combined with our O(n) solver, or LAPack's optimised O(n^2) solver.
# Here is a demonstration of using this. For small n the dense QR is only marginally slower:

n = 1000
l, m, r, p = 4, 5, 2, 3
B = brandn(n,n,l,m)
U,V = randn(n,r), randn(n,r)
W,S = randn(n,p), randn(n,p)
b = randn(n)

A = BandedPlusSemiseparableMatrix(B,(U,V),(W,S))
à = Matrix(A) # dense A
@time F = qr(A); # BPS QR, 0.01s
@time F̃ = qr(Ã); # Dense QRCompactWY, 0.04s
F̄ = QR(Matrix(F.factors), F.τ); # convert to dense format

@time F\b; # 0.02s
@time F̃\b; # 0.001s
@time F̄\b; # 0.004s

# But if we make n much bigger dense QR becomes prohibitively slow:

n = 10_000
l, m, r, p = 4, 5, 2, 3
B = brandn(n,n,l,m)
U,V = randn(n,r), randn(n,r)
W,S = randn(n,p), randn(n,p)
b = randn(n)

A = BandedPlusSemiseparableMatrix(B,(U,V),(W,S))
à = Matrix(A) # dense A
@time F = qr(A); # BPS QR, 0.11s
@time F̃ = qr(Ã); # Dense QRCompactWY, 27s
@time F̄ = QR(Matrix(F.factors), F.τ); # convert to dense format 2s, A 10x speedup!

@time F.R\(F.Q'b); # 0.001s
@time F̃\b; # 0.08s
@time F̄\b; # 0.25s
3 changes: 2 additions & 1 deletion src/BandedPlusSemiseparableMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ function getindex(A::BandedPlusSemiseparableMatrix, k::Integer, j::Integer)
end
end

function ldiv!(R::UpperTriangular{<:Any,<:BandedPlusSemiseparableMatrix}, b::StridedVector)
function ArrayLayouts.ldiv!(R::UpperTriangular{<:Any,<:BandedPlusSemiseparableMatrix}, b::StridedVecOrMat)
@assert b isa AbstractVector
F = parent(R)
n, p = size(F.W)
l, m = bandwidths(F.B)
Expand Down
4 changes: 3 additions & 1 deletion src/semiseparableqr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,8 @@ function getproperty(F::QR{<:Any,<:BandedPlusSemiseparableMatrix}, d::Symbol)
end
end

function lmul!(adjQ::AdjointQ{<:Any,<:QRPackedQ{<:Any,<:BandedPlusSemiseparableMatrix}}, b::StridedVector)
function ArrayLayouts.lmul!(adjQ::AdjointQ{<:Any,<:QRPackedQ{<:Any,<:BandedPlusSemiseparableMatrix}}, b::StridedVecOrMat)
@assert b isa AbstractVector
Q = parent(adjQ)
F = Q.factors
τ = Q.τ
Expand Down Expand Up @@ -902,6 +903,7 @@ function lmul!(adjQ::AdjointQ{<:Any,<:QRPackedQ{<:Any,<:BandedPlusSemiseparableM
end
#b[n] += F.U[n,:]'*h
b[n] += view(F.U, n, :)' * h
b
end

function Uᵀb_lookup_table(F, b)
Expand Down
2 changes: 2 additions & 0 deletions test/test_qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ using SemiseparableMatrices: BandedPlusSemiseparableQRPerturbedFactors
x = triu(F) \ b
ldiv!(R, b)
@test b ≈ x

@test F \ b_true ≈ x
end
end
Loading