Skip to content

Commit

Permalink
Support Julia v1.6 (#53)
Browse files Browse the repository at this point in the history
* Use oneto

* Update InfiniteArrays.jl

* Fixes for v1.5

* Update runtests.jl

* Update runtests.jl

* v0.9.4
  • Loading branch information
dlfivefifty authored Feb 4, 2021
1 parent cfbe560 commit a807898
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "InfiniteArrays"
uuid = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
version = "0.9.3"
version = "0.9.4"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
61 changes: 36 additions & 25 deletions src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,40 @@ import ArrayLayouts: RangeCumsum

export ∞, Hcat, Vcat, Zeros, Ones, Fill, Eye, BroadcastArray, cache


if VERSION v"1.6-"
import Base: unitrange, oneto
end

include("Infinity.jl")
include("infrange.jl")
include("infarrays.jl")
include("reshapedarray.jl")

if VERSION < v"1.6-"
##
# Temporary hacks for base support
##
Base.OneTo(::Infinity) = OneToInf()
function Base.OneTo(x::OrientedInfinity)
iszero(x.angle) && return oneto(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end
function Base.OneTo(x::SignedInfinity)
signbit(x) || return oneto(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end
Base.OneTo{T}(::Infinity) where T<:Integer = OneToInf{T}()
Base.UnitRange(start::Integer, ::Infinity) = InfUnitRange(start)
Base.UnitRange{T}(start::Integer, ::Infinity) where T<:Real = InfUnitRange{T}(start)
Base.OneTo(a::OneToInf) = a
Base.OneTo{T}(::OneToInf) where T<:Integer = OneToInf{T}()

Base.Int(::Infinity) =

unitrange(a, b) = UnitRange(a, b)
oneto(n) = Base.OneTo(n)
end

##
# Fill FillArrays
##
Expand Down Expand Up @@ -105,10 +132,14 @@ vcat(a::AbstractVector, b::AbstractVector, c::AbstractVector, d::AbstractFill{<:

vcat(a::AbstractMatrix, b::AbstractFill{<:Any,2,<:Tuple{OneToInf,OneTo}}) = Vcat(a, b)

cat_similar(A, T, shape::Tuple{Infinity}) = zeros(T,∞)
cat_similar(A::AbstractArray, T, shape::Tuple{Infinity}) =
Base.invoke(cat_similar, Tuple{AbstractArray, Any, Any}, A, T, shape)

cat_similar(A, ::Type{T}, shape::Tuple{Infinity}) where T = zeros(T,∞)
if VERSION < v"1.6-"
cat_similar(A::AbstractArray, ::Type{T}, shape::Tuple{Infinity}) where T =
Base.invoke(cat_similar, Tuple{AbstractArray, Any, Any}, A, T, shape)
else
cat_similar(A::AbstractArray, ::Type{T}, shape::Tuple{Infinity}) where T =
Base.invoke(cat_similar, Tuple{AbstractArray, Type{T}, Any}, A, T, shape)
end
function Base.__cat(A, shape::NTuple{N,Infinity}, catdims, X...) where N
offsets = zeros(Union{Int,Infinity}, N)
inds = Vector{Union{UnitRange{Int},InfUnitRange{Int}}}(undef, N)
Expand Down Expand Up @@ -140,26 +171,6 @@ reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, OneToInf, Vararg

# cat_similar(A, T, ::Tuple{Infinity}) = zeros(T, ∞)

##
# Temporary hacks for base support
##
OneTo(::Infinity) = OneToInf()
function OneTo(x::OrientedInfinity)
iszero(x.angle) && return OneTo(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end
function OneTo(x::SignedInfinity)
signbit(x) || return OneTo(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end
OneTo{T}(::Infinity) where T<:Integer = OneToInf{T}()
UnitRange(start::Integer, ::Infinity) = InfUnitRange(start)
UnitRange{T}(start::Integer, ::Infinity) where T<:Real = InfUnitRange{T}(start)
OneTo(a::OneToInf) = a
OneTo{T}(::OneToInf) where T<:Integer = OneToInf{T}()

Int(::Infinity) =

axistype(::OneTo{T}, ::OneToInf{V}) where {T,V} = OneToInf{promote_type(T,V)}()
axistype(::OneToInf{V}, ::OneTo{T}) where {T,V} = OneToInf{promote_type(T,V)}()

Expand Down
16 changes: 8 additions & 8 deletions src/Infinity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ cld(x::T, ::Infinity) where T<:Real = signbit(x) ? zero(T) : one(T)

mod(::Infinity, ::Infinity) = NotANumber()
mod(::Infinity, ::Real) = NotANumber()
function mod(x::Real, ::Infinity)
function mod(x::Real, ::Infinity)
x 0 || throw(ArgumentError("mod(x,∞) is unbounded for x < 0"))
x
end
Expand Down Expand Up @@ -139,7 +139,7 @@ sign(y::SignedInfinity) = 1-2signbit(y)
angle(x::SignedInfinity) = π*signbit(x)
mod(::SignedInfinity, ::SignedInfinity) = NotANumber()
mod(::SignedInfinity, ::Real) = NotANumber()
function mod(x::Real, y::SignedInfinity)
function mod(x::Real, y::SignedInfinity)
signbit(x) == signbit(y) || throw(ArgumentError("mod($x,$y) is unbounded"))
x
end
Expand All @@ -163,7 +163,7 @@ for Typ in (:Number, :Real, :Integer, :AbstractFloat)
+(y::SignedInfinity, ::$Typ) = y
-(y::SignedInfinity, ::$Typ) = y
-(::$Typ, y::SignedInfinity) = -y
function *(a::$Typ, y::SignedInfinity)
function *(a::$Typ, y::SignedInfinity)
iszero(a) && throw(ArgumentError("Cannot multiply $a * $y"))
a > 0 ? y : (-y)
end
Expand All @@ -182,17 +182,17 @@ end



function -(::Infinity, y::SignedInfinity)
function -(::Infinity, y::SignedInfinity)
signbit(y) || throw(ArgumentError("Cannot subtract ∞ from ∞"))
end

function -(x::SignedInfinity, ::Infinity)
function -(x::SignedInfinity, ::Infinity)
signbit(x) || throw(ArgumentError("Cannot subtract ∞ from ∞"))
x
end

function -(x::SignedInfinity, y::SignedInfinity)
function -(x::SignedInfinity, y::SignedInfinity)
signbit(x) == !signbit(y) || throw(ArgumentError("Cannot subtract ∞ from ∞"))
x
end
Expand Down Expand Up @@ -376,6 +376,6 @@ Base.Checked.checked_add(x::SignedInfinity, ::Integer) = x


Base.to_index(::Infinity) =
Base.to_shape(::Infinity) =


Base.hash(::Infinity) = 0x020113134b21797f # made up
Base.hash(::Infinity) = 0x020113134b21797f # made up
12 changes: 12 additions & 0 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ end

InfUnitRange(a::InfUnitRange) = a
InfUnitRange{T}(a::AbstractInfUnitRange) where T<:Real = InfUnitRange{T}(first(a))
InfUnitRange(a::AbstractInfUnitRange{T}) where T<:Real = InfUnitRange{T}(first(a))
unitrange(a::AbstractInfUnitRange) = InfUnitRange(a)

AbstractArray{T}(a::InfUnitRange) where T<:Real = InfUnitRange{T}(a.start)
AbstractVector{T}(a::InfUnitRange) where T<:Real = InfUnitRange{T}(a.start)
AbstractArray{T}(a::InfStepRange) where T<:Real = InfStepRange(convert(T,a.start), convert(T,a.step))
Expand All @@ -97,6 +100,15 @@ be 1 and ∞.
struct OneToInf{T<:Integer} <: AbstractInfUnitRange{T} end

OneToInf() = OneToInf{Int}()
oneto(::Infinity) = OneToInf()
function oneto(x::OrientedInfinity)
iszero(x.angle) && return oneto(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end
function oneto(x::SignedInfinity)
signbit(x) || return oneto(∞)
throw(ArgumentError("Cannot create infinite range with negative length"))
end

AbstractArray{T}(a::OneToInf) where T<:Integer = OneToInf{T}()
AbstractVector{T}(a::OneToInf) where T<:Integer = OneToInf{T}()
Expand Down
62 changes: 33 additions & 29 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LinearAlgebra, SparseArrays, InfiniteArrays, FillArrays, LazyArrays, Statistics, DSP, BandedMatrices, LazyBandedMatrices, Test, Base64
import InfiniteArrays: OrientedInfinity, SignedInfinity, InfUnitRange, InfStepRange, OneToInf, NotANumber
import InfiniteArrays: OrientedInfinity, SignedInfinity, InfUnitRange, InfStepRange, OneToInf, NotANumber, oneto, unitrange
import LazyArrays: CachedArray, MemoryLayout, LazyLayout, DiagonalLayout, LazyArrayStyle, colsupport, DualLayout
import BandedMatrices: _BandedMatrix, BandedColumns
import Base.Broadcast: broadcasted, Broadcasted, instantiate
Expand Down Expand Up @@ -42,7 +42,7 @@ import Base.Broadcast: broadcasted, Broadcasted, instantiate

@test string(∞) == ""

@test Base.OneTo(∞) == OneToInf()
@test oneto(∞) == OneToInf()

@test isinf(∞)
@test !isfinite(∞)
Expand Down Expand Up @@ -147,8 +147,8 @@ import Base.Broadcast: broadcasted, Broadcasted, instantiate
@test (-∞)*2 2*(-∞) -2 ** (-2) (-2) * SignedInfinity() -
@test (-∞)*2.3 2.3*(-∞) -2.3 ** (-2.3) (-2.3) * SignedInfinity() -

@test Base.OneTo(1*∞) == OneToInf()
@test_throws ArgumentError Base.OneTo(-∞)
@test oneto(1*∞) == OneToInf()
@test_throws ArgumentError oneto(-∞)

@test isinf(-∞)
@test !isfinite(-∞)
Expand Down Expand Up @@ -190,6 +190,8 @@ import Base.Broadcast: broadcasted, Broadcasted, instantiate

@test exp(im*π/4)*== Inf+im*Inf
@test exp(im*π/4)+==

@test_throws ArgumentError oneto(exp(im*π/4)*∞)
end
end

Expand Down Expand Up @@ -234,9 +236,9 @@ end
@test similar(a, Float64, ∞) isa CachedArray{Float64}
@test similar(a, Float64, (∞,)) isa CachedArray{Float64}
@test similar(a, Float64, (∞,∞)) isa CachedArray{Float64}
@test similar(a, Float64, Base.OneTo(∞)) isa CachedArray{Float64}
@test similar(a, Float64, (Base.OneTo(∞),)) isa CachedArray{Float64}
@test similar(a, Float64, (Base.OneTo(∞),Base.OneTo(∞))) isa CachedArray{Float64}
@test similar(a, Float64, oneto(∞)) isa CachedArray{Float64}
@test similar(a, Float64, (oneto(∞),)) isa CachedArray{Float64}
@test similar(a, Float64, (oneto(∞),oneto(∞))) isa CachedArray{Float64}

@test similar([1,2,3],Float64,()) isa Array{Float64,0}

Expand Down Expand Up @@ -443,8 +445,8 @@ end
end

@testset "Base.OneTo (misleading) overrides" begin
@test Base.OneTo{BigInt}(∞) isa OneToInf{BigInt}
@test Base.OneTo(∞) isa OneToInf{Int}
@test_skip Base.OneTo{BigInt}(∞) isa OneToInf{BigInt}
@test oneto(∞) isa OneToInf{Int}
end

@testset "issue #6973" begin
Expand Down Expand Up @@ -501,6 +503,8 @@ end

@test AbstractArray{Float64}(1:2:∞) AbstractVector{Float64}(1:2:∞)
convert(AbstractVector{Float64}, 1:2:∞) convert(AbstractArray{Float64}, 1:2:∞)

@test unitrange(oneto(∞)) InfUnitRange(oneto(∞)) InfUnitRange{Int}(oneto(∞)) InfUnitRange(1)
end

@testset "inf-range[inf-range]" begin
Expand Down Expand Up @@ -545,7 +549,7 @@ end
end

@testset "end" begin
@test Base.OneTo(∞)[end] Base.OneTo(∞)[∞]
@test oneto(∞)[end] oneto(∞)[∞]
@test (1:∞)[end] (1:∞)[∞]
@test (1:2:∞)[end] (1:2:∞)[∞]
@test (1.0:2:∞)[end] (1.0:2:∞)[∞]
Expand Down Expand Up @@ -834,10 +838,10 @@ end
@testset "Cumsum and diff" begin
@test cumsum(Ones(∞)) 1.0:1.0:
@test cumsum(Fill(2,∞)) 2:2:
@test cumsum(Ones{Int}(∞)) Base.OneTo(∞)
@test cumsum(Ones{BigInt}(∞)) Base.OneTo{BigInt}()
@test cumsum(Ones{Int}(∞)) oneto(∞)
@test cumsum(Ones{BigInt}(∞)) OneToInf{BigInt}()

@test diff(Base.OneTo(∞)) Ones{Int}(∞)
@test diff(oneto(∞)) Ones{Int}(∞)
@test diff(1:∞) Fill(1,∞)
@test diff(1:2:∞) Fill(2,∞)
@test diff(1:2.0:∞) Fill(2.0,∞)
Expand All @@ -853,7 +857,7 @@ end
@test cumsum(x).args[2] 8:12
@test last(y.args) == sum(x[1:9]):2:

for r in (3:4:∞, 2:∞, Base.OneTo(∞))
for r in (3:4:∞, 2:∞, oneto(∞))
c = cumsum(r)
@test c isa InfiniteArrays.RangeCumsum
@test c[Base.OneTo(20)] == c[1:20] == [c[k] for k=1:20] == cumsum(r[1:20])
Expand All @@ -877,8 +881,8 @@ end
@test conv(1:2:∞, [2]) conv([2], 1:2:∞) 2:4:
@test conv(1:∞, Ones(∞))[1:5] == conv(Ones(∞),1:∞)[1:5] == [1,3,6,10,15]
@test conv(Ones(∞), Ones(∞)) 1.0:1.0:
@test conv(Ones{Int}(∞), Ones{Int}(∞)) Base.OneTo(∞)
@test conv(Ones{Bool}(∞), Ones{Bool}(∞)) Base.OneTo(∞)
@test conv(Ones{Int}(∞), Ones{Int}(∞)) oneto(∞)
@test conv(Ones{Bool}(∞), Ones{Bool}(∞)) oneto(∞)
@test conv(Fill{Int}(2,∞), Fill{Int}(1,∞)) conv(Fill{Int}(2,∞), Ones{Int}(∞))
conv(Ones{Int}(∞), Fill{Int}(2,∞)) 2:2:
@test conv(Ones{Int}(∞), [1,5,8])[1:10] == conv([1,5,8], Ones{Int}(∞))[1:10] ==
Expand Down Expand Up @@ -1008,21 +1012,21 @@ end
end

@testset "convert infrange" begin
@test convert(AbstractArray{Float64}, 1:∞) convert(AbstractArray{Float64}, Base.OneTo(∞))
convert(AbstractVector{Float64}, 1:∞) convert(AbstractVector{Float64}, Base.OneTo(∞))
AbstractVector{Float64}(1:∞) AbstractVector{Float64}(Base.OneTo(∞))
AbstractArray{Float64}(1:∞) AbstractArray{Float64}(Base.OneTo(∞)) InfUnitRange(1.0)

@test convert(AbstractArray{Float64}, (1:∞)') convert(AbstractArray{Float64}, Base.OneTo(∞)')
convert(AbstractMatrix{Float64}, (1:∞)') convert(AbstractMatrix{Float64}, Base.OneTo(∞)')
AbstractMatrix{Float64}((1:∞)') AbstractMatrix{Float64}(Base.OneTo(∞)')
AbstractArray{Float64}((1:∞)') AbstractArray{Float64}(Base.OneTo(∞)')
@test convert(AbstractArray{Float64}, 1:∞) convert(AbstractArray{Float64}, oneto(∞))
convert(AbstractVector{Float64}, 1:∞) convert(AbstractVector{Float64}, oneto(∞))
AbstractVector{Float64}(1:∞) AbstractVector{Float64}(oneto(∞))
AbstractArray{Float64}(1:∞) AbstractArray{Float64}(oneto(∞)) InfUnitRange(1.0)

@test convert(AbstractArray{Float64}, (1:∞)') convert(AbstractArray{Float64}, oneto(∞)')
convert(AbstractMatrix{Float64}, (1:∞)') convert(AbstractMatrix{Float64}, oneto(∞)')
AbstractMatrix{Float64}((1:∞)') AbstractMatrix{Float64}(oneto(∞)')
AbstractArray{Float64}((1:∞)') AbstractArray{Float64}(oneto(∞)')
InfUnitRange(1.0)'

@test convert(AbstractArray{Float64}, transpose(1:∞)) convert(AbstractArray{Float64}, transpose(Base.OneTo(∞)))
convert(AbstractMatrix{Float64}, transpose(1:∞)) convert(AbstractMatrix{Float64}, transpose(Base.OneTo(∞)))
AbstractMatrix{Float64}(transpose(1:∞)) AbstractMatrix{Float64}(transpose(Base.OneTo(∞)))
AbstractArray{Float64}(transpose(1:∞)) AbstractArray{Float64}(transpose(Base.OneTo(∞)))
@test convert(AbstractArray{Float64}, transpose(1:∞)) convert(AbstractArray{Float64}, transpose(oneto(∞)))
convert(AbstractMatrix{Float64}, transpose(1:∞)) convert(AbstractMatrix{Float64}, transpose(oneto(∞)))
AbstractMatrix{Float64}(transpose(1:∞)) AbstractMatrix{Float64}(transpose(oneto(∞)))
AbstractArray{Float64}(transpose(1:∞)) AbstractArray{Float64}(transpose(oneto(∞)))
transpose(InfUnitRange(1.0))
end

Expand Down

2 comments on commit a807898

@dlfivefifty
Copy link
Member 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.

Registration pull request created: JuliaRegistries/General/29340

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.4 -m "<description of version>" a8078983c46485ab0afec55861fce014815a4001
git push origin v0.9.4

Please sign in to comment.