Skip to content

Commit

Permalink
BlockArrray extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty committed Nov 29, 2024
1 parent 210d087 commit fb98c9d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[weakdeps]
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[extensions]
InfiniteArraysBandedMatricesExt = "BandedMatrices"
InfiniteArraysBlockArraysExt = "BlockArrays"
InfiniteArraysDSPExt = "DSP"
InfiniteArraysStatisticsExt = "Statistics"

Expand Down
113 changes: 113 additions & 0 deletions ext/InfiniteArraysBlockArraysExt.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
module InfiniteArraysBlockArraysExt
using InfiniteArrays, BlockArrays
using InfiniteArrays.ArrayLayouts, InfiniteArrays.LazyArrays
import ArrayLayouts: sub_materialize

const OneToInfCumsum = RangeCumsum{Int,OneToInf{Int}}

BlockArrays.sortedunion(::AbstractVector{<:PosInfinity}, ::AbstractVector{<:PosInfinity}) = [∞]
function BlockArrays.sortedunion(::AbstractVector{<:PosInfinity}, b)
@assert isinf(length(b))
b

Check warning on line 11 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L8-L11

Added lines #L8 - L11 were not covered by tests
end

function BlockArrays.sortedunion(b, ::AbstractVector{<:PosInfinity})
@assert isinf(length(b))
b

Check warning on line 16 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
end
BlockArrays.sortedunion(a::OneToInfCumsum, ::OneToInfCumsum) = a

Check warning on line 18 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L18

Added line #L18 was not covered by tests

BlockArrays.blocklasts(a::InfRanges) = Fill(length(a),1)

Check warning on line 20 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L20

Added line #L20 was not covered by tests

BlockArrays.findblock(::BlockedOneTo, ::RealInfinity) = Block(ℵ₀)

Check warning on line 22 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L22

Added line #L22 was not covered by tests

function BlockArrays.sortedunion(a::Vcat{Int,1,<:Tuple{Union{Int,AbstractVector{Int}},<:AbstractRange}},

Check warning on line 24 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L24

Added line #L24 was not covered by tests
b::Vcat{Int,1,<:Tuple{Union{Int,AbstractVector{Int}},<:AbstractRange}})
@assert a == b # TODO: generailse? Not sure how to do so in a type stable fashion
a

Check warning on line 27 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L26-L27

Added lines #L26 - L27 were not covered by tests
end

sizes_from_blocks(A::AbstractVector, ::Tuple{OneToInf{Int}}) = (map(length,A),)
length(::BlockedOneTo{Int,<:InfRanges}) = ℵ₀

Check warning on line 31 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L30-L31

Added lines #L30 - L31 were not covered by tests

const OneToInfBlocks = BlockedOneTo{Int,OneToInfCumsum}
const OneToBlocks = BlockedOneTo{Int,OneToCumsum}

axes(a::OneToInfBlocks) = (a,)
axes(a::OneToBlocks) = (a,)

Check warning on line 37 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L36-L37

Added lines #L36 - L37 were not covered by tests


