def test_apart_list(): assert apart_list(1) == 1 w0, w1, w2 = Symbol('w0'), Symbol('w1'), Symbol('w2') _a = Dummy('a') f = (-2*x - 2*x**2) / (3*x**2 - 6*x) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (-1, Poly(Rational(2, 3), x), [(Poly(w0 - 2, w0), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])) assert (apart_list(2/(x**2-2), x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(w0**2 - 2, w0), Lambda(_a, _a/2), Lambda(_a, -_a + x), 1)])) f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(w0 - 2, w0), Lambda(_a, 4), Lambda(_a, -_a + x), 1), (Poly(w1**2 - 1, w1), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2), (Poly(w2 + 1, w2), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])) f = 1/(2*(x - 1)**2) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(2, w0), Lambda(_a, 0), Lambda(_a, x - _a), 1), (Poly(w1 - 1, w1), Lambda(_a, Rational(1, 2)), Lambda(_a, x - _a), 2), (Poly(1, w2), Lambda(_a, 0), Lambda(_a, x - _a), 1)]))
def test_satisfiable_all_models(): assert next(satisfiable(False, all_models=True)) is False assert list(satisfiable((A >> ~A) & A, all_models=True)) == [False] assert list(satisfiable(True, all_models=True)) == [{true: true}] models = [{A: True, B: False}, {A: False, B: True}] result = satisfiable(A ^ B, all_models=True) models.remove(next(result)) models.remove(next(result)) pytest.raises(StopIteration, lambda: next(result)) assert not models assert list(satisfiable(Equivalent(A, B), all_models=True)) == \ [{A: False, B: False}, {A: True, B: True}] models = [{A: False, B: False}, {A: False, B: True}, {A: True, B: True}] for model in satisfiable(A >> B, all_models=True): models.remove(model) assert not models # This is a santiy test to check that only the required number # of solutions are generated. The expr below has 2**100 - 1 models # which would time out the test if all are generated at once. sym = numbered_symbols() X = [next(sym) for i in range(100)] result = satisfiable(Or(*X), all_models=True) for i in range(10): assert next(result)
def test_satisfiable_all_models(): assert next(satisfiable(False, all_models=True)) is False assert list(satisfiable((A >> ~A) & A, all_models=True)) == [False] assert list(satisfiable(True, all_models=True)) == [{true: true}] models = [{A: True, B: False}, {A: False, B: True}] result = satisfiable(A ^ B, all_models=True) models.remove(next(result)) models.remove(next(result)) pytest.raises(StopIteration, lambda: next(result)) assert not models assert list(satisfiable(Equivalent(A, B), all_models=True)) == \ [{A: False, B: False}, {A: True, B: True}] models = [{A: False, B: False}, {A: False, B: True}, {A: True, B: True}] for model in satisfiable(A >> B, all_models=True): models.remove(model) assert not models # This is a santiy test to check that only the required number # of solutions are generated. The expr below has 2**100 - 1 models # which would time out the test if all are generated at once. sym = numbered_symbols() X = [next(sym) for i in range(100)] result = satisfiable(Or(*X), all_models=True) for i in range(10): assert next(result)
def test_numbered_symbols(): s = numbered_symbols(cls=Dummy) assert isinstance(next(s), Dummy) assert next(numbered_symbols('C', start=1, exclude=[symbols('C1')])) == \ symbols('C2')
def test_filter_symbols(): s = numbered_symbols() filtered = filter_symbols(s, symbols('x0 x2 x3')) assert list(itertools.islice(filtered, 3)) == list(symbols('x1 x4 x5')) assert set(filter_symbols((), set())) == set()