ITensors.jl icon indicating copy to clipboard operation
ITensors.jl copied to clipboard

Add `blockrange` function

Open mtfishman opened this issue 4 years ago • 1 comments

Add a function blockrange which returns the range of Index values of a specified block or QN. For example:

# Get the range of values of a block
blockrange(i::Index, b::Block) = _blockrange(i, Int(b))
function _blockrange(i::Index, b)
  start = 1
  for j in 1:b-1
    start += ITensors.blockdim(i, j)
  end
  return start:start+ITensors.blockdim(i, b)-1
end

function blockrange(i::Index, find_qn::QN)
  # The block the QN is in
  b = ITensors.findfirstblock(x -> qn(x) == find_qn, i)
  return blockrange(i, b)
end

The version taking QN may be ambiguous since multiple blocks could have the same QN.

This could also work for an ITensor, where a block or a tuple of QNs in each dimension is specified and it returns a CartesianIndices object for the range of values of the block.

See the discussion here for a motivating example: http://itensor.org/support/2879/extract-project-iqtensor-on-a-qn-subspace

mtfishman avatar Apr 23 '21 18:04 mtfishman

I think better names for this could be eachindex to match with Julia's usage of index or eachval to match with the ITensor usage of val as a value an Index gets set to.

mtfishman avatar Apr 28 '21 13:04 mtfishman