Skip to content

Commit

Permalink
allow kronecker product between recursive triangular matrices (#55527)
Browse files Browse the repository at this point in the history
Using the recently introduced recursive `zero` I can remove the
specialization to `<:Number` as @dkarrasch wanted to do in #54413.

---------

Co-authored-by: Jishnu Bhattacharya <[email protected]>
  • Loading branch information
araujoms and jishnub authored Sep 22, 2024
1 parent 911e025 commit d9555c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 8 additions & 8 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -919,21 +919,21 @@ function -(A::UnitLowerTriangular, B::UnitLowerTriangular)
end
-(A::AbstractTriangular, B::AbstractTriangular) = copyto!(similar(parent(A)), A) - copyto!(similar(parent(B)), B)

function kron(A::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
C = UpperTriangular(Matrix{promote_op(*, eltype(A), eltype(B))}(undef, _kronsize(A, B)))
function kron(A::UpperTriangular{T,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{S,<:StridedMaybeAdjOrTransMat}) where {T,S}
C = UpperTriangular(Matrix{promote_op(*, T, S)}(undef, _kronsize(A, B)))
return kron!(C, A, B)
end
function kron(A::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
C = LowerTriangular(Matrix{promote_op(*, eltype(A), eltype(B))}(undef, _kronsize(A, B)))
function kron(A::LowerTriangular{T,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{S,<:StridedMaybeAdjOrTransMat}) where {T,S}
C = LowerTriangular(Matrix{promote_op(*, T, S)}(undef, _kronsize(A, B)))
return kron!(C, A, B)
end

function kron!(C::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, A::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
function kron!(C::UpperTriangular{<:Any,<:StridedMaybeAdjOrTransMat}, A::UpperTriangular{<:Any,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{<:Any,<:StridedMaybeAdjOrTransMat})
size(C) == _kronsize(A, B) || throw(DimensionMismatch("kron!"))
_triukron!(C.data, A.data, B.data)
return C
end
function kron!(C::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, A::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
function kron!(C::LowerTriangular{<:Any,<:StridedMaybeAdjOrTransMat}, A::LowerTriangular{<:Any,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{<:Any,<:StridedMaybeAdjOrTransMat})
size(C) == _kronsize(A, B) || throw(DimensionMismatch("kron!"))
_trilkron!(C.data, A.data, B.data)
return C
Expand All @@ -952,7 +952,7 @@ function _triukron!(C, A, B)
C[inB+k, jnB+l] = Aij * B[k, l]
end
for k = 1:(l-1)
C[inB+l, jnB+k] = zero(eltype(C))
C[inB+l, jnB+k] = zero(C[inB+k, jnB+l])
end
end
end
Expand Down Expand Up @@ -984,7 +984,7 @@ function _trilkron!(C, A, B)
C[inB+k, jnB+l] = Aij * B[k, l]
end
for k = (l+1):n_B
C[inB+l, jnB+k] = zero(eltype(C))
C[inB+l, jnB+k] = zero(C[inB+k, jnB+l])
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,9 @@ end
@test 2\L == 2\B
@test real(L) == real(B)
@test imag(L) == imag(B)
if MT == LowerTriangular
@test isa(kron(L,L), MT)
end
@test kron(L,L) == kron(B,B)
@test transpose!(MT(copy(A))) == transpose(L) broken=!(A isa Matrix)
@test adjoint!(MT(copy(A))) == adjoint(L) broken=!(A isa Matrix)
Expand All @@ -1070,6 +1073,9 @@ end
@test 2\U == 2\B
@test real(U) == real(B)
@test imag(U) == imag(B)
if MT == UpperTriangular
@test isa(kron(U,U), MT)
end
@test kron(U,U) == kron(B,B)
@test transpose!(MT(copy(A))) == transpose(U) broken=!(A isa Matrix)
@test adjoint!(MT(copy(A))) == adjoint(U) broken=!(A isa Matrix)
Expand All @@ -1081,10 +1087,20 @@ end
for T in (UpperTriangular, LowerTriangular)
t = T(fill(ones(2,2), 2, 2))
m = Matrix(t)
@test isa(kron(t,t), T)
@test kron(t, t) kron(m, m)
end
end

@testset "kron with triangular matrices of mixed eltypes" begin
for T in (UpperTriangular, LowerTriangular)
U = T(Matrix{Union{Missing,Int}}(fill(2, 2, 2)))
U[1, 1] = missing
@test kron(U, U)[2, 3] == 0
@test kron(U, U)[3, 2] == 0
end
end

@testset "copyto! tests" begin
@testset "copyto! with aliasing (#39460)" begin
M = Matrix(reshape(1:36, 6, 6))
Expand Down

2 comments on commit d9555c6

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.