Exemplo n.º 1
0
def test_fuzz_sqrt():
    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x = field.random()
            if x.is_square():
                y = x.sqrt()
                assert y*y == x
            else:
                assert_raises(ValueError, x.sqrt)
Exemplo n.º 2
0
def test_fuzz_sqrt():
    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x = field.random()
            if x.is_square():
                y = x.sqrt()
                assert y * y == x
            else:
                assert_raises(ValueError, x.sqrt)
Exemplo n.º 3
0
def test_fuzz_reduce():
    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x, y, z = field.random(), field.random(), field.random()
            x1, y1, z1 = x.reduce([y, z])
            assert x*y1 == y*x1
            assert y*z1 == z*y1
            assert z*x1 == x*z1
            
            x1.reduce([])[0] == x1
            y1.reduce([])[0] == y1
            z1.reduce([])[0] == z1
Exemplo n.º 4
0
def test_fuzz_reduce():
    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x, y, z = field.random(), field.random(), field.random()
            x1, y1, z1 = x.reduce([y, z])
            assert x * y1 == y * x1
            assert y * z1 == z * y1
            assert z * x1 == x * z1

            x1.reduce([])[0] == x1
            y1.reduce([])[0] == y1
            z1.reduce([])[0] == z1
Exemplo n.º 5
0
def test_fuzz_axioms():

    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x, y, z = field.random(), field.random(), field.random()
            zero = field(0)
            one = field(1)

            # 1. Closure under addition
            assert type(x + y) == field
        

            # 2. Associative law of addition
            assert (x + y) + z == x + (y + z)

            # 3. Commutative law of addition
            assert x + y == y + x

            # 4. Existance of a zero
            assert x + zero == zero + x == x

            # 5. Existance of a negative
            w = -x
            assert x + w == 0

            # 6. Closure under multiplication
            assert type(x*y) == field

            # 7. Associative law of multiplication
            assert x*(y*z) == (x*y)*z

            # 8. Commutative law of multiplication
            assert x*y == y*x

            # 9. Existance of a one
            assert x*one == one*x == x
        
            # 10. Existance of an inverse for multiplication
            if x != 0:
                print "X =", x, x == 0, x != 0
                w = one/x
                assert x*w == x*w == one
            else:
                assert_raises(ZeroDivisionError, x.__rdiv__, one)
                assert_raises(ZeroDivisionError, one.__div__, x)

            # 11. Distributive law
            assert x*(y + z) == x*y + x*z

            # 12. Distributive law
            assert (x + y)*z == x*z + y*z
Exemplo n.º 6
0
def test_fuzz_axioms():

    for field in list(random_finite_fields(3)) + [Rational]:
        for _ in range(100):
            x, y, z = field.random(), field.random(), field.random()
            zero = field(0)
            one = field(1)

            # 1. Closure under addition
            assert type(x + y) == field

            # 2. Associative law of addition
            assert (x + y) + z == x + (y + z)

            # 3. Commutative law of addition
            assert x + y == y + x

            # 4. Existance of a zero
            assert x + zero == zero + x == x

            # 5. Existance of a negative
            w = -x
            assert x + w == 0

            # 6. Closure under multiplication
            assert type(x * y) == field

            # 7. Associative law of multiplication
            assert x * (y * z) == (x * y) * z

            # 8. Commutative law of multiplication
            assert x * y == y * x

            # 9. Existance of a one
            assert x * one == one * x == x

            # 10. Existance of an inverse for multiplication
            if x != 0:
                print "X =", x, x == 0, x != 0
                w = one / x
                assert x * w == x * w == one
            else:
                assert_raises(ZeroDivisionError, x.__rdiv__, one)
                assert_raises(ZeroDivisionError, one.__div__, x)

            # 11. Distributive law
            assert x * (y + z) == x * y + x * z

            # 12. Distributive law
            assert (x + y) * z == x * z + y * z