def test_membership(self):
     self.assertTrue(is_member(Multiset()))
     self.assertTrue(is_member(Multiset(3)))
     self.assertFalse(is_member(Atom(3)))
     self.assertTrue(is_absolute_member(Multiset(3)))
     self.assertFalse(is_absolute_member(Multiset(Couplet(3, 4))))
     self.assertRaises(TypeError, lambda: is_member(3))
 def test_membership(self):
     self.assertTrue(is_member(Multiset()))
     self.assertTrue(is_member(Multiset(3)))
     self.assertFalse(is_member(Atom(3)))
     self.assertTrue(is_absolute_member(Multiset(3)))
     self.assertFalse(is_absolute_member(Multiset(Couplet(3, 4))))
     self.assertRaises(AttributeError, lambda: is_member(3))
Exemple #3
0
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))
Exemple #4
0
def binary_multi_extend(multiset1: 'P( M x N )',
                        multiset2: 'P( M x N )',
                        op,
                        _checked=True) -> 'P( M x N )':
    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:`multiset`\s of the
    elements of the original algebra.

    :param multiset1: A :term:`multiset` with elements on which ``op`` operates.
    :param multiset2: A multiset with elements on which ``op`` operates.
    :param op: A :term:`binary operation` that operates on the elements of ``multiset1`` and
        ``multiset2``.
    :return: A multiset that consists of the defined results of ``op`` when executed on all
        combinations of the elements of ``multiset1`` and ``multiset2``, or `Undef()` if either
        set is not a :class:`~.Multiset`.
    """
    if _checked:
        if not _multisets.is_member(multiset1):
            return _undef.make_or_raise_undef2(multiset1)
        if not _multisets.is_member(multiset2):
            return _undef.make_or_raise_undef2(multiset2)
    else:
        assert _multisets.is_member_or_undef(multiset1)
        assert _multisets.is_member_or_undef(multiset2)
        if multiset1 is _undef.Undef() or multiset2 is _undef.Undef():
            return _undef.make_or_raise_undef(2)

    def _get_values(_set1, _set2):
        return_count = _collections.Counter()
        for elem1, multi1 in _set1.data.items():
            for elem2, multi2 in _set2.data.items():
                result = op(elem1, elem2)
                if result is not _undef.Undef():
                    return_count[result] += multi1 * multi2

        return return_count

    return _mo.Multiset(_get_values(multiset1, multiset2), direct_load=True)
Exemple #5
0
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))
Exemple #6
0
def binary_multi_extend(multiset1: 'P( M x N )', multiset2: 'P( M x N )', op,
                        _checked=True) -> 'P( M x N )':
    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:`multiset`\s of the
    elements of the original algebra.

    :param multiset1: A :term:`multiset` with elements on which ``op`` operates.
    :param multiset2: A multiset with elements on which ``op`` operates.
    :param op: A :term:`binary operation` that operates on the elements of ``multiset1`` and
        ``multiset2``.
    :return: A multiset that consists of the defined results of ``op`` when executed on all
        combinations of the elements of ``multiset1`` and ``multiset2``, or `Undef()` if either
        set is not a :class:`~.Multiset`.
    """
    if _checked:
        if not _multisets.is_member(multiset1):
            return _undef.make_or_raise_undef2(multiset1)
        if not _multisets.is_member(multiset2):
            return _undef.make_or_raise_undef2(multiset2)
    else:
        assert _multisets.is_member_or_undef(multiset1)
        assert _multisets.is_member_or_undef(multiset2)
        if multiset1 is _undef.Undef() or multiset2 is _undef.Undef():
            return _undef.make_or_raise_undef(2)

    def _get_values(_set1, _set2):
        return_count = _collections.Counter()
        for elem1, multi1 in _set1.data.items():
            for elem2, multi2 in _set2.data.items():
                result = op(elem1, elem2)
                if result is not _undef.Undef():
                    return_count[result] += multi1 * multi2

        return return_count

    return _mo.Multiset(_get_values(multiset1, multiset2), direct_load=True)