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

Resolve stack-overflow in Diagonal*Bidiagonal and (Sym)Tridiagonal #242

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
36 changes: 30 additions & 6 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,36 @@


## bi/tridiagonal copy
copy(M::Rmul{<:BidiagonalLayout,<:DiagonalLayout}) = convert(Bidiagonal, M.A) * M.B
copy(M::Lmul{<:DiagonalLayout,<:BidiagonalLayout}) = M.A * convert(Bidiagonal, M.B)
copy(M::Rmul{<:TridiagonalLayout,<:DiagonalLayout}) = convert(Tridiagonal, M.A) * M.B
copy(M::Lmul{<:DiagonalLayout,<:TridiagonalLayout}) = M.A * convert(Tridiagonal, M.B)
copy(M::Rmul{<:SymTridiagonalLayout,<:DiagonalLayout}) = convert(SymTridiagonal, M.A) * M.B
copy(M::Lmul{<:DiagonalLayout,<:SymTridiagonalLayout}) = M.A * convert(SymTridiagonal, M.B)
# hack around the fact that a SymTridiagonal isn't fully mutable
_similar(A) = similar(A)
_similar(A::SymTridiagonal) = similar(Tridiagonal(A.ev, A.dv, A.ev))
_copy_diag(M::T, ::T) where {T<:Rmul} = copyto!(_similar(M.A), M)
_copy_diag(M::T, ::T) where {T<:Lmul} = copyto!(_similar(M.B), M)
_copy_diag(M, _) = copy(M)
function copy(M::Rmul{<:BidiagonalLayout,<:DiagonalLayout})
A = convert(Bidiagonal, M.A)
_copy_diag(Rmul(A, M.B), M)

Check warning on line 68 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L61-L68

Added lines #L61 - L68 were not covered by tests
end
function copy(M::Lmul{<:DiagonalLayout,<:BidiagonalLayout})
B = convert(Bidiagonal, M.B)
_copy_diag(Lmul(M.A, B), M)

Check warning on line 72 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L70-L72

Added lines #L70 - L72 were not covered by tests
end
function copy(M::Rmul{<:TridiagonalLayout,<:DiagonalLayout})
A = convert(Tridiagonal, M.A)
_copy_diag(Rmul(A, M.B), M)

Check warning on line 76 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L74-L76

Added lines #L74 - L76 were not covered by tests
end
function copy(M::Lmul{<:DiagonalLayout,<:TridiagonalLayout})
B = convert(Tridiagonal, M.B)
_copy_diag(Lmul(M.A, B), M)

Check warning on line 80 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L78-L80

Added lines #L78 - L80 were not covered by tests
end
function copy(M::Rmul{<:SymTridiagonalLayout,<:DiagonalLayout})
A = convert(SymTridiagonal, M.A)
_copy_diag(Rmul(A, M.B), M)

Check warning on line 84 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L82-L84

Added lines #L82 - L84 were not covered by tests
end
function copy(M::Lmul{<:DiagonalLayout,<:SymTridiagonalLayout})
B = convert(SymTridiagonal, M.B)
_copy_diag(Lmul(M.A, B), M)

Check warning on line 88 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L86-L88

Added lines #L86 - L88 were not covered by tests
end

copy(M::Lmul{DiagonalLayout{OnesLayout}}) = _copy_oftype(M.B, eltype(M))
copy(M::Lmul{DiagonalLayout{OnesLayout},<:DiagonalLayout}) = Diagonal(_copy_oftype(diagonaldata(M.B), eltype(M)))
Expand Down
16 changes: 16 additions & 0 deletions test/test_layoutarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@ MemoryLayout(::Type{MyVector}) = DenseColumnMajor()
@test B̃\D ≈ B̃\Matrix(D)
@test D\D̃ ≈ D̃\D
@test B̃/D ≈ B̃/Matrix(D)

@testset "Diagonal * Bidiagonal/Tridiagonal with structured diags" begin
n = size(D,1)
B = Bidiagonal(map(MyVector, (rand(n), rand(n-1)))..., :U)
S = SymTridiagonal(map(MyVector, (rand(n), rand(n-1)))...)
T = Tridiagonal(map(MyVector, (rand(n-1), rand(n), rand(n-1)))...)
DA, BA, SA, TA = map(Array, (D, B, S, T))
@test D * B ≈ DA * BA
@test B * D ≈ BA * DA
if VERSION >= v"1.12.0-DEV.824"
@test D * S ≈ DA * SA
@test D * T ≈ DA * TA
@test S * D ≈ SA * DA
@test T * D ≈ TA * DA
end
end
end

@testset "Adj/Trans" begin
Expand Down
Loading