def test_transpose(self):
     """Basic tests of couplets.transpose()."""
     # Wrong argument types.
     self.assertRaises(TypeError, lambda: transpose(3))
     self.assertIs(transpose(Atom(3)), Undef())
     RaiseOnUndef.set_level(1)
     self.assertRaises(UndefException, lambda: transpose(Set('a', 'b')))
     RaiseOnUndef.reset()
     # T(a^b) = b^a
     result = transpose(_couplet_a_to_b)
     self.assertEqual(result, _couplet_b_to_a)
    def test_transpose(self):
        """Basic tests of couplets.transpose()."""
        # Wrong argument types.
        try:
            self.assertRaises(AttributeError, lambda: transpose(3))
            self.assertIs(transpose(Atom(3)), Undef())
            RaiseOnUndef.set_level(1)
            self.assertRaises(UndefException, lambda: transpose(Set('a', 'b')))
            RaiseOnUndef.reset()
        except:  # Make sure RaiseOnUndef level gets reset.
            RaiseOnUndef.reset()
            raise

        result = transpose(_couplet_b_to_a)
        self.assertEqual(result, _couplet_a_to_b)
Exemple #3
0
def is_symmetric(rel, _checked=True) -> bool:
    """Return ``True`` if ``rel`` is :term:`symmetric`.

    :return: ``True`` if ``rel`` is :term:`symmetric` or `Undef()` if ``rel`` is not a
        :term:`relation`.
    """
    if _checked:
        if not is_member(rel):
            return _make_or_raise_undef()
    else:
        assert is_member(rel)
    if not rel.cached_is_symmetric and not rel.cached_is_not_symmetric:
        symmetric = all(rel.has_element(
            _couplets.transpose(couplet, _checked=False)) for couplet in rel)
        rel.cache_is_symmetric(symmetric)
    return rel.cached_is_symmetric
Exemple #4
0
def is_symmetric(rel, _checked=True) -> bool:
    """Return whether ``rel`` is symmetric.

    :return: ``True`` if ``rel`` is :term:`symmetric`, ``False`` if it is not, or `Undef()` if
        ``rel`` is not a :term:`relation`.
    """
    if _checked:
        if not is_member(rel):
            return _undef.make_or_raise_undef()
    else:
        assert is_member(rel)
    if rel.cached_symmetric == _mo.CacheStatus.UNKNOWN:
        symmetric = all(rel.has_element(
            _couplets.transpose(couplet, _checked=False)) for couplet in rel)
        rel.cache_symmetric(_mo.CacheStatus.from_bool(symmetric))
    return rel.cached_symmetric == _mo.CacheStatus.IS
    def test_transpose(self):
        """Basic tests of couplets.transpose()."""

        # Wrong argument types.
        self.assertIs(transpose(Atom(3)), Undef())
        self.assertIs(transpose(Undef()), Undef())
        self.assertIs(transpose(Undef(), _checked=False), Undef())
        self.assertRaises(AttributeError, lambda: transpose(3))
        self.assertRaises(AssertionError, lambda: transpose(Atom(3), _checked=False))
        RaiseOnUndef.set_level(1)
        self.assertRaises(UndefException, lambda: transpose(Set('a', 'b')))
        RaiseOnUndef.reset()

        result = transpose(_couplet_b_to_a)
        self.assertEqual(result, _couplet_a_to_b)
    def test_transpose(self):
        """Basic tests of couplets.transpose()."""

        # Wrong argument types.
        self.assertIs(transpose(Atom(3)), Undef())
        self.assertIs(transpose(Undef()), Undef())
        self.assertIs(transpose(Undef(), _checked=False), Undef())
        self.assertRaises(AttributeError, lambda: transpose(3))
        self.assertRaises(AssertionError, lambda: transpose(Atom(3), _checked=False))
        RaiseOnUndef.set_level(1)
        self.assertRaises(UndefException, lambda: transpose(Set("a", "b")))
        RaiseOnUndef.reset()

        result = transpose(_couplet_b_to_a)
        self.assertEqual(result, _couplet_a_to_b)
Exemple #7
0
def is_symmetric(rel, _checked=True) -> bool:
    """Return whether ``rel`` is symmetric.

    :return: ``True`` if ``rel`` is :term:`symmetric`, ``False`` if it is not, or `Undef()` if
        ``rel`` is not a :term:`relation`.
    """
    if _checked:
        if not is_member(rel):
            return _undef.make_or_raise_undef2(rel)
    else:
        assert is_member_or_undef(rel)
        if rel is _undef.Undef():
            return _undef.make_or_raise_undef(2)
    if rel.cached_symmetric == _mo.CacheStatus.UNKNOWN:
        symmetric = all(
            rel.has_element(_couplets.transpose(couplet, _checked=False))
            for couplet in rel)
        rel.cache_symmetric(_mo.CacheStatus.from_bool(symmetric))
    return rel.cached_symmetric == _mo.CacheStatus.IS
Exemple #8
0
together = Couplet(peanut_butter, jelly)
print(together)

# MathObject initializers will coerce their arguments to be Atoms if non-MathObjects are passed.
coerced = Couplet("this", "that")
print(repr(coerced))

# The components of a Couplet are known as its left and right (abbreviated as 'right').
# Sometimes initializng a Couplet with named arguments can add clarity.
up_down = Couplet(left="up", right="down")
print("left is {}, right is {}".format(up_down.left, up_down.right))

# A Couplet's components can be swapped by evaluating the unary operation transpose.
import algebraixlib.algebras.couplets as couplets
one_two = Couplet(1, 2)
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
Exemple #9
0
together = Couplet(peanut_butter, jelly)
print(together)

# MathObject initializers will coerce their arguments to be Atoms if non-MathObjects are passed.
coerced = Couplet("this", "that")
print(repr(coerced))

# The components of a Couplet are known as its left and right (abbreviated as 'right').
# Sometimes initializng a Couplet with named arguments can add clarity.
up_down = Couplet(left="up", right="down")
print("left is {}, right is {}".format(up_down.left, up_down.right))

# A Couplet's components can be swapped by evaluating the unary operation transpose.
import algebraixlib.algebras.couplets as couplets
one_two = Couplet(1, 2)
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