def test_membership(self): self.assertTrue(is_member(Multiset(Set(Couplet(1, 2))))) self.assertFalse(is_member(Multiset(Couplet(3, 4)))) self.assertTrue(is_absolute_member(Multiset(Set(Couplet(1, 2))))) self.assertFalse(is_absolute_member(Multiset(Set(Couplet(Set([2, 3]), 4))))) # noinspection PyTypeChecker self.assertRaises(TypeError, lambda: is_member(3))
def test_membership(self): self.assertTrue(is_member(Multiset(Set(Couplet(1, 2))))) self.assertFalse(is_member(Multiset(Couplet(3, 4)))) self.assertTrue(is_absolute_member(Multiset(Set(Couplet(1, 2))))) self.assertFalse( is_absolute_member(Multiset(Set(Couplet(Set(2, 3), 4))))) self.assertFalse(is_absolute_member(Set(2, 3))) # noinspection PyTypeChecker self.assertRaises(AttributeError, lambda: is_member(3))
def is_absolute_member(obj: _mo.MathObject) -> bool: """Return whether ``obj`` is a member of the :term:`absolute ground set` of this algebra. :type obj: _mo.MathObject|_mo.Multiset :return: ``True`` if ``obj`` is an :term:`absolute multiset`, ``False`` if not. """ import algebraixlib.algebras.multiclans as _multiclans if not obj.is_multiset: # If known to not be a multiset, it's also not an absolute multiset. No further checking or # caching. return False # From this point on, `obj` is known to be a multiset. if obj.cached_absolute == _mo.CacheStatus.UNKNOWN: # In order to find out whether this is an absolute multiset, we need to know whether `obj` # is a multiclan (also a multiset). If it is one, it is not an absolute multiset -- but # we also don't know whether it is an absolute multiclan. So we return `False` but don't # cache anything. (But we have now cached that it is a multiclan.) if _multiclans.is_member(obj): return False is_absolute_multiset = all(elem.is_atom for elem in obj.data) obj.cache_absolute(_mo.CacheStatus.from_bool(is_absolute_multiset)) # In order to determine whether this is an absolute multiset, we need to also examine whether # this is a multiclan (also a multisets). Absolute multiclans are not absolute multisets. return obj.cached_is_absolute and not obj.cached_is_multiclan
def is_absolute_member(obj: _mo.MathObject) -> bool: """Return whether ``obj`` is a member of the :term:`absolute ground set` of this algebra. :type obj: _mo.MathObject|_mo.Multiset :return: ``True`` if ``obj`` is an :term:`absolute multiset`, ``False`` if not. """ import algebraixlib.algebras.multiclans as _multiclans if not obj.is_multiset: # If known to not be a multiset, it's also not an absolute multiset. No further checking or # caching. return False # From this point on, `obj` is known to be a multiset. if obj.cached_absolute == CacheStatus.UNKNOWN: # In order to find out whether this is an absolute multiset, we need to know whether `obj` # is a multiclan (also a multiset). If it is one, it is not an absolute multiset -- but # we also don't know whether it is an absolute multiclan. So we return `False` but don't # cache anything. (But we have now cached that it is a multiclan.) if _multiclans.is_member(obj): return False is_absolute_multiset = all(elem.is_atom for elem in obj.data) obj.cache_absolute(CacheStatus.from_bool(is_absolute_multiset)) # In order to determine whether this is an absolute multiset, we need to also examine whether # this is a multiclan (also a multisets). Absolute multiclans are not absolute multisets. return obj.cached_is_absolute and not obj.cached_is_multiclan
def export_csv(absolute_clan_or_multiclan, file_or_path, ordered_lefts=None, sort_key=None): r"""Export an absolute clan or absolute multiclan as CSV file with header row. The :term:`left component`\s of the :term:`clan` or term:`multiclan` are interpreted as column names and are exported as header row. Every :term:`relation` in the input becomes a data row in the CSV file. :param absolute_clan_or_multiclan: An :term:`absolute clan` or term:`absolute multiclan`. If it is not :term:`regular`, ``ordered_lefts`` must be given. :param file_or_path: Either a file path (in this case the CSV data is written to a file at this location) or a file object (in this case the CSV data is written to its ``.write()`` function). :param ordered_lefts: (Optional) A ``Sequence`` of :term:`left`\s that are exported in the given order. Default is the sequence that is the lexically sorted :term:`left set` of the (multi)clan. This parameter is required if ``absolute_clan_or_multiclan`` is not term:`regular`. :param sort_key: (Optional) A function that compares two row-:term:`relation`\s and provides an order (for use with :func:`sorted`). The output is not sorted if ``sort_key`` is missing. :return: ``True`` if the CSV export succeeded, ``False`` if not. """ if not _clans.is_absolute_member(absolute_clan_or_multiclan) \ and not _multiclans.is_absolute_member(absolute_clan_or_multiclan): return False regular_clan = _clans.is_member(absolute_clan_or_multiclan) \ and _clans.is_regular(absolute_clan_or_multiclan) regular_mclan = _multiclans.is_member(absolute_clan_or_multiclan) \ and _multiclans.is_regular(absolute_clan_or_multiclan) if ordered_lefts is None and not (regular_clan or regular_mclan): return False if ordered_lefts is None: # Since this clan is regular, get first relation to acquire left set. rel = next(iter(absolute_clan_or_multiclan)) # left_set is sorted to guarantee consistent iterations ordered_lefts = sorted([left.value for left in rel.get_left_set()]) # Generate dictionaries that associates left components with their right components for each # relation. clan_as_list_of_dicts = _convert_clan_to_list_of_dicts( ordered_lefts, (absolute_clan_or_multiclan if sort_key is None else sorted(absolute_clan_or_multiclan, key=sort_key))) # Write the dictionaries. _csv_dict_writer(file_or_path, ordered_lefts, clan_as_list_of_dicts) return True
def is_right_regular(mo: _mo.MathObject, _checked: bool = True) -> bool: r"""Return whether ``mo`` is :term:`right-regular` or `Undef()` if not applicable. Is implemented for :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 clans. """ # 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_right_regular == _mo.CacheStatus.IS: return True if mo.cached_right_regular == _mo.CacheStatus.IS_NOT: return False if mo.cached_right_regular == _mo.CacheStatus.N_A: return _undef.make_or_raise_undef(2) # Check type (right-regular is only defined on Sets and Multisets) and algebra memberships. if not mo.is_set and not mo.is_multiset: mo.cache_right_regular(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2) if _clans.is_member(mo): return _clans.is_right_regular(mo, _checked=False) if _multiclans.is_member(mo): return _multiclans.is_right_regular(mo, _checked=False) # Check higher (not yet defined) algebras. if mo.get_ground_set().get_powerset_level(_clans.get_ground_set()) > 0: mo_iter = iter(mo) elem1 = next(mo_iter) if not is_right_regular(elem1): mo.cache_right_regular(_mo.CacheStatus.IS_NOT) return False elem1_rights = elem1.get_rights() right_regular = all( is_right_regular(elem, _checked=False) and elem.get_rights() == elem1_rights for elem in mo_iter) mo.cache_right_regular(_mo.CacheStatus.from_bool(right_regular)) return mo.cached_is_right_regular # Nothing applied: 'right-regular' is not defined. mo.cache_right_regular(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2)
def is_right_regular(mo: _mo.MathObject, _checked: bool=True) -> bool: r"""Return whether ``mo`` is :term:`right-regular` or `Undef()` if not applicable. Is implemented for :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 clans. """ # 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_right_regular == _mo.CacheStatus.IS: return True if mo.cached_right_regular == _mo.CacheStatus.IS_NOT: return False if mo.cached_right_regular == _mo.CacheStatus.N_A: return _undef.make_or_raise_undef(2) # Check type (right-regular is only defined on Sets and Multisets) and algebra memberships. if not mo.is_set and not mo.is_multiset: mo.cache_right_regular(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2) if _clans.is_member(mo): return _clans.is_right_regular(mo, _checked=False) if _multiclans.is_member(mo): return _multiclans.is_right_regular(mo, _checked=False) # Check higher (not yet defined) algebras. if mo.get_ground_set().get_powerset_level(_clans.get_ground_set()) > 0: mo_iter = iter(mo) elem1 = next(mo_iter) if not is_right_regular(elem1): mo.cache_right_regular(_mo.CacheStatus.IS_NOT) return False elem1_rights = elem1.get_rights() right_regular = all( is_right_regular(elem, _checked=False) and elem.get_rights() == elem1_rights for elem in mo_iter) mo.cache_right_regular(_mo.CacheStatus.from_bool(right_regular)) return mo.cached_is_right_regular # Nothing applied: 'right-regular' is not defined. mo.cache_right_regular(_mo.CacheStatus.N_A) return _undef.make_or_raise_undef(2)
def test_multiset_with_one_empty_element(self): m = Multiset(Set()) self.assertEqual(m.cardinality, 1) m_repr = repr(m) m_str = str(m) self.assertEqual(m_repr, "Multiset({Set(): 1})") self.assertEqual(m_str, "[{}:1]") self.assertEqual(m.get_multiplicity(Set()), 1) self.assertEqual(m.get_multiplicity(Atom('b')), 0) m_struct = m.get_ground_set() m_expected_struct = PowerSet(CartesianProduct(Structure(), GenesisSetN())) self.assertEqual(m_struct, m_expected_struct) import algebraixlib.algebras.multiclans as _multiclans self.assertTrue(_multiclans.is_member(m)) self.assertTrue(_multiclans.is_absolute_member(m))
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 test_multiset_with_one_empty_element(self): m = Multiset(Set()) self.assertEqual(m.cardinality, 1) m_repr = repr(m) m_str = str(m) self.assertEqual(m_repr, "Multiset({Set(): 1})") self.assertEqual(m_str, "[{}:1]") self.assertEqual(m.get_multiplicity(Set()), 1) self.assertEqual(m.get_multiplicity(Atom('b')), 0) m_struct = m.get_ground_set() m_expected_struct = PowerSet( CartesianProduct(Structure(), GenesisSetN())) self.assertEqual(m_struct, m_expected_struct) import algebraixlib.algebras.multiclans as _multiclans self.assertTrue(_multiclans.is_member(m)) self.assertTrue(_multiclans.is_absolute_member(m))
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)