Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions docs/src/lib/tensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,11 @@ subblock
subblocks
```

To access the data associated with a specific fusion tree pair, you can use:
One can access the data of an `AbstractTensorMap` in multiple ways, its availability depending on the sector type of the tensor.
In particular, the data of a tensor `t` can be accessed by specifying the fusion tree pair, the outcoming sectors if `FusionStyle(sectortype(t)) isa UniqueFusion`, or by multidimensional array indexing if `sectortype(t) == Trivial`.
```@docs
Base.getindex(::AbstractTensorMap, ::FusionTree, ::FusionTree)
Base.setindex!(::AbstractTensorMap, ::Any, ::FusionTree, ::FusionTree)
```

For a tensor `t` with `FusionStyle(sectortype(t)) isa UniqueFusion`, fusion trees are completely determined by the outcoming sectors, and the data can be accessed in a more straightforward way:
```@docs
Base.getindex(::AbstractTensorMap, ::Tuple{I,Vararg{I}}) where {I<:Sector}
```

For tensor `t` with `sectortype(t) == Trivial`, the data can be accessed and manipulated directly as multidimensional arrays:
```@docs
Base.getindex(::AbstractTensorMap)
Base.getindex(::AbstractTensorMap, ::Vararg{SliceIndex})
Base.setindex!(::AbstractTensorMap, ::Any, ::Vararg{SliceIndex})
Base.getindex(::AbstractTensorMap, args...)
Base.setindex!(::AbstractTensorMap, args...)
```

The tensor data can also be filled with random numbers via
Expand Down
42 changes: 19 additions & 23 deletions src/tensors/abstracttensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Return a view into the data of `t` corresponding to the splitting - fusion tree
`(f₁, f₂)`. In particular, this is an `AbstractArray{T}` with `T = scalartype(t)`, of size
`(dims(codomain(t), f₁.uncoupled)..., dims(codomain(t), f₂.uncoupled)...)`.

Whenever `FusionStyle(sectortype(t)) isa UniqueFusion` , it is also possible to provide only
Whenever `FusionStyle(sectortype(t)) isa UniqueFusion`, it is also possible to provide only
the external `sectors`, in which case the fusion tree pair will be constructed automatically.
"""

Expand Down Expand Up @@ -496,8 +496,18 @@ $_doc_subblock
As a result, modifying the view will modify the data in the tensor.

See also [`subblock`](@ref), [`subblocks`](@ref) and [`fusiontrees`](@ref).
""" Base.getindex(::AbstractTensorMap, ::Tuple{I, Vararg{I}}) where {I <: Sector},
Base.getindex(::AbstractTensorMap, ::FusionTree, ::FusionTree)

Base.getindex(t::AbstractTensorMap, indices::Vararg{Int})
t[indices]

Return a view into the data slice of `t` corresponding to `indices`, by slicing the
`StridedViews.StridedView` into the full data array.

Base.getindex(t::AbstractTensorMap)
t[]

Return a view into the data of `t` as a `StridedViews.StridedView` of size `dims(t)`.
""" Base.getindex(::AbstractTensorMap, args...)

@inline Base.getindex(t::AbstractTensorMap, sectors::Tuple{I, Vararg{I}}) where {I <: Sector} =
subblock(t, sectors)
Expand All @@ -514,8 +524,12 @@ Copies `v` into the data slice of `t` corresponding to the splitting - fusion tr
By default, `v` can be any object that can be copied into the view associated with `t[f₁, f₂]`.

See also [`subblock`](@ref), [`subblocks`](@ref) and [`fusiontrees`](@ref).
""" Base.setindex!(::AbstractTensorMap, ::Any, ::Tuple{I, Vararg{I}}) where {I <: Sector},
Base.setindex!(::AbstractTensorMap, ::Any, ::FusionTree, ::FusionTree)

Base.setindex!(t::AbstractTensorMap, v, indices::Vararg{Int})
t[indices] = v

Assigns `v` to the data slice of `t` corresponding to `indices`.
""" Base.setindex!(::AbstractTensorMap, args...)

@inline Base.setindex!(t::AbstractTensorMap, v, sectors::Tuple{I, Vararg{I}}) where {I <: Sector} =
copy!(subblock(t, sectors), v)
Expand All @@ -530,25 +544,13 @@ using TensorKit.Strided: SliceIndex
# TODO: should we allow range indices as well
# TODO 2: should we enable this for (abelian) symmetric tensors with some CUDA like `allowscalar` flag?
# TODO 3: should we then also allow at least `getindex` for nonabelian tensors
"""
Base.getindex(t::AbstractTensorMap, indices::Vararg{Int})
t[indices]

Return a view into the data slice of `t` corresponding to `indices`, by slicing the
`StridedViews.StridedView` into the full data array.
"""
@inline function Base.getindex(t::AbstractTensorMap, indices::Vararg{SliceIndex})
data = t[trivial_fusiontree(t)...]
@boundscheck checkbounds(data, indices...)
@inbounds v = data[indices...]
return v
end
"""
Base.setindex!(t::AbstractTensorMap, v, indices::Vararg{Int})
t[indices] = v

Assigns `v` to the data slice of `t` corresponding to `indices`.
"""
@inline function Base.setindex!(t::AbstractTensorMap, v, indices::Vararg{SliceIndex})
data = t[trivial_fusiontree(t)...]
@boundscheck checkbounds(data, indices...)
Expand All @@ -558,12 +560,6 @@ end

# TODO : probably deprecate the following
# For a tensor with trivial symmetry, allow no argument indexing
"""
Base.getindex(t::AbstractTensorMap)
t[]

Return a view into the data of `t` as a `StridedViews.StridedView` of size `dims(t)`.
"""
@inline function Base.getindex(t::AbstractTensorMap)
return t[trivial_fusiontree(t)...]
end
Expand Down
Loading