def test_intersects(): assert Interval.closed(1, 3).intersects(Interval.closed(2, 3)) assert Interval.closed(1, 3).intersects((2, 3)) assert Interval.closed(1, 3).intersects((1, 3)) assert Interval.closed(1, 3).intersects(Interval.open(1, 3)) assert Interval.closed(1, 3).intersects(Interval.closed(3, 4)) assert not Interval.closed(1, 3).intersects(Interval.open(3, 4)) assert not Interval.open(1, 3).intersects(Interval.closed(3, 4)) assert Interval.point(3).intersects(Interval.closed(3, 4)) assert Interval.point(3).intersects(Interval.closed(1, 3)) assert not Interval.point(3).intersects(Interval.open(3, 4)) assert not Interval.point(3).intersects(Interval.open(1, 3)) assert Interval.closed(1, 3).intersects(Interval.closed(0, 1)) assert not Interval.closed(1, 3).intersects(Interval.open(0, 1)) assert not Interval.open(1, 3).intersects(Interval.closed(0, 1)) assert not Interval.closed(1, 3).intersects(Interval.closed(4, 5)) assert not Interval.closed(1, 3).intersects(Interval.closed(-2, 0)) assert not Interval.closed(1, 3).intersects(Interval.empty()) assert Interval.closed(1, 3).intersects(Interval.infinite()) assert not Interval.point(1).intersects(Interval.open_closed(1, 2))
def replace(self, point, or_append=False): if not self.domain.contains(point[0]): if or_append: return self.append(point) else: raise Exception( 'Attempting to replace point outside of bounds') update_domain = Interval.point(point[0]) self.begin_update(update_domain) nearest_i = int(math.ceil(self.x_index(point[0]))) nearest_p = self._points[nearest_i] if point[0] != nearest_p[0]: if self._force_equally_spaced: raise Exception( 'Attempting to replace point {} between existing points. The nearest is {}.' .format(point, nearest_p)) else: self._points.insert(nearest_i, point) self._is_equally_spaced = False else: self._points[nearest_i] = point self._did_change_points() self.end_update(update_domain)
def update_interval(self): len_trend_points = len(self.trend_points) if self._line is None: if len_trend_points == 1: self.search_interval = Interval.point(self.trend_points[0][0]) else: self.search_interval = Interval.empty() return p0, p1 = self._line if p0[0] == p1[0]: # vertical line self.search_interval = Interval.point(p0[0]) return if self.search_length_rel <= 0: self.search_interval = Interval.closed(p0[0], p1[0]) return x_min = p0[0] x_max = p1[0] intersection = self.intersections[0] if len( self.intersections) != 0 else None if intersection is not None and intersection[0] <= x_min: intersection = None if intersection is None or len_trend_points >= self.min_points: # search ahead x_length = (x_max - x_min) * self.search_length_rel else: # not enought points to go through intersection x_length = intersection[0] - x_min # if intersection is None: # # search ahead # x_length = (x_max - x_min) * self.search_length_rel # elif len_trend_points >= self.min_points: # # search ahead of intersection # x_length = (intersection[0] - x_min) * self.search_length_int_rel # else: # # not enought points to go through intersection # x_length = intersection[0] - x_min x_max = x_min + x_length self.search_interval = Interval.closed_open(x_min, x_max)
def test_duplicate_multiple_nested_function_update(): points = Points([]) f = (points * 0.5 + points * 0.5) * 2 points.append((0, 1)) assert f.is_updating is False assert f.domain == Interval.point(0) assert f.y(0) == 2 points.append((1, 1)) assert f.is_updating is False assert f.domain == Interval(0, 1) assert f.y(1) == 2
def test_duplicate_function_update(): points = Points([]) f = points + points points.append((0, 1)) assert points.is_updating is False assert f.is_updating is False assert f.domain == Interval.point(0) assert f.y(0) == 2 points.append((1, 1)) assert points.is_updating is False assert f.is_updating is False assert f.domain == Interval(0, 1) assert f.y(1) == 2
def test_intersection_inf(): assert Interval.intersection([Interval.gte(100), (98, 101)]) == (100, 101) assert Interval.intersection([Interval.point(100), Interval.open_closed(100, 101)]) == Interval.empty()
def test_single_point(): f = Points([(0, 0)]).extension('tangent', start=True, end=True) assert f.domain == Interval.point(0) assert f(0) == 0 assert f(1) is None