Skip to content

IteratorSize for Tensorizer and BlockInterlacer #365

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 2 commits into from
Jan 23, 2023
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
5 changes: 5 additions & 0 deletions src/ApproxFunBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ function assert_integer(k::Number)
return nothing
end

function _IteratorSize(::Type{T}) where {T<:Tuple}
s = ntuple(i-> Base.IteratorSize(fieldtype(T, i)), fieldcount(T))
any(x -> x isa Base.IsInfinite, s) ? Base.IsInfinite() : Base.HasLength()
end

include("LinearAlgebra/LinearAlgebra.jl")
include("Fun.jl")
include("onehotvector.jl")
Expand Down
7 changes: 6 additions & 1 deletion src/LinearAlgebra/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ end

eltype(it::Type{<:CachedIterator{T}}) where {T} = T

Base.IteratorSize(::Type{<:CachedIterator{<:Any,IT}}) where {IT} = Base.IteratorSize(IT)

Base.keys(c::CachedIterator) = keys(c.iterator)

iterate(it::CachedIterator) = iterate(it,1)
function iterate(it::CachedIterator,st::Int)
if st == it.length + 1 && iterate(it.iterator,it.state...) === nothing
Expand Down Expand Up @@ -702,8 +706,9 @@ eltype(::Type{<:BlockInterlacer}) = Tuple{Int,Int}

dimensions(b::BlockInterlacer) = map(sum,b.blocks)
dimension(b::BlockInterlacer,k) = sum(b.blocks[k])
Base.length(b::BlockInterlacer) = mapreduce(sum,+,b.blocks)
length(b::BlockInterlacer) = mapreduce(sum,+,b.blocks)

Base.IteratorSize(::Type{BlockInterlacer{T}}) where {T} = _IteratorSize(T)

# the state is always (whichblock,curblock,cursubblock,curcoefficients)
# start(it::BlockInterlacer) = (1,1,map(start,it.blocks),ntuple(zero,length(it.blocks)))
Expand Down
17 changes: 11 additions & 6 deletions src/Multivariate/TensorSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ struct Tensorizer{DMS<:Tuple}
blocks::DMS
end

const InfOnes = Ones{Int,1,Tuple{OneToInf{Int}}}
const Tensorizer2D{AA, BB} = Tensorizer{Tuple{AA, BB}}
const TrivialTensorizer{d} = Tensorizer{NTuple{d,Ones{Int,1,Tuple{OneToInf{Int}}}}}
const TrivialTensorizer{d} = Tensorizer{NTuple{d,InfOnes}}

eltype(::Type{<:Tensorizer{<:Tuple{Vararg{Any,N}}}}) where {N} = NTuple{N,Int}
dimensions(a::Tensorizer) = map(sum,a.blocks)
Base.length(a::Tensorizer) = reduce(*, dimensions(a)) # easier type-inference than mapreduce
length(a::Tensorizer) = reduce(*, dimensions(a)) # easier type-inference than mapreduce

Base.IteratorSize(::Type{Tensorizer{T}}) where {T<:Tuple} = _IteratorSize(T)

Base.keys(a::Tensorizer) = oneto(length(a))

Expand Down Expand Up @@ -129,7 +132,9 @@ end

cache(a::Tensorizer) = CachedIterator(a)

function Base.findfirst(::TrivialTensorizer{2},kj::Tuple{Int,Int})
@deprecate findfirst(t::Tensorizer, kj::NTuple{2,Int}) findfirst(kj, t)

function Base.findfirst(kj::NTuple{2,Int}, ::TrivialTensorizer{2})
k,j=kj
if k > 0 && j > 0
n=k+j-2
Expand All @@ -138,7 +143,7 @@ function Base.findfirst(::TrivialTensorizer{2},kj::Tuple{Int,Int})
nothing
end
end
function Base.findfirst(sp::Tensorizer{<:NTuple{2,Ones{Int}}}, kj::NTuple{2,Int})
function Base.findfirst(kj::NTuple{2,Int}, sp::Tensorizer{<:NTuple{2,Ones{Int}}})
k,j=kj

len1, len2 = length(sp.blocks[1]), length(sp.blocks[2])
Expand Down Expand Up @@ -212,9 +217,9 @@ blocklengths(::TrivialTensorizer{2}) = 1:∞
blocklengths(it::Tensorizer) = tensorblocklengths(it.blocks...)
blocklengths(it::CachedIterator) = blocklengths(it.iterator)

function getindex(it::TrivialTensorizer{2},n::Integer)
function getindex(it::TrivialTensorizer{2}, n::Integer)
m=Int(block(it,n))
p=findfirst(it,(1,m))
p=findfirst((1,m), it)
j=1+n-p
j,m-j+1
end
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,15 @@ end
@test findfirst(t, vi) == i
end
end
@testset "cache" begin
ax = Ones{Int}(4);
t = ApproxFunBase.Tensorizer((ax,ax))
c = ApproxFunBase.cache(t)
v = collect(c)
@testset for i in eachindex(c)
@test c[i] == v[i]
end
end
end

@time include("ETDRK4Test.jl")
Expand Down