Skip to content

Fix banded-diagonal Lmul/Rmul copy #255

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

Merged
merged 3 commits into from
Feb 19, 2025
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArrayLayouts"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
authors = ["Sheehan Olver <[email protected]>"]
version = "1.11.0"
version = "1.11.1"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
31 changes: 25 additions & 6 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,47 @@
_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)
_bidiagonal(A::Bidiagonal) = A
function _bidiagonal(A)
# we assume that the matrix is indeed bidiagonal,
# so that the conversion is lossless
if iszero(view(A, diagind(A, -1)))
uplo = :U
else
uplo = :L
end
Bidiagonal(A, uplo)
end
function copy(M::Rmul{<:BidiagonalLayout,<:DiagonalLayout})
A = convert(Bidiagonal, M.A)
A = _bidiagonal(M.A)
_copy_diag(Rmul(A, M.B), M)
end
function copy(M::Lmul{<:DiagonalLayout,<:BidiagonalLayout})
B = convert(Bidiagonal, M.B)
B = _bidiagonal(M.B)
_copy_diag(Lmul(M.A, B), M)
end
# we assume that the matrix is indeed tridiagonal,
# so that the conversion is lossless
_tridiagonal(A::Tridiagonal) = A
_tridiagonal(A) = Tridiagonal(A)

Check warning on line 88 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L88

Added line #L88 was not covered by tests
function copy(M::Rmul{<:TridiagonalLayout,<:DiagonalLayout})
A = convert(Tridiagonal, M.A)
A = _tridiagonal(M.A)
_copy_diag(Rmul(A, M.B), M)
end
function copy(M::Lmul{<:DiagonalLayout,<:TridiagonalLayout})
B = convert(Tridiagonal, M.B)
B = _tridiagonal(M.B)
_copy_diag(Lmul(M.A, B), M)
end
# we assume that the matrix is indeed symmetric tridiagonal,
# so that the conversion is lossless
_symtridiagonal(A::SymTridiagonal) = A
_symtridiagonal(A) = SymTridiagonal(A)

Check warning on line 100 in src/diagonal.jl

View check run for this annotation

Codecov / codecov/patch

src/diagonal.jl#L100

Added line #L100 was not covered by tests
function copy(M::Rmul{<:SymTridiagonalLayout,<:DiagonalLayout})
A = convert(SymTridiagonal, M.A)
A = _symtridiagonal(M.A)
_copy_diag(Rmul(A, M.B), M)
end
function copy(M::Lmul{<:DiagonalLayout,<:SymTridiagonalLayout})
B = convert(SymTridiagonal, M.B)
B = _symtridiagonal(M.B)
_copy_diag(Lmul(M.A, B), M)
end

Expand Down
20 changes: 13 additions & 7 deletions test/test_layoutarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,24 @@ Base.copy(A::MyVector) = MyVector(copy(A.A))

@testset "Diagonal * Bidiagonal/Tridiagonal with structured diags" begin
n = size(D,1)
B = Bidiagonal(map(MyVector, (rand(n), rand(n-1)))..., :U)
MB = MyMatrix(B)
BU = Bidiagonal(map(MyVector, (rand(n), rand(n-1)))..., :U)
MBU = MyMatrix(BU)
BL = Bidiagonal(map(MyVector, (rand(n), rand(n-1)))..., :L)
MBL = MyMatrix(BL)
S = SymTridiagonal(map(MyVector, (rand(n), rand(n-1)))...)
MS = MyMatrix(S)
T = Tridiagonal(map(MyVector, (rand(n-1), rand(n), rand(n-1)))...)
MT = MyMatrix(T)
DA, BA, SA, TA = map(Array, (D, B, S, T))
DA, BUA, BLA, SA, TA = map(Array, (D, BU, BL, S, T))
if VERSION >= v"1.11"
@test D * B ≈ DA * BA
@test B * D ≈ BA * DA
@test D * MB ≈ DA * BA
@test MB * D ≈ BA * DA
@test D * BU ≈ DA * BUA
@test BU * D ≈ BUA * DA
@test D * MBU ≈ DA * BUA
@test MBU * D ≈ BUA * DA
@test D * BL ≈ DA * BLA
@test BL * D ≈ BLA * DA
@test D * MBL ≈ DA * BLA
@test MBL * D ≈ BLA * DA
end
if VERSION >= v"1.12.0-DEV.824"
@test D * S ≈ DA * SA
Expand Down
Loading