-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Infinite length ranges inconsistently implemented #15562
Comments
Looks like I think the error when trying to print the range is caused by Also, the |
Yeah, the root trouble here is In this case, you can use |
Maybe |
I think allowing length to return a float is indeed troublesome. Reading #6062 convinced me that I should create a A[2:∞,2:∞] to take subarrays. I can then overload |
You could also overload Another option (that I took in https://github.com/eschnett/FlexibleArrays.jl) is to use a different syntax for this case, i.e. to not use the For example, you could define a type |
overriding |
For my own usage, I've done the following to make infinite-length ranges work as expected: ## New Inf
const ∞ = Irrational{:∞}()
Base.show(io::IO, x::Irrational{:∞}) = print(io, "∞")
Base.convert{F<:AbstractFloat}(::Type{F},::Irrational{:∞}) = convert(F,Inf)
## My Count
# Need to reimplement, to avoid changing behaviour in Base
immutable Count{S<:Number}
start::S
step::S
end
countfrom(start::Number, step::Number) = Count(promote(start, step)...)
countfrom(start::Number) = Count(start, one(start))
countfrom() = Count(1, 1)
Base.eltype{S}(::Type{Count{S}}) = S
Base.start(it::Count) = it.start
Base.next(it::Count, state) = (state, state + it.step)
Base.done(it::Count, state) = false
Base.length(it::Count) = ∞
getindex(it::Count,k) = it.start + it.step*(k-1)
## Colon
Base.colon(a::Real,b::Irrational{:∞}) = countfrom(a)
Base.colon(::Irrational{:∞},::AbstractFloat,::Irrational{:∞}) = [∞]
Base.colon(a::Real,st::Real,b::Irrational{:∞}) = countfrom(a,st)
Base.isinf(::Irrational{:∞}) = true |
|
So far it works with very few modifications. The only modifications that might be worth changing Base I can think of are:
Surprisingly, that's about it. And the package is really fun: julia> using InfiniteArrays
julia> Vcat(Ones(1,∞), Hcat(Zeros(∞), Diagonal(1:∞)))
Vcat{Float64,2,Tuple{Ones{Float64,2,Tuple{Int64,InfiniteArrays.Infinity}},Hcat{Float64,Tuple{Zeros{Float64,1,Tuple{InfiniteArrays.Infinity}},Diagonal{Int64,InfiniteArrays.InfUnitRange{Int64}}}}}} with indices OneToInf()×OneToInf():
1.0 1.0 1.0 1.0 1.0 1.0 1.0 …
0.0 1.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 2.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 3.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 4.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 5.0 0.0 …
0.0 0.0 0.0 0.0 0.0 0.0 6.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 …
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 …
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 …
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 …
0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0
⋮ ⋮ ⋱
julia> exp.((-).((1:∞).^2))
BroadcastArray{Float64,1,getfield(Main, Symbol("##7#8")),Tuple{InfiniteArrays.InfUnitRange{Int64}},Tuple{InfiniteArrays.OneToInf{Int64}}} with indices OneToInf():
0.36787944117144233
0.01831563888873418
0.00012340980408667956
1.1253517471925912e-7
1.3887943864964021e-11
2.3195228302435696e-16
5.242885663363464e-22
1.603810890548638e-28
6.639677199580735e-36
3.720075976020836e-44
2.8207700884601357e-53
2.8946403116483003e-63
4.020060215743355e-74
7.555819019711961e-86
1.921947727823849e-98
6.616261056709485e-112
3.082440696949098e-126
1.9435148500492928e-141
1.6584104776811452e-157
1.9151695967140057e-174
2.993184452260193e-192
6.3309773362105915e-211
1.8122540257939923e-230
7.020667798504735e-251
3.6808558548018004e-272
2.6117417612840555e-294
2.507972e-317
0.0
⋮ |
Also, I had to make |
I don't recall in particular, but there are a number of calls to Line 91 in 6fb4569
|
An alternative (but probably overkill) solution is change struct OneTo{T<:Integer,V<:Integer} <: AbstractUnitRange{T}
stop::V
OneTo{T,V}(stop) where {T<:Integer,V<:Integer} = new(max(zero(T), stop))
end
OneTo{T}(stop::V) where V = OneTo{T,V}(stop)
OneTo(stop) = OneTo{typeof(one(T))}(stop) |
The following is inconsistent:
In 0.5, show is also broken.
I would argue that infinite length ranges are worth implementing: it is more convenient to do
than
The text was updated successfully, but these errors were encountered: