Skip to content

Commit

Permalink
Fix #97 by adding promote_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty committed Mar 28, 2023
1 parent 10b4c00 commit 1c0304f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero,
show, view, in, mapreduce, one, reverse, promote_op
show, view, in, mapreduce, one, reverse, promote_op, promote_rule

import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
dot, norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!, diagzero, AdjointAbsVec, TransposeAbsVec,
Expand Down Expand Up @@ -34,6 +34,7 @@ const AbstractFillVecOrMat{T} = Union{AbstractFillVector{T},AbstractFillMatrix{T

==(a::AbstractFill, b::AbstractFill) = axes(a) == axes(b) && getindex_value(a) == getindex_value(b)


@inline function _fill_getindex(F::AbstractFill, kj::Integer...)
@boundscheck checkbounds(F, kj...)
getindex_value(F)
Expand Down Expand Up @@ -273,6 +274,14 @@ for (Typ, funcs, func) in ((:Zeros, :zeros, :zero), (:Ones, :ones, :one))
copy(F::$Typ) = F

getindex(F::$Typ{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}
convert(T, getindex_value(A)) # checks that the types are convertible
$Typ{T,N,Axes}(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)
end
end

Expand All @@ -284,6 +293,8 @@ for TYPE in (:Fill, :AbstractFill, :Ones, :Zeros), STYPE in (:AbstractArray, :Ab
end
end

promote_rule(::Type{<:AbstractFill{T, N, Axes}}, ::Type{<:AbstractFill{V, N, Axes}}) where {T,V,N,Axes} = Fill{promote_type(T,V),N,Axes}

"""
fillsimilar(a::AbstractFill, axes)
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ include("infinitearrays.jl")
A = FillArrays.RectDiagonal(Int[], (1:0, 1:4))
@test !(0 in A)
end

@testset "promotion" begin
Z = Zeros{Int}(5)
Zf = Zeros(5)
O = Ones{Int}(5)
Of = Ones{Float64}(5)
@test [Z,O] isa Vector{Fill{Int,1,Tuple{Base.OneTo{Int}}}}
@test [Z,Of] isa Vector{Fill{Float64,1,Tuple{Base.OneTo{Int}}}}
@test [O,O] isa Vector{Ones{Int,1,Tuple{Base.OneTo{Int}}}}
@test [O,Of] isa Vector{Ones{Float64,1,Tuple{Base.OneTo{Int}}}}
@test [Z,Zf] isa Vector{Zeros{Float64,1,Tuple{Base.OneTo{Int}}}}

@test convert(Ones{Int}, Of) convert(Ones{Int,1}, Of) convert(typeof(O), Of) O
@test convert(Zeros{Int}, Zf) convert(Zeros{Int,1}, Zf) convert(typeof(Z), Zf) Z

@test_throws MethodError convert(Zeros{SVector{2,Int}}, Zf)
end
end

@testset "indexing" begin
Expand Down

0 comments on commit 1c0304f

Please sign in to comment.