Skip to content
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

Add blocksize keyword argument to qr[!] #33053

Merged
merged 1 commit into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -32,6 +32,8 @@ Standard library changes

#### LinearAlgebra

* `qr` and `qr!` functions support `blocksize` keyword argument ([#33053]).


#### SparseArrays

Expand Down
28 changes: 16 additions & 12 deletions stdlib/LinearAlgebra/src/qr.jl
Expand Up @@ -246,20 +246,23 @@ function qrfactPivotedUnblocked!(A::StridedMatrix)
end

# LAPACK version
qr!(A::StridedMatrix{<:BlasFloat}, ::Val{false}) = QRCompactWY(LAPACK.geqrt!(A, min(min(size(A)...), 36))...)
qr!(A::StridedMatrix{<:BlasFloat}, ::Val{false} = Val(false); blocksize=36) =
QRCompactWY(LAPACK.geqrt!(A, min(min(size(A)...), blocksize))...)
qr!(A::StridedMatrix{<:BlasFloat}, ::Val{true}) = QRPivoted(LAPACK.geqp3!(A)...)
qr!(A::StridedMatrix{<:BlasFloat}) = qr!(A, Val(false))

# Generic fallbacks

"""
qr!(A, pivot=Val(false))
qr!(A, pivot=Val(false); blocksize)

`qr!` is the same as [`qr`](@ref) when `A` is a subtype of
`StridedMatrix`, but saves space by overwriting the input `A`, instead of creating a copy.
An [`InexactError`](@ref) exception is thrown if the factorization produces a number not
representable by the element type of `A`, e.g. for integer types.

!!! compat "Julia 1.4"
The `blocksize` keyword argument requires Julia 1.4 or later.

# Examples
```jldoctest
julia> a = [1. 2.; 3. 4.]
Expand Down Expand Up @@ -296,7 +299,7 @@ qr!(A::StridedMatrix) = qr!(A, Val(false))
_qreltype(::Type{T}) where T = typeof(zero(T)/sqrt(abs2(one(T))))

"""
qr(A, pivot=Val(false)) -> F
qr(A, pivot=Val(false); blocksize) -> F

Compute the QR factorization of the matrix `A`: an orthogonal (or unitary if `A` is
complex-valued) matrix `Q`, and an upper triangular matrix `R` such that
Expand Down Expand Up @@ -336,6 +339,13 @@ and `F.Q*A` are supported. A `Q` matrix can be converted into a regular matrix w
`m`×`m` orthogonal matrix, use `F.Q*Matrix(I,m,m)`. If `m<=n`, then `Matrix(F.Q)` yields an `m`×`m`
orthogonal matrix.

The block size for QR decomposition can be specified by keyword argument
`blocksize :: Integer` when `pivot == Val(false)` and `A isa StridedMatrix{<:BlasFloat}`.
It is ignored when `blocksize > minimum(size(A))`. See [`QRCompactWY`](@ref).

!!! compat "Julia 1.4"
The `blocksize` keyword argument requires Julia 1.4 or later.

# Examples
```jldoctest
julia> A = [3.0 -6.0; 4.0 -8.0; 0.0 1.0]
Expand Down Expand Up @@ -366,17 +376,11 @@ true
elementary reflectors, so that the `Q` and `R` matrices can be stored
compactly rather as two separate dense matrices.
"""
function qr(A::AbstractMatrix{T}, arg) where T
require_one_based_indexing(A)
AA = similar(A, _qreltype(T), size(A))
copyto!(AA, A)
return qr!(AA, arg)
end
function qr(A::AbstractMatrix{T}) where T
function qr(A::AbstractMatrix{T}, arg...; kwargs...) where T
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qr(A::AbstractMatrix{T}, arg) and qr(A::AbstractMatrix{T}) had almost identical function body. I think vararg is a cleaner solution to make it DRY. What do you think?

require_one_based_indexing(A)
AA = similar(A, _qreltype(T), size(A))
copyto!(AA, A)
return qr!(AA)
return qr!(AA, arg...; kwargs...)
end
qr(x::Number) = qr(fill(x,1,1))
function qr(v::AbstractVector)
Expand Down