Skip to content

Commit e3158b0

Browse files
authored
Add constructors for Fill(::Fill) and varients (#103)
* Add constructors for Fill(::Fill) and varients * Add special - overloads
1 parent a037294 commit e3158b0

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "0.8.11"
3+
version = "0.8.12"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
7676
@inline Fill(x::T, sz::Vararg{<:Integer,N}) where {T, N} = Fill{T, N}(x, sz)
7777
@inline Fill(x::T, sz::Tuple{Vararg{<:Any,N}}) where {T, N} = Fill{T, N}(x, sz)
7878

79+
# We restrict to when T is specified to avoid ambiguity with a Fill of a Fill
80+
@inline Fill{T}(F::Fill{T}) where T = F
81+
@inline Fill{T,N}(F::Fill{T,N}) where {T,N} = F
82+
@inline Fill{T,N,Axes}(F::Fill{T,N,Axes}) where {T,N,Axes} = F
83+
7984
@inline axes(F::Fill) = F.axes
8085
@inline size(F::Fill) = length.(F.axes)
8186

@@ -204,10 +209,10 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
204209
@inline $Typ{T}(n::Integer) where T = $Typ{T,1}(n)
205210
@inline $Typ(n::Integer) = $Typ{Float64,1}(n)
206211

207-
212+
@inline $Typ{T,N,Axes}(A::AbstractArray{V,N}) where{T,V,N,Axes} = $Typ{T,N,Axes}(axes(A))
208213
@inline $Typ{T,N}(A::AbstractArray{V,N}) where{T,V,N} = $Typ{T,N}(size(A))
209214
@inline $Typ{T}(A::AbstractArray) where{T} = $Typ{T}(size(A))
210-
@inline $Typ(A::AbstractArray) = $Typ(size(A))
215+
@inline $Typ(A::AbstractArray) = $Typ{eltype(A)}(A)
211216

212217
@inline axes(Z::$Typ) = Z.axes
213218
@inline size(Z::$Typ) = length.(Z.axes)

src/fillalgebra.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function +(a::Zeros{T}, b::Zeros{V}) where {T, V}
148148
return Zeros{promote_type(T,V)}(size(a)...)
149149
end
150150
-(a::Zeros, b::Zeros) = -(a + b)
151+
-(a::Ones, b::Ones) = Zeros(a)+Zeros(b)
151152

152153
# Zeros +/- Fill and Fill +/- Zeros
153154
function +(a::AbstractFill{T}, b::Zeros{V}) where {T, V}

src/fillbroadcast.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ broadcasted(::DefaultArrayStyle, ::typeof(+), a::Zeros, b::Zeros) = _broadcasted
3434
broadcasted(::DefaultArrayStyle, ::typeof(+), a::Ones, b::Zeros) = _broadcasted_ones(a, b)
3535
broadcasted(::DefaultArrayStyle, ::typeof(+), a::Zeros, b::Ones) = _broadcasted_ones(a, b)
3636

37+
broadcasted(::DefaultArrayStyle, ::typeof(-), a::Zeros, b::Zeros) = _broadcasted_zeros(a, b)
38+
broadcasted(::DefaultArrayStyle, ::typeof(-), a::Ones, b::Zeros) = _broadcasted_ones(a, b)
39+
broadcasted(::DefaultArrayStyle, ::typeof(-), a::Ones, b::Ones) = _broadcasted_zeros(a, b)
40+
3741
broadcasted(::DefaultArrayStyle, ::typeof(*), a::Zeros, b::Zeros) = _broadcasted_zeros(a, b)
3842

3943
for op in (:*, :/)

test/runtests.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,24 @@ import FillArrays: AbstractFill, RectDiagonal, SquareEye
4343
@test convert(Fill{Float64}, Fill(1.0,2)) Fill(1.0, 2) # was ambiguous
4444
@test convert(Fill{Int}, Ones(20)) Fill(1, 20)
4545

46-
@test $Typ{T,2}(2ones(T,5,5)) == Z
47-
@test $Typ{T}(2ones(T,5,5)) == Z
48-
@test $Typ(2ones(T,5,5)) == Z
46+
@test $Typ{T,2}(2ones(T,5,5)) $Typ{T}(5,5)
47+
@test $Typ{T}(2ones(T,5,5)) $Typ{T}(5,5)
48+
@test $Typ(2ones(T,5,5)) $Typ{T}(5,5)
4949

50-
@test AbstractArray{Float32}(Z) == $Typ{Float32}(5,5)
51-
@test AbstractArray{Float32,2}(Z) == $Typ{Float32}(5,5)
50+
@test $Typ(Z) $Typ{T}(Z) $Typ{T,2}(Z) typeof(Z)(Z) Z
51+
52+
@test AbstractArray{Float32}(Z) $Typ{Float32}(5,5)
53+
@test AbstractArray{Float32,2}(Z) $Typ{Float32}(5,5)
5254
end
5355
end
5456
end
5557

58+
@test Fill(1) Fill{Int}(1) Fill{Int,0}(1) Fill{Int,0,Tuple{}}(1,())
5659
@test Fill(1,(-1,5)) Fill(1,(0,5))
5760
@test Fill(1.0,5) isa AbstractVector{Float64}
5861
@test Fill(1.0,5,5) isa AbstractMatrix{Float64}
59-
@test Fill(1,5) == Fill(1,(5,))
60-
@test Fill(1,5,5) == Fill(1,(5,5))
62+
@test Fill(1,5) Fill(1,(5,))
63+
@test Fill(1,5,5) Fill(1,(5,5))
6164
@test eltype(Fill(1.0,5,5)) == Float64
6265

6366
@test Matrix{Float64}(Zeros{ComplexF64}(10,10)) == zeros(10,10)
@@ -82,11 +85,12 @@ import FillArrays: AbstractFill, RectDiagonal, SquareEye
8285
@test convert(AbstractArray{T},F) AbstractArray{T}(F) F
8386
@test convert(AbstractMatrix{T},F) AbstractMatrix{T}(F) F
8487

85-
86-
@test convert(AbstractArray{Float32},F) == AbstractArray{Float32}(F) ==
88+
@test convert(AbstractArray{Float32},F) AbstractArray{Float32}(F)
8789
Fill{Float32}(one(Float32),5,5)
88-
@test convert(AbstractMatrix{Float32},F) == AbstractMatrix{Float32}(F) ==
90+
@test convert(AbstractMatrix{Float32},F) AbstractMatrix{Float32}(F)
8991
Fill{Float32}(one(Float32),5,5)
92+
93+
@test Fill{T}(F) Fill{T,2}(F) typeof(F)(F) F
9094
end
9195

9296
@test Eye(5) isa Diagonal{Float64}
@@ -575,7 +579,12 @@ end
575579
@testset "Zeros -" begin
576580
@test Zeros(10) - Zeros(10) Zeros(10)
577581
@test Ones(10) - Zeros(10) Ones(10)
582+
@test Ones(10) - Ones(10) Zeros(10)
578583
@test Fill(1,10) - Zeros(10) Fill(1.0,10)
584+
585+
@test Zeros(10) .- Zeros(1,9) Zeros(10,9)
586+
@test Ones(10) .- Zeros(1,9) Ones(10,9)
587+
@test Ones(10) .- Ones(1,9) Zeros(10,9)
579588
end
580589

581590
@testset "Zero .*" begin

0 commit comments

Comments
 (0)