def test_xreplace(): assert b21.xreplace({b2: b1}) == Basic(b1, b1) assert b21.xreplace({b2: b21}) == Basic(b21, b1) assert b3.xreplace({b2: b1}) == b2 assert Basic(b1, b2).xreplace({b1: b2, b2: b1}) == Basic(b2, b1) assert Atom(b1).xreplace({b1: b2}) == Atom(b1) assert Atom(b1).xreplace({Atom(b1): b2}) == b2 pytest.raises(TypeError, lambda: b1.xreplace()) pytest.raises(TypeError, lambda: b1.xreplace([b1, b2]))
def test_equality(): instances = [b1, b2, b3, b21, Basic(b1, b1, b1), Basic] for i, b_i in enumerate(instances): for j, b_j in enumerate(instances): assert (b_i == b_j) == (i == j) assert (b_i != b_j) == (i != j) assert Basic() != [] assert not (Basic() == []) assert Basic() != 0 assert not (Basic() == 0)
def test_subs(): assert b21.subs(b2, b1) == Basic(b1, b1) assert b21.subs(b2, b21) == Basic(b21, b1) assert b3.subs(b2, b1) == b2 assert b21.subs([(b2, b1), (b1, b2)]) == Basic(b2, b2) assert b21.subs({b1: b2, b2: b1}) == Basic(b2, b2) pytest.raises(ValueError, lambda: b21.subs('bad arg')) pytest.raises(ValueError, lambda: b21.subs(b1, b2, b3))
def test_matches_basic(): instances = [ Basic(b1, b1, b2), Basic(b1, b2, b1), Basic(b2, b1, b1), Basic(b1, b2), Basic(b2, b1), b2, b1 ] for i, b_i in enumerate(instances): for j, b_j in enumerate(instances): if i == j: assert b_i.matches(b_j) == {} else: assert b_i.matches(b_j) is None assert b1.match(b1) == {}
def test_Singleton(): global instantiated instantiated = 0 class MyNewSingleton(Basic, metaclass=Singleton): def __new__(cls): global instantiated instantiated += 1 return Basic.__new__(cls) assert instantiated == 0 MyNewSingleton() # force instantiation assert instantiated == 1 assert MyNewSingleton() is not Basic() assert MyNewSingleton() is MyNewSingleton() assert S.MyNewSingleton is MyNewSingleton() assert instantiated == 1 class MySingleton_sub(MyNewSingleton): pass assert instantiated == 1 MySingleton_sub() assert instantiated == 2 assert MySingleton_sub() is not MyNewSingleton() assert MySingleton_sub() is MySingleton_sub()
def __new__(cls, *args, **kwargs): from diofant.geometry.point import Point args = [ Tuple(*a) if is_sequence(a) and not isinstance(a, Point) else sympify(a) for a in args ] return Basic.__new__(cls, *args)
def test_subs(): assert b21.subs(b2, b1) == Basic(b1, b1) assert b21.subs(b2, b21) == Basic(b21, b1) assert b3.subs(b2, b1) == b2 assert b21.subs([(b2, b1), (b1, b2)]) == Basic(b2, b2) assert b21.subs({b1: b2, b2: b1}) == Basic(b2, b2) pytest.raises(ValueError, lambda: b21.subs('bad arg')) pytest.raises(ValueError, lambda: b21.subs(b1, b2, b3)) assert b21.subs(collections.ChainMap({b1: b2}, {b2: b1})) == Basic(b2, b2) assert b21.subs(collections.OrderedDict([(b2, b1), (b1, b2)])) == Basic(b2, b2)
def test_preorder_traversal(): expr = Basic(b21, b3) assert list( preorder_traversal(expr)) == [expr, b21, b2, b1, b1, b3, b2, b1] assert list(preorder_traversal( ('abc', ('d', 'ef')))) == [('abc', ('d', 'ef')), 'abc', ('d', 'ef'), 'd', 'ef'] result = [] pt = preorder_traversal(expr) for i in pt: result.append(i) if i == b2: pt.skip() assert result == [expr, b21, b2, b1, b3, b2] expr = z + w * (x + y) assert list(preorder_traversal([expr], keys=default_sort_key)) == \ [[w*(x + y) + z], w*(x + y) + z, z, w*(x + y), w, x + y, x, y] assert list(preorder_traversal((x + y)*z, keys=True)) == \ [z*(x + y), z, x + y, x, y]
def __new__(cls, *args): from diofant.functions.elementary.integers import ceiling # expand range slc = slice(*args) start, stop, step = slc.start or 0, slc.stop, slc.step or 1 try: start, stop, step = [ w if w in [S.NegativeInfinity, S.Infinity] else Integer( as_int(w)) for w in (start, stop, step) ] except ValueError: raise ValueError("Inputs to Range must be Integer Valued\n" + "Use ImageSets of Ranges for other cases") if not step.is_finite: raise ValueError("Infinite step is not allowed") if start == stop: return S.EmptySet n = ceiling((stop - start) / step) if n <= 0: return S.EmptySet # normalize args: regardless of how they are entered they will show # canonically as Range(inf, sup, step) with step > 0 if n.is_finite: start, stop = sorted((start, start + (n - 1) * step)) else: start, stop = sorted((start, stop - step)) step = abs(step) if (start, stop) == (S.NegativeInfinity, S.Infinity): raise ValueError("Both the start and end value of " "Range cannot be unbounded") else: return Basic.__new__(cls, start, stop + step, step)
def eval(cls, *args): try: newargs = [] for x in args: if isinstance(x, Number) or x in (0, 1): newargs.append(True if x else False) else: newargs.append(x) A, B = newargs except ValueError: raise ValueError("%d operand(s) used for an Implies " "(pairs are required): %s" % (len(args), str(args))) if A == S.true or A == S.false or B == S.true or B == S.false: return Or(Not(A), B) elif A == B: return S.true elif A.is_Relational and B.is_Relational: if A.canonical == B.canonical: return S.true if (~A).canonical == B.canonical: return B else: return Basic.__new__(cls, *args)
def test_core_basic(): for c in (Atom, Atom(), Basic, Basic(), SingletonRegistry, SingletonRegistry()): check(c)
def __new__(cls): global instantiated instantiated += 1 return Basic.__new__(cls)
"""This tests diofant/core/basic.py with (ideally) no reference to subclasses of Basic or Atom.""" import pytest from diofant.core.basic import Basic, Atom, preorder_traversal from diofant.core.singleton import S, Singleton from diofant.core.symbol import symbols from diofant.core.compatibility import default_sort_key from diofant import sin, cos, Lambda b1 = Basic() b2 = Basic(b1) b3 = Basic(b2) b21 = Basic(b2, b1) def test_structure(): assert b21.args == (b2, b1) assert b21.func(*b21.args) == b21 assert bool(b1) def test_equality(): instances = [b1, b2, b3, b21, Basic(b1, b1, b1), Basic] for i, b_i in enumerate(instances): for j, b_j in enumerate(instances): assert (b_i == b_j) == (i == j) assert (b_i != b_j) == (i != j) assert Basic() != []
def __new__(cls, lamda, base_set): return Basic.__new__(cls, lamda, base_set)