Skip to content

Commit 1efbdbb

Browse files
authored
Add missing convert methods from Fill to Zeros/Ones (#284)
1 parent 41715aa commit 1efbdbb

File tree

3 files changed

+81
-59
lines changed

3 files changed

+81
-59
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 = "1.4.2"
3+
version = "1.5.0"
44

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

src/FillArrays.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
329329
end
330330
convert(::Type{$Typ{T,N}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
331331
convert(::Type{$Typ{T}}, A::$Typ{V,N,Axes}) where {T,V,N,Axes} = convert($Typ{T,N,Axes}, A)
332+
function convert(::Type{$Typ{T,N,Axes}}, A::AbstractFill{V,N}) where {T,V,N,Axes}
333+
axes(A) isa Axes || throw(ArgumentError("cannot convert, as axes of array are not $Axes"))
334+
val = getindex_value(A)
335+
y = convert(T, val)
336+
y == $func(T) || throw(ArgumentError(string("cannot convert an array containinig $val to ", $Typ)))
337+
$Typ{T,N,Axes}(axes(A))
338+
end
339+
function convert(::Type{$Typ{T,N}}, A::AbstractFill{<:Any,N}) where {T,N}
340+
convert($Typ{T,N,typeof(axes(A))}, A)
341+
end
342+
function convert(::Type{$Typ{T}}, A::AbstractFill{<:Any,N}) where {T,N}
343+
convert($Typ{T,N}, A)
344+
end
345+
function convert(::Type{$Typ}, A::AbstractFill{V,N}) where {V,N}
346+
convert($Typ{V,N}, A)
347+
end
332348
end
333349
end
334350

test/runtests.jl

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,70 @@ oneton(T::Type, sz...) = reshape(T.(1:prod(sz)), sz)
1313
oneton(sz...) = oneton(Float64, sz...)
1414

1515
@testset "fill array constructors and convert" begin
16-
for (Typ, funcs) in ((:Zeros, :zeros), (:Ones, :ones))
17-
@eval begin
18-
@test $Typ((-1,5)) == $Typ((0,5))
19-
@test $Typ(5) isa AbstractVector{Float64}
20-
@test $Typ(5,5) isa AbstractMatrix{Float64}
21-
@test $Typ(5) == $Typ((5,))
22-
@test $Typ(5,5) == $Typ((5,5))
23-
@test eltype($Typ(5,5)) == Float64
24-
25-
for T in (Int, Float64)
26-
Z = $Typ{T}(5)
27-
@test $Typ(T, 5) Z
28-
@test eltype(Z) == T
29-
@test Array(Z) == $funcs(T,5)
30-
@test Array{T}(Z) == $funcs(T,5)
31-
@test Array{T,1}(Z) == $funcs(T,5)
32-
33-
@test convert(AbstractArray,Z) Z
34-
@test convert(AbstractArray{T},Z) AbstractArray{T}(Z) Z
35-
@test convert(AbstractVector{T},Z) AbstractVector{T}(Z) Z
36-
@test convert(AbstractFill{T},Z) AbstractFill{T}(Z) Z
37-
38-
@test $Typ{T,1}(2ones(T,5)) == Z
39-
@test $Typ{T}(2ones(T,5)) == Z
40-
@test $Typ(2ones(T,5)) == Z
41-
42-
Z = $Typ{T}(5, 5)
43-
@test $Typ(T, 5, 5) Z
44-
@test eltype(Z) == T
45-
@test Array(Z) == $funcs(T,5,5)
46-
@test Array{T}(Z) == $funcs(T,5,5)
47-
@test Array{T,2}(Z) == $funcs(T,5,5)
48-
49-
@test convert(AbstractArray,Z) convert(AbstractFill,Z) Z
50-
@test convert(AbstractArray{T},Z) convert(AbstractFill{T},Z) AbstractArray{T}(Z) Z
51-
@test convert(AbstractMatrix{T},Z) convert(AbstractFill{T,2},Z) AbstractMatrix{T}(Z) Z
52-
53-
@test_throws Exception convert(Fill{Float64}, [1,1,2])
54-
@test_throws Exception convert(Fill, [])
55-
@test convert(Fill{Float64}, [1,1,1]) Fill(1.0, 3)
56-
@test convert(Fill, Float64[1,1,1]) Fill(1.0, 3)
57-
@test convert(Fill{Float64}, Fill(1.0,2)) Fill(1.0, 2) # was ambiguous
58-
@test convert(Fill{Int}, Ones(20)) Fill(1, 20)
59-
@test convert(Fill{Int,1}, Ones(20)) Fill(1, 20)
60-
@test convert(Fill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) Fill(1, 20)
61-
@test convert(AbstractFill{Int}, Ones(20)) AbstractFill{Int}(Ones(20)) Ones{Int}(20)
62-
@test convert(AbstractFill{Int,1}, Ones(20)) AbstractFill{Int,1}(Ones(20)) Ones{Int}(20)
63-
@test convert(AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}(Ones(20)) Ones{Int}(20)
64-
65-
@test $Typ{T,2}(2ones(T,5,5)) $Typ{T}(5,5)
66-
@test $Typ{T}(2ones(T,5,5)) $Typ{T}(5,5)
67-
@test $Typ(2ones(T,5,5)) $Typ{T}(5,5)
68-
69-
@test $Typ(Z) $Typ{T}(Z) $Typ{T,2}(Z) typeof(Z)(Z) Z
70-
71-
@test AbstractArray{Float32}(Z) $Typ{Float32}(5,5)
72-
@test AbstractArray{Float32,2}(Z) $Typ{Float32}(5,5)
73-
end
16+
for (Typ, funcs) in ((Zeros, zeros), (Ones, ones))
17+
@test Typ((-1,5)) == Typ((0,5))
18+
@test Typ(5) isa AbstractVector{Float64}
19+
@test Typ(5,5) isa AbstractMatrix{Float64}
20+
@test Typ(5) == Typ((5,))
21+
@test Typ(5,5) == Typ((5,5))
22+
@test eltype(Typ(5,5)) == Float64
23+
24+
for T in (Int, Float64)
25+
Z = Typ{T}(5)
26+
@test Typ(T, 5) Z
27+
@test eltype(Z) == T
28+
@test Array(Z) == funcs(T,5)
29+
@test Array{T}(Z) == funcs(T,5)
30+
@test Array{T,1}(Z) == funcs(T,5)
31+
32+
@test convert(AbstractArray,Z) Z
33+
@test convert(AbstractArray{T},Z) AbstractArray{T}(Z) Z
34+
@test convert(AbstractVector{T},Z) AbstractVector{T}(Z) Z
35+
@test convert(AbstractFill{T},Z) AbstractFill{T}(Z) Z
36+
37+
@test Typ{T,1}(2ones(T,5)) == Z
38+
@test Typ{T}(2ones(T,5)) == Z
39+
@test Typ(2ones(T,5)) == Z
40+
41+
Z = Typ{T}(5, 5)
42+
@test Typ(T, 5, 5) Z
43+
@test eltype(Z) == T
44+
@test Array(Z) == funcs(T,5,5)
45+
@test Array{T}(Z) == funcs(T,5,5)
46+
@test Array{T,2}(Z) == funcs(T,5,5)
47+
48+
@test convert(AbstractArray,Z) convert(AbstractFill,Z) Z
49+
@test convert(AbstractArray{T},Z) convert(AbstractFill{T},Z) AbstractArray{T}(Z) Z
50+
@test convert(AbstractMatrix{T},Z) convert(AbstractFill{T,2},Z) AbstractMatrix{T}(Z) Z
51+
52+
@test_throws Exception convert(Fill{Float64}, [1,1,2])
53+
@test_throws Exception convert(Fill, [])
54+
@test convert(Fill{Float64}, [1,1,1]) Fill(1.0, 3)
55+
@test convert(Fill, Float64[1,1,1]) Fill(1.0, 3)
56+
@test convert(Fill{Float64}, Fill(1.0,2)) Fill(1.0, 2) # was ambiguous
57+
@test convert(Fill{Int}, Ones(20)) Fill(1, 20)
58+
@test convert(Fill{Int,1}, Ones(20)) Fill(1, 20)
59+
@test convert(Fill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) Fill(1, 20)
60+
@test convert(AbstractFill{Int}, Ones(20)) AbstractFill{Int}(Ones(20)) Ones{Int}(20)
61+
@test convert(AbstractFill{Int,1}, Ones(20)) AbstractFill{Int,1}(Ones(20)) Ones{Int}(20)
62+
@test convert(AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}(Ones(20)) Ones{Int}(20)
63+
64+
@test Typ{T,2}(2ones(T,5,5)) Typ{T}(5,5)
65+
@test Typ{T}(2ones(T,5,5)) Typ{T}(5,5)
66+
@test Typ(2ones(T,5,5)) Typ{T}(5,5)
67+
68+
z = Typ{T}()[]
69+
@test convert(Typ, Fill(z,2)) === Typ{T}(2)
70+
z = Typ{Int8}()[]
71+
@test convert(Typ{T}, Fill(z,2)) === Typ{T}(2)
72+
@test convert(Typ{T,1}, Fill(z,2)) === Typ{T}(2)
73+
@test convert(Typ{T,1,Tuple{Base.OneTo{Int}}}, Fill(z,2)) === Typ{T}(2)
74+
@test_throws ArgumentError convert(Typ, Fill(2,2))
75+
76+
@test Typ(Z) Typ{T}(Z) Typ{T,2}(Z) typeof(Z)(Z) Z
77+
78+
@test AbstractArray{Float32}(Z) Typ{Float32}(5,5)
79+
@test AbstractArray{Float32,2}(Z) Typ{Float32}(5,5)
7480
end
7581
end
7682

0 commit comments

Comments
 (0)