コード例 #1
0
    def test_compose(self):
        """Basic tests of couplets.compose()."""
        # Wrong argument types.
        try:
            self.assertRaises(AttributeError, lambda: compose(3, Couplet(1, 2)))
            self.assertRaises(AttributeError, lambda: compose(Couplet(1, 2), 3))
            self.assertIs(compose(Atom(3), Couplet(1, 2)), Undef())
            self.assertIs(compose(Couplet(1, 2), Atom(3)), Undef())
            RaiseOnUndef.set_level(1)
            self.assertRaises(UndefException, lambda: compose(Set('a', 'b'), Set('c', 'd')))
            RaiseOnUndef.reset()
        except:  # Make sure RaiseOnUndef level gets reset.
            RaiseOnUndef.reset()
            raise

        result = compose(_couplet_b_to_a, _couplet_c_to_b)
        self.assertEqual(result, _couplet_c_to_a)

        result = compose(_couplet_c_to_b, _couplet_d_to_c)
        self.assertEqual(result, _couplet_d_to_b)

        try:
            result = compose(_couplet_b_to_a, _couplet_d_to_c)
            self.assertIs(result, Undef())
            RaiseOnUndef.set_level(2)
            self.assertRaises(UndefException, lambda: compose(_couplet_b_to_a, _couplet_d_to_c))
            RaiseOnUndef.reset()
        except:  # Make sure RaiseOnUndef level gets reset.
            RaiseOnUndef.reset()
            raise

        result = compose(_couplet_c_to_b, _couplet_b_to_a)
        self.assertIs(result, Undef())
コード例 #2
0
 def test_compose(self):
     """Basic tests of couplets.compose()."""
     # Wrong argument types.
     self.assertRaises(TypeError, lambda: compose(3, Couplet(1, 2)))
     self.assertRaises(TypeError, lambda: compose(Couplet(1, 2), 3))
     self.assertIs(compose(Atom(3), Couplet(1, 2)), Undef())
     self.assertIs(compose(Couplet(1, 2), Atom(3)), Undef())
     RaiseOnUndef.set_level(1)
     self.assertRaises(UndefException, lambda: compose(Set('a', 'b'), Set('c', 'd')))
     RaiseOnUndef.reset()
     # a^b * b^c = a^c
     result = compose(_couplet_a_to_b, _couplet_b_to_c)
     self.assertEqual(result, _couplet_a_to_c)
     # b^c * c^d = b^d
     result = compose(_couplet_b_to_c, _couplet_c_to_d)
     self.assertEqual(result, _couplet_b_to_d)
     # a^b * c^d = Undef (b != c)
     result = compose(_couplet_a_to_b, _couplet_c_to_d)
     self.assertIs(result, Undef())
     RaiseOnUndef.set_level(2)
     self.assertRaises(UndefException, lambda: compose(_couplet_a_to_b, _couplet_c_to_d))
     RaiseOnUndef.reset()
     # b^c * a^b = Undef (not commutative)
     result = compose(_couplet_b_to_c, _couplet_a_to_b)
     self.assertIs(result, Undef())
コード例 #3
0
ファイル: hello_world.py プロジェクト: jdcasi/algebraixlib
transpose_result = couplets.transpose(one_two)
print("A couplet {} and its transpose {}".format(one_two, transpose_result))

# When an expression is undefined in algebraixlib, it returns a special value, the singleton Undef.
# Undef cannot be used as a value in a MathObject and cannot be compared to any value (even itself).
# Use the is and is not operators to test if a value is undefined.
from algebraixlib.undef import Undef
print(Undef() is Undef())
print(Undef() is not Undef())
print(None is not Undef())

# The binary operation composition(a^b, c^d) evaluates to a^d when b == c, otherwise it is
# undefined.
a_to_b = Couplet('a', 'b')  # b^a
b_to_c = Couplet('b', 'c')  # c^b
print(couplets.compose(b_to_c, a_to_b))  # c^a
print(couplets.compose(a_to_b, b_to_c))  # undef, composition is not commutative

# Sets are used to create unordered collections of unique MathObjects. Non-MathObjects will be
# coerced into Atoms.
from algebraixlib.mathobjects import Set
many = Set(Atom("hello"), "world", Couplet("hola", "mundo"), "duplicate", "duplicate")
print(repr(many))

# Sets support for...in syntax for iteration and in and not in syntax for membership tests. Because
# sets are unordered, they do not support random access (no bracket operator).
nums = Set(1, 2, 3, 4, 5)
for elem in nums:
    print(elem, " ")
print(1 in nums)
print(7 in nums)
コード例 #4
0
transpose_result = couplets.transpose(one_two)
print("A couplet {} and its transpose {}".format(one_two, transpose_result))

# When an expression is undefined in algebraixlib, it returns a special value, the singleton Undef.
# Undef cannot be used as a value in a MathObject and cannot be compared to any value (even itself).
# Use the is and is not operators to test if a value is undefined.
from algebraixlib.undef import Undef
print(Undef() is Undef())
print(Undef() is not Undef())
print(None is not Undef())

# The binary operation composition(c->d, a->b) evaluates to a->d when b == c, otherwise it is
# undefined.
a_to_b = Couplet('a', 'b')  # a->b
b_to_c = Couplet('b', 'c')  # b->c
print(couplets.compose(b_to_c, a_to_b))  # a->c
print(couplets.compose(a_to_b,
                       b_to_c))  # undef, composition is not commutative

