From 4498570abcd2e2e52745df79736e5df5c4cc7e6d Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 28 Mar 2023 21:11:03 +0100 Subject: [PATCH] Fix #97 by adding promote_rules (#234) --- src/FillArrays.jl | 13 ++++++++++++- test/runtests.jl | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/FillArrays.jl b/src/FillArrays.jl index b6f43624..7abc8236 100644 --- a/src/FillArrays.jl +++ b/src/FillArrays.jl @@ -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, @@ -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) @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index d8449543..e74ca316 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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