def test_Not(): assert Not(Equality(x, y)) == Unequality(x, y) assert Not(Unequality(x, y)) == Equality(x, y) assert Not(StrictGreaterThan(x, y)) == LessThan(x, y) assert Not(StrictLessThan(x, y)) == GreaterThan(x, y) assert Not(GreaterThan(x, y)) == StrictLessThan(x, y) assert Not(LessThan(x, y)) == StrictGreaterThan(x, y)
def test_simplification(): """Test working of simplification methods.""" set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]] set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]] assert SOPform([x, y, z], set1) == Or(And(Not(x), z), And(Not(z), x)) assert Not(SOPform([x, y, z], set2)) == Not(Or(And(Not(x), Not(z)), And(x, z))) assert POSform([x, y, z], set1 + set2) is true assert SOPform([x, y, z], set1 + set2) is true assert SOPform([Dummy(), Dummy(), Dummy()], set1 + set2) is true minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1]] dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]] assert ( SOPform([w, x, y, z], minterms, dontcares) == Or(And(Not(w), z), And(y, z))) assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z) # test simplification ans = And(A, Or(B, C)) assert simplify_logic(A & (B | C)) == ans assert simplify_logic((A & B) | (A & C)) == ans assert simplify_logic(Implies(A, B)) == Or(Not(A), B) assert simplify_logic(Equivalent(A, B)) == \ Or(And(A, B), And(Not(A), Not(B))) assert simplify_logic(And(Equality(A, 2), C)) == And(Equality(A, 2), C) assert simplify_logic(And(Equality(A, 2), A)) == And(Equality(A, 2), A) assert simplify_logic(And(Equality(A, B), C)) == And(Equality(A, B), C) assert simplify_logic(Or(And(Equality(A, 3), B), And(Equality(A, 3), C))) \ == And(Equality(A, 3), Or(B, C)) e = And(A, x**2 - x) assert simplify_logic(e) == And(A, x*(x - 1)) assert simplify_logic(e, deep=False) == e pytest.raises(ValueError, lambda: simplify_logic(A & (B | C), form='spam')) e = x & y ^ z | (z ^ x) res = [(x & ~z) | (z & ~x) | (z & ~y), (x & ~y) | (x & ~z) | (z & ~x)] assert simplify_logic(e) in res assert SOPform([z, y, x], [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]) == res[1] # check input ans = SOPform([x, y], [[1, 0]]) assert SOPform([x, y], [[1, 0]]) == ans assert POSform([x, y], [[1, 0]]) == ans pytest.raises(ValueError, lambda: SOPform([x], [[1]], [[1]])) assert SOPform([x], [[1]], [[0]]) is true assert SOPform([x], [[0]], [[1]]) is true assert SOPform([x], [], []) is false pytest.raises(ValueError, lambda: POSform([x], [[1]], [[1]])) assert POSform([x], [[1]], [[0]]) is true assert POSform([x], [[0]], [[1]]) is true assert POSform([x], [], []) is false # check working of simplify assert simplify((A & B) | (A & C)) == And(A, Or(B, C)) assert simplify(And(x, Not(x))) is false assert simplify(Or(x, Not(x))) is true
def test_nan_equality_exceptions(): # See issue sympy/sympy#7774 assert Equality(nan, nan) is false assert Unequality(nan, nan) is true # See issue sympy/sympy#7773 A = (x, Integer(0), Rational(1, 3), pi, oo, -oo) assert Equality(nan, random.choice(A)) is false assert Equality(random.choice(A), nan) is false assert Unequality(nan, random.choice(A)) is true assert Unequality(random.choice(A), nan) is true
def test_m_matrixsymbol_slice3(): A = MatrixSymbol('A', 8, 7) B = MatrixSymbol('B', 2, 2) C = MatrixSymbol('C', 4, 2) name_expr = ('test', [Equality(B, A[6:, 1::3]), Equality(C, A[::2, ::3])]) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function [B, C] = test(A)\n' ' B = A(7:end, 2:3:end);\n' ' C = A(1:2:end, 1:3:end);\n' 'end\n') assert source == expected
def test_m_matrixsymbol_slice2(): A = MatrixSymbol('A', 3, 4) B = MatrixSymbol('B', 2, 2) C = MatrixSymbol('C', 2, 2) name_expr = ("test", [Equality(B, A[0:2, 0:2]), Equality(C, A[0:2, 1:3])]) result, = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ("function [B, C] = test(A)\n" " B = A(1:2, 1:2);\n" " C = A(1:2, 2:3);\n" "end\n") assert source == expected
def test_simplification(): set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]] set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]] assert _SOPform([x, y, z], set1) == (~x & z) | (~z & x) assert ~_SOPform([x, y, z], set2) == ~((~x & ~z) | (x & z)) assert _POSform([x, y, z], set1 + set2) is true assert _SOPform([x, y, z], set1 + set2) is true assert _SOPform([w, x, y, z], set1 + set2) is true minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1]] dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]] assert _SOPform([w, x, y, z], minterms, dontcares) == (~w & z) | (y & z) assert _POSform([w, x, y, z], minterms, dontcares) == (~w | y) & z # test simplification assert (a & (b | c)).simplify() == a & (b | c) assert ((a & b) | (a & c)).simplify() == a & (b | c) assert (a >> b).simplify() == ~a | b assert to_dnf(Equivalent(a, b), simplify=True) == (a & b) | (~a & ~b) assert (Equality(a, 2) & c).simplify() == Equality(a, 2) & c assert (Equality(a, 2) & a).simplify() == Equality(a, 2) & a assert (Equality(a, b) & c).simplify() == Equality(a, b) & c assert ((Equality(a, 3) & b) | (Equality(a, 3) & c)).simplify() == Equality(a, 3) & (b | c) e = a & (x**2 - x) assert e.simplify() == a & x*(x - 1) assert simplify_logic(e, deep=False) == e pytest.raises(ValueError, lambda: simplify_logic(a & (b | c), form='spam')) e = x & y ^ z | (z ^ x) res = [(x & ~z) | (z & ~x) | (z & ~y), (x & ~y) | (x & ~z) | (z & ~x)] assert to_dnf(e, simplify=True) in res assert _SOPform([z, y, x], [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]) == res[1] # check input ans = _SOPform([x, y], [[1, 0]]) assert _SOPform([x, y], [[1, 0]]) == ans assert _POSform([x, y], [[1, 0]]) == ans pytest.raises(ValueError, lambda: _SOPform([x], [[1]], [[1]])) assert _SOPform([x], [[1]], [[0]]) is true assert _SOPform([x], [[0]], [[1]]) is true assert _SOPform([x], [], []) is false pytest.raises(ValueError, lambda: _POSform([x], [[1]], [[1]])) assert _POSform([x], [[1]], [[0]]) is true assert _POSform([x], [[0]], [[1]]) is true assert _POSform([x], [], []) is false # check working of simplify assert ((a & b) | (a & c)).simplify() == a & (b | c) assert (x & ~x).simplify() is false assert (x | ~x).simplify() is true
def test_results_named_unordered(): # Here output order is based on name_expr expr1 = Equality(C, (x + y) * z) expr2 = Equality(A, (x - y) * z) expr3 = Equality(B, 2 * x) name_expr = ('test', [expr1, expr2, expr3]) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function [C, A, B] = test(x, y, z)\n' ' C = z.*(x + y);\n' ' A = z.*(x - y);\n' ' B = 2*x;\n' 'end\n') assert source == expected
def test_m_output_arg_mixed_unordered(): # named outputs are alphabetical, unnamed output appear in the given order name_expr = ("foo", [cos(2*x), Equality(y, sin(x)), cos(x), Equality(a, sin(2*x))]) result, = codegen(name_expr, "Octave", header=False, empty=False) assert result[0] == "foo.m" source = result[1] expected = ( 'function [out1, y, out3, a] = foo(x)\n' ' out1 = cos(2*x);\n' ' y = sin(x);\n' ' out3 = cos(x);\n' ' a = sin(2*x);\n' 'end\n' ) assert source == expected
def test_results_named_unordered(): # Here output order is based on name_expr A, B, C = symbols('A,B,C') expr1 = Equality(C, (x + y) * z) expr2 = Equality(A, (x - y) * z) expr3 = Equality(B, 2 * x) name_expr = ("test", [expr1, expr2, expr3]) result, = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ("function [C, A, B] = test(x, y, z)\n" " C = z.*(x + y);\n" " A = z.*(x - y);\n" " B = 2*x;\n" "end\n") assert source == expected
def test_core_relational(): for c in (Equality, Equality(x, y), GreaterThan, GreaterThan(x, y), LessThan, LessThan(x, y), Relational, Relational(x, y), StrictGreaterThan, StrictGreaterThan(x, y), StrictLessThan, StrictLessThan(x, y), Unequality, Unequality(x, y)): check(c)
def test_m_simple_code_nameout(): expr = Equality(z, (x + y)) name_expr = ("test", expr) result, = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ("function z = test(x, y)\n" " z = x + y;\n" "end\n") assert source == expected
def test_m_InOutArgument(): expr = Equality(x, x**2) name_expr = ("mysqr", expr) result, = codegen(name_expr, "Octave", header=False, empty=False) source = result[1] expected = ("function x = mysqr(x)\n" " x = x.^2;\n" "end\n") assert source == expected
def test_m_InOutArgument(): expr = Equality(x, x**2) name_expr = ('mysqr', expr) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function x = mysqr(x)\n' ' x = x.^2;\n' 'end\n') assert source == expected
def test_m_simple_code_nameout(): expr = Equality(z, (x + y)) name_expr = ('test', expr) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function z = test(x, y)\n' ' z = x + y;\n' 'end\n') assert source == expected
def test_m_results_matrix_named_ordered(): A = MatrixSymbol('A', 1, 3) expr1 = Equality(C, (x + y)*z) expr2 = Equality(A, Matrix([[1, 2, x]])) expr3 = Equality(B, 2*x) name_expr = ("test", [expr1, expr2, expr3]) result, = codegen(name_expr, "Octave", header=False, empty=False, argument_sequence=(x, z, y)) source = result[1] expected = ( "function [C, A, B] = test(x, z, y)\n" " C = z.*(x + y);\n" " A = [1 2 x];\n" " B = 2*x;\n" "end\n" ) assert source == expected
def test_results_named_ordered(): expr1 = Equality(C, (x + y)*z) expr2 = Equality(A, (x - y)*z) expr3 = Equality(B, 2*x) name_expr = ("test", [expr1, expr2, expr3]) result = codegen(name_expr, "Octave", header=False, empty=False, argument_sequence=(x, z, y)) assert result[0][0] == "test.m" source = result[0][1] expected = ( "function [C, A, B] = test(x, z, y)\n" " C = z.*(x + y);\n" " A = z.*(x - y);\n" " B = 2*x;\n" "end\n" ) assert source == expected
def test_Not(): pytest.raises(TypeError, lambda: Not(True, False)) assert Not(True) is false assert Not(False) is true assert Not(0) is true assert Not(1) is false assert Not(2) is false assert Not(Unequality(a, b)) == Equality(a, b)
def test_m_matrixsymbol_slice(): A = MatrixSymbol('A', 2, 3) B = MatrixSymbol('B', 1, 3) C = MatrixSymbol('C', 1, 3) D = MatrixSymbol('D', 2, 1) name_expr = ('test', [ Equality(B, A[0, :]), Equality(C, A[1, :]), Equality(D, A[:, 2]) ]) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function [B, C, D] = test(A)\n' ' B = A(1, :);\n' ' C = A(2, :);\n' ' D = A(:, 3);\n' 'end\n') assert source == expected
def test_results_named_ordered(): expr1 = Equality(C, (x + y) * z) expr2 = Equality(A, (x - y) * z) expr3 = Equality(B, 2 * x) name_expr = ('test', [expr1, expr2, expr3]) result = codegen(name_expr, 'Octave', header=False, empty=False, argument_sequence=(x, z, y)) assert result[0][0] == 'test.m' source = result[0][1] expected = ('function [C, A, B] = test(x, z, y)\n' ' C = z.*(x + y);\n' ' A = z.*(x - y);\n' ' B = 2*x;\n' 'end\n') assert source == expected
def test_m_matrix_named(): e2 = Matrix([[x, 2 * y, pi * z]]) name_expr = ("test", Equality(MatrixSymbol('myout1', 1, 3), e2)) result = codegen(name_expr, "Octave", header=False, empty=False) assert result[0][0] == "test.m" source = result[0][1] expected = ("function myout1 = test(x, y, z)\n" " myout1 = [x 2*y pi*z];\n" "end\n") assert source == expected
def test_m_matrix_named_matsym(): myout1 = MatrixSymbol('myout1', 1, 3) e2 = Matrix([[x, 2 * y, pi * z]]) name_expr = ('test', Equality(myout1, e2, evaluate=False)) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function myout1 = test(x, y, z)\n' ' myout1 = [x 2*y pi*z];\n' 'end\n') assert source == expected
def test_m_matrix_named(): e2 = Matrix([[x, 2 * y, pi * z]]) name_expr = ('test', Equality(MatrixSymbol('myout1', 1, 3), e2)) result = codegen(name_expr, 'Octave', header=False, empty=False) assert result[0][0] == 'test.m' source = result[0][1] expected = ('function myout1 = test(x, y, z)\n' ' myout1 = [x 2*y pi*z];\n' 'end\n') assert source == expected
def test_m_InOutArgument_order(): # can specify the order as (x, y) expr = Equality(x, x**2 + y) name_expr = ('test', expr) result, = codegen(name_expr, 'Octave', header=False, empty=False, argument_sequence=(x, y)) source = result[1] expected = ('function x = test(x, y)\n' ' x = x.^2 + y;\n' 'end\n') assert source == expected # make sure it gives (x, y) not (y, x) expr = Equality(x, x**2 + y) name_expr = ('test', expr) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function x = test(x, y)\n' ' x = x.^2 + y;\n' 'end\n') assert source == expected
def test_m_matrixsymbol_slice_autoname(): A = MatrixSymbol('A', 2, 3) B = MatrixSymbol('B', 1, 3) name_expr = ('test', [Equality(B, A[0, :]), A[1, :], A[:, 0], A[:, 1]]) result, = codegen(name_expr, 'Octave', header=False, empty=False) source = result[1] expected = ('function [B, out2, out3, out4] = test(A)\n' ' B = A(1, :);\n' ' out2 = A(2, :);\n' ' out3 = A(:, 1);\n' ' out4 = A(:, 2);\n' 'end\n') assert source == expected
def test_cython_wrapper_inoutarg(): from diofant import Equality x, y, z = symbols('x,y,z') code_gen = CythonCodeWrapper(CCodeGen()) routine = make_routine("test", Equality(z, x + y + z)) source = get_string(code_gen.dump_pyx, [routine]) expected = ("cdef extern from 'file.h':\n" " void test(double x, double y, double *z)\n" "\n" "def test_c(double x, double y, double z):\n" "\n" " test(x, y, &z)\n" " return z") assert source == expected
def test_Equality(): assert Equality(IM, IM) is true assert Unequality(IM, IM) is false assert Equality(IM, IM.subs({1: 2})) is false assert Unequality(IM, IM.subs({1: 2})) is true assert Equality(IM, 2) is false assert Unequality(IM, 2) is true M = ImmutableMatrix([x, y]) assert Equality(M, IM) is false assert Unequality(M, IM) is true assert Equality(M, M.subs({x: 2})).subs({x: 2}) is true assert Unequality(M, M.subs({x: 2})).subs({x: 2}) is false assert Equality(M, M.subs({x: 2})).subs({x: 3}) is false assert Unequality(M, M.subs({x: 2})).subs({x: 3}) is true
def test_Equality(): assert Equality(IM, IM) is S.true assert Unequality(IM, IM) is S.false assert Equality(IM, IM.subs(1, 2)) is S.false assert Unequality(IM, IM.subs(1, 2)) is S.true assert Equality(IM, 2) is S.false assert Unequality(IM, 2) is S.true M = ImmutableMatrix([x, y]) assert Equality(M, IM) is S.false assert Unequality(M, IM) is S.true assert Equality(M, M.subs(x, 2)).subs(x, 2) is S.true assert Unequality(M, M.subs(x, 2)).subs(x, 2) is S.false assert Equality(M, M.subs(x, 2)).subs(x, 3) is S.false assert Unequality(M, M.subs(x, 2)).subs(x, 3) is S.true
def test_relational(): p = NumPyPrinter() e = Equality(x, 1) assert p.doprint(e) == 'equal(x, 1)' e = Unequality(x, 1) assert p.doprint(e) == 'not_equal(x, 1)' e = (x < 1) assert p.doprint(e) == 'less(x, 1)' e = (x <= 1) assert p.doprint(e) == 'less_equal(x, 1)' e = (x > 1) assert p.doprint(e) == 'greater(x, 1)' e = (x >= 1) assert p.doprint(e) == 'greater_equal(x, 1)'
def eval(cls, arg): from diofant import (Equality, GreaterThan, LessThan, StrictGreaterThan, StrictLessThan, Unequality) if isinstance(arg, Number) or arg in (True, False): return false if arg else true if arg.is_Not: return arg.args[0] # Simplify Relational objects. if isinstance(arg, Equality): return Unequality(*arg.args) if isinstance(arg, Unequality): return Equality(*arg.args) if isinstance(arg, StrictLessThan): return GreaterThan(*arg.args) if isinstance(arg, StrictGreaterThan): return LessThan(*arg.args) if isinstance(arg, LessThan): return StrictGreaterThan(*arg.args) if isinstance(arg, GreaterThan): return StrictLessThan(*arg.args)
def test_new_relational(): assert Eq(x, 0) == Relational(x, 0) # None ==> Equality assert Eq(x, 0) == Relational(x, 0, '==') assert Eq(x, 0) == Relational(x, 0, 'eq') assert Eq(x, 0) == Equality(x, 0) assert Eq(x, -1) == Relational(x, -1) # None ==> Equality assert Eq(x, -1) == Relational(x, -1, '==') assert Eq(x, -1) == Relational(x, -1, 'eq') assert Eq(x, -1) == Equality(x, -1) assert Eq(x, 0) != Relational(x, 1) # None ==> Equality assert Eq(x, 0) != Relational(x, 1, '==') assert Eq(x, 0) != Relational(x, 1, 'eq') assert Eq(x, 0) != Equality(x, 1) assert Eq(x, -1) != Relational(x, 1) # None ==> Equality assert Eq(x, -1) != Relational(x, 1, '==') assert Eq(x, -1) != Relational(x, 1, 'eq') assert Eq(x, -1) != Equality(x, 1) assert Ne(x, 0) == Relational(x, 0, '!=') assert Ne(x, 0) == Relational(x, 0, '<>') assert Ne(x, 0) == Relational(x, 0, 'ne') assert Ne(x, 0) == Unequality(x, 0) assert Ne(x, 0) != Relational(x, 1, '!=') assert Ne(x, 0) != Relational(x, 1, '<>') assert Ne(x, 0) != Relational(x, 1, 'ne') assert Ne(x, 0) != Unequality(x, 1) assert Ge(x, 0) == Relational(x, 0, '>=') assert Ge(x, 0) == Relational(x, 0, 'ge') assert Ge(x, 0) == GreaterThan(x, 0) assert Ge(x, 1) != Relational(x, 0, '>=') assert Ge(x, 1) != Relational(x, 0, 'ge') assert Ge(x, 1) != GreaterThan(x, 0) assert (x >= 1) == Relational(x, 1, '>=') assert (x >= 1) == Relational(x, 1, 'ge') assert (x >= 1) == GreaterThan(x, 1) assert (x >= 0) != Relational(x, 1, '>=') assert (x >= 0) != Relational(x, 1, 'ge') assert (x >= 0) != GreaterThan(x, 1) assert Le(x, 0) == Relational(x, 0, '<=') assert Le(x, 0) == Relational(x, 0, 'le') assert Le(x, 0) == LessThan(x, 0) assert Le(x, 1) != Relational(x, 0, '<=') assert Le(x, 1) != Relational(x, 0, 'le') assert Le(x, 1) != LessThan(x, 0) assert (x <= 1) == Relational(x, 1, '<=') assert (x <= 1) == Relational(x, 1, 'le') assert (x <= 1) == LessThan(x, 1) assert (x <= 0) != Relational(x, 1, '<=') assert (x <= 0) != Relational(x, 1, 'le') assert (x <= 0) != LessThan(x, 1) assert Gt(x, 0) == Relational(x, 0, '>') assert Gt(x, 0) == Relational(x, 0, 'gt') assert Gt(x, 0) == StrictGreaterThan(x, 0) assert Gt(x, 1) != Relational(x, 0, '>') assert Gt(x, 1) != Relational(x, 0, 'gt') assert Gt(x, 1) != StrictGreaterThan(x, 0) assert (x > 1) == Relational(x, 1, '>') assert (x > 1) == Relational(x, 1, 'gt') assert (x > 1) == StrictGreaterThan(x, 1) assert (x > 0) != Relational(x, 1, '>') assert (x > 0) != Relational(x, 1, 'gt') assert (x > 0) != StrictGreaterThan(x, 1) assert Lt(x, 0) == Relational(x, 0, '<') assert Lt(x, 0) == Relational(x, 0, 'lt') assert Lt(x, 0) == StrictLessThan(x, 0) assert Lt(x, 1) != Relational(x, 0, '<') assert Lt(x, 1) != Relational(x, 0, 'lt') assert Lt(x, 1) != StrictLessThan(x, 0) assert (x < 1) == Relational(x, 1, '<') assert (x < 1) == Relational(x, 1, 'lt') assert (x < 1) == StrictLessThan(x, 1) assert (x < 0) != Relational(x, 1, '<') assert (x < 0) != Relational(x, 1, 'lt') assert (x < 0) != StrictLessThan(x, 1) # finally, some fuzz testing for i in range(100): while 1: strtype, length = (chr, 65535) if random.randint(0, 1) else (chr, 255) relation_type = strtype(random.randint(0, length)) if random.randint(0, 1): relation_type += strtype(random.randint(0, length)) if relation_type not in ('==', 'eq', '!=', '<>', 'ne', '>=', 'ge', '<=', 'le', '>', 'gt', '<', 'lt', ':='): break pytest.raises(ValueError, lambda: Relational(x, 1, relation_type)) assert all(Relational(x, 0, op).rel_op == '==' for op in ('eq', '==')) assert all( Relational(x, 0, op).rel_op == '!=' for op in ('ne', '<>', '!=')) assert all(Relational(x, 0, op).rel_op == '>' for op in ('gt', '>')) assert all(Relational(x, 0, op).rel_op == '<' for op in ('lt', '<')) assert all(Relational(x, 0, op).rel_op == '>=' for op in ('ge', '>=')) assert all(Relational(x, 0, op).rel_op == '<=' for op in ('le', '<='))