Skip to content
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

support +/- AbstractFill from FillArrays #118

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
[compat]
Aqua = "0.5"
ArrayLayouts = "0.8"
FillArrays = "0.13"
FillArrays = "0.13.8"
Infinities = "0.1.1"
LazyArrays = "0.22.13"
julia = "1.6"
Expand Down
2 changes: 1 addition & 1 deletion src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import LinearAlgebra: BlasInt, BlasFloat, norm, diag, diagm, ishermitian, issymm

import Statistics: mean, median

import FillArrays: AbstractFill, getindex_value, fill_reshape, RectDiagonal, Fill, Ones, Zeros, Eye
import FillArrays: AbstractFill, getindex_value, fill_reshape, RectDiagonal, Fill, Ones, Zeros, Eye, elconvert
import LazyArrays: LazyArrayStyle, AbstractBandedLayout, MemoryLayout, LazyLayout, UnknownLayout,
ZerosLayout, AbstractCachedVector, CachedArray, CachedVector, ApplyLayout, LazyMatrix,
reshapedlayout, sub_materialize, sublayout, LayoutMatrix, LayoutVector, _padded_sub_materialize, PaddedLayout,
Expand Down
13 changes: 8 additions & 5 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,19 @@ struct InfUnitRange{T<:Real} <: AbstractInfUnitRange{T}
start::T
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)
for TYPE in (:AbstractArray, :AbstractVector)
@eval $TYPE{T}(a::InfUnitRange) where T<:Integer = InfUnitRange{T}(a.start)
@eval $TYPE{T}(a::InfUnitRange) where T = InfStepRange(T(a.start), one(T))
end
AbstractArray{T}(a::InfStepRange) where T<:Real = InfStepRange(convert(T,a.start), convert(T,a.step))
AbstractVector{T}(a::InfStepRange) where T<:Real = InfStepRange(convert(T,a.start), convert(T,a.step))
elconvert(::Type{T}, r::AbstractInfUnitRange) where T = AbstractArray{T}(r)
elconvert(::Type{T}, r::InfStepRange) where T = AbstractArray{T}(r)

const InfRanges{T} = Union{InfStepRange{T},AbstractInfUnitRange{T}}
const InfAxes = Union{InfRanges{<:Integer},Slice{<:AbstractInfUnitRange{<:Integer}},IdentityUnitRange{<:AbstractInfUnitRange{<:Integer}}}
Expand Down Expand Up @@ -133,8 +136,8 @@ function oneto(x::ComplexInfinity)

AbstractArray{T}(a::OneToInf) where T<:Integer = OneToInf{T}()
AbstractVector{T}(a::OneToInf) where T<:Integer = OneToInf{T}()
AbstractArray{T}(a::OneToInf) where T<:Real = InfUnitRange{T}(a)
AbstractVector{T}(a::OneToInf) where T<:Real = InfUnitRange{T}(a)
AbstractArray{T}(a::OneToInf) where T<:Real = InfStepRange(one(T),one(T))
AbstractVector{T}(a::OneToInf) where T<:Real = InfStepRange(one(T),one(T))


(==)(::OneToInf, ::OneToInf) = true
Expand Down
16 changes: 13 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ end
@test sum([1; zeros(∞)]) ≡ 1.0
@test sum([1; ones(∞)]) ≡ 1.0∞
end

@testset "fill algebra" begin
@test Zeros(∞) + (1:∞) ≡ 1.0:∞
@test Ones(∞) + (1:∞) ≡ 2.0:∞
@test Zeros(∞) - (1:∞) ≡ -1.0:-1.0:-∞
end
end

@testset "diagonal" begin
Expand Down Expand Up @@ -1002,19 +1008,23 @@ end
@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)
AbstractArray{Float64}(1:∞) ≡ AbstractArray{Float64}(oneto(∞)) ≡
float(1:∞) ≡ float(oneto(∞)) ≡
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)'
float((1:∞)') ≡ float(oneto(∞)') ≡
(1.0:∞)'

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

Choose a reason for hiding this comment

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

Why doesn't this work anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I replaced InfUnitRange(1.0) with 1.0:∞ which is InfStepRange

Copy link
Contributor Author

Choose a reason for hiding this comment

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

julia> typeof(float(1:5))
StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}

Copy link
Member

Choose a reason for hiding this comment

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

Ah should InfUnitRange be restricted to integers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a subtype of OrdinalRange which by definition only accepts integers.

StepRange throws error for float input while UnitRange doesn't... I'm not sure

Copy link
Member

Choose a reason for hiding this comment

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

It's a subtype of OrdinalRange which by definition only accepts integers.

I don't think there's a strict requirement on element types, only that the step should be an integer. Floating point UnitRanges are currently allowed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Floating point UnitRanges are currently allowed

They are, but the only way to construct them is the default constructor. The tests convert eltypes, which always result in StepRangeLen in Julia Base.

float(transpose(1:∞)) ≡ float(transpose(oneto(∞))) ≡
transpose(1.0:∞)
end

@testset "cached indexing" begin
Expand Down