Skip to content

Commit

Permalink
Introduce AbstractZeros and AbstractOnes (#299)
Browse files Browse the repository at this point in the history
* Introduce AbstractZeros and AbstractOnes

* Remove repeated AbstractZeros and AbstractOnes definitions

* Refactor to allow customizing algebra outputs

* Remove extraneous comment

* Bump minor version number
  • Loading branch information
mtfishman authored Oct 18, 2023
1 parent 77cf6d6 commit 7036a50
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 151 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.6.2"
version = "1.7.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
60 changes: 33 additions & 27 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,15 @@ Base._reshape(parent::AbstractFill, dims::Tuple{Integer,Vararg{Integer}}) = fill
# Resolves ambiguity error with `_reshape(v::AbstractArray{T, 1}, dims::Tuple{Int})`
Base._reshape(parent::AbstractFill{T, 1, Axes}, dims::Tuple{Int}) where {T, Axes} = fill_reshape(parent, dims...)

for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
for (AbsTyp, Typ, funcs, func) in ((:AbstractZeros, :Zeros, :zeros, :zero), (:AbstractOnes, :Ones, :ones, :one))
@eval begin
abstract type $AbsTyp{T, N, Axes} <: AbstractFill{T, N, Axes} end
$(Symbol(AbsTyp,"Vector")){T} = $AbsTyp{T,1}
$(Symbol(AbsTyp,"Matrix")){T} = $AbsTyp{T,2}
$(Symbol(AbsTyp,"VecOrMat")){T} = Union{$(Symbol(AbsTyp,"Vector")){T},$(Symbol(AbsTyp,"Matrix"))}

""" `$($Typ){T, N, Axes} <: AbstractFill{T, N, Axes}` (lazy `$($funcs)` with axes)"""
struct $Typ{T, N, Axes} <: AbstractFill{T, N, Axes}
struct $Typ{T, N, Axes} <: $AbsTyp{T, N, Axes}
axes::Axes
@inline $Typ{T,N,Axes}(sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
new{T,N,Axes}(sz)
Expand Down Expand Up @@ -312,29 +317,29 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
@inline $Typ(::Type{T}, m...) where T = $Typ{T}(m...)

@inline axes(Z::$Typ) = Z.axes
@inline size(Z::$Typ) = length.(Z.axes)
@inline getindex_value(Z::$Typ{T}) where T = $func(T)
@inline size(Z::$AbsTyp) = length.(axes(Z))
@inline getindex_value(Z::$AbsTyp{T}) where T = $func(T)

AbstractArray{T}(F::$Typ) where T = $Typ{T}(F.axes)
AbstractArray{T,N}(F::$Typ{V,N}) where {T,V,N} = $Typ{T}(F.axes)
AbstractArray{T}(F::$AbsTyp) where T = $Typ{T}(axes(F))
AbstractArray{T,N}(F::$AbsTyp{V,N}) where {T,V,N} = $Typ{T}(axes(F))

copy(F::$Typ) = F
copy(F::$AbsTyp) = F

getindex(F::$Typ{T,0}) where T = getindex_value(F)
getindex(F::$AbsTyp{T,0}) where T = getindex_value(F)

promote_rule(::Type{$Typ{T, N, Axes}}, ::Type{$Typ{V, N, Axes}}) where {T,V,N,Axes} = $Typ{promote_type(T,V),N,Axes}
function convert(::Type{$Typ{T,N,Axes}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes}
function convert(::Type{Typ}, A::$AbsTyp{V,N,Axes}) where {T,V,N,Axes,Typ<:$AbsTyp{T,N,Axes}}
convert(T, getindex_value(A)) # checks that the types are convertible
$Typ{T,N,Axes}(axes(A))
Typ(axes(A))
end
convert(::Type{$Typ{T,N}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
convert(::Type{$Typ{T}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
function convert(::Type{$Typ{T,N,Axes}}, A::AbstractFill{V,N}) where {T,V,N,Axes}
convert(::Type{$Typ{T,N}}, A::$AbsTyp{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
convert(::Type{$Typ{T}}, A::$AbsTyp{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
function convert(::Type{Typ}, A::AbstractFill{V,N}) where {T,V,N,Axes,Typ<:$AbsTyp{T,N,Axes}}
axes(A) isa Axes || throw(ArgumentError("cannot convert, as axes of array are not $Axes"))
val = getindex_value(A)
y = convert(T, val)
y == $func(T) || throw(ArgumentError(string("cannot convert an array containinig $val to ", $Typ)))
$Typ{T,N,Axes}(axes(A))
Typ(axes(A))
end
function convert(::Type{$Typ{T,N}}, A::AbstractFill{<:Any,N}) where {T,N}
convert($Typ{T,N,typeof(axes(A))}, A)
Expand Down Expand Up @@ -517,7 +522,7 @@ Base.Array{T,N}(F::AbstractFill{V,N}) where {T,V,N} =
convert(Array{T,N}, fill(convert(T, getindex_value(F)), size(F)))

# These are in case `zeros` or `ones` are ever faster than `fill`
for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
for (Typ, funcs, func) in ((:AbstractZeros, :zeros, :zero), (:AbstractOnes, :ones, :one))
@eval begin
Base.Array{T,N}(F::$Typ{V,N}) where {T,V,N} = $funcs(T,size(F))
end
Expand Down Expand Up @@ -562,16 +567,16 @@ end
# These methods are necessary to deal with infinite arrays
sum(x::AbstractFill) = getindex_value(x)*length(x)
sum(f, x::AbstractFill) = length(x) * f(getindex_value(x))
sum(x::Zeros) = getindex_value(x)
sum(x::AbstractZeros) = getindex_value(x)

# needed to support infinite case
steprangelen(st...) = StepRangeLen(st...)
cumsum(x::AbstractFill{<:Any,1}) = steprangelen(getindex_value(x), getindex_value(x), length(x))

cumsum(x::ZerosVector) = x
cumsum(x::ZerosVector{Bool}) = x
cumsum(x::OnesVector{II}) where II<:Integer = convert(AbstractVector{II}, oneto(length(x)))
cumsum(x::OnesVector{Bool}) = oneto(length(x))
cumsum(x::AbstractZerosVector) = x
cumsum(x::AbstractZerosVector{Bool}) = x
cumsum(x::AbstractOnesVector{II}) where II<:Integer = convert(AbstractVector{II}, oneto(length(x)))
cumsum(x::AbstractOnesVector{Bool}) = oneto(length(x))


#########
Expand All @@ -591,8 +596,9 @@ allunique(x::AbstractFill) = length(x) < 2
# zero
#########

zero(r::Zeros{T,N}) where {T,N} = r
zero(r::Ones{T,N}) where {T,N} = Zeros{T,N}(r.axes)
zero(r::AbstractZeros{T,N}) where {T,N} = r
# TODO: Make this required?
zero(r::AbstractOnes{T,N}) where {T,N} = Zeros{T,N}(axes(r))
zero(r::Fill{T,N}) where {T,N} = Zeros{T,N}(r.axes)

#########
Expand Down Expand Up @@ -641,8 +647,8 @@ all(f::Function, x::AbstractFill) = isempty(x) || all(f(getindex_value(x)))
any(x::AbstractFill) = any(identity, x)
all(x::AbstractFill) = all(identity, x)

count(x::Ones{Bool}) = length(x)
count(x::Zeros{Bool}) = 0
count(x::AbstractOnes{Bool}) = length(x)
count(x::AbstractZeros{Bool}) = 0
count(f, x::AbstractFill) = f(getindex_value(x)) ? length(x) : 0

#########
Expand Down Expand Up @@ -671,7 +677,7 @@ end
##
# print
##
Base.replace_in_print_matrix(::Zeros, ::Integer, ::Integer, s::AbstractString) =
Base.replace_in_print_matrix(::AbstractZeros, ::Integer, ::Integer, s::AbstractString) =
Base.replace_with_centered_mark(s)

# following support blocked fill array printing via
Expand Down Expand Up @@ -704,7 +710,7 @@ function Base.show(io::IO, ::MIME"text/plain", x::Union{Eye, AbstractFill})
return show(io, x)
end
summary(io, x)
if x isa Union{Zeros, Ones, Eye}
if x isa Union{AbstractZeros, AbstractOnes, Eye}
# then no need to print entries
elseif length(x) > 1
print(io, ", with entries equal to ", getindex_value(x))
Expand All @@ -716,7 +722,7 @@ end
function Base.show(io::IO, x::AbstractFill) # for example (Fill(π,3),)
print(io, nameof(typeof(x)))
sz = size(x)
args = if x isa Union{Zeros, Ones}
args = if x isa Union{AbstractZeros, AbstractOnes}
T = eltype(x)
if T != Float64
print(io,"{", T, "}")
Expand Down
Loading

2 comments on commit 7036a50

@dlfivefifty
Copy link
Member

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/93651

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 v1.7.0 -m "<description of version>" 7036a5005d2a96f79b67ffc9bfd4a1e07517c85e
git push origin v1.7.0

Please sign in to comment.