Skip to content

Introduce QRPackedQMatrix #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
21 changes: 14 additions & 7 deletions src/MatrixFactorizations.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MatrixFactorizations
using Base, LinearAlgebra, ArrayLayouts
import Base: axes, axes1, getproperty, iterate, tail, oneto
import Base: axes, axes1, getproperty, iterate, tail, oneto, BroadcastStyle
import LinearAlgebra: BlasInt, BlasReal, BlasFloat, BlasComplex, axpy!,
copy_oftype, checksquare, adjoint, transpose, AdjOrTrans, HermOrSym,
det, logdet, logabsdet, isposdef
Expand Down Expand Up @@ -50,12 +50,18 @@
abstract type LayoutQ{T} <: AbstractQ{T} end


# Support orthogonal matrices as an abstract matrix
abstract type LayoutQMatrix{T} <: LayoutMatrix{T} end

const LayoutQTypes{T} = Union{LayoutQ{T}, LayoutQMatrix{T}}


@_layoutlmul LayoutQ
@_layoutlmul AdjointQtype{<:Any,<:LayoutQ}
@_layoutrmul LayoutQ
@_layoutrmul AdjointQtype{<:Any,<:LayoutQ}

LinearAlgebra.copymutable(Q::LayoutQ) = copymutable_size(size(Q), Q)
LinearAlgebra.copymutable(Q::LayoutQTypes) = copymutable_size(size(Q), Q)
copymutable_size(sz, Q) = lmul!(Q, Matrix{eltype(Q)}(I, sz))

(*)(Q::LayoutQ, b::AbstractVector) = _mul(Q, b)
Expand Down Expand Up @@ -106,16 +112,17 @@
*(A::AbstractTriangular, B::LayoutQ) = mul(A, B)
*(A::AbstractTriangular, B::AdjointQtype{<:Any,<:LayoutQ}) = mul(A, B)

axes(Q::LayoutQ, dim::Integer) = axes(getfield(Q, :factors), dim == 2 ? 1 : dim)
axes(Q::LayoutQ) = axes(Q, 1), axes(Q, 2)
copy(Q::LayoutQ) = Q
axes(Q::LayoutQTypes, dim::Integer) = axes(getfield(Q, :factors), dim == 2 ? 1 : dim)
axes(Q::LayoutQTypes) = axes(Q, 1), axes(Q, 2)
copy(Q::LayoutQTypes) = Q
Base.@propagate_inbounds getindex(A::LayoutQ, I...) = layout_getindex(A, I...)
Base.@propagate_inbounds getindex(A::LayoutQMatrix, k::Int, j::Int) = AbstractQ(A)[k, j]

Check warning on line 119 in src/MatrixFactorizations.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixFactorizations.jl#L119

Added line #L119 was not covered by tests
# by default, fall back to AbstractQ methods
layout_getindex(A::LayoutQ, I...) =
Base.invoke(Base.getindex, Tuple{AbstractQ, typeof.(I)...}, A, I...)

size(Q::LayoutQ, dim::Integer) = size(getfield(Q, :factors), dim == 2 ? 1 : dim)
size(Q::LayoutQ) = size(Q, 1), size(Q, 2)
size(Q::LayoutQTypes, dim::Integer) = size(getfield(Q, :factors), dim == 2 ? 1 : dim)
size(Q::LayoutQTypes) = size(Q, 1), size(Q, 2)

include("ul.jl")
include("qr.jl")
Expand Down
55 changes: 43 additions & 12 deletions src/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
ldiv!(F::QR, B::LayoutVector) = ArrayLayouts.ldiv!(F, B)
ldiv!(F::QR, B::LayoutMatrix) = ArrayLayouts.ldiv!(F, B)

size(F::QR, dim::Integer) = size(getfield(F, :factors), dim)
size(F::QR) = size(getfield(F, :factors))

"""
QRPackedQ <: LinearAlgebra.AbstractQ
Expand All @@ -163,28 +165,51 @@



QRPackedQ(factors::AbstractMatrix{T}, τ::AbstractVector{T}) where {T} = QRPackedQ{T,typeof(factors),typeof(τ)}(factors, τ)
function QRPackedQ{T}(factors::AbstractMatrix, τ::AbstractVector) where {T}
QRPackedQ(convert(AbstractMatrix{T}, factors), convert(AbstractVector{T}, τ))
"""
QRPackedQMatrix <: LayoutQMatrix

