def is_reflexive(mo: _mo.MathObject, _checked: bool = True) -> bool: r"""Return whether ``mo`` is :term:`reflexive` or `Undef()` if not applicable. Is implemented for :term:`couplet`\s, :term:`relation`\s, :term:`clan`\s, :term:`multiclan`\s and :term:`set`\s of (sets of ...) clans. Is also defined (but not yet implemented) for any combination of sets or :term:`multiset`\s of relations. """ # pylint: disable=too-many-return-statements if _checked: if not isinstance(mo, _mo.MathObject): return _undef.make_or_raise_undef() # Check cache status. if mo.cached_reflexive == _mo.CacheStatus.IS: return True if mo.cached_reflexive == _mo.CacheStatus.IS_NOT: return False if mo.cached_reflexive == _mo.CacheStatus.N_A: return _undef.make_or_raise_undef(2) # Check types and algebra memberships. if _couplets.is_member(mo): return _couplets.is_reflexive(mo, _checked=False) if not mo.is_set and not mo.is_multiset: mo.cache_reflexive(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2) if _relations.is_member(mo): return _relations.is_reflexive(mo, _checked=False) if _clans.is_member(mo): return _clans.is_reflexive(mo, _checked=False) if _multiclans.is_member(mo): return _multiclans.is_reflexive(mo, _checked=False) # Check higher (not yet defined) algebras. reflexive = _is_powerset_property(mo, _clans.get_ground_set(), is_reflexive) if reflexive is not _undef.Undef(): mo.cache_reflexive(_mo.CacheStatus.from_bool(reflexive)) return reflexive # Nothing applied: 'reflexive' is not defined. mo.cache_reflexive(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2)
def is_reflexive(mo: _mo.MathObject, _checked: bool=True) -> bool: r"""Return whether ``mo`` is :term:`reflexive` or `Undef()` if not applicable. Is implemented for :term:`couplet`\s, :term:`relation`\s, :term:`clan`\s, :term:`multiclan`\s and :term:`set`\s of (sets of ...) clans. Is also defined (but not yet implemented) for any combination of sets or :term:`multiset`\s of relations. """ # pylint: disable=too-many-return-statements if _checked: if not isinstance(mo, _mo.MathObject): return _undef.make_or_raise_undef() # Check cache status. if mo.cached_reflexive == _mo.CacheStatus.IS: return True if mo.cached_reflexive == _mo.CacheStatus.IS_NOT: return False if mo.cached_reflexive == _mo.CacheStatus.N_A: return _undef.make_or_raise_undef(2) # Check types and algebra memberships. if _couplets.is_member(mo): return _couplets.is_reflexive(mo, _checked=False) if not mo.is_set and not mo.is_multiset: mo.cache_reflexive(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2) if _relations.is_member(mo): return _relations.is_reflexive(mo, _checked=False) if _clans.is_member(mo): return _clans.is_reflexive(mo, _checked=False) if _multiclans.is_member(mo): return _multiclans.is_reflexive(mo, _checked=False) # Check higher (not yet defined) algebras. reflexive = _is_powerset_property(mo, _clans.get_ground_set(), is_reflexive) if reflexive is not _undef.Undef(): mo.cache_reflexive(_mo.CacheStatus.from_bool(reflexive)) return reflexive # Nothing applied: 'reflexive' is not defined. mo.cache_reflexive(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2)
def functional_add(func: 'P(M x M)', element: 'M x M') -> 'P(M x M)': """Add ``element`` to ``func`` and return the new functional relation. :param func: The source data. Must be a :term:`function`. It must not contain a :term:`couplet` with the same :term:`left component` as ``element``. :param element: The element to be added to ``func``. Must be a :class:`~.Couplet` and its :term:`left component` must not be a left component in ``func``. :return: The new relation, composed of ``func`` and ``element``. """ if not is_member(func) or not is_functional(func): return _undef.make_or_raise_undef2(func) if not _couplets.is_member(element): return _undef.make_or_raise_undef2(element) if _sets.is_subset_of(_mo.Set(element.left), get_lefts(func)): return _undef.make_or_raise_undef(2) element_func = _mo.Set(element).cache_relation(_mo.CacheStatus.IS) result = _sets.union(func, element_func) assert result.cached_is_relation and is_functional(result) result.cache_functional(_mo.CacheStatus.IS) return result
def test_membership(self): self.assertTrue(is_member(Couplet(1, 2))) self.assertFalse(is_member(Atom(3))) self.assertTrue(is_absolute_member(Couplet(1, 2))) self.assertFalse(is_absolute_member(Couplet(Set(1), 2))) self.assertRaises(AttributeError, lambda: is_member(3))
def test_membership(self): self.assertTrue(is_member(Couplet(1, 2))) self.assertFalse(is_member(Atom(3))) self.assertFalse(is_member(Undef())) self.assertRaises(AttributeError, lambda: is_member(3))