# Sets are used to create unordered collections of unique MathObjects. Non-MathObjects will be
# coerced into Atoms.
from algebraixlib.mathobjects import Set
many = Set(Atom("hello"), "world", Couplet("hola", "mundo"), "duplicate",
           "duplicate")
print(repr(many))

# Sets support for...in syntax for iteration and in and not in syntax for membership tests. Because
# sets are unordered, they do not support random access (no bracket operator).
nums = Set(1, 2, 3, 4, 5)
for elem in nums:
    print(elem, " ")
コード例 #5
0
    def test_compose(self):
        """Basic tests of couplets.compose()."""

        # Wrong argument types.
        self.assertRaises(AttributeError, lambda: compose(3, Couplet(1, 2)))
        self.assertRaises(AttributeError, lambda: compose(Couplet(1, 2), 3))
        self.assertIs(compose(Atom(3), Couplet(1, 2)), Undef())
        self.assertIs(compose(Couplet(1, 2), Atom(3)), Undef())
        self.assertRaises(
            AssertionError,
            lambda: compose(Atom(3), Couplet(1, 2), _checked=False))
        self.assertRaises(
            AssertionError,
            lambda: compose(Couplet(1, 2), Atom(3), _checked=False))

        # Wrong argument type is will raise with error level 1, but not with the default of 0
        self.assertIs(compose(Set('a', 'b'), Set('c', 'd')), Undef())
        RaiseOnUndef.set_level(1)
        # Repeat the same compose with new level...generates exception
        self.assertRaises(UndefException,
                          lambda: compose(Set('a', 'b'), Set('c', 'd')))
        # At raise level 1, Undef will not raise
        self.assertIs(compose(Couplet(1, 2), Undef()), Undef())
        RaiseOnUndef.set_level(2)
        # Repeat the same compose with new level...generates exception
        self.assertRaises(UndefException,
                          lambda: compose(Couplet(1, 2), Undef()))
        RaiseOnUndef.reset()

        result = compose(_couplet_b_to_a, _couplet_c_to_b)
        self.assertEqual(result, _couplet_c_to_a)

        result = compose(_couplet_c_to_b, _couplet_d_to_c)
        self.assertEqual(result, _couplet_d_to_b)

        result = compose(_couplet_b_to_a, _couplet_d_to_c)
        self.assertIs(result, Undef())
        result = compose(Undef(), _couplet_d_to_c)
        self.assertIs(result, Undef())
        result = compose(_couplet_b_to_a, Undef())
        self.assertIs(result, Undef())
        result = compose(Undef(), _couplet_d_to_c, _checked=False)
        self.assertIs(result, Undef())
        RaiseOnUndef.set_level(2)
        self.assertRaises(UndefException,
                          lambda: compose(_couplet_b_to_a, _couplet_d_to_c))
        RaiseOnUndef.reset()

        result = compose(_couplet_c_to_b, _couplet_b_to_a)
        self.assertIs(result, Undef())
コード例 #6
0
    def test_compose(self):
        """Basic tests of couplets.compose()."""

        # Wrong argument types.
        self.assertRaises(AttributeError, lambda: compose(3, Couplet(1, 2)))
        self.assertRaises(AttributeError, lambda: compose(Couplet(1, 2), 3))
        self.assertIs(compose(Atom(3), Couplet(1, 2)), Undef())
        self.assertIs(compose(Couplet(1, 2), Atom(3)), Undef())
        self.assertRaises(AssertionError, lambda: compose(Atom(3), Couplet(1, 2), _checked=False))
        self.assertRaises(AssertionError, lambda: compose(Couplet(1, 2), Atom(3), _checked=False))

        # Wrong argument type is will raise with error level 1, but not with the default of 0
        self.assertIs(compose(Set("a", "b"), Set("c", "d")), Undef())
        RaiseOnUndef.set_level(1)
        # Repeat the same compose with new level...generates exception
        self.assertRaises(UndefException, lambda: compose(Set("a", "b"), Set("c", "d")))
        # At raise level 1, Undef will not raise
        self.assertIs(compose(Couplet(1, 2), Undef()), Undef())
        RaiseOnUndef.set_level(2)
        # Repeat the same compose with new level...generates exception
        self.assertRaises(UndefException, lambda: compose(Couplet(1, 2), Undef()))
        RaiseOnUndef.reset()

        result = compose(_couplet_b_to_a, _couplet_c_to_b)
        self.assertEqual(result, _couplet_c_to_a)

        result = compose(_couplet_c_to_b, _couplet_d_to_c)
        self.assertEqual(result, _couplet_d_to_b)

        result = compose(_couplet_b_to_a, _couplet_d_to_c)
        self.assertIs(result, Undef())
        result = compose(Undef(), _couplet_d_to_c)
        self.assertIs(result, Undef())
        result = compose(_couplet_b_to_a, Undef())
        self.assertIs(result, Undef())
        result = compose(Undef(), _couplet_d_to_c, _checked=False)
        self.assertIs(result, Undef())
        RaiseOnUndef.set_level(2)
        self.assertRaises(UndefException, lambda: compose(_couplet_b_to_a, _couplet_d_to_c))
        RaiseOnUndef.reset()

        result = compose(_couplet_c_to_b, _couplet_b_to_a)
        self.assertIs(result, Undef())