Exemplo n.º 1
0
def test_dummy_eq():
    C = ConditionSet
    I = S.Integers
    c = C(x, x < 1, I)
    assert c.dummy_eq(C(y, y < 1, I))
    assert c.dummy_eq(1) == False
    assert c.dummy_eq(C(x, x < 1, S.Reals)) == False

    c1 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals)
    c2 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals)
    c3 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Complexes)
    assert c1.dummy_eq(c2)
    assert c1.dummy_eq(c3) is False
    assert c.dummy_eq(c1) is False
    assert c1.dummy_eq(c) is False

    # issue 19496
    m = Symbol('m')
    n = Symbol('n')
    a = Symbol('a')
    d1 = ImageSet(Lambda(m, m * pi), S.Integers)
    d2 = ImageSet(Lambda(n, n * pi), S.Integers)
    c1 = ConditionSet(x, Ne(a, 0), d1)
    c2 = ConditionSet(x, Ne(a, 0), d2)
    assert c1.dummy_eq(c2)
Exemplo n.º 2
0
def test_SetExpr_Interval_pow():
    assert SetExpr(Interval(0, 2))**2 == SetExpr(Interval(0, 4))
    assert SetExpr(Interval(-1, 1))**2 == SetExpr(Interval(0, 1))
    assert SetExpr(Interval(1, 2))**2 == SetExpr(Interval(1, 4))
    assert SetExpr(Interval(-1, 2))**3 == SetExpr(Interval(-1, 8))
    assert SetExpr(Interval(-1, 1))**0 == SetExpr(FiniteSet(1))


    #assert SetExpr(Interval(1, 2))**Rational(5, 2) == SetExpr(Interval(1, 4*sqrt(2)))
    #assert SetExpr(Interval(-1, 2))**Rational(1, 3) == SetExpr(Interval(-1, 2**Rational(1, 3)))
    #assert SetExpr(Interval(0, 2))**S.Half == SetExpr(Interval(0, sqrt(2)))

    #assert SetExpr(Interval(-4, 2))**Rational(2, 3) == SetExpr(Interval(0, 2*2**Rational(1, 3)))

    #assert SetExpr(Interval(-1, 5))**S.Half == SetExpr(Interval(0, sqrt(5)))
    #assert SetExpr(Interval(-oo, 2))**S.Half == SetExpr(Interval(0, sqrt(2)))
    #assert SetExpr(Interval(-2, 3))**(Rational(-1, 4)) == SetExpr(Interval(0, oo))

    assert SetExpr(Interval(1, 5))**(-2) == SetExpr(Interval(Rational(1, 25), 1))
    assert SetExpr(Interval(-1, 3))**(-2) == SetExpr(Interval(0, oo))

    assert SetExpr(Interval(0, 2))**(-2) == SetExpr(Interval(Rational(1, 4), oo))
    assert SetExpr(Interval(-1, 2))**(-3) == SetExpr(Union(Interval(-oo, -1), Interval(Rational(1, 8), oo)))
    assert SetExpr(Interval(-3, -2))**(-3) == SetExpr(Interval(Rational(-1, 8), Rational(-1, 27)))
    assert SetExpr(Interval(-3, -2))**(-2) == SetExpr(Interval(Rational(1, 9), Rational(1, 4)))
    #assert SetExpr(Interval(0, oo))**S.Half == SetExpr(Interval(0, oo))
    #assert SetExpr(Interval(-oo, -1))**Rational(1, 3) == SetExpr(Interval(-oo, -1))
    #assert SetExpr(Interval(-2, 3))**(Rational(-1, 3)) == SetExpr(Interval(-oo, oo))

    assert SetExpr(Interval(-oo, 0))**(-2) == SetExpr(Interval.open(0, oo))
    assert SetExpr(Interval(-2, 0))**(-2) == SetExpr(Interval(Rational(1, 4), oo))

    assert SetExpr(Interval(Rational(1, 3), S.Half))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(0, S.Half))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(S.Half, 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(0, 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(2, 3))**oo == SetExpr(FiniteSet(oo))
    assert SetExpr(Interval(1, 2))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(S.Half, 3))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(Rational(-1, 3), Rational(-1, 4)))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(-1, Rational(-1, 2)))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-3, -2))**oo == SetExpr(FiniteSet(-oo, oo))
    assert SetExpr(Interval(-2, -1))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-2, Rational(-1, 2)))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(Rational(-1, 2), S.Half))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(Rational(-1, 2), 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(Rational(-2, 3), 2))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(-1, 1))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-1, S.Half))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-1, 2))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-2, S.Half))**oo == SetExpr(Interval(-oo, oo))

    assert (SetExpr(Interval(1, 2))**x).dummy_eq(SetExpr(ImageSet(Lambda(_d, _d**x), Interval(1, 2))))

    assert SetExpr(Interval(2, 3))**(-oo) == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(0, 2))**(-oo) == SetExpr(Interval(0, oo))
    assert (SetExpr(Interval(-1, 2))**(-oo)).dummy_eq(SetExpr(ImageSet(Lambda(_d, _d**(-oo)), Interval(-1, 2))))
