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

Optimizing some structured matrix multiply methods #32521

Closed
wants to merge 7 commits into from
Closed
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
14 changes: 14 additions & 0 deletions stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,20 @@ 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::Bidiagonal, B::Bidiagonal)
TS = promote_op(matprod, eltype(A), eltype(B))
if A.uplo == B.uplo
A_mul_B_td!(similar(B, TS, size(B, 1), size(B, 2)), A, B)
else
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
end

function *(A::StridedMatrix, B::BiTriSym)
TS = promote_op(matprod, eltype(A), eltype(B))
A_mul_B_td!(similar(A, TS), A, B)
end

#Linear solvers
ldiv!(A::Union{Bidiagonal, AbstractTriangular}, b::AbstractVector) = naivesub!(A, b)
ldiv!(A::Transpose{<:Any,<:Bidiagonal}, b::AbstractVector) = ldiv!(copy(A), b)
Expand Down
35 changes: 29 additions & 6 deletions stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Random.seed!(1)

# Issue #31870
# Bi/Tri/Sym times Diagonal
Dense = rand(elty, 10, 10)
Diag = Diagonal(rand(elty, 10))
BidiagU = Bidiagonal(rand(elty, 10), rand(elty, 9), 'U')
BidiagL = Bidiagonal(rand(elty, 10), rand(elty, 9), 'L')
Expand All @@ -325,20 +326,42 @@ Random.seed!(1)

mats = [Diag, BidiagU, BidiagL, Tridiag, SymTri]
for a in mats
@test typeof(Dense*a) <: Array
@test typeof(a*Dense) <: Array
for b in mats
@test a*b ≈ Matrix(a)*Matrix(b)
end
end

@test typeof(BidiagU*Diag) <: Bidiagonal
@test typeof(Diag*Diag) <: Diagonal
@test typeof(Diag*BidiagL) <: Bidiagonal
@test typeof(Diag*BidiagU) <: Bidiagonal
@test typeof(Diag*Tridiag) <: Tridiagonal
@test typeof(Diag*SymTri) <: Tridiagonal

@test typeof(BidiagL*Diag) <: Bidiagonal
@test typeof(Tridiag*Diag) <: Tridiagonal
@test typeof(SymTri*Diag) <: Tridiagonal
@test typeof(BidiagL*BidiagL) <: SparseMatrixCSC
@test typeof(BidiagL*BidiagU) <: Tridiagonal
@test typeof(BidiagL*Tridiag) <: SparseMatrixCSC
@test typeof(BidiagL*SymTri) <: SparseMatrixCSC

@test typeof(BidiagU*Diag) <: Bidiagonal
@test typeof(Diag*BidiagL) <: Bidiagonal
@test typeof(Diag*Tridiag) <: Tridiagonal
@test typeof(Diag*SymTri) <: Tridiagonal
@test typeof(BidiagU*BidiagL) <: Tridiagonal
@test typeof(BidiagU*BidiagU) <: SparseMatrixCSC
@test typeof(BidiagU*Tridiag) <: SparseMatrixCSC
@test typeof(BidiagU*SymTri) <: SparseMatrixCSC

@test typeof(Tridiag*Diag) <: Tridiagonal
@test typeof(Tridiag*BidiagL) <: SparseMatrixCSC
@test typeof(Tridiag*BidiagU) <: SparseMatrixCSC
@test typeof(Tridiag*Tridiag) <: SparseMatrixCSC
@test typeof(Tridiag*SymTri) <: SparseMatrixCSC

@test typeof(SymTri*Diag) <: Tridiagonal
@test typeof(SymTri*BidiagL) <: SparseMatrixCSC
@test typeof(SymTri*BidiagU) <: SparseMatrixCSC
@test typeof(SymTri*Tridiag) <: SparseMatrixCSC
@test typeof(SymTri*SymTri) <: SparseMatrixCSC
end

@test inv(T)*Tfull ≈ Matrix(I, n, n)
Expand Down