ITensor / NDTensors.jl

A Julia package for n-dimensional sparse tensors.
Apache License 2.0
27 stars 7 forks source link

Introduce `setstorage` and `setinds` #71

Closed mtfishman closed 3 years ago

mtfishman commented 3 years ago

It would be helpful to have generic functions setstorage and setinds that create a copy of a tensor with the specified storage or indices. For example, right now complex(::Tensor) is defined as:

complex(T::Tensor) = tensor(complex(store(T)), inds(T))

which could be replaced by:

complex(T::Tensor) = setstorage(T, complex(store(T)))

Also a note that I would like to deprecate store(T) in favor of storage(T).

A similar generic code pattern would be to add setdata for storage types. This could help with making generic code across different storage types. For example right now there is a generic complex defined for all storage types:

Base.complex(S::T) where {T <: TensorStorage} = complex(T)(complex(data(S)))

but also more specialized versions for sparse storage types:

complex(D::BlockSparse{T}) where {T} =
  BlockSparse{complex(T)}(complex(data(D)), blockoffsets(D))

Many functions like this only act on the data part of the storage and not the block offset list, so could be replaced by a single generic version:

Base.complex(S::T) where {T <: TensorStorage} = setdata(T, complex(data(S)))

where the generic function setdata would have to be defined for each storage type.