Skip to content

Commit

Permalink
Merge pull request #105 from invenia/mz/convert
Browse files Browse the repository at this point in the history
Convert between `BlockDiagonals` of different block types
  • Loading branch information
mzgubic authored Jun 22, 2022
2 parents f920f23 + 3e3d38c commit 1ab40c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/blockdiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ end
@inbounds return p > 0 ? getblock(B, p)[i, end + j] : zero(T)
end

function Base.convert(::Type{BlockDiagonal{T, M}}, b::BlockDiagonal) where {T, M}
new_blocks = convert.(M, blocks(b))
return BlockDiagonal(new_blocks)::BlockDiagonal{T, M}
end

# Transform indices `i, j` (identifying entry `Matrix(B)[i, j]`) into indices `p, i, j` such
# that the same entry is available via `getblock(B, p)[i, end+j]`; `p = -1` if no such `p`.
function _block_indices(B::BlockDiagonal, i::Integer, j::Integer)
Expand Down
12 changes: 12 additions & 0 deletions test/blockdiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,16 @@ using Test
b = BlockDiagonal(AbstractMatrix{Float64}[ones(2, 2)])
@test b[1] == 1
end

@testset "convert(BlockDiagonal{F, T{F}}, block_diagonal)" for T in (
Symmetric, UpperTriangular, Transpose, Hermitian
)
special = T(rand(2, 2))
b = BlockDiagonal([special])

convert_first = BlockDiagonal([convert(Matrix, special)])
convert_last = convert(BlockDiagonal{Float64, Matrix{Float64}}, b)

@test convert_first == convert_last
end
end

2 comments on commit 1ab40c9

@mzgubic
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Error while trying to register: "Tag with name v0.1.31 already exists and points to a different commit"

Please sign in to comment.