diff --git a/NDTensors/Project.toml b/NDTensors/Project.toml index 65771b0c5a..5fa45ba4d6 100644 --- a/NDTensors/Project.toml +++ b/NDTensors/Project.toml @@ -1,7 +1,7 @@ name = "NDTensors" uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" authors = ["Matthew Fishman "] -version = "0.3.43" +version = "0.3.44" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/NDTensors/src/empty/EmptyTensor.jl b/NDTensors/src/empty/EmptyTensor.jl index 3fcbf4ed63..f5e04e0652 100644 --- a/NDTensors/src/empty/EmptyTensor.jl +++ b/NDTensors/src/empty/EmptyTensor.jl @@ -152,8 +152,6 @@ end setindex!!(T::EmptyTensor, x, I...) = setindex(T, x, I...) -promote_rule(::Type{EmptyNumber}, ::Type{T}) where {T<:Number} = T - function promote_rule( ::Type{T1}, ::Type{T2} ) where {T1<:EmptyStorage{EmptyNumber},T2<:TensorStorage} diff --git a/NDTensors/src/emptynumber.jl b/NDTensors/src/emptynumber.jl index d84abb52ca..1d9799b740 100644 --- a/NDTensors/src/emptynumber.jl +++ b/NDTensors/src/emptynumber.jl @@ -18,6 +18,14 @@ convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T)) # This helps with defining `norm` of `EmptyStorage{EmptyNumber}`. AbstractFloat(::EmptyNumber) = zero(AbstractFloat) +# Extra definitions fix ambiguity errors. +Base.promote_rule(::Type{EmptyNumber}, T::Type{<:Number}) = T +Base.promote_rule(T::Type{<:Number}, ::Type{EmptyNumber}) = T +Base.promote_rule(::Type{EmptyNumber}, ::Type{Bool}) = Bool +Base.promote_rule(::Type{Bool}, ::Type{EmptyNumber}) = Bool +Base.promote_rule(::Type{EmptyNumber}, T::Type{Complex{R}}) where {R<:Real} = T +Base.promote_rule(T::Type{Complex{R}}, ::Type{EmptyNumber}) where {R<:Real} = T + # Basic arithmetic (::EmptyNumber + ::EmptyNumber) = EmptyNumber() (::EmptyNumber - ::EmptyNumber) = EmptyNumber() diff --git a/NDTensors/test/test_emptynumber.jl b/NDTensors/test/test_emptynumber.jl index dc8357a115..73d82117f5 100644 --- a/NDTensors/test/test_emptynumber.jl +++ b/NDTensors/test/test_emptynumber.jl @@ -1,14 +1,21 @@ @eval module $(gensym()) -using NDTensors +using LinearAlgebra: norm +using NDTensors: EmptyNumber using Test: @testset, @test, @test_throws -const 𝟎 = NDTensors.EmptyNumber() +const 𝟎 = EmptyNumber() @testset "NDTensors.EmptyNumber" begin x = 2.3 @test complex(𝟎) == 𝟎 - @test complex(NDTensors.EmptyNumber) == Complex{NDTensors.EmptyNumber} + @test complex(EmptyNumber) == Complex{EmptyNumber} + + # Promotion + for T in (Bool, Float32, Float64, Complex{Float32}, Complex{Float64}) + @test promote_type(EmptyNumber, T) === T + @test promote_type(T, EmptyNumber) === T + end # Basic arithmetic @test 𝟎 + 𝟎 == 𝟎