Exemplo n.º 3
0
def test_SetExpr_Intersection():
    x, y, z, w = symbols("x y z w")
    set1 = Interval(x, y)
    set2 = Interval(w, z)
    inter = Intersection(set1, set2)
    se = SetExpr(inter)
    assert exp(se).set == Intersection(ImageSet(Lambda(x, exp(x)), set1),
                                       ImageSet(Lambda(x, exp(x)), set2))
    assert cos(se).set == ImageSet(Lambda(x, cos(x)), inter)
Exemplo n.º 4
0
def test_as_dummy():
    _0 = Symbol('_0')
    assert ConditionSet(x, x < 1, Interval(y, oo)).as_dummy() == ConditionSet(
        _0, _0 < 1, Interval(y, oo))
    assert ConditionSet(x, x < 1, Interval(x, oo)).as_dummy() == ConditionSet(
        _0, _0 < 1, Interval(x, oo))
    assert ConditionSet(x, x < 1,
                        ImageSet(Lambda(y, y**2),
                                 S.Integers)).as_dummy() == ConditionSet(
                                     _0, _0 < 1,
                                     ImageSet(Lambda(_0, _0**2), S.Integers))
Exemplo n.º 5
0
def test_as_dummy():
    _0, _1 = symbols('_0 _1')
    assert ConditionSet(x, x < 1, Interval(y, oo)
        ).as_dummy() == ConditionSet(_0, _0 < 1, Interval(y, oo))
    assert ConditionSet(x, x < 1, Interval(x, oo)
        ).as_dummy() == ConditionSet(_0, _0 < 1, Interval(x, oo))
    assert ConditionSet(x, x < 1, ImageSet(Lambda(y, y**2), S.Integers)
        ).as_dummy() == ConditionSet(
            _0, _0 < 1, ImageSet(Lambda(_0, _0**2), S.Integers))
    e = ConditionSet((x, y), x <= y, S.Reals**2)
    assert e.bound_symbols == [x, y]
    assert e.as_dummy() == ConditionSet((_0, _1), _0 <= _1, S.Reals**2)
    assert e.as_dummy() == ConditionSet((y, x), y <= x, S.Reals**2
        ).as_dummy()
def _set_function(f, x):
    from sympy.sets.sets import is_function_invertible_in_set
    # If the function is invertible, intersect the maps of the sets.
    if is_function_invertible_in_set(f, x):
        return Intersection(imageset(f, arg) for arg in x.args)
    else:
        return ImageSet(Lambda(_x, f(_x)), x)
Exemplo n.º 7
0
def _apply_operation(op, x, y, commutative):
    out = _handle_finite_sets(op, x, y, commutative)
    if out is None:
        out = op(x, y)

    if out is None and commutative:
        out = op(y, x)
    if out is None:
        _x, _y = symbols("x y")
        if isinstance(x, Set) and not isinstance(y, Set):
            out = ImageSet(Lambda(d, op(d, y)), x).doit()
        elif not isinstance(x, Set) and isinstance(y, Set):
            out = ImageSet(Lambda(d, op(x, d)), y).doit()
        else:
            out = ImageSet(Lambda((_x, _y), op(_x, _y)), x, y)
    return out
