-
-
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
making Bi/Tri/Sym times Diag output a structured matrix (not sparse) #31889
Changes from 11 commits
15e56c9
aa30721
440ad8a
9250a0d
f4896d8
b59d135
801c09d
ef8019e
cab2dec
91a21bc
9c917c6
f4b7f9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,7 +336,6 @@ end | |
const BiTriSym = Union{Bidiagonal,Tridiagonal,SymTridiagonal} | ||
const BiTri = Union{Bidiagonal,Tridiagonal} | ||
mul!(C::AbstractMatrix, A::SymTridiagonal, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTri, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTriSym, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::AbstractTriangular, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::AbstractMatrix, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
|
@@ -347,12 +346,12 @@ mul!(C::AbstractMatrix, A::Adjoint{<:Any,<:AbstractTriangular}, B::BiTriSym) = A | |
mul!(C::AbstractMatrix, A::Transpose{<:Any,<:AbstractTriangular}, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::Adjoint{<:Any,<:AbstractVecOrMat}, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::Transpose{<:Any,<:AbstractVecOrMat}, B::BiTriSym) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractVector, A::BiTri, B::AbstractVector) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTri, B::AbstractVecOrMat) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractVecOrMat, A::BiTri, B::AbstractVecOrMat) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTri, B::Transpose{<:Any,<:AbstractVecOrMat}) = A_mul_B_td!(C, A, B) # around bidiag line 330 | ||
mul!(C::AbstractMatrix, A::BiTri, B::Adjoint{<:Any,<:AbstractVecOrMat}) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractVector, A::BiTri, B::Transpose{<:Any,<:AbstractVecOrMat}) = throw(MethodError(mul!, (C, A, B))) | ||
mul!(C::AbstractVector, A::BiTriSym, B::AbstractVector) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTriSym, B::AbstractVecOrMat) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractVecOrMat, A::BiTriSym, B::AbstractVecOrMat) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractMatrix, A::BiTriSym, B::Transpose{<:Any,<:AbstractVecOrMat}) = A_mul_B_td!(C, A, B) # around bidiag line 330 | ||
mul!(C::AbstractMatrix, A::BiTriSym, B::Adjoint{<:Any,<:AbstractVecOrMat}) = A_mul_B_td!(C, A, B) | ||
mul!(C::AbstractVector, A::BiTriSym, B::Transpose{<:Any,<:AbstractVecOrMat}) = throw(MethodError(mul!, (C, A, B))) | ||
|
||
function check_A_mul_B!_sizes(C, A, B) | ||
require_one_based_indexing(C) | ||
|
@@ -437,6 +436,39 @@ function A_mul_B_td!(C::AbstractMatrix, A::BiTriSym, B::BiTriSym) | |
C | ||
end | ||
|
||
function A_mul_B_td!(C::AbstractMatrix, A::BiTriSym, B::Diagonal) | ||
check_A_mul_B!_sizes(C, A, B) | ||
n = size(A,1) | ||
n <= 3 && return mul!(C, Array(A), Array(B)) | ||
fill!(C, zero(eltype(C))) | ||
Al = _diag(A, -1) | ||
Ad = _diag(A, 0) | ||
Au = _diag(A, 1) | ||
Bd = B.diag | ||
@inbounds begin | ||
# first row of C | ||
C[1,1] = A[1,1]*B[1,1] | ||
C[1,2] = A[1,2]*B[2,2] | ||
# second row of C | ||
C[2,1] = A[2,1]*B[1,1] | ||
C[2,2] = A[2,2]*B[2,2] | ||
C[2,3] = A[2,3]*B[3,3] | ||
for j in 3:n-2 | ||
C[j, j-1] = Al[j-1]*Bd[j-1] | ||
C[j, j ] = Ad[j ]*Bd[j ] | ||
C[j, j+1] = Au[j ]*Bd[j+1] | ||
end | ||
# row before last of C | ||
C[n-1,n-2] = A[n-1,n-2]*B[n-2,n-2] | ||
C[n-1,n-1] = A[n-1,n-1]*B[n-1,n-1] | ||
C[n-1,n ] = A[n-1, n]*B[n ,n ] | ||
# last row of C | ||
C[n,n-1] = A[n,n-1]*B[n-1,n-1] | ||
C[n,n ] = A[n,n ]*B[n, n ] | ||
end # inbounds | ||
C | ||
end | ||
|
||
function A_mul_B_td!(C::AbstractVecOrMat, A::BiTriSym, B::AbstractVecOrMat) | ||
require_one_based_indexing(C) | ||
require_one_based_indexing(B) | ||
|
@@ -497,7 +529,39 @@ function A_mul_B_td!(C::AbstractMatrix, A::AbstractMatrix, B::BiTriSym) | |
C | ||
end | ||
|
||
const SpecialMatrix = Union{Bidiagonal,SymTridiagonal,Tridiagonal} | ||
function A_mul_B_td!(C::AbstractMatrix, A::Diagonal, B::BiTriSym) | ||
check_A_mul_B!_sizes(C, A, B) | ||
n = size(A,1) | ||
n <= 3 && return mul!(C, Array(A), Array(B)) | ||
fill!(C, zero(eltype(C))) | ||
Ad = A.diag | ||
Bl = _diag(B, -1) | ||
Bd = _diag(B, 0) | ||
Bu = _diag(B, 1) | ||
@inbounds begin | ||
# first row of C | ||
C[1,1] = A[1,1]*B[1,1] | ||
C[1,2] = A[1,1]*B[1,2] | ||
# second row of C | ||
C[2,1] = A[2,2]*B[2,1] | ||
C[2,2] = A[2,2]*B[2,2] | ||
C[2,3] = A[2,2]*B[2,3] | ||
for j in 3:n-2 | ||
Ajj = Ad[j] | ||
C[j, j-1] = Ajj*Bl[j-1] | ||
C[j, j ] = Ajj*Bd[j] | ||
C[j, j+1] = Ajj*Bu[j] | ||
end | ||
# row before last of C | ||
C[n-1,n-2] = A[n-1,n-1]*B[n-1,n-2] | ||
C[n-1,n-1] = A[n-1,n-1]*B[n-1,n-1] | ||
C[n-1,n ] = A[n-1,n-1]*B[n-1,n ] | ||
# last row of C | ||
C[n,n-1] = A[n,n]*B[n,n-1] | ||
C[n,n ] = A[n,n]*B[n,n ] | ||
end # inbounds | ||
C | ||
end | ||
|
||
function *(A::AbstractTriangular, B::Union{SymTridiagonal, Tridiagonal}) | ||
TS = promote_op(matprod, eltype(A), eltype(B)) | ||
|
@@ -545,7 +609,7 @@ function *(A::Bidiagonal, B::LowerTriangular) | |
end | ||
end | ||
|
||
function *(A::Bidiagonal, B::Diagonal) | ||
function *(A::BiTri, B::Diagonal) | ||
TS = promote_op(matprod, eltype(A), eltype(B)) | ||
A_mul_B_td!(similar(A, TS), A, B) | ||
end | ||
|
@@ -565,6 +629,11 @@ function *(A::SymTridiagonal, B::Diagonal) | |
A_mul_B_td!(Tridiagonal(zeros(TS, size(A, 1)-1), zeros(TS, size(A, 1)), zeros(TS, size(A, 1)-1)), A, B) | ||
end | ||
|
||
function *(A::BiTri, B::Transpose{<:Any, <:Diagonal}) | ||
TS = promote_op(matprod, eltype(A), eltype(B)) | ||
A_mul_B_td!(similar(A, TS), A, B) | ||
end | ||
|
||
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. What about 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. The thing is that even if we add all these Personally, I think that 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. Perhaps this should be removed and put into another PR where we handle the Adjoint/Transpose case? |
||
#Generic multiplication | ||
*(A::Bidiagonal{T}, B::AbstractVector{T}) where {T} = *(Array(A), B) | ||
*(adjA::Adjoint{<:Any,<:Bidiagonal{T}}, B::AbstractVector{T}) where {T} = *(adjoint(Array(adjA.parent)), B) | ||
|
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.
Similarly to the comment below: do we need methods for
Transpose
s andAdjoint
s ofB
? In line 634, you callA_mul_B_td!
with aB::Transpose{<:Any, <:Diagonal}
.Hm, actually, it seems that the exact same code works for
B::Union{Diagonal, Adjoint{<:Any, <:Diagonal}, Transpose{<:Any, <:Diagonal}
. The elementwise transposition/adjoint is done atgetindex
. You could define a constantAdjOrTransDiagonal
somewhere above?