Skip to content

Commit

Permalink
[NDTensors] Fix type promotion of EmptyNumber in Julia 1.11 (#1541)
Browse files Browse the repository at this point in the history
* [NDTensors] Fix type promotion of EmptyNumber in Julia 1.11

* [NDTensors] Bump to v0.3.44
  • Loading branch information
mtfishman authored Oct 12, 2024
1 parent 795f3ab commit 020efa3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion NDTensors/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NDTensors"
uuid = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
authors = ["Matthew Fishman <[email protected]>"]
version = "0.3.43"
version = "0.3.44"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
2 changes: 0 additions & 2 deletions NDTensors/src/empty/EmptyTensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions NDTensors/src/emptynumber.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 10 additions & 3 deletions NDTensors/test/test_emptynumber.jl
Original file line number Diff line number Diff line change
@@ -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 𝟎 + 𝟎 == 𝟎
Expand Down

2 comments on commit 020efa3

@mtfishman
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir=NDTensors

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117124

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a NDTensors-v0.3.44 -m "<description of version>" 020efa3aeee0170c532de2bdae84ef3b555fd354
git push origin NDTensors-v0.3.44

Please sign in to comment.