def _set_function(f, self):
    expr = f.expr
    if not isinstance(expr, Expr):
        return

    if len(f.variables) > 1:
        return

    n = f.variables[0]

    # f(x) + c and f(-x) + c cover the same integers
    # so choose the form that has the fewest negatives
    c = f(0)
    fx = f(n) - c
    f_x = f(-n) - c
    neg_count = lambda e: sum(_coeff_isneg(_) for _ in Add.make_args(e))
    if neg_count(f_x) < neg_count(fx):
        expr = f_x + c

    a = Wild('a', exclude=[n])
    b = Wild('b', exclude=[n])
    match = expr.match(a*n + b)
    if match and match[a]:
        # canonical shift
        expr = match[a]*n + match[b] % match[a]

    if expr != f.expr:
        return ImageSet(Lambda(n, expr), S.Integers)
Exemplo n.º 9
0
def test_free_symbols():
    assert ConditionSet(x, Eq(y, 0), FiniteSet(z)).free_symbols == {y, z}
    assert ConditionSet(x, Eq(x, 0), FiniteSet(z)).free_symbols == {z}
    assert ConditionSet(x, Eq(x, 0), FiniteSet(x, z)).free_symbols == {x, z}
    assert ConditionSet(x, Eq(x, 0),
                        ImageSet(Lambda(y, y**2),
                                 S.Integers)).free_symbols == set()
Exemplo n.º 10
0
def test_subs_CondSet():
    s = FiniteSet(z, y)
    c = ConditionSet(x, x < 2, s)
    # you can only replace sym with a symbol that is not in
    # the free symbols
    assert c.subs(x, 1) == c
    assert c.subs(x, y) == ConditionSet(y, y < 2, s)

    # double subs needed to change dummy if the base set
    # also contains the dummy
    orig = ConditionSet(y, y < 2, s)
    base = orig.subs(y, w)
    and_dummy = base.subs(y, w)
    assert base == ConditionSet(y, y < 2, {w, z})
    assert and_dummy == ConditionSet(w, w < 2, {w, z})

    assert c.subs(x, w) == ConditionSet(w, w < 2, s)
    assert ConditionSet(x, x < y,
                        s).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w))
    # if the user uses assumptions that cause the condition
    # to evaluate, that can't be helped from SymPy's end
    n = Symbol('n', negative=True)
    assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet
    p = Symbol('p', positive=True)
    assert ConditionSet(n, n < y, S.Integers).subs(n, x) == ConditionSet(
        x, x < y, S.Integers)
    nc = Symbol('nc', commutative=False)
    raises(ValueError, lambda: ConditionSet(x, x < p, S.Integers).subs(x, nc))
    raises(ValueError, lambda: ConditionSet(x, x < p, S.Integers).subs(x, n))
    raises(ValueError, lambda: ConditionSet(x + 1, x < 1, S.Integers))
    raises(ValueError, lambda: ConditionSet(x + 1, x < 1, s))
    assert ConditionSet(n, n < x, Interval(0, oo)).subs(x,
                                                        p) == Interval(0, oo)
    assert ConditionSet(n, n < x, Interval(-oo,
                                           0)).subs(x, p) == Interval(-oo, 0)

    assert ConditionSet(f(x),
                        f(x) < 1,
                        {w, z}).subs(f(x),
                                     y) == ConditionSet(y, y < 1, {w, z})

    # issue 17341
    k = Symbol('k')
    img1 = ImageSet(Lambda(k, 2 * k * pi + asin(y)), S.Integers)
    img2 = ImageSet(Lambda(k, 2 * k * pi + asin(S.One / 3)), S.Integers)
    assert ConditionSet(x, Contains(y, Interval(-1, 1)),
                        img1).subs(y, S.One / 3).dummy_eq(img2)
