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 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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 src/InfiniteArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import LinearAlgebra: BlasInt, BlasFloat, norm, diag, diagm, ishermitian, issymm
det, logdet, istriu, istril, adjoint, tr, AbstractTriangular,
norm2, norm1, normp, AdjOrTrans, HermOrSym

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, LazyLayout,
AbstractCachedVector, CachedArray, CachedVector, ApplyLayout, LazyMatrix,
Expand Down
13 changes: 8 additions & 5 deletions src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,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 @@ -135,8 +138,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 @@ -583,6 +583,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 @@ -1065,19 +1071,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