-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
fix LAPACK.ormlq! and postmultiplication with LQPackedQ #23803
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,17 +92,22 @@ full(Q::LQPackedQ; thin::Bool = true) = | |
|
||
size(A::LQ, dim::Integer) = size(A.factors, dim) | ||
size(A::LQ) = size(A.factors) | ||
function size(A::LQPackedQ, dim::Integer) | ||
if 0 < dim && dim <= 2 | ||
return size(A.factors, dim) | ||
elseif 0 < dim && dim > 2 | ||
return 1 | ||
else | ||
|
||
# size(Q::LQPackedQ) yields the shape of Q's square form | ||
function size(Q::LQPackedQ) | ||
n = size(Q.factors, 2) | ||
return n, n | ||
end | ||
function size(Q::LQPackedQ, dim::Integer) | ||
if dim < 1 | ||
throw(BoundsError()) | ||
elseif dim <= 2 # && 1 <= dim | ||
return size(Q.factors, 2) | ||
else # 2 < dim | ||
return 1 | ||
end | ||
end | ||
|
||
size(A::LQPackedQ) = size(A.factors) | ||
|
||
## Multiplication by LQ | ||
A_mul_B!(A::LQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = | ||
|
@@ -159,39 +164,60 @@ for (f1, f2) in ((:A_mul_Bc, :A_mul_B!), | |
end | ||
end | ||
|
||
### AQ | ||
A_mul_B!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasFloat} = LAPACK.ormlq!('R', 'N', B.factors, B.τ, A) | ||
function *(A::StridedMatrix{TA}, B::LQPackedQ{TB}) where {TA,TB} | ||
TAB = promote_type(TA,TB) | ||
if size(B.factors,2) == size(A,2) | ||
A_mul_B!(copy_oftype(A, TAB),convert(AbstractMatrix{TAB},B)) | ||
elseif size(B.factors,1) == size(A,2) | ||
A_mul_B!( [A zeros(TAB, size(A,1), size(B.factors,2)-size(B.factors,1))], convert(AbstractMatrix{TAB},B)) | ||
# in-place right-application of LQPackedQs | ||
# these methods require that the applied-to matrix's (A's) number of columns | ||
# match the number of columns (nQ) of the LQPackedQ (Q) (necessary for in-place | ||
# operation, and the underlying LAPACK routine (ormlq) treats the implicit Q | ||
# as its (nQ-by-nQ) square form) | ||
A_mul_B!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasFloat} = | ||
LAPACK.ormlq!('R', 'N', B.factors, B.τ, A) | ||
A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasReal} = | ||
LAPACK.ormlq!('R', 'T', B.factors, B.τ, A) | ||
A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasComplex} = | ||
LAPACK.ormlq!('R', 'C', B.factors, B.τ, A) | ||
|
||
# out-of-place right-application of LQPackedQs | ||
# unlike their in-place equivalents, these methods: (1) check whether the applied-to | ||
# matrix's (A's) appropriate dimension (columns for A_*, rows for Ac_*) matches the | ||
# number of columns (nQ) of the LQPackedQ (Q), as the underlying LAPACK routine (ormlq) | ||
# treats the implicit Q as its (nQ-by-nQ) square form; and (2) if the preceding dimensions | ||
# do not match, these methods check whether the appropriate dimension of A instead matches | ||
# the number of rows of the matrix of which Q is a factor (i.e. size(Q.factors, 1)), | ||
# and if so zero-extends A as necessary for check (1) to pass (if possible). | ||
*(A::StridedVecOrMat, Q::LQPackedQ) = _A_mul_Bq(A_mul_B!, A, Q) | ||
A_mul_Bc(A::StridedVecOrMat, Q::LQPackedQ) = _A_mul_Bq(A_mul_Bc!, A, Q) | ||
function _A_mul_Bq(A_mul_Bop!::FT, A::StridedVecOrMat, Q::LQPackedQ) where FT<:Function | ||
TR = promote_type(eltype(A), eltype(Q)) | ||
if size(A, 2) == size(Q.factors, 2) | ||
C = copy_oftype(A, TR) | ||
elseif size(A, 2) == size(Q.factors, 1) | ||
C = zeros(TR, size(A, 1), size(Q.factors, 2)) | ||
copy!(C, 1, A, 1, length(A)) | ||
else | ||
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must equal one of the dimensions of B, $(size(B))")) | ||
_rightappdimmismatch("columns") | ||
end | ||
end | ||
|
||
### AQc | ||
A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasReal} = LAPACK.ormlq!('R','T',B.factors,B.τ,A) | ||
A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasComplex} = LAPACK.ormlq!('R','C',B.factors,B.τ,A) | ||
function A_mul_Bc(A::StridedVecOrMat{TA}, B::LQPackedQ{TB}) where {TA<:Number,TB<:Number} | ||
TAB = promote_type(TA,TB) | ||
A_mul_Bc!(copy_oftype(A, TAB), convert(AbstractMatrix{TAB},(B))) | ||
end | ||
|
||
### AcQ/AcQc | ||
for (f1, f2) in ((:Ac_mul_B, :A_mul_B!), | ||
(:Ac_mul_Bc, :A_mul_Bc!)) | ||
@eval begin | ||
function ($f1)(A::StridedMatrix, B::LQPackedQ) | ||
TAB = promote_type(eltype(A), eltype(B)) | ||
AA = similar(A, TAB, (size(A, 2), size(A, 1))) | ||
adjoint!(AA, A) | ||
return ($f2)(AA, B) | ||
end | ||
return A_mul_Bop!(C, convert(AbstractMatrix{TR}, Q)) | ||
end | ||
Ac_mul_B(A::StridedMatrix, Q::LQPackedQ) = _Ac_mul_Bq(A_mul_B!, A, Q) | ||
Ac_mul_Bc(A::StridedMatrix, Q::LQPackedQ) = _Ac_mul_Bq(A_mul_Bc!, A, Q) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method also does not require |
||
function _Ac_mul_Bq(A_mul_Bop!::FT, A::StridedMatrix, Q::LQPackedQ) where FT<:Function | ||
TR = promote_type(eltype(A), eltype(Q)) | ||
if size(A, 1) == size(Q.factors, 2) | ||
C = adjoint!(similar(A, TR, reverse(size(A))), A) | ||
elseif size(A, 1) == size(Q.factors, 1) | ||
C = zeros(TR, size(A, 2), size(Q.factors, 2)) | ||
adjoint!(view(C, :, 1:size(A, 1)), A) | ||
else | ||
_rightappdimmismatch("rows") | ||
end | ||
return A_mul_Bop!(C, convert(AbstractMatrix{TR}, Q)) | ||
end | ||
_rightappdimmismatch(rowsorcols) = | ||
throw(DimensionMismatch(string("the number of $(rowsorcols) of the matrix on the left ", | ||
"must match either (1) the number of columns of the (LQPackedQ) matrix on the right ", | ||
"or (2) the number of rows of that (LQPackedQ) matrix's internal representation ", | ||
"(the factorization's originating matrix's number of rows)"))) | ||
|
||
|
||
function (\)(A::LQ{TA}, b::StridedVector{Tb}) where {TA,Tb} | ||
S = promote_type(TA,Tb) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method does not require
LQPackedQ
's context-dependent multiplication behavior, asLQPackedQ
's second dimension is not context-dependent.