Esempio n. 1
0
 def test_simple_expression(self):
     x1, x2 = symbols('x1, x2')
     function = Or(x1, x2)
     self.assertEquals(function.subs({'x1': False, 'x2': False}), False)
     self.assertEquals(function.subs({'x1': True, 'x2': False}), True)
     self.assertEquals(function.subs({'x1': False, 'x2': True}), True)
     self.assertEquals(function.subs({'x1': True, 'x2': True}), True)
Esempio n. 2
0
 def test_complex_expression(self):
     x1, x2, x3 = symbols('x1, x2, x3')
     subfunction = And(x2, x3)
     function = Or(x1, subfunction, And(x1, x2))
     print(function.args[0])
     print(function.args)
     for combination in itertools.combinations((True, False), 3):
         self.assertEquals(function.subs(dict(zip(('x1', 'x2', 'x3'), combination))),
                           combination[0] or (combination[1] and combination[2]))
Esempio n. 3
0
def test_PythonCodePrinter():
    prntr = PythonCodePrinter()

    assert not prntr.module_imports

    assert prntr.doprint(x**y) == 'x**y'
    assert prntr.doprint(Mod(x, 2)) == 'x % 2'
    assert prntr.doprint(And(x, y)) == 'x and y'
    assert prntr.doprint(Or(x, y)) == 'x or y'
    assert not prntr.module_imports

    assert prntr.doprint(pi) == 'math.pi'
    assert prntr.module_imports == {'math': {'pi'}}

    assert prntr.doprint(x**Rational(1, 2)) == 'math.sqrt(x)'
    assert prntr.doprint(sqrt(x)) == 'math.sqrt(x)'
    assert prntr.module_imports == {'math': {'pi', 'sqrt'}}

    assert prntr.doprint(acos(x)) == 'math.acos(x)'
    assert prntr.doprint(Assignment(x, 2)) == 'x = 2'
    assert prntr.doprint(Piecewise(
        (1, Eq(x, 0)),
        (2, x > 6))) == '((1) if (x == 0) else (2) if (x > 6) else None)'
    assert prntr.doprint(Piecewise((2, Le(x, 0)),
                        (3, Gt(x, 0)), evaluate=False)) == '((2) if (x <= 0) else'\
                                                        ' (3) if (x > 0) else None)'
    assert prntr.doprint(sign(x)) == '(0.0 if x == 0 else math.copysign(1, x))'
    assert prntr.doprint(p[0, 1]) == 'p[0, 1]'
Esempio n. 4
0
def test_PythonCodePrinter():
    prntr = PythonCodePrinter()

    assert not prntr.module_imports

    assert prntr.doprint(x**y) == "x**y"
    assert prntr.doprint(Mod(x, 2)) == "x % 2"
    assert prntr.doprint(And(x, y)) == "x and y"
    assert prntr.doprint(Or(x, y)) == "x or y"
    assert not prntr.module_imports

    assert prntr.doprint(pi) == "math.pi"
    assert prntr.module_imports == {"math": {"pi"}}

    assert prntr.doprint(x**Rational(1, 2)) == "math.sqrt(x)"
    assert prntr.doprint(sqrt(x)) == "math.sqrt(x)"
    assert prntr.module_imports == {"math": {"pi", "sqrt"}}

    assert prntr.doprint(acos(x)) == "math.acos(x)"
    assert prntr.doprint(Assignment(x, 2)) == "x = 2"
    assert (prntr.doprint(Piecewise(
        (1, Eq(x, 0)),
        (2, x > 6))) == "((1) if (x == 0) else (2) if (x > 6) else None)")
    assert (prntr.doprint(
        Piecewise((2, Le(x, 0)), (3, Gt(x, 0)),
                  evaluate=False)) == "((2) if (x <= 0) else"
            " (3) if (x > 0) else None)")
    assert prntr.doprint(sign(x)) == "(0.0 if x == 0 else math.copysign(1, x))"
    assert prntr.doprint(p[0, 1]) == "p[0, 1]"