The orthogonal/unitary ``Q`` matrix of a QR factorization stored in [`QR`](@ref),
conforming to the `AbstractMatrix` interface.
"""
struct QRPackedQMatrix{T,S<:AbstractMatrix{T},Tau<:AbstractVector{T}} <: LayoutQMatrix{T}
factors::S
τ::Tau

function QRPackedQMatrix{T,S,Tau}(factors, τ) where {T,S<:AbstractMatrix{T},Tau<:AbstractVector{T}}
require_one_based_indexing(factors)
new{T,S,Tau}(factors, τ)

Check warning on line 180 in src/qr.jl

View check run for this annotation

Codecov / codecov/patch

src/qr.jl#L178-L180

Added lines #L178 - L180 were not covered by tests
end
end


for QTyp in (:QRPackedQ, :QRPackedQMatrix)
@eval begin
$QTyp(factors::AbstractMatrix{T}, τ::AbstractVector{T}) where {T} = $QTyp{T,typeof(factors),typeof(τ)}(factors, τ)
function $QTyp{T}(factors::AbstractMatrix, τ::AbstractVector) where {T}
$QTyp(convert(AbstractMatrix{T}, factors), convert(AbstractVector{T}, τ))

Check warning on line 189 in src/qr.jl

View check run for this annotation

Codecov / codecov/patch

src/qr.jl#L188-L189

Added lines #L188 - L189 were not covered by tests
end

$QTyp{T}(Q::$QTyp) where {T} = $QTyp(convert(AbstractMatrix{T}, Q.factors), convert(AbstractVector{T}, Q.τ))
end
end

QRPackedQ{T}(Q::QRPackedQ) where {T} = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(AbstractVector{T}, Q.τ))
QRPackedQ(Q::LinearAlgebra.QRPackedQ) = QRPackedQ(Q.factors, Q.τ)

const QRPackedQTypes{T,S<:AbstractMatrix{T},Tau<:AbstractVector{T}} = Union{QRPackedQ{T,S,Tau}, QRPackedQMatrix{T,S,Tau}}

Matrix{T}(Q::QRPackedQTypes{S}) where {T,S} = convert(Matrix{T}, lmul!(Q, Matrix{S}(I, size(Q, 1), min(size(Q.factors)...))))
Matrix(Q::QRPackedQTypes{S}) where {S} = Matrix{S}(Q)

AbstractQ{T}(Q::QRPackedQ{T}) where {T} = Q
AbstractQ{T}(Q::QRPackedQ) where {T} = QRPackedQ{T}(Q)
AbstractQ{T}(Q::QRPackedQMatrix{T}) where {T} = QRPackedQ(Q.factors, Q.τ)
AbstractQ{T}(Q::QRPackedQMatrix) where {T} = QRPackedQ{T}(Q.factors, Q.τ)
AbstractQ(Q::QRPackedQMatrix) = QRPackedQ(Q.factors, Q.τ)

Check warning on line 207 in src/qr.jl

View check run for this annotation

Codecov / codecov/patch

src/qr.jl#L205-L207

Added lines #L205 - L207 were not covered by tests
convert(::Type{AbstractQ{T}}, Q::QRPackedQ) where {T} = QRPackedQ{T}(Q)

Matrix{T}(Q::QRPackedQ{S}) where {T,S} =
convert(Matrix{T}, lmul!(Q, Matrix{S}(I, size(Q, 1), min(size(Q.factors)...))))
Matrix(Q::QRPackedQ{S}) where {S} = Matrix{S}(Q)

AbstractMatrix{T}(Q::QRPackedQ) where {T} = Matrix{T}(Q)

size(F::QR, dim::Integer) = size(getfield(F, :factors), dim)
size(F::QR) = size(getfield(F, :factors))

MemoryLayout(::Type{<:QRPackedQ{<:Any,S,T}}) where {S,T} =
MemoryLayout(::Type{<:QRPackedQTypes{<:Any,S,T}}) where {S,T} =
QRPackedQLayout{typeof(MemoryLayout(S)),typeof(MemoryLayout(T))}()

MemoryLayout(::Type{<:QR{<:Any,S,T}}) where {S,T} =
Expand Down Expand Up @@ -227,3 +252,9 @@
XX = reshape(collect(reinterpret(Complex{T}, copy(transpose(reshape(X, div(length(X), 2), 2))))), _ret_size(A, BIn))
return _cut_B(XX, 1:n)
end



# support lazy broadcasting

BroadcastStyle(::Type{<:QRPackedQMatrix{<:Any,F}}) where {F} = BroadcastStyle(F) # TODO: broken for banded?

Check warning on line 260 in src/qr.jl

View check run for this annotation

Codecov / codecov/patch

src/qr.jl#L260

Added line #L260 was not covered by tests
Loading