Skip to content

Commit

Permalink
[NDTensors] Fix zero for EmptyStorage (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Aug 3, 2023
1 parent a878e60 commit 0724830
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
2 changes: 2 additions & 0 deletions NDTensors/src/NDTensors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ include("abstractarray/ndims.jl")
include("abstractarray/fill.jl")
include("array/set_types.jl")
include("tupletools.jl")
include("emptynumber.jl")
include("nodata.jl")
include("tensorstorage/tensorstorage.jl")
include("tensorstorage/set_types.jl")
include("tensorstorage/default_storage.jl")
Expand Down
2 changes: 1 addition & 1 deletion NDTensors/src/empty/EmptyTensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end
getindex(T::EmptyTensor, I::Integer...) = zero(eltype(T))

function getindex(T::EmptyTensor{Complex{EmptyNumber}}, I::Integer...)
return Complex(NDTensors.EmptyNumber(), NDTensors.EmptyNumber())
return Complex(EmptyNumber(), EmptyNumber())
end

similar(T::EmptyTensor, inds::Tuple) = setinds(T, inds)
Expand Down
31 changes: 0 additions & 31 deletions NDTensors/src/empty/empty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,6 @@

struct EmptyOrder end

#
# Represents a number that can be set to any type.
#

struct EmptyNumber <: Real end

zero(::Type{EmptyNumber}) = EmptyNumber()
zero(n::EmptyNumber) = zero(typeof(n))

# This helps handle a lot of basic algebra, like:
# EmptyNumber() + 2.3 == 2.3
convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T))

# TODO: Should this be implemented?
#Complex(x::Real, ::EmptyNumber) = x

# This is to help define `float(::EmptyNumber) = 0.0`.
# This helps with defining `norm` of `EmptyStorage{EmptyNumber}`.
AbstractFloat(::NDTensors.EmptyNumber) = zero(AbstractFloat)

# Basic arithmetic
(::EmptyNumber + ::EmptyNumber) = EmptyNumber()
(::EmptyNumber - ::EmptyNumber) = EmptyNumber()
(::Number * ::EmptyNumber) = EmptyNumber()
(::EmptyNumber * ::Number) = EmptyNumber()
(::EmptyNumber * ::EmptyNumber) = EmptyNumber()
(::EmptyNumber / ::Number) = EmptyNumber()
(::Number / ::EmptyNumber) = throw(DivideError())
(::EmptyNumber / ::EmptyNumber) = throw(DivideError())
-(::EmptyNumber) = EmptyNumber()

function similartype(StoreT::Type{<:TensorStorage{EmptyNumber}}, ElT::Type)
return set_eltype(StoreT, ElT)
end
Expand Down
30 changes: 30 additions & 0 deletions NDTensors/src/emptynumber.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Represents a number that can be set to any type.
#

struct EmptyNumber <: Real end

zero(::Type{EmptyNumber}) = EmptyNumber()
zero(n::EmptyNumber) = zero(typeof(n))

# This helps handle a lot of basic algebra, like:
# EmptyNumber() + 2.3 == 2.3
convert(::Type{T}, x::EmptyNumber) where {T<:Number} = T(zero(T))

# TODO: Should this be implemented?
#Complex(x::Real, ::EmptyNumber) = x

# This is to help define `float(::EmptyNumber) = 0.0`.
# This helps with defining `norm` of `EmptyStorage{EmptyNumber}`.
AbstractFloat(::EmptyNumber) = zero(AbstractFloat)

# Basic arithmetic
(::EmptyNumber + ::EmptyNumber) = EmptyNumber()
(::EmptyNumber - ::EmptyNumber) = EmptyNumber()
(::Number * ::EmptyNumber) = EmptyNumber()
(::EmptyNumber * ::Number) = EmptyNumber()
(::EmptyNumber * ::EmptyNumber) = EmptyNumber()
(::EmptyNumber / ::Number) = EmptyNumber()
(::Number / ::EmptyNumber) = throw(DivideError())
(::EmptyNumber / ::EmptyNumber) = throw(DivideError())
-(::EmptyNumber) = EmptyNumber()
6 changes: 6 additions & 0 deletions NDTensors/src/nodata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Denotes when a storage type has no data
struct NoData end

size(::NoData) = (0,)
length(::NoData) = 0
fill!(::NoData, ::EmptyNumber) = NoData()
6 changes: 0 additions & 6 deletions NDTensors/src/tensorstorage/tensorstorage.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
abstract type TensorStorage{ElT} <: AbstractVector{ElT} end

# Denotes when a storage type has no data
struct NoData end

size(::NoData) = (0,)
length(::NoData) = 0

data(S::TensorStorage) = S.data

datatype(::Type{<:TensorStorage}) = error("Not implemented")
Expand Down
6 changes: 6 additions & 0 deletions NDTensors/test/emptystorage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ using Test
@test blockoffsets(T) == BlockOffsets{2}()
T = dev(EmptyBlockSparseTensor(Float64, ([1, 1], [1, 1])))
@test blockoffsets(T) == BlockOffsets{2}()

T = dev(EmptyStorage(NDTensors.EmptyNumber))
@test zero(T) isa typeof(T)

T = dev(EmptyTensor(NDTensors.EmptyNumber, (2, 2)))
@test zero(T) isa typeof(T)
end
end
6 changes: 6 additions & 0 deletions test/base/test_emptyitensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ end
@test blockoffsets(A) == NDTensors.BlockOffsets{2}()
end

@testset "zero" for space in (2, [QN(0) => 1, QN(1) => 1])
i = Index(space)
A = ITensor(i', dag(i))
@test NDTensors.tensor(zero(A)) isa typeof(NDTensors.tensor(A))
end

nothing

0 comments on commit 0724830

Please sign in to comment.