Exemplo n.º 11
0
def test_bound_symbols():
    assert ConditionSet(x, Eq(y, 0), FiniteSet(z)).bound_symbols == {x}
    assert ConditionSet(x, Eq(x, 0), FiniteSet(x, y)).bound_symbols == {x}
    assert ConditionSet(x, x < 10, ImageSet(Lambda(y, y**2),
                                            S.Integers)).bound_symbols == {x}
    assert ConditionSet(x, x < 10,
                        ConditionSet(y, y > 1,
                                     S.Integers)).bound_symbols == {x}
Exemplo n.º 12
0
def _set_function(f, self):
    if self.is_integer:
        #for positive integers:
        if self.args == (1, S.Infinity, 1):
            expr = f.expr
            if not isinstance(expr, Expr):
                return

            x = f.variables[0]
            if not expr.free_symbols - {x}:
                step = expr.coeff(x)
                c = expr.subs(x, 0)
                if c.is_Integer and step.is_Integer and expr == step * x + c:
                    if self is S.Naturals:
                        c += step
                    if step > 0:
                        return Range(c, S.Infinity, step)
                    return Range(c, S.NegativeInfinity, step)
            return

        expr = f.expr
        if not isinstance(expr, Expr):
            return

        n = f.variables[0]

        # f(x) + c and f(-x) + c cover the same integers
        # so choose the form that has the fewest negatives
        c = f(0)
        fx = f(n) - c
        f_x = f(-n) - c
        neg_count = lambda e: sum(_._coeff_isneg() for _ in Add.make_args(e))
        if neg_count(f_x) < neg_count(fx):
            expr = f_x + c

        a = Wild('a', exclude=[n])
        b = Wild('b', exclude=[n])
        match = expr.match(a * n + b)
        if match and match[a]:
            # canonical shift
            b = match[b]
            if abs(match[a]) == 1:
                nonint = []
                for bi in Add.make_args(b):
                    if not bi.is_integer:
                        nonint.append(bi)
                b = Add(*nonint)
            if b.is_number and match[a].is_real:
                mod = b % match[a]
                reps = dict([(m, m.args[0]) for m in mod.atoms(Mod)
                             if not m.args[0].is_real])
                mod = mod.xreplace(reps)
                expr = match[a] * n + mod
            else:
                expr = match[a] * n + b

        if expr != f.expr:
            return ImageSet(Lambda(n, expr), S.Integers)
def test_sympy_range_to_isabelle():
    from sympy import Dummy, ImageSet, Lambda, S, pi
    from sympy.sets import Interval
    _n = Dummy("n")

    assert_equals(
        sympy_range_to_isabelle(
            ImageSet(Lambda(_n, 2 * _n * pi + pi / 2), S.Integers)),
        "{pi*1/2+pi*n*2|n. n \<in> Ints}")

    assert_equals(sympy_range_to_isabelle(Interval.Lopen(1, 2)), "{1<..2}")
    assert_equals(sympy_range_to_isabelle(S.Reals), "UNIV")
Exemplo n.º 14
0
def test_subs_CondSet():
    s = FiniteSet(z, y)
    c = ConditionSet(x, x < 2, s)
    assert c.subs(x, y) == c
    assert c.subs(z, y) == ConditionSet(x, x < 2, FiniteSet(y))
    assert c.xreplace({x: y}) == ConditionSet(y, y < 2, s)

    assert ConditionSet(x, x < y, s
        ).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w))
    # if the user uses assumptions that cause the condition
    # to evaluate, that can't be helped from SymPy's end
    n = Symbol('n', negative=True)
    assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet
    p = Symbol('p', positive=True)
    assert ConditionSet(n, n < y, S.Integers
        ).subs(n, x) == ConditionSet(n, n < y, S.Integers)
    raises(ValueError, lambda: ConditionSet(
        x + 1, x < 1, S.Integers))
    assert ConditionSet(
        p, n < x, Interval(-5, 5)).subs(x, p) == Interval(-5, 5), ConditionSet(
        p, n < x, Interval(-5, 5)).subs(x, p)
    assert ConditionSet(
        n, n < x, Interval(-oo, 0)).subs(x, p
        ) == Interval(-oo, 0)

    assert ConditionSet(f(x), f(x) < 1, {w, z}
        ).subs(f(x), y) == ConditionSet(f(x), f(x) < 1, {w, z})

    # issue 17341
    k = Symbol('k')
    img1 = ImageSet(Lambda(k, 2*k*pi + asin(y)), S.Integers)
    img2 = ImageSet(Lambda(k, 2*k*pi + asin(S.One/3)), S.Integers)
    assert ConditionSet(x, Contains(
        y, Interval(-1,1)), img1).subs(y, S.One/3).dummy_eq(img2)

    assert (0, 1) in ConditionSet((x, y), x + y < 3, S.Integers**2)

    raises(TypeError, lambda: ConditionSet(n, n < -10, Interval(0, 10)))
