From 871c7de13d4931b27659c2aafa5d7f7d3ea63cc1 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 11 Oct 2024 17:20:04 -0400 Subject: [PATCH 1/3] [NDTensors] Fix type promotion of EmptyNumber in Julia 1.11 --- NDTensors/src/empty/EmptyTensor.jl | 2 -- NDTensors/src/emptynumber.jl | 5 +++++ NDTensors/test/test_emptynumber.jl | 13 ++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) 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..86f7e9e8fe 100644 --- a/NDTensors/src/emptynumber.jl +++ b/NDTensors/src/emptynumber.jl @@ -18,6 +18,11 @@ convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T)) # This helps with defining `norm` of `EmptyStorage{EmptyNumber}`. AbstractFloat(::EmptyNumber) = zero(AbstractFloat) +Base.promote_rule(::Type{EmptyNumber}, ::Type{T}) where {T<:Number} = T +Base.promote_rule(::Type{T}, ::Type{EmptyNumber}) where {T<:Number} = T +Base.promote_rule(::Type{EmptyNumber}, ::Type{Bool}) = Bool +Base.promote_rule(::Type{Bool}, ::Type{EmptyNumber}) = Bool + # Basic arithmetic (::EmptyNumber + ::EmptyNumber) = EmptyNumber() (::EmptyNumber - ::EmptyNumber) = EmptyNumber() diff --git a/NDTensors/test/test_emptynumber.jl b/NDTensors/test/test_emptynumber.jl index dc8357a115..1dbddfb55a 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, Float64) + @test promote_type(EmptyNumber, T) === T + @test promote_type(T, EmptyNumber) === T + end # Basic arithmetic @test 𝟎 + 𝟎 == 𝟎 From 91a09c387a41e1254b4b0e6a69b3a24f6505641c Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 11 Oct 2024 17:21:50 -0400 Subject: [PATCH 2/3] Bump to v0.3.44 --- NDTensors/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From cdb175b82786b108b08f90610d0b4b07d23df601 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 11 Oct 2024 18:31:39 -0400 Subject: [PATCH 3/3] Fix complex number promotion --- NDTensors/src/emptynumber.jl | 7 +++++-- NDTensors/test/test_emptynumber.jl | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/NDTensors/src/emptynumber.jl b/NDTensors/src/emptynumber.jl index 86f7e9e8fe..1d9799b740 100644 --- a/NDTensors/src/emptynumber.jl +++ b/NDTensors/src/emptynumber.jl @@ -18,10 +18,13 @@ convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T)) # This helps with defining `norm` of `EmptyStorage{EmptyNumber}`. AbstractFloat(::EmptyNumber) = zero(AbstractFloat) -Base.promote_rule(::Type{EmptyNumber}, ::Type{T}) where {T<:Number} = T -Base.promote_rule(::Type{T}, ::Type{EmptyNumber}) where {T<:Number} = T +# 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() diff --git a/NDTensors/test/test_emptynumber.jl b/NDTensors/test/test_emptynumber.jl index 1dbddfb55a..73d82117f5 100644 --- a/NDTensors/test/test_emptynumber.jl +++ b/NDTensors/test/test_emptynumber.jl @@ -12,7 +12,7 @@ const 𝟎 = EmptyNumber() @test complex(EmptyNumber) == Complex{EmptyNumber} # Promotion - for T in (Bool, Float64) + for T in (Bool, Float32, Float64, Complex{Float32}, Complex{Float64}) @test promote_type(EmptyNumber, T) === T @test promote_type(T, EmptyNumber) === T end