def __new__(cls, arg): # Mostly type checking here arg = _sympify(arg) predicates = arg.atoms(Predicate) applied_predicates = arg.atoms(AppliedPredicate) if predicates and applied_predicates: raise ValueError( "arg must be either completely free or singly applied") if not applied_predicates: obj = BooleanFunction.__new__(cls, arg) obj.pred = arg obj.expr = None return obj predicate_args = {pred.args[0] for pred in applied_predicates} if len(predicate_args) > 1: raise ValueError( "The AppliedPredicates in arg must be applied to a single expression." ) obj = BooleanFunction.__new__(cls, arg) obj.expr = predicate_args.pop() obj.pred = arg.xreplace( Transform(lambda e: e.func, lambda e: isinstance(e, AppliedPredicate))) applied = obj.apply() if applied is None: return obj return applied
def test_BooleanFunction(self): # Raise exception self.assertRaises(TypeError, And(0, 1).__lt__, true) self.assertRaises(AttributeError, BooleanFunction._to_nnf, Boolean, true, false) assert BooleanFunction(Application(), true)._eval_derivative(Equality(1, 2)) is not true assert BooleanFunction._apply_patternbased_simplification(true, Equality(1, 0), None, None, None) is not true assert BooleanFunction._apply_patternbased_simplification(true, Equality(1, 0), None, false, true) is not true assert BooleanFunction._apply_patternbased_simplification(false, Equality(1, 0), Equality(1,1), true, true, None) is not true
def test_Xor(self): assert Xor(BooleanFunction(Application(), true)).to_nnf() is not true assert isinstance(Xor(A, B), Xor) assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D) assert Xor(A, B, Xor(B, C)) == Xor(A, C) assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B) e = A > 1 assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
def subs(self, *args, **kwargs): if len(args) == 1: eq, *_ = args if eq.is_Equality: args = eq.args return self.func(self.lhs._subs(*args, **kwargs).simplify(), self.rhs._subs(*args, **kwargs).simplify(), equivalent=[self, eq]).simplify() if isinstance(eq, dict): return self if eq.is_ConditionalBoolean: return self.bfn(self.subs, eq) return BooleanFunction.subs(self, *args, **kwargs)
def __new__(cls, arg): # Mostly type checking here arg = _sympify(arg) predicates = arg.atoms(Predicate) applied_predicates = arg.atoms(AppliedPredicate) if predicates and applied_predicates: raise ValueError("arg must be either completely free or singly applied") if not applied_predicates: obj = BooleanFunction.__new__(cls, arg) obj.pred = arg obj.expr = None return obj predicate_args = set([pred.args[0] for pred in applied_predicates]) if len(predicate_args) > 1: raise ValueError("The AppliedPredicates in arg must be applied to a single expression.") obj = BooleanFunction.__new__(cls, arg) obj.expr = predicate_args.pop() obj.pred = arg.xreplace(Transform(lambda e: e.func, lambda e: isinstance(e, AppliedPredicate))) applied = obj.apply() if applied is None: return obj return applied
def test_sympy__logic__boolalg__BooleanFunction(): from sympy.logic.boolalg import BooleanFunction assert _test_args(BooleanFunction(1, 2, 3))