Exemplo n.º 15
0
def test_solve_decomposition():
    x = Symbol('x')
    n = Dummy('n')

    f1 = exp(3*x) - 6*exp(2*x) + 11*exp(x) - 6
    f2 = sin(x)**2 - 2*sin(x) + 1
    f3 = sin(x)**2 - sin(x)
    f4 = sin(x + 1)
    f5 = exp(x + 2) - 1
    f6 = 1/log(x)

    s1 = ImageSet(Lambda(n, 2*n*pi), S.Integers)
    s2 = ImageSet(Lambda(n, 2*n*pi + pi), S.Integers)
    s3 = ImageSet(Lambda(n, 2*n*pi + pi/2), S.Integers)
    s4 = ImageSet(Lambda(n, 2*n*pi - 1), S.Integers)
    s5 = ImageSet(Lambda(n, (2*n + 1)*pi - 1), S.Integers)

    assert solve_decomposition(f1, x, S.Reals) == FiniteSet(0, log(2), log(3))
    assert solve_decomposition(f2, x, S.Reals) == s3
    assert solve_decomposition(f3, x, S.Reals) == Union(s1, s2, s3)
    assert solve_decomposition(f4, x, S.Reals) == Union(s4, s5)
    assert solve_decomposition(f5, x, S.Reals) == FiniteSet(-2)
    assert solve_decomposition(f6, x, S.Reals) == ConditionSet(x, Eq(f6, 0), S.Reals)
Exemplo n.º 16
0
def _set_function(f, self):  # noqa:F811
    expr = f.expr
    if not isinstance(expr, Expr):
        return

    n = f.variables[0]
    if expr == abs(n):
        return S.Naturals0

    # f(x) + c and f(-x) + c cover the same integers
    # so choose the form that has the fewest negatives
    c = f(0)
    fx = f(n) - c
    f_x = f(-n) - c
    neg_count = lambda e: sum(_.could_extract_minus_sign()
                              for _ in Add.make_args(e))
    if neg_count(f_x) < neg_count(fx):
        expr = f_x + c

    a = Wild('a', exclude=[n])
    b = Wild('b', exclude=[n])
    match = expr.match(a * n + b)
    if match and match[a] and (not match[a].atoms(Float)
                               and not match[b].atoms(Float)):
        # canonical shift
        a, b = match[a], match[b]
        if a in [1, -1]:
            # drop integer addends in b
            nonint = []
            for bi in Add.make_args(b):
                if not bi.is_integer:
                    nonint.append(bi)
            b = Add(*nonint)
        if b.is_number and a.is_real:
            # avoid Mod for complex numbers, #11391
            br, bi = match_real_imag(b)
            if br and br.is_comparable and a.is_comparable:
                br %= a
                b = br + S.ImaginaryUnit * bi
        elif b.is_number and a.is_imaginary:
            br, bi = match_real_imag(b)
            ai = a / S.ImaginaryUnit
            if bi and bi.is_comparable and ai.is_comparable:
                bi %= ai
                b = br + S.ImaginaryUnit * bi
        expr = a * n + b

    if expr != f.expr:
        return ImageSet(Lambda(n, expr), S.Integers)
