def test_imageset_intersect_interval(): from sympy.abc import n f1 = ImageSet(Lambda(n, n*pi), S.Integers) f2 = ImageSet(Lambda(n, 2*n), Interval(0, pi)) f3 = ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers) # complex expressions f4 = ImageSet(Lambda(n, n*I*pi), S.Integers) f5 = ImageSet(Lambda(n, 2*I*n*pi + pi/2), S.Integers) # non-linear expressions f6 = ImageSet(Lambda(n, log(n)), S.Integers) f7 = ImageSet(Lambda(n, n**2), S.Integers) f8 = ImageSet(Lambda(n, Abs(n)), S.Integers) f9 = ImageSet(Lambda(n, exp(n)), S.Naturals0) assert f1.intersect(Interval(-1, 1)) == FiniteSet(0) assert f2.intersect(Interval(1, 2)) == Interval(1, 2) assert f3.intersect(Interval(-1, 1)) == S.EmptySet assert f3.intersect(Interval(-5, 5)) == FiniteSet(-3*pi/2, pi/2) assert f4.intersect(Interval(-1, 1)) == FiniteSet(0) assert f4.intersect(Interval(1, 2)) == S.EmptySet assert f5.intersect(Interval(0, 1)) == S.EmptySet assert f6.intersect(Interval(0, 1)) == FiniteSet(S.Zero, log(2)) assert f7.intersect(Interval(0, 10)) == Intersection(f7, Interval(0, 10)) assert f8.intersect(Interval(0, 2)) == Intersection(f8, Interval(0, 2)) assert f9.intersect(Interval(1, 2)) == Intersection(f9, Interval(1, 2))
def test_ImageSet(): assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == FiniteSet(y) squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert harmonics.is_iterable c = ComplexRegion(Interval(1, 3) * Interval(1, 3)) assert Tuple(2, 6) in ImageSet(Lambda((x, y), (x, 2 * y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda((x, y), (x, 1 / y)), c) assert Tuple(2, -2) not in ImageSet(Lambda((x, y), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda((x, y), (x, -2)), c) c3 = Interval(3, 7) * Interval(8, 11) * Interval(5, 9) assert Tuple(8, 3, 9) in ImageSet(Lambda((t, y, x), (y, t, x)), c3) assert Tuple(S(1) / 8, 3, 9) in ImageSet(Lambda((t, y, x), (1 / y, t, x)), c3) assert 2 / pi not in ImageSet(Lambda((x, y), 2 / x), c) assert 2 / S(100) not in ImageSet(Lambda((x, y), 2 / x), c) assert 2 / S(3) in ImageSet(Lambda((x, y), 2 / x), c)
def test_ImageSet(): assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == FiniteSet(y) squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1/x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert harmonics.is_iterable c = ComplexRegion(Interval(1, 3)*Interval(1, 3)) assert Tuple(2, 6) in ImageSet(Lambda((x, y), (x, 2*y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda((x, y), (x, 1/y)), c) assert Tuple(2, -2) not in ImageSet(Lambda((x, y), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda((x, y), (x, -2)), c) c3 = Interval(3, 7)*Interval(8, 11)*Interval(5, 9) assert Tuple(8, 3, 9) in ImageSet(Lambda((t, y, x), (y, t, x)), c3) assert Tuple(S(1)/8, 3, 9) in ImageSet(Lambda((t, y, x), (1/y, t, x)), c3) assert 2/pi not in ImageSet(Lambda((x, y), 2/x), c) assert 2/S(100) not in ImageSet(Lambda((x, y), 2/x), c) assert 2/S(3) in ImageSet(Lambda((x, y), 2/x), c)
def test_imageset_intersection(): n = Dummy() 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 * pi * n + pi * Rational(7, 4)), S.Integers)
def test_imageset_intersect_real(): from sympy import I from sympy.abc import n assert imageset(Lambda(n, n + (n - 1)*(n + 1)*I), S.Integers).intersect(S.Reals) == FiniteSet(-1, 1) im = (n - 1)*(n + S.Half) assert imageset(Lambda(n, n + im*I), S.Integers ).intersect(S.Reals) == FiniteSet(1) assert imageset(Lambda(n, n + im*(n + 1)*I), S.Naturals0 ).intersect(S.Reals) == FiniteSet(1) assert imageset(Lambda(n, n/2 + im.expand()*I), S.Integers ).intersect(S.Reals) == ImageSet(Lambda(x, x/2), ConditionSet( n, Eq(n**2 - n/2 - S(1)/2, 0), S.Integers)) assert imageset(Lambda(n, n/(1/n - 1) + im*(n + 1)*I), S.Integers ).intersect(S.Reals) == FiniteSet(S.Half) assert imageset(Lambda(n, n/(n - 6) + (n - 3)*(n + 1)*I/(2*n + 2)), S.Integers).intersect( S.Reals) == FiniteSet(-1) assert imageset(Lambda(n, n/(n**2 - 9) + (n - 3)*(n + 1)*I/(2*n + 2)), S.Integers).intersect( S.Reals) is S.EmptySet s = ImageSet( Lambda(n, -I*(I*(2*pi*n - pi/4) + log(Abs(sqrt(-I))))), S.Integers) # s is unevaluated, but after intersection the result # should be canonical assert s.intersect(S.Reals) == imageset( Lambda(n, 2*n*pi - pi/4), S.Integers) == ImageSet( Lambda(n, 2*pi*n + pi*Rational(7, 4)), S.Integers)
def test_ImageSet(): raises(ValueError, lambda: ImageSet(x, S.Integers)) assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == {y} assert ImageSet(Lambda(x, 1), S.EmptySet) == S.EmptySet empty = Intersection(FiniteSet(log(2)/pi), S.Integers) assert unchanged(ImageSet, Lambda(x, 1), empty) # issue #17471 squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1/x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert (1, 2) not in harmonics assert harmonics.is_iterable assert imageset(x, -x, Interval(0, 1)) == Interval(-1, 0) assert ImageSet(Lambda(x, x**2), Interval(0, 2)).doit() == Interval(0, 4) assert ImageSet(Lambda((x, y), 2*x), {4}, {3}).doit() == FiniteSet(8) assert (ImageSet(Lambda((x, y), x+y), {1, 2, 3}, {10, 20, 30}).doit() == FiniteSet(11, 12, 13, 21, 22, 23, 31, 32, 33)) c = Interval(1, 3) * Interval(1, 3) assert Tuple(2, 6) in ImageSet(Lambda(((x, y),), (x, 2*y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda(((x, y),), (x, 1/y)), c) assert Tuple(2, -2) not in ImageSet(Lambda(((x, y),), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda(((x, y),), (x, -2)), c) c3 = ProductSet(Interval(3, 7), Interval(8, 11), Interval(5, 9)) assert Tuple(8, 3, 9) in ImageSet(Lambda(((t, y, x),), (y, t, x)), c3) assert Tuple(Rational(1, 8), 3, 9) in ImageSet(Lambda(((t, y, x),), (1/y, t, x)), c3) assert 2/pi not in ImageSet(Lambda(((x, y),), 2/x), c) assert 2/S(100) not in ImageSet(Lambda(((x, y),), 2/x), c) assert Rational(2, 3) in ImageSet(Lambda(((x, y),), 2/x), c) S1 = imageset(lambda x, y: x + y, S.Integers, S.Naturals) assert S1.base_pset == ProductSet(S.Integers, S.Naturals) assert S1.base_sets == (S.Integers, S.Naturals) # Passing a set instead of a FiniteSet shouldn't raise assert unchanged(ImageSet, Lambda(x, x**2), {1, 2, 3}) S2 = ImageSet(Lambda(((x, y),), x+y), {(1, 2), (3, 4)}) assert 3 in S2.doit() # FIXME: This doesn't yet work: #assert 3 in S2 assert S2._contains(3) is None raises(TypeError, lambda: ImageSet(Lambda(x, x**2), 1))
def test_imageset_intersect_real(): from sympy import I from sympy.abc import n 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(): raises(ValueError, lambda: ImageSet(x, S.Integers)) assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == {y} assert ImageSet(Lambda(x, 1), S.EmptySet) == S.EmptySet empty = Intersection(FiniteSet(log(2) / pi), S.Integers) assert unchanged(ImageSet, Lambda(x, 1), empty) # issue #17471 squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert (1, 2) not in harmonics assert harmonics.is_iterable assert imageset(x, -x, Interval(0, 1)) == Interval(-1, 0) assert ImageSet(Lambda(x, x**2), Interval(0, 2)).doit() == Interval(0, 4) c = ComplexRegion(Interval(1, 3) * Interval(1, 3)) assert Tuple(2, 6) in ImageSet(Lambda((x, y), (x, 2 * y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda((x, y), (x, 1 / y)), c) assert Tuple(2, -2) not in ImageSet(Lambda((x, y), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda((x, y), (x, -2)), c) c3 = Interval(3, 7) * Interval(8, 11) * Interval(5, 9) assert Tuple(8, 3, 9) in ImageSet(Lambda((t, y, x), (y, t, x)), c3) assert Tuple(Rational(1, 8), 3, 9) in ImageSet(Lambda((t, y, x), (1 / y, t, x)), c3) assert 2 / pi not in ImageSet(Lambda((x, y), 2 / x), c) assert 2 / S(100) not in ImageSet(Lambda((x, y), 2 / x), c) assert Rational(2, 3) in ImageSet(Lambda((x, y), 2 / x), c) assert imageset(lambda x, y: x + y, S.Integers, S.Naturals).base_set == ProductSet(S.Integers, S.Naturals) # Passing a set instead of a FiniteSet shouldn't raise assert unchanged(ImageSet, Lambda(x, x**2), {1, 2, 3}) raises(TypeError, lambda: ImageSet(Lambda(x, x**2), 1))
def test_imageset_intersect_diophantine(): from sympy.abc import m, n # Check that same lambda variable for both ImageSets is handled correctly img1 = ImageSet(Lambda(n, 2 * n + 1), S.Integers) img2 = ImageSet(Lambda(n, 4 * n + 1), S.Integers) assert img1.intersect(img2) == img2 # Empty solution set returned by diophantine: assert (ImageSet(Lambda(n, 2 * n), S.Integers).intersect( ImageSet(Lambda(n, 2 * n + 1), S.Integers)) == S.EmptySet) # Check intersection with S.Integers: assert ImageSet(Lambda(n, 9 / n + 20 * n / 3), S.Integers).intersect(S.Integers) == FiniteSet( -61, -23, 23, 61) # Single solution (2, 3) for diophantine solution: assert ImageSet(Lambda(n, (n - 2)**2), S.Integers).intersect( ImageSet(Lambda(n, -((n - 3)**2)), S.Integers)) == FiniteSet(0) # Single parametric solution for diophantine solution: assert ImageSet(Lambda(n, n**2 + 5), S.Integers).intersect( ImageSet(Lambda(m, 2 * m), S.Integers)) == ImageSet(Lambda(n, 4 * n**2 + 4 * n + 6), S.Integers) # 4 non-parametric solution couples for dioph. equation: assert ImageSet(Lambda(n, n**2 - 9), S.Integers).intersect( ImageSet(Lambda(m, -(m**2)), S.Integers)) == FiniteSet(-9, 0) # Double parametric solution for diophantine solution: assert ImageSet(Lambda(m, m**2 + 40), S.Integers).intersect( ImageSet(Lambda(n, 41 * n), S.Integers)) == Intersection( ImageSet(Lambda(m, m**2 + 40), S.Integers), ImageSet(Lambda(n, 41 * n), S.Integers), ) # Check that diophantine returns *all* (8) solutions (permute=True) assert ImageSet(Lambda(n, n**4 - 2**4), S.Integers).intersect( ImageSet(Lambda(m, -(m**4) + 3**4), S.Integers)) == FiniteSet(0, 65) assert ImageSet(Lambda(n, pi / 12 + n * 5 * pi / 12), S.Integers).intersect( ImageSet(Lambda(n, 7 * pi / 12 + n * 11 * pi / 12), S.Integers)) == ImageSet( Lambda(n, 55 * pi * n / 12 + 17 * pi / 4), S.Integers) # TypeError raised by diophantine (#18081) assert ImageSet(Lambda(n, n * log(2)), S.Integers).intersection(S.Integers) == Intersection( ImageSet(Lambda(n, n * log(2)), S.Integers), S.Integers) # NotImplementedError raised by diophantine (no solver for cubic_thue) assert ImageSet(Lambda(n, n**3 + 1), S.Integers).intersect( ImageSet(Lambda(n, n**3), S.Integers)) == Intersection( ImageSet(Lambda(n, n**3 + 1), S.Integers), ImageSet(Lambda(n, n**3), S.Integers), )
def test_imageset_intersect_real(): from sympy import I from sympy.abc import n 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) # s is unevaluated, but after intersection the result # should be canonical assert s.intersect(S.Reals) == imageset(Lambda( n, 2 * n * pi - pi / 4), S.Integers) == ImageSet( Lambda(n, 2 * pi * n + pi * Rational(7, 4)), S.Integers)
def test_ImageSet(): squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1/x), S.Naturals) assert Rational(1, 5) in harmonics assert .25 in harmonics assert .3 not in harmonics assert harmonics.is_iterable
def test_ImageSet(): squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert .25 in harmonics assert .3 not in harmonics assert harmonics.is_iterable
def test_ImageSet(): raises(ValueError, lambda: ImageSet(x, S.Integers)) assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == {y} squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert (1, 2) not in harmonics assert harmonics.is_iterable assert imageset(x, -x, Interval(0, 1)) == Interval(-1, 0) assert ImageSet(Lambda(x, x**2), Interval(0, 2)).doit() == Interval(0, 4) c = ComplexRegion(Interval(1, 3) * Interval(1, 3)) assert Tuple(2, 6) in ImageSet(Lambda((x, y), (x, 2 * y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda((x, y), (x, 1 / y)), c) assert Tuple(2, -2) not in ImageSet(Lambda((x, y), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda((x, y), (x, -2)), c) c3 = Interval(3, 7) * Interval(8, 11) * Interval(5, 9) assert Tuple(8, 3, 9) in ImageSet(Lambda((t, y, x), (y, t, x)), c3) assert Tuple(S(1) / 8, 3, 9) in ImageSet(Lambda((t, y, x), (1 / y, t, x)), c3) assert 2 / pi not in ImageSet(Lambda((x, y), 2 / x), c) assert 2 / S(100) not in ImageSet(Lambda((x, y), 2 / x), c) assert 2 / S(3) in ImageSet(Lambda((x, y), 2 / x), c) assert imageset(lambda x, y: x + y, S.Integers, S.Naturals).base_set == ProductSet(S.Integers, S.Naturals)
def test_infinitely_indexed_set_1(): from sympy.abc import n, m, t assert imageset(Lambda(n, n), S.Integers) == imageset(Lambda(m, m), S.Integers) assert imageset(Lambda(n, 2*n), S.Integers).intersect( imageset(Lambda(m, 2*m + 1), S.Integers)) is S.EmptySet assert imageset(Lambda(n, 2*n), S.Integers).intersect( imageset(Lambda(n, 2*n + 1), S.Integers)) is S.EmptySet assert imageset(Lambda(m, 2*m), S.Integers).intersect( imageset(Lambda(n, 3*n), S.Integers)).dummy_eq( ImageSet(Lambda(t, 6*t), S.Integers)) assert imageset(x, x/2 + Rational(1, 3), S.Integers).intersect(S.Integers) is S.EmptySet assert imageset(x, x/2 + S.Half, S.Integers).intersect(S.Integers) is S.Integers # https://github.com/sympy/sympy/issues/17355 S53 = ImageSet(Lambda(n, 5*n + 3), S.Integers) assert S53.intersect(S.Integers) == S53