def intersection(self, o): if self.is_compatible(o): svl, svu = Vector(self.lower, smart=True), Vector(self.upper, smart=True) ovl, ovu = Vector(o.lower, smart=True), Vector(o.upper, smart=True) return Interval(self.dim, vmax(svl, ovl)[0], vmin(svu, ovu)[0], self.stamp) else: return NullInterval(self.dim)
def union(self, o): if o.is_Null and self.dim is o.dim: return self._rebuild() elif self.is_compatible(o): svl, svu = Vector(self.lower, smart=True), Vector(self.upper, smart=True) ovl, ovu = Vector(o.lower, smart=True), Vector(o.upper, smart=True) return Interval(self.dim, vmin(svl, ovl)[0], vmax(svu, ovu)[0], self.stamp) else: raise ValueError("Cannot compute union of non-compatible Intervals (%s, %s)" % (self, o))
def union(self, o): if o.is_Null and self.dim == o.dim: return self._rebuild() elif self.is_compatible(o): svl, svu = Vector(self.lower, smart=True), Vector(self.upper, smart=True) ovl, ovu = Vector(o.lower, smart=True), Vector(o.upper, smart=True) return Interval(self.dim, vmin(svl, ovl)[0], vmax(svu, ovu)[0], self.stamp) else: return IntervalGroup([self._rebuild(), o._rebuild()])
def distance(self, other, findex=None): """ Compute the distance from ``self`` to ``other``. Parameters ---------- other : TimedAccess The TimedAccess from which the distance is computed. findex : Dimension, optional If supplied, compute the distance only up to and including ``findex``. """ assert isinstance(other, TimedAccess) if not self.rank: return Vector() # Compute distance up to `limit`, ignoring `directions` for the moment if findex is None: findex = self.findices[-1] limit = self.rank else: try: limit = self._cached_findices_index[findex] + 1 except KeyError: raise TypeError( "Cannot compute distance as `findex` not in `findices`") ret = [] for n, (i, o) in enumerate(zip(self[:limit], other)): try: iit = self.itintervals[n] oit = other.itintervals[n] except IndexError: # E.g., self=R<u,[t+1, ii_src_0+1, ii_src_1+2]> # itintervals=(time, p_src) break if iit.direction is oit.direction and iit.interval == oit.interval: if iit.direction is Backward: # Backward direction => flip the sign ret.append(o - i) else: ret.append(i - o) else: # Mismatching `itinterval` => Infinity ret.append(S.Infinity) break return Vector(*ret)
def distance(self, other, findex=None): if not self.rank: return Vector() findex = findex or self.findices[-1] ret = [] for i, sd, od in zip(self.findices, self.directions, other.directions): if sd == od: ret = list(super(TimedAccess, self).distance(other, i)) if i == findex: break else: ret.append(S.Infinity) break directions = self.directions[:self._cached_findices_index[i] + 1] assert len(directions) == len(ret) return Vector(*[(-i) if d == Backward else i for i, d in zip(ret, directions)])
def section(self, findices): """ Return a view of ``self`` in which the slots corresponding to the provided ``findices`` have been zeroed. """ return Vector(*[d if i not in as_tuple(findices) else 0 for d, i in zip(self, self.findices)])
def distance(self, other, findex=None, view=None): """ Compute the distance from ``self`` to ``other``. Parameters ---------- other : IterationInstance The IterationInstance from which the distance is computed. findex : Dimension, optional If supplied, compute the distance only up to and including ``findex``. view : list of Dimension, optional If supplied, project the distance along these Dimensions. """ if not isinstance(other, IterationInstance): raise TypeError("Cannot compute distance from obj of type %s", type(other)) if self.findices != other.findices: raise TypeError("Cannot compute distance due to mismatching `findices`") if findex is not None: try: limit = self._cached_findices_index[findex] + 1 except KeyError: raise TypeError("Cannot compute distance as `findex` not in `findices`") else: limit = self.rank distance = super(IterationInstance, self).distance(other)[:limit] if view is None: return distance else: proj = [d for d, i in zip(distance, self.findices) if i in as_tuple(view)] return Vector(*proj)
def distance(self, other, findex=None): """ Compute the distance from ``self`` to ``other``. Parameters ---------- other : TimedAccess The TimedAccess from which the distance is computed. findex : Dimension, optional If supplied, compute the distance only up to and including ``findex``. """ assert isinstance(other, TimedAccess) if not self.rank: return Vector() # Compute distance up to `limit`, ignoring `directions` for the moment if findex is None: findex = self.findices[-1] limit = self.rank + 1 else: try: limit = self._cached_findices_index[findex] + 1 except KeyError: raise TypeError( "Cannot compute distance as `findex` not in `findices`") distance = list(super(TimedAccess, self).distance(other)[:limit]) # * If mismatching `directions`, set the distance to infinity # * If direction is Backward, flip the sign ret = [] for i, d0, d1 in zip(distance, self.directions, other.directions): if d0 is d1: ret.append(-i if d0 is Backward else i) else: ret.append(S.Infinity) break return Vector(*ret)
def distance(self, other): """ Compute the distance from ``self`` to ``other``. Parameters ---------- other : TimedAccess The TimedAccess w.r.t. which the distance is computed. """ ret = [] for sit, oit in zip(self.itintervals, other.itintervals): n = len(ret) try: sai = self.aindices[n] oai = other.aindices[n] except IndexError: # E.g., `self=R<f,[x]>` and `self.itintervals=(x, i)` break try: if not (sit == oit and sai.root is oai.root): # E.g., `self=R<f,[x + 2]>` and `other=W<f,[i + 1]>` # E.g., `self=R<f,[x]>`, `other=W<f,[x + 1]>`, # `self.itintervals=(x<0>,)` and `other.itintervals=(x<1>,)` ret.append(S.Infinity) break except AttributeError: # E.g., `self=R<f,[cy]>` and `self.itintervals=(y,)` => `sai=None` pass ai = sai fi = self.findices[n] if not ai or ai._defines & sit.dim._defines: # E.g., `self=R<f,[t + 1, x]>`, `self.itintervals=(time, x)` and `ai=t` if sit.direction is Backward: ret.append(other[n] - self[n]) else: ret.append(self[n] - other[n]) elif fi in sit.dim._defines: # E.g., `self=R<u,[t+1, ii_src_0+1, ii_src_1+2]>` and `fi=p_src` (`n=1`) ret.append(S.Infinity) break return Vector(*ret)