Exemplo n.º 17
0
def test_SetExpr_Integers():
    assert SetExpr(S.Integers) + 1 == SetExpr(S.Integers)
    assert SetExpr(S.Integers) + I == SetExpr(ImageSet(Lambda(_d, _d + I), S.Integers))
    assert SetExpr(S.Integers)*(-1) == SetExpr(S.Integers)
    assert SetExpr(S.Integers)*2 == SetExpr(ImageSet(Lambda(_d, 2*_d), S.Integers))
    assert SetExpr(S.Integers)*I == SetExpr(ImageSet(Lambda(_d, I*_d), S.Integers))
    # issue #18050:
    assert SetExpr(S.Integers)._eval_func(Lambda(x, I*x + 1)) == SetExpr(
            ImageSet(Lambda(_d, I*_d + 1), S.Integers))
    # needs improvement:
    assert SetExpr(S.Integers)*I + 1 == SetExpr(
            ImageSet(Lambda(x, x + 1), ImageSet(Lambda(_d, _d*I), S.Integers)))
Exemplo n.º 18
0
def _set_function(f, self):
    expr = f.expr
    if not isinstance(expr, Expr):
        return

    n = f.variables[0]
    if expr == abs(n):
        return S.Naturals0

    # f(x) + c and f(-x) + c cover the same integers
    # so choose the form that has the fewest negatives
    c = f(0)
    fx = f(n) - c
    f_x = f(-n) - c
    neg_count = lambda e: sum(_coeff_isneg(_) for _ in Add.make_args(e))
    if neg_count(f_x) < neg_count(fx):
        expr = f_x + c

    a = Wild('a', exclude=[n])
    b = Wild('b', exclude=[n])
    match = expr.match(a * n + b)
    if match and match[a]:
        # canonical shift
        b = match[b]
        if abs(match[a]) == 1:
            nonint = []
            for bi in Add.make_args(b):
                if not bi.is_integer:
                    nonint.append(bi)
            b = Add(*nonint)
        if b.is_number and match[a].is_real:
            mod = b % match[a]
            reps = dict([(m, m.args[0]) for m in mod.atoms(Mod)
                         if not m.args[0].is_real])
            mod = mod.xreplace(reps)
            expr = match[a] * n + mod
        else:
            expr = match[a] * n + b

    if expr != f.expr:
        return ImageSet(Lambda(n, expr), S.Integers)
Exemplo n.º 19
0
def _set_function(f, x):  # noqa:F811
    return ImageSet(Lambda(_x, f(_x)), x)
Exemplo n.º 20
0
def div_sets(x, y):
    return ImageSet(Lambda((_x, _y), (_x / _y)), x, y)
Exemplo n.º 21
0
def dispatch_on_operation(x, y, op):
    return ImageSet(Lambda(d, op(d, y)), x).doit()
Exemplo n.º 22
0
def _set_pow(x, y):
    return ImageSet(Lambda((_x, _y), (_x ** _y)), x, y)
Exemplo n.º 23
0
def test_solve_trig_abs():
    assert solveset(Eq(sin(Abs(x)), 1), x, domain=S.Reals) == \
        Union(ImageSet(Lambda(n, n*pi + (-1)**n*pi/2), S.Naturals0),
              ImageSet(Lambda(n, -n*pi - (-1)**n*pi/2), S.Naturals0))
Exemplo n.º 24
0
 def _eval_func(self, func):
     # TODO: this could be implemented straight into `imageset`:
     res = set_function(func, self.set)
     if res is None:
         return SetExpr(ImageSet(func, self.set))
     return SetExpr(res)
