def test_imageset_intersect_real(): assert (imageset(Lambda(n, n + (n - 1)*(n + 1)*I), S.Integers).intersect(S.Reals) == FiniteSet(-1, 1)) s = ImageSet(Lambda(n, -I*(I*(2*pi*n - pi/4) + log(Abs(sqrt(-I))))), S.Integers) assert s.intersect(S.Reals) == imageset(Lambda(n, 2*n*pi - pi/4), S.Integers)
def test_imageset_intersection_real(): assert (imageset(Lambda(n, n + (n - 1)*(n + 1)*I), S.Integers).intersection(S.Reals) == FiniteSet(-1, 1)) s = ImageSet(Lambda(n, -I*(I*(2*pi*n - pi/4) + log(Abs(sqrt(-I))))), S.Integers) assert s.intersection(S.Reals) == imageset(Lambda(n, 2*n*pi - pi/4), S.Integers)
def _intersect(self, other): from diofant import Dummy from diofant.solvers.diophantine import diophantine from diofant.sets.sets import imageset if self.base_set is S.Integers: if isinstance(other, ImageSet) and other.base_set is S.Integers: f, g = self.lamda.expr, other.lamda.expr n, m = self.lamda.variables[0], other.lamda.variables[0] # Diophantine sorts the solutions according to the alphabetic # order of the variable names, since the result should not depend # on the variable name, they are replaced by the dummy variables # below a, b = Dummy('a'), Dummy('b') f, g = f.subs(n, a), g.subs(m, b) solns_set = diophantine(f - g) if solns_set == set(): return EmptySet() solns = list(diophantine(f - g)) if len(solns) == 1: t = list(solns[0][0].free_symbols)[0] else: return # since 'a' < 'b' return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers) if other == S.Reals: from diofant.solvers.diophantine import diophantine from diofant.core.function import expand_complex if len(self.lamda.variables ) > 1 or self.base_set is not S.Integers: return f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, integer=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) sols = list(diophantine(im, n_)) if not sols: return S.EmptySet elif all(s[0].has(n_) is False for s in sols): s = FiniteSet(*[s[0] for s in sols]) elif len(sols) == 1 and sols[0][0].has(n_): s = imageset(Lambda(n_, sols[0][0]), S.Integers) else: return return imageset(Lambda(n_, re), self.base_set.intersect(s))
def test_infinitely_indexed_set_2(): a = Symbol('a', integer=True) assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(n, n + a), S.Integers) assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(n, -n + a), S.Integers) assert imageset(Lambda(n, -6*n), S.Integers) == ImageSet(Lambda(n, 6*n), S.Integers) assert imageset(Lambda(n, 2*n + pi), S.Integers) == ImageSet(Lambda(n, 2*n + pi), S.Integers) assert imageset(Lambda(n, pi*n + pi), S.Integers) == ImageSet(Lambda(n, pi*n + pi), S.Integers) assert imageset(Lambda(n, exp(n)), S.Integers) != imageset(Lambda(n, n), S.Integers)
def test_infinitely_indexed_set_1(): assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(m, m), S.Integers) assert (imageset(Lambda(n, 2 * n), S.Integers).intersection( imageset(Lambda(m, 2 * m + 1), S.Integers)) == EmptySet()) assert (imageset(Lambda(n, 2 * n), S.Integers).intersection( imageset(Lambda(n, 2 * n + 1), S.Integers)) == EmptySet()) assert (imageset(Lambda(m, 2 * m), S.Integers).intersection( imageset(Lambda(n, 3 * n), S.Integers)) == ImageSet(Lambda(t, 6 * t), S.Integers))
def test_infinitely_indexed_set_1(): assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(m, m), S.Integers) assert (imageset(Lambda(n, 2*n), S.Integers).intersection(imageset(Lambda(m, 2*m + 1), S.Integers)) == EmptySet()) assert (imageset(Lambda(n, 2*n), S.Integers).intersection(imageset(Lambda(n, 2*n + 1), S.Integers)) == EmptySet()) assert (imageset(Lambda(m, 2*m), S.Integers).intersection(imageset(Lambda(n, 3*n), S.Integers)) == ImageSet(Lambda(t, 6*t), S.Integers))
def test_integers(): Z = S.Integers assert 5 in Z assert -5 in Z assert 5.5 not in Z zi = iter(Z) a, b, c, d = next(zi), next(zi), next(zi), next(zi) assert (a, b, c, d) == (0, 1, -1, 2) assert isinstance(a, Basic) assert Z.intersection(Interval(-5, 5)) == Range(-5, 6) assert Z.intersection(Interval(-5, 5, True, True)) == Range(-4, 5) assert Z.intersection(Set(x)) == Intersection(Z, Set(x), evaluate=False) assert Z.inf == -oo assert Z.sup == oo assert Z.boundary == Z assert imageset(Lambda((x, y), x*y), Z) == ImageSet(Lambda((x, y), x*y), Z)
def test_integers(): Z = S.Integers assert 5 in Z assert -5 in Z assert 5.5 not in Z zi = iter(Z) a, b, c, d = next(zi), next(zi), next(zi), next(zi) assert (a, b, c, d) == (0, 1, -1, 2) assert isinstance(a, Basic) assert Z.intersection(Interval(-5, 5)) == Range(-5, 6) assert Z.intersection(Interval(-5, 5, True, True)) == Range(-4, 5) assert Z.intersection(Set(x)) == Intersection(Z, Set(x), evaluate=False) assert Z.inf == -oo assert Z.sup == oo assert Z.boundary == Z assert imageset(Lambda((x, y), x * y), Z) == ImageSet(Lambda((x, y), x * y), Z)
def test_ImageSet_simplification(): assert imageset(Lambda(n, n), S.Integers) == S.Integers assert (imageset(Lambda(n, sin(n)), imageset(Lambda(m, tan(m)), S.Integers)) == imageset(Lambda(m, sin(tan(m))), S.Integers))
def test_infinitely_indexed_set_3(): assert imageset(Lambda(n, 2*n + 1), S.Integers) == imageset(Lambda(n, 2*n - 1), S.Integers) assert imageset(Lambda(n, 3*n + 2), S.Integers) == imageset(Lambda(n, 3*n - 1), S.Integers)
def test_infinitely_indexed_failed_diophantine(): assert (imageset(Lambda(m, 2*pi*m), S.Integers).intersect(imageset(Lambda(n, 3*pi*n), S.Integers)) == ImageSet(Lambda(t, -6*pi*t), S.Integers))
def test_infinitely_indexed_diophantine(): assert (imageset(Lambda(m, 2*pi*m), S.Integers).intersection(imageset(Lambda(n, 3*pi*n), S.Integers)) == ImageSet(Lambda(t, 6*pi*t), S.Integers))
def test_image_is_ImageSet(): assert isinstance(imageset(x, sqrt(sin(x)), Range(5)), ImageSet)