From e255beae36f57dbfb4b723ac68fea210353a97fe Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Sun, 10 Nov 2024 08:30:47 -0500 Subject: [PATCH] Further update constructors --- src/tensors/blocktensor.jl | 24 ++++++++++++++++++++---- src/tensors/sparseblocktensor.jl | 23 +++++++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/tensors/blocktensor.jl b/src/tensors/blocktensor.jl index 7977814..d37026c 100644 --- a/src/tensors/blocktensor.jl +++ b/src/tensors/blocktensor.jl @@ -9,16 +9,16 @@ struct BlockTensorMap{TT<:AbstractTensorMap,E,S,N₁,N₂,N} <: space::TensorMapSumSpace{S,N₁,N₂} # uninitialized constructor - function BlockTensorMap{TT}( + function BlockTensorMap{TT,E,S,N₁,N₂,N}( ::UndefBlocksInitializer, space::TensorMapSumSpace{S,N₁,N₂} - ) where {E,S,N₁,N₂,TT<:AbstractTensorMap{E,S,N₁,N₂}} - N = N₁ + N₂ + ) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} + @assert N₁ + N₂ == N "BlockTensorMap: data has wrong number of dimensions" data = Array{TT,N}(undef, size(SumSpaceIndices(space))) return new{TT,E,S,N₁,N₂,N}(data, space) end # constructor from data - function BlockTensorMap{TT}( + function BlockTensorMap{TT,E,S,N₁,N₂,N}( data::Array{TT,N}, space::TensorMapSumSpace{S,N₁,N₂} ) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} @assert N₁ + N₂ == N "BlockTensorMap: data has wrong number of dimensions" @@ -26,6 +26,22 @@ struct BlockTensorMap{TT<:AbstractTensorMap,E,S,N₁,N₂,N} <: end end +# uninitialized constructor +function BlockTensorMap{TT}( + ::UndefBlocksInitializer, space::TensorMapSumSpace{S,N₁,N₂} +) where {E,S,N₁,N₂,TT<:AbstractTensorMap{E,S,N₁,N₂}} + N = N₁ + N₂ + return BlockTensorMap{TT,E,S,N₁,N₂,N}(undef_blocks, space) +end + +# constructor from data +function BlockTensorMap{TT}( + data::Array{TT,N}, space::TensorMapSumSpace{S,N₁,N₂} +) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} + @assert N₁ + N₂ == N "BlockTensorMap: data has wrong number of dimensions" + return BlockTensorMap{TT,E,S,N₁,N₂,N}(data, space) +end + function blocktensormaptype(::Type{S}, N₁::Int, N₂::Int, ::Type{T}) where {S,T} TT = tensormaptype(S, N₁, N₂, T) return BlockTensorMap{TT} diff --git a/src/tensors/sparseblocktensor.jl b/src/tensors/sparseblocktensor.jl index 61b7964..0e99526 100644 --- a/src/tensors/sparseblocktensor.jl +++ b/src/tensors/sparseblocktensor.jl @@ -9,16 +9,16 @@ struct SparseBlockTensorMap{TT<:AbstractTensorMap,E,S,N₁,N₂,N} <: space::TensorMapSumSpace{S,N₁,N₂} # uninitialized constructor - function SparseBlockTensorMap{TT}( + function SparseBlockTensorMap{TT,E,S,N₁,N₂,N}( ::UndefBlocksInitializer, space::TensorMapSumSpace{S,N₁,N₂} - ) where {E,S,N₁,N₂,TT<:AbstractTensorMap{E,S,N₁,N₂}} - N = N₁ + N₂ + ) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} + @assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions" data = Dict{CartesianIndex{N},TT}() return new{TT,E,S,N₁,N₂,N}(data, space) end # constructor from data - function SparseBlockTensorMap{TT}( + function SparseBlockTensorMap{TT,E,S,N₁,N₂,N}( data::Dict{CartesianIndex{N},TT}, space::TensorMapSumSpace{S,N₁,N₂} ) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} @assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions" @@ -26,6 +26,21 @@ struct SparseBlockTensorMap{TT<:AbstractTensorMap,E,S,N₁,N₂,N} <: end end +# uninitialized constructor +function SparseBlockTensorMap{TT}( + ::UndefBlocksInitializer, space::TensorMapSumSpace{S,N₁,N₂} +) where {E,S,N₁,N₂,TT<:AbstractTensorMap{E,S,N₁,N₂}} + N = N₁ + N₂ + return SparseBlockTensorMap{TT,E,S,N₁,N₂,N}(data, space) +end + +# constructor from data +function SparseBlockTensorMap{TT}( + data::Dict{CartesianIndex{N},TT}, space::TensorMapSumSpace{S,N₁,N₂} +) where {E,S,N₁,N₂,N,TT<:AbstractTensorMap{E,S,N₁,N₂}} + return SparseBlockTensorMap{TT,E,S,N₁,N₂,N}(data, space) +end + function sparseblocktensormaptype(::Type{S}, N₁::Int, N₂::Int, ::Type{T}) where {S,T} TT = tensormaptype(S, N₁, N₂, T) return SparseBlockTensorMap{TT}