Exemplo n.º 25
0
def test_SetExpr_Interval_pow():
    assert SetExpr(Interval(0, 2))**2 == SetExpr(Interval(0, 4))
    assert SetExpr(Interval(-1, 1))**2 == SetExpr(Interval(0, 1))
    assert SetExpr(Interval(1, 2))**2 == SetExpr(Interval(1, 4))
    assert SetExpr(Interval(-1, 2))**3 == SetExpr(Interval(-1, 8))
    assert SetExpr(Interval(-1, 1))**0 == SetExpr(FiniteSet(1))

    #assert SetExpr(Interval(1, 2))**(S(5)/2) == SetExpr(Interval(1, 4*sqrt(2)))
    #assert SetExpr(Interval(-1, 2))**(S.One/3) == SetExpr(Interval(-1, 2**(S.One/3)))
    #assert SetExpr(Interval(0, 2))**(S.One/2) == SetExpr(Interval(0, sqrt(2)))

    #assert SetExpr(Interval(-4, 2))**(S(2)/3) == SetExpr(Interval(0, 2*2**(S.One/3)))

    #assert SetExpr(Interval(-1, 5))**(S.One/2) == SetExpr(Interval(0, sqrt(5)))
    #assert SetExpr(Interval(-oo, 2))**(S.One/2) == SetExpr(Interval(0, sqrt(2)))
    #assert SetExpr(Interval(-2, 3))**(S(-1)/4) == SetExpr(Interval(0, oo))

    assert SetExpr(Interval(1, 5))**(-2) == SetExpr(Interval(S.One / 25, 1))
    assert SetExpr(Interval(-1, 3))**(-2) == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(0, 2))**(-2) == SetExpr(Interval(S.One / 4, oo))
    assert SetExpr(Interval(-1, 2))**(-3) == SetExpr(
        Union(Interval(-oo, -1), Interval(S(1) / 8, oo)))
    assert SetExpr(Interval(-3, -2))**(-3) == SetExpr(
        Interval(S(-1) / 8, -S.One / 27))
    assert SetExpr(Interval(-3, -2))**(-2) == SetExpr(
        Interval(S.One / 9, S.One / 4))
    #assert SetExpr(Interval(0, oo))**(S.One/2) == SetExpr(Interval(0, oo))
    #assert SetExpr(Interval(-oo, -1))**(S.One/3) == SetExpr(Interval(-oo, -1))
    #assert SetExpr(Interval(-2, 3))**(-S.One/3) == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-oo, 0))**(-2) == SetExpr(Interval.open(0, oo))
    assert SetExpr(Interval(-2, 0))**(-2) == SetExpr(Interval(S.One / 4, oo))

    assert SetExpr(Interval(S.One / 3, S.One / 2))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(0, S.One / 2))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(S.One / 2, 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(0, 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(2, 3))**oo == SetExpr(FiniteSet(oo))
    assert SetExpr(Interval(1, 2))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(S.One / 2, 3))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(-S.One / 3,
                            -S.One / 4))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(-1, -S.One / 2))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-3, -2))**oo == SetExpr(FiniteSet(-oo, oo))
    assert SetExpr(Interval(-2, -1))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-2, -S.One / 2))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-S.One / 2,
                            S.One / 2))**oo == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(-S.One / 2, 1))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(-S(2) / 3, 2))**oo == SetExpr(Interval(0, oo))
    assert SetExpr(Interval(-1, 1))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-1, S.One / 2))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-1, 2))**oo == SetExpr(Interval(-oo, oo))
    assert SetExpr(Interval(-2, S.One / 2))**oo == SetExpr(Interval(-oo, oo))

    assert (SetExpr(Interval(1, 2))**x).dummy_eq(
        SetExpr(ImageSet(Lambda(_d, _d**x), Interval(1, 2))))

    assert SetExpr(Interval(2, 3))**(-oo) == SetExpr(FiniteSet(0))
    assert SetExpr(Interval(0, 2))**(-oo) == SetExpr(Interval(0, oo))
    assert (SetExpr(Interval(-1, 2))**(-oo)).dummy_eq(
        SetExpr(ImageSet(Lambda(_d, _d**(-oo)), Interval(-1, 2))))
def _set_function(f, x):
    return ImageSet(Lambda(_x, f(_x)), x)
Exemplo n.º 27
0
def _set_pow(x, y):  # noqa:F811
    return ImageSet(Lambda((_x, _y), (_x ** _y)), x, y)
def _set_function(f, x):
    if f == exp:
        return Interval(exp(x.start), exp(x.end), x.left_open, x.right_open)
    elif f == log:
        return Interval(log(x.start), log(x.end), x.left_open, x.right_open)
    return ImageSet(Lambda(_x, f(_x)), x)
Exemplo n.º 29
0
def mul_sets(x, y):
    return ImageSet(Lambda((_x, _y), (_x * _y)), x, y)
Exemplo n.º 30
0
def pow_sets(x, y):
    return ImageSet(Lambda((_x, _y), (_x**_y)), x, y)