Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _copy_helper(self, index: "Index", copied_index: "Index") -> None:
"""
Helper function for copy
"""
if index.left_child != None:
left_copy = Index(
charges=copy.copy(index.left_child.charges),
flow=copy.copy(index.left_child.flow),
name=copy.copy(index.left_child.name))
copied_index.left_child = left_copy
self._copy_helper(index.left_child, left_copy)
if index.right_child != None:
right_copy = Index(
charges=copy.copy(index.right_child.charges),
flow=copy.copy(index.right_child.flow),
name=copy.copy(index.right_child.name))
copied_index.right_child = right_copy
self._copy_helper(index.right_child, right_copy)
def _copy_helper(self, index: "Index", copied_index: "Index") -> None:
"""
Helper function for copy
"""
if index.left_child != None:
left_copy = Index(
charges=copy.copy(index.left_child.charges),
flow=copy.copy(index.left_child.flow),
name=copy.copy(index.left_child.name))
copied_index.left_child = left_copy
self._copy_helper(index.left_child, left_copy)
if index.right_child != None:
right_copy = Index(
charges=copy.copy(index.right_child.charges),
flow=copy.copy(index.right_child.flow),
name=copy.copy(index.right_child.name))
copied_index.right_child = right_copy
self._copy_helper(index.right_child, right_copy)
flow: Optional[int] = 1) -> Index:
"""
Fuse two consecutive indices (legs) of a symmetric tensor.
Args:
left_index: A tensor Index.
right_index: A tensor Index.
flow: An optional flow of the resulting `Index` object.
Returns:
Index: The result of fusing `index1` and `index2`.
"""
#Fuse the charges of the two indices
if left_index is right_index:
raise ValueError(
"index1 and index2 are the same object. Can only fuse distinct objects")
return Index(
charges=None, flow=flow, left_child=left_index, right_child=right_index)
def copy(self):
"""
Returns:
Index: A deep copy of `Index`. Note that all children of
`Index` are copied as well.
"""
index_copy = Index(
charges=self._charges.copy(), flow=copy.copy(self.flow), name=self.name)
self._copy_helper(self, index_copy)
return index_copy
print(A.shape) #prints (6,6,6)
A.reshape((2,3,6,6)) #raises ValueError
```
raises a `ValueError` since (2,3,6,6)
is incompatible with the elementary shape (6,6,6) of the tensor.
Args:
tensor: A symmetric tensor.
shape: The new shape. Can either be a list of `Index`
or a list of `int`.
Returns:
BlockSparseTensor: A new tensor reshaped into `shape`
"""
dense_shape = []
for s in shape:
if isinstance(s, Index):
dense_shape.append(s.dimension)
else:
dense_shape.append(s)
# a few simple checks
if np.prod(dense_shape) != np.prod(self.dense_shape):
raise ValueError("A tensor with {} elements cannot be "
"reshaped into a tensor with {} elements".format(
np.prod(self.shape), np.prod(dense_shape)))
#keep a copy of the old indices for the case where reshaping fails
#FIXME: this is pretty hacky!
index_copy = [i.copy() for i in self.indices]
def raise_error():
#if this error is raised then `shape` is incompatible
#with the elementary indices. We then reset the shape