sub_materialize(_, V, ::Tuple{BlockedOneTo{Int,<:InfRanges}}) = V
sub_materialize(::AbstractBlockLayout, V, ::Tuple{BlockedOneTo{Int,<:InfRanges}}) = V
Expand All @@ -7,4 +44,80 @@ function sub_materialize(::PaddedColumns, v::AbstractVector{T}, ax::Tuple{Blocke
BlockedVector(Vcat(sub_materialize(dat), Zeros{T}(∞)), ax)
end

BlockArrays.dimlength(start, ::Infinity) = ℵ₀

Check warning on line 47 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L47

Added line #L47 was not covered by tests

function copy(bc::Broadcasted{<:BroadcastStyle,<:Any,typeof(*),<:Tuple{Ones{T,1,Tuple{OneToInfBlocks}},AbstractArray{V,N}}}) where {N,T,V}
a,b = bc.args
@assert bc.axes == axes(b)
convert(AbstractArray{promote_type(T,V),N}, b)

Check warning on line 52 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L49-L52

Added lines #L49 - L52 were not covered by tests
end

function copy(bc::Broadcasted{<:BroadcastStyle,<:Any,typeof(*),<:Tuple{AbstractArray{T,N},Ones{V,1,Tuple{OneToInfBlocks}}}}) where {N,T,V}
a,b = bc.args
@assert bc.axes == axes(a)
convert(AbstractArray{promote_type(T,V),N}, a)

Check warning on line 58 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L55-L58

Added lines #L55 - L58 were not covered by tests
end

_block_interlace_axes(::Int, ax::Tuple{BlockedOneTo{Int,OneToInf{Int}}}...) = (blockedrange(Fill(length(ax), ∞)),)

Check warning on line 61 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L61

Added line #L61 was not covered by tests

_block_interlace_axes(nbc::Int, ax::NTuple{2,BlockedOneTo{Int,OneToInf{Int}}}...) =

Check warning on line 63 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L63

Added line #L63 was not covered by tests
(blockedrange(Fill(length(ax) ÷ nbc, ∞)),blockedrange(Fill(mod1(length(ax),nbc), ∞)))

#######
# block broadcasted
######


BroadcastStyle(::Type{<:SubArray{T,N,Arr,<:NTuple{N,BlockSlice{BlockRange{1,Tuple{II}}}},false}}) where {T,N,Arr<:BlockArray,II<:InfRanges} =

Check warning on line 71 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L71

Added line #L71 was not covered by tests
LazyArrayStyle{N}()

# TODO: generalise following
BroadcastStyle(::Type{<:BlockArray{T,N,<:AbstractArray{<:AbstractArray{T,N},N},<:NTuple{N,BlockedOneTo{Int,<:InfRanges}}}}) where {T,N} = LazyArrayStyle{N}()

Check warning on line 75 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L75

Added line #L75 was not covered by tests
# BroadcastStyle(::Type{<:BlockedArray{T,N,<:AbstractArray{T,N},<:NTuple{N,BlockedOneTo{Int,<:InfRanges}}}}) where {T,N} = LazyArrayStyle{N}()
BroadcastStyle(::Type{<:BlockArray{T,N,<:AbstractArray{<:AbstractArray{T,N},N},<:NTuple{N,BlockedOneTo{Int,<:RangeCumsum{Int,<:InfRanges}}}}}) where {T,N} = LazyArrayStyle{N}()

Check warning on line 77 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L77

Added line #L77 was not covered by tests
# BroadcastStyle(::Type{<:BlockedArray{T,N,<:AbstractArray{T,N},<:NTuple{N,BlockedOneTo{Int,<:RangeCumsum{Int,<:InfRanges}}}}}) where {T,N} = LazyArrayStyle{N}()


# Block banded support

sizes_from_blocks(A::Diagonal, ::NTuple{2,OneToInf{Int}}) = size.(A.diag, 1), size.(A.diag,2)
sizes_from_blocks(A::Tridiagonal, ::NTuple{2,OneToInf{Int}}) = size.(A.d, 1), size.(A.d,2)
sizes_from_blocks(A::Bidiagonal, ::NTuple{2,OneToInf{Int}}) = size.(A.dv, 1), size.(A.dv,2)

Check warning on line 85 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L83-L85

Added lines #L83 - L85 were not covered by tests


axes_print_matrix_row(::NTuple{2,AbstractBlockedUnitRange}, io, X, A, i, ::AbstractVector{<:PosInfinity}, sep) = nothing

Check warning on line 88 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L88

Added line #L88 was not covered by tests


const BlockTriPertToeplitz{T} = BlockMatrix{T,Tridiagonal{Matrix{T},Vcat{Matrix{T},1,Tuple{Vector{Matrix{T}},Fill{Matrix{T},1,Tuple{OneToInf{Int}}}}}},
NTuple{2,BlockedOneTo{Int,Vcat{Int,1,Tuple{Vector{Int},InfStepRange{Int,Int}}}}}}

const BlockTridiagonalToeplitzLayout = BlockLayout{TridiagonalToeplitzLayout,DenseColumnMajor}

function BlockTridiagonal(adjA::Adjoint{T,BlockTriPertToeplitz{T}}) where T
A = parent(adjA)
BlockTridiagonal(Matrix.(adjoint.(A.blocks.du)),

Check warning on line 98 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L96-L98

Added lines #L96 - L98 were not covered by tests
Matrix.(adjoint.(A.blocks.d)),
Matrix.(adjoint.(A.blocks.dl)))
end

for op in (:-, :+)
@eval begin
function $op(A::BlockTriPertToeplitz{T}, λ::UniformScaling) where T
TV = promote_type(T,eltype(λ))
BlockTridiagonal(Vcat(convert.(AbstractVector{Matrix{TV}}, A.blocks.dl.args)...),

Check warning on line 107 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L105-L107

Added lines #L105 - L107 were not covered by tests
Vcat(convert.(AbstractVector{Matrix{TV}}, broadcast($op, A.blocks.d, Ref(λ)).args)...),
Vcat(convert.(AbstractVector{Matrix{TV}}, A.blocks.du.args)...))
end
function $op::UniformScaling, A::BlockTriPertToeplitz{V}) where V
TV = promote_type(eltype(λ),V)
BlockTridiagonal(Vcat(convert.(AbstractVector{Matrix{TV}}, broadcast($op, A.blocks.dl.args))...),

Check warning on line 113 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L111-L113

Added lines #L111 - L113 were not covered by tests
Vcat(convert.(AbstractVector{Matrix{TV}}, broadcast($op, Ref(λ), A.blocks.d).args)...),
Vcat(convert.(AbstractVector{Matrix{TV}}, broadcast($op, A.blocks.du.args))...))
end
$op(adjA::Adjoint{T,BlockTriPertToeplitz{T}}, λ::UniformScaling) where T = $op(BlockTridiagonal(adjA), λ)
$op::UniformScaling, adjA::Adjoint{T,BlockTriPertToeplitz{T}}) where T = $op(λ, BlockTridiagonal(adjA))

Check warning on line 118 in ext/InfiniteArraysBlockArraysExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/InfiniteArraysBlockArraysExt.jl#L117-L118

Added lines #L117 - L118 were not covered by tests
end
end


end # module

0 comments on commit fb98c9d

Please sign in to comment.