def _get_slices(self): if self._tensor.indices is None: # Not using index ranges return tuple([slice(None)] * len(self._tensor.shape)) else: # make sure all of the indices are well-known... if len(self.indices) != len(self._tensor.indices): raise IndexError('dimension mismatch ({} != {})'.format(self.indices, self._tensor.indices)) if self._has_only_known_indices(): slices = [] for aidx, myidx in zip(self.indices, self._tensor.indices): arange = self._tensor.index_range_set.known_ranges[aidx] myrange = self._tensor.index_range_set.known_ranges[myidx] if arange is myrange: slices.append(slice(None)) elif is_subrange(arange, myrange): slices.append(arange.slice_in(myrange)) return tuple(slices) else: badidx = [self.indices[i] for i in xrange(len(self.indices)) if not is_subrange( self._tensor.index_range_set.known_ranges[self.indices[i]], self._tensor.index_range_set.known_ranges[self._tensor.indices[i]] ) ] raise IndexError("Index '{0[0]}' unknown index range used on a tensor '{1}' with indices from" " well-defined ranges. Probably a typo? If not, check your Tensor and" " IndexRange constructors.".format( badidx, self._tensor.name if self._tensor.name is not None else "(unnamed tensor)" ))
def _has_only_known_indices(self): if self._tensor.index_range_set is None: return False else: return all( is_subrange( self._tensor.index_range_set.known_ranges[self.indices[i]], self._tensor.index_range_set.known_ranges[self._tensor.indices[i]] ) for i in xrange(len(self.indices)))