Skip to content

Commit 8c7ace3

Browse files
authored
Non-numeric eltypes in OneElement (#315)
1 parent ba796e8 commit 8c7ace3

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
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.8.0"
3+
version = "1.9.0"
44

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

src/FillArrays.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,10 @@ const FillVecOrMat{T} = Union{FillVector{T},FillMatrix{T}}
119119
Fill{T,N,Axes}(x, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
120120
Fill{T,N,Axes}(convert(T, x)::T, sz)
121121

122-
Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
122+
Fill{T,0}(x, ::Tuple{}) where T = Fill{T,0,Tuple{}}(convert(T, x)::T, ()) # ambiguity fix
123123

124-
@inline Fill{T, N}(x::T, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
125-
Fill{T,N,Axes}(x, sz)
126124
@inline Fill{T, N}(x, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
127-
Fill{T,N}(convert(T, x)::T, sz)
125+
Fill{T,N,Axes}(convert(T, x)::T, sz)
128126

129127
@inline Fill{T, N}(x, sz::SZ) where SZ<:Tuple{Vararg{Integer,N}} where {T, N} =
130128
Fill{T,N}(x, oneto.(sz))

src/oneelement.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ struct OneElement{T,N,I,A} <: AbstractArray{T,N}
99
val::T
1010
ind::I
1111
axes::A
12-
OneElement(val::T, ind::I, axes::A) where {T<:Number, I<:NTuple{N,Int}, A<:NTuple{N,AbstractUnitRange}} where {N} = new{T,N,I,A}(val, ind, axes)
12+
OneElement(val::T, ind::I, axes::A) where {T, I<:NTuple{N,Int}, A<:NTuple{N,AbstractUnitRange}} where {N} = new{T,N,I,A}(val, ind, axes)
13+
OneElement(val::T, ind::Tuple{}, axes::Tuple{}) where {T} = new{T,0,Tuple{},Tuple{}}(val, ind, axes)
1314
end
1415

1516
const OneElementVector{T,I,A} = OneElement{T,1,I,A}

test/runtests.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ oneton(sz...) = oneton(Float64, sz...)
9797

9898

9999
for T in (Int, Float64)
100-
F = Fill{T}(one(T), 5)
100+
F = Fill{T, 0}(2)
101+
@test size(F) == ()
102+
@test F[] === T(2)
103+
104+
F = Fill{T}(1, 5)
101105

102106
@test eltype(F) == T
103107
@test Array(F) == fill(one(T),5)
104108
@test Array{T}(F) == fill(one(T),5)
105109
@test Array{T,1}(F) == fill(one(T),5)
106110

107-
F = Fill{T}(one(T), 5, 5)
111+
F = Fill{T}(1, 5, 5)
108112
@test eltype(F) == T
109113
@test Array(F) == fill(one(T),5,5)
110114
@test Array{T}(F) == fill(one(T),5,5)
@@ -1901,6 +1905,10 @@ end
19011905
end
19021906

19031907
@testset "OneElement" begin
1908+
A = OneElement(2, (), ())
1909+
@test A == Fill(2, ())
1910+
@test A[] === 2
1911+
19041912
e₁ = OneElement(2, 5)
19051913
@test e₁ == [0,1,0,0,0]
19061914
@test_throws BoundsError e₁[6]
@@ -1936,6 +1944,13 @@ end
19361944
@test Base.setindex(Zeros(5,3), 2, 2, 3) OneElement(2.0, (2,3), (5,3))
19371945
@test_throws BoundsError Base.setindex(Zeros(5), 2, 6)
19381946

1947+
@testset "non-numeric" begin
1948+
S = SMatrix{2,2}(1:4)
1949+
A = OneElement(S, (2,2), (2,2))
1950+
@test A[2,2] === S
1951+
@test A[1,1] === A[1,2] === A[2,1] === zero(S)
1952+
end
1953+
19391954
@testset "adjoint/transpose" begin
19401955
A = OneElement(3im, (2,4), (4,6))
19411956
@test A' === OneElement(-3im, (4,2), (6,4))

0 commit comments

Comments
 (0)