Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ixs1 = Interval(x, -2, 2, stamp=1)
for i, j in [(ix, nully), (ix, iy), (iy, ix), (ix, ixs1), (ixs1, ix)]:
try:
i.union(j)
assert False # Shouldn't arrive here
except ValueError:
assert True
except:
# No other types of exception expected
assert False
# Mixed symbolic and non-symbolic
c = Constant(name='c')
ix7 = Interval(x, c, c + 4)
ix8 = Interval(x, c - 1, c + 5)
assert ix7.union(ix8) == Interval(x, c - 1, c + 5)
assert ix8.union(ix7) == Interval(x, c - 1, c + 5)
# Symbolic with properties
s = Scalar(name='s', nonnegative=True)
ix9 = Interval(x, s - 2, s + 2)
ix10 = Interval(x, s - 1, s + 1)
assert ix.union(ix9) == Interval(x, -2, s + 2)
assert ix9.union(ix) == Interval(x, -2, s + 2)
assert ix9.union(ix10) == ix9
assert ix10.union(ix9) == ix9
# Mixed nulls and defined on the same dimension
assert nullx.subtract(ix) == nullx
assert ix.subtract(ix) == Interval(x, 0, 0)
assert ix.subtract(nullx) == ix
ix2 = Interval(x, 4, -4)
ix3 = Interval(x, 6, -6)
# All defined same dimension
assert ix2.subtract(ix) == ix
assert ix.subtract(ix2) == Interval(x, -2, 2)
assert ix3.subtract(ix) == ix2
c = Constant(name='c')
ix4 = Interval(x, c + 2, c + 4)
ix5 = Interval(x, c + 1, c + 5)
# All defined symbolic
assert ix4.subtract(ix5) == Interval(x, 1, -1)
assert ix5.subtract(ix4) == Interval(x, -1, 1)
assert ix5.subtract(ix) == Interval(x, c - 1, c + 7)
def test_intervals_union(self):
nullx = NullInterval(x)
# All nulls
assert nullx.union(nullx) == nullx
ix = Interval(x, -2, 2)
# Mixed nulls and defined
assert nullx.union(ix) == ix
assert ix.union(ix) == ix
assert ix.union(nullx) == ix
ix2 = Interval(x, 1, 4)
ix3 = Interval(x, -3, 6)
assert ix.union(ix2) == Interval(x, -2, 4)
assert ix.union(ix3) == ix3
assert ix2.union(ix3) == ix3
ix4 = Interval(x, 4, 8)
ix5 = Interval(x, -3, -3)
ix6 = Interval(x, -10, -3)
nully = NullInterval(y)
iy = Interval(y, -2, 2)
assert ix.union(ix4) == Interval(x, -2, 8)
assert ix.union(ix5) == Interval(x, -3, 2)
assert ix6.union(ix) == Interval(x, -10, 2)
def test_intervals_union(self):
nullx = NullInterval(x)
# All nulls
assert nullx.union(nullx) == nullx
ix = Interval(x, -2, 2)
# Mixed nulls and defined on the same dimension
assert nullx.union(ix) == ix
assert ix.union(ix) == ix
assert ix.union(nullx) == ix
ix2 = Interval(x, 1, 4)
ix3 = Interval(x, -3, 6)
# All defined overlapping
assert ix.union(ix2) == Interval(x, -2, 4)
assert ix.union(ix3) == ix3
assert ix2.union(ix3) == ix3
ix4 = Interval(x, 4, 8)
ix5 = Interval(x, -3, -3)
ix = Interval(x, -2, 2)
# Mixed nulls and defined
assert nullx.union(ix) == ix
assert ix.union(ix) == ix
assert ix.union(nullx) == ix
ix2 = Interval(x, 1, 4)
ix3 = Interval(x, -3, 6)
assert ix.union(ix2) == Interval(x, -2, 4)
assert ix.union(ix3) == ix3
assert ix2.union(ix3) == ix3
ix4 = Interval(x, 4, 8)
ix5 = Interval(x, -3, -3)
ix6 = Interval(x, -10, -3)
nully = NullInterval(y)
iy = Interval(y, -2, 2)
assert ix.union(ix4) == Interval(x, -2, 8)
assert ix.union(ix5) == Interval(x, -3, 2)
assert ix6.union(ix) == Interval(x, -10, 2)
# The union of non-compatible Intervals isn't possible, and an exception
# is expected
ixs1 = Interval(x, -2, 2, stamp=1)
for i, j in [(ix, nully), (ix, iy), (iy, ix), (ix, ixs1), (ixs1, ix)]:
try:
i.union(j)
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 _rebuild(self):
return Interval(self.dim, self.lower, self.upper)
def lift(self):
return Interval(self.dim, self.lower, self.upper, self.stamp + 1)
def __init__(self, dim, lower, upper, stamp=0):
assert is_integer(lower) or isinstance(lower, Expr)
assert is_integer(upper) or isinstance(upper, Expr)
super(Interval, self).__init__(dim, stamp)
self.lower = lower
self.upper = upper
def flip(self):
return Interval(self.dim, self.upper, self.lower)