Esempio n. 5
0
def test_contains():
    assert Interval(0, 2).contains(1) is S.true
    assert Interval(0, 2).contains(3) is S.false
    assert Interval(0, 2, True, False).contains(0) is S.false
    assert Interval(0, 2, True, False).contains(2) is S.true
    assert Interval(0, 2, False, True).contains(0) is S.true
    assert Interval(0, 2, False, True).contains(2) is S.false
    assert Interval(0, 2, True, True).contains(0) is S.false
    assert Interval(0, 2, True, True).contains(2) is S.false

    assert (Interval(0, 2) in Interval(0, 2)) is False

    assert FiniteSet(1, 2, 3).contains(2) is S.true
    assert FiniteSet(1, 2, Symbol('x')).contains(Symbol('x')) is S.true

    assert FiniteSet(y)._contains(x) is None
    raises(TypeError, lambda: x in FiniteSet(y))
    assert FiniteSet({x, y})._contains({x}) is None
    assert FiniteSet({x, y}).subs(y, x)._contains({x}) is True
    assert FiniteSet({x, y}).subs(y, x + 1)._contains({x}) is False

    # issue 8197
    from sympy.abc import a, b
    assert isinstance(FiniteSet(b).contains(-a), Contains)
    assert isinstance(FiniteSet(b).contains(a), Contains)
    assert isinstance(FiniteSet(a).contains(1), Contains)
    raises(TypeError, lambda: 1 in FiniteSet(a))

    # issue 8209
    rad1 = Pow(Pow(2, S(1) / 3) - 1, S(1) / 3)
    rad2 = Pow(S(1) / 9,
               S(1) / 3) - Pow(S(2) / 9,
                               S(1) / 3) + Pow(S(4) / 9,
                                               S(1) / 3)
    s1 = FiniteSet(rad1)
    s2 = FiniteSet(rad2)
    assert s1 - s2 == S.EmptySet

    items = [1, 2, S.Infinity, S('ham'), -1.1]
    fset = FiniteSet(*items)
    assert all(item in fset for item in items)
    assert all(fset.contains(item) is S.true for item in items)

    assert Union(Interval(0, 1), Interval(2, 5)).contains(3) is S.true
    assert Union(Interval(0, 1), Interval(2, 5)).contains(6) is S.false
    assert Union(Interval(0, 1), FiniteSet(2, 5)).contains(3) is S.false

    assert S.EmptySet.contains(1) is S.false
    assert FiniteSet(rootof(x**3 + x - 1, 0)).contains(S.Infinity) is S.false

    assert rootof(x**5 + x**3 + 1, 0) in S.Reals
    assert not rootof(x**5 + x**3 + 1, 1) in S.Reals

    # non-bool results
    assert Union(Interval(1, 2), Interval(3, 4)).contains(x) == \
        Or(And(S(1) <= x, x <= 2), And(S(3) <= x, x <= 4))
    assert Intersection(Interval(1, x), Interval(2, 3)).contains(y) == \
        And(y <= 3, y <= x, S(1) <= y, S(2) <= y)

    assert (S.Complexes).contains(S.ComplexInfinity) == S.false
Esempio n. 6
0
def test_PythonCodePrinter():
    prntr = PythonCodePrinter()
    assert not prntr.module_imports
    assert prntr.doprint(x**y) == 'x**y'
    assert prntr.doprint(Mod(x, 2)) == 'x % 2'
    assert prntr.doprint(And(x, y)) == 'x and y'
    assert prntr.doprint(Or(x, y)) == 'x or y'
    assert not prntr.module_imports
    assert prntr.doprint(pi) == 'math.pi'
    assert prntr.module_imports == {'math': {'pi'}}
    assert prntr.doprint(acos(x)) == 'math.acos(x)'
Esempio n. 7
0
    def convert_conjunction_try_flip(self, expr):
        try:
            return self.convert_conjunction(expr)
        except CannotRepresentRule:
            # try conversion to a disjunction with De Morgan
            expr = Or(*(Not(a) for a in expr.args))
            access_rules = self.convert_disjunction(expr)
            # we must invert the resulting rule, cannot invert more than one
            if len(access_rules) > 1:
                raise CannotRepresentRule(
                    expr,
                    "tried to invert conjunction, but more than one access rule returned"
                )

            return ((access_rules[0][0], True), )
