def test_membership(self): self.assertTrue(is_member(Set())) self.assertTrue(is_member(Set(3))) self.assertFalse(is_member(Atom(3))) self.assertTrue(is_absolute_member(Set(3))) self.assertFalse(is_absolute_member(Set(Couplet(3, 4)))) self.assertRaises(AttributeError, lambda: is_member(3))
def binary_extend(set1: 'P( M )', set2: 'P( M )', op, _checked=True) -> 'P( M )': r"""Return the :term:`binary extension` of ``op`` from one :term:`algebra` to another algebra. For this extension, the elements of the extended algebra must be :term:`set`\s of the elements of the original algebra. :param set1: A :term:`set` with elements on which ``op`` operates. :param set2: A set with elements on which ``op`` operates. :param op: A :term:`binary operation` that operates on the elements of ``set1`` and ``set2``. :return: A set that consists of the defined results of ``op`` when executed on all combinations of the elements of ``set1`` and ``set2``, or `Undef()` if either set is not a :class:`~.Set`. """ if _checked: if not _sets.is_member(set1): return _undef.make_or_raise_undef2(set1) if not _sets.is_member(set2): return _undef.make_or_raise_undef2(set2) else: assert _sets.is_member_or_undef(set1) assert _sets.is_member_or_undef(set2) if set1 is _undef.Undef() or set2 is _undef.Undef(): return _undef.make_or_raise_undef(2) def _get_values(_set1, _set2): for e1 in _set1: for e2 in _set2: result = op(e1, e2) if result is not _undef.Undef(): yield result return _mo.Set(_get_values(set1, set2), direct_load=True)
def test_flags_relation(self): r = Set(Couplet(s, c) for s, c in zip('abc', [1, 2, 3])) self.assertEqual(r.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(r.cached_clan, CacheStatus.UNKNOWN) self.assertTrue(sets.is_member(r)) # This will NOT trigger structure check self.assertEqual(r.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(r.cached_clan, CacheStatus.UNKNOWN) self.assertTrue(relations.is_member(r)) # This will trigger structure check self.assertEqual(r.cached_relation, CacheStatus.IS) self.assertEqual(r.cached_clan, CacheStatus.IS_NOT)
def test_flags_set(self): s = Set(1, 2, 3) self.assertEqual(s.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(s.cached_clan, CacheStatus.UNKNOWN) self.assertTrue(sets.is_member(s)) # This will NOT trigger structure check self.assertEqual(s.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(s.cached_clan, CacheStatus.UNKNOWN) self.assertFalse(relations.is_member(s)) # This will trigger structure check self.assertEqual(s.cached_relation, CacheStatus.IS_NOT) self.assertEqual(s.cached_clan, CacheStatus.UNKNOWN)
def test_flags_relation(self): r = Set(Couplet(s, c) for s, c in zip("abc", [1, 2, 3])) self.assertEqual(r.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(r.cached_clan, CacheStatus.UNKNOWN) self.assertTrue(sets.is_member(r)) # This will NOT trigger structure check self.assertEqual(r.cached_relation, CacheStatus.UNKNOWN) self.assertEqual(r.cached_clan, CacheStatus.UNKNOWN) self.assertTrue(relations.is_member(r)) # This will trigger structure check self.assertEqual(r.cached_relation, CacheStatus.IS) self.assertEqual(r.cached_clan, CacheStatus.IS_NOT)
def unary_multi_extend(set_or_mset, op, _checked=True) -> 'P( M x N )': r"""Return the :term:`unary extension` of ``op`` from one :term:`algebra` to another algebra. For this extension, the elements of the extended algebra must be :term:`multiset`\s of the elements of the original algebra. :param set_or_mset: A :term:`set` or a :term:`multiset` with elements on which ``op`` operates. :param op: A :term:`unary operation` that operates on the elements of ``set_or_mset``. :return: A set that consists of the defined results of ``op`` when executed on the elements of ``set_or_mset``, or `Undef()` if ``set_or_mset`` is neither a set nor a multiset. """ if _checked: if not _multisets.is_member(set_or_mset) and not _sets.is_member( set_or_mset): return _undef.make_or_raise_undef2(set_or_mset) else: assert _multisets.is_member(set_or_mset) or _sets.is_member(set_or_mset) \ or set_or_mset is _undef.Undef() if set_or_mset is _undef.Undef(): return _undef.make_or_raise_undef(2) def _get_values_set(set_): result_counter = _collections.Counter() for elem in set_: result = op(elem) if result is not _undef.Undef(): result_counter[result] += 1 return result_counter def _get_values_multiset(mset): result_counter = _collections.Counter() for elem, multiplicity in mset.data.items(): result = op(elem) if result is not _undef.Undef(): result_counter[result] += multiplicity return result_counter get_values = _get_values_multiset if _multisets.is_member( set_or_mset) else _get_values_set return _mo.Multiset(get_values(set_or_mset))
def unary_multi_extend(set_or_mset, op, _checked=True) -> 'P( M x N )': r"""Return the :term:`unary extension` of ``op`` from one :term:`algebra` to another algebra. For this extension, the elements of the extended algebra must be :term:`multiset`\s of the elements of the original algebra. :param set_or_mset: A :term:`set` or a :term:`multiset` with elements on which ``op`` operates. :param op: A :term:`unary operation` that operates on the elements of ``set_or_mset``. :return: A set that consists of the defined results of ``op`` when executed on the elements of ``set_or_mset``, or `Undef()` if ``set_or_mset`` is neither a set nor a multiset. """ if _checked: if not _multisets.is_member(set_or_mset) and not _sets.is_member(set_or_mset): return _undef.make_or_raise_undef2(set_or_mset) else: assert _multisets.is_member(set_or_mset) or _sets.is_member(set_or_mset) \ or set_or_mset is _undef.Undef() if set_or_mset is _undef.Undef(): return _undef.make_or_raise_undef(2) def _get_values_set(set_): result_counter = _collections.Counter() for elem in set_: result = op(elem) if result is not _undef.Undef(): result_counter[result] += 1 return result_counter def _get_values_multiset(mset): result_counter = _collections.Counter() for elem, multiplicity in mset.data.items(): result = op(elem) if result is not _undef.Undef(): result_counter[result] += multiplicity return result_counter get_values = _get_values_multiset if _multisets.is_member(set_or_mset) else _get_values_set return _mo.Multiset(get_values(set_or_mset))