Skip to content

Commit

Permalink
Further update constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Nov 10, 2024
1 parent f140cf0 commit e255bea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 20 additions & 4 deletions src/tensors/blocktensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@ 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"
return new{TT,E,S,N₁,N₂,N}(data, space)
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}
Expand Down
23 changes: 19 additions & 4 deletions src/tensors/sparseblocktensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,38 @@ 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"
return new{TT,E,S,N₁,N₂,N}(data, space)
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}
Expand Down

0 comments on commit e255bea

Please sign in to comment.