Esempio n. 8
0
def test_PythonCodePrinter():
    prntr = PythonCodePrinter()
    assert prntr.doprint(x**y) == 'x**y'
    assert prntr.doprint(Mod(x, 2)) == 'x % 2'
    assert prntr.doprint(And(x, y)) == 'x and y'
    assert prntr.doprint(Or(x, y)) == 'x or y'
    assert prntr.doprint(Piecewise((x, x > 1), (y, True))) == ('if x > 1:\n'
                                                               '    return x\n'
                                                               'else:\n'
                                                               '    return y')
    pw = Piecewise((x, x > 1), (y, x > 0))
    assert prntr.doprint(pw) == (
        'if x > 1:\n'
        '    return x\n'
        'elif x > 0:\n'
        '    return y\n'
        'else:\n'
        '    raise NotImplementedError("Unhandled condition in: %s")' % pw)
Esempio n. 9
0
def generate_boolean_function(a, d=8, random_seed=0):
    """Generate a Boolean function in disjunctive normal form according to the
    given parameters.
    
    NOTE: This function implements the Boolean function generation algorithm as
    given in "(2001) Sadohara - Learning of Boolean Functions using Support
    Vector Machines - ALT".
    
    Parameters
    ----------
    a : int
        The number of attributes/variables of the generated function.
    d : int
        The expected number of attributes/variables in a disjunct.
    random_seed : int
        The random seed with which to initialize a private Random object.
    
    Returns
    -------
    attributes : list
        The sympy's Symbol objects representing the attributes of the Boolean
        function.
    function : Boolean function comprised of Boolean operators from sympy.logic
        The Boolean function.
    
    """
    rand_obj = random.Random(random_seed)
    attributes = symbols("x1:{}".format(a + 1))
    function = []
    for i in range(2**(d - 2)):
        disjunct = []
        for attr in attributes:
            if rand_obj.random() < d / a:
                if rand_obj.random() < 0.5:
                    disjunct.append(attr)
                else:
                    disjunct.append(Not(attr))
        # Note: Empty disjuncts are discarded since expression 'And(*disjunct)'
        # would return value True for them and consequentially, the whole
        # Boolean function would simplify to the value True.
        if len(disjunct) > 0:
            disjunct = And(*disjunct)
            function.append(disjunct)
    return attributes, Or(*function)
Esempio n. 10
0
def test_union_contains():
    x = Symbol('x')
    i1 = Interval(0, 1)
    i2 = Interval(2, 3)
    i3 = Union(i1, i2)
    assert i3.as_relational(x) == Or(And(S(0) <= x, x <= 1),
                                     And(S(2) <= x, x <= 3))
    raises(TypeError, lambda: x in i3)
    e = i3.contains(x)
    assert e == i3.as_relational(x)
    assert e.subs(x, -0.5) is false
    assert e.subs(x, 0.5) is true
    assert e.subs(x, 1.5) is false
    assert e.subs(x, 2.5) is true
    assert e.subs(x, 3.5) is false

    U = Interval(0, 2, True, True) + Interval(10, oo) + FiniteSet(-1, 2, 5, 6)
    assert all(el not in U for el in [0, 4, -oo])
    assert all(el in U for el in [2, 5, 10])
Esempio n. 11
0
def test_Union_as_relational():
    x = Symbol('x')
    assert (Interval(0, 1) + FiniteSet(2)).as_relational(x) == \
        Or(And(Le(0, x), Le(x, 1)), Eq(x, 2))
    assert (Interval(0, 1, True, True) + FiniteSet(1)).as_relational(x) == \
        And(Lt(0, x), Le(x, 1))
Esempio n. 12
0
def test_Finite_as_relational():
    x = Symbol('x')
    y = Symbol('y')

    assert FiniteSet(1, 2).as_relational(x) == Or(Eq(x, 1), Eq(x, 2))
    assert FiniteSet(y, -5).as_relational(x) == Or(Eq(x, y), Eq(x, -5))
Esempio n. 13
0
 def relationalize(gen):
     return Or(*[i.as_relational(gen) for i in intervals])