Exemplo n.º 1
0
 def __new__(cls, *args, **options):
     args = [_sympify(arg) for arg in args]
     argset = multiset(args)  # dictionary
     args_final = []
     # xor is commutative and is false if count of x is even and x
     # if count of x is odd. Here x can be True, False or any Symbols
     for x, freq in argset.items():
         if freq % 2 == 0:
             argset[x] = false
         else:
             argset[x] = x
     for _, z in argset.items():
         args_final.append(z)
     argset = set(args_final)
     truecount = 0
     for x in args:
         if isinstance(x, Number) or x in [True, False]:  # Includes 0, 1
             argset.discard(x)
             if x:
                 truecount += 1
     if len(argset) < 1:
         return true if truecount % 2 != 0 else false
     if truecount % 2 != 0:
         return Not(Xor(*argset))
     _args = frozenset(argset)
     obj = super(Xor, cls).__new__(cls, *_args, **options)
     if isinstance(obj, Xor):
         obj._argset = _args
     return obj
Exemplo n.º 2
0
def test_multiset_permutations():
    ans = [
        'abby', 'abyb', 'aybb', 'baby', 'bayb', 'bbay', 'bbya', 'byab', 'byba',
        'yabb', 'ybab', 'ybba'
    ]
    assert [''.join(i) for i in multiset_permutations('baby')] == ans
    assert [''.join(i) for i in multiset_permutations(multiset('baby'))] == ans
    assert list(multiset_permutations([0, 0, 0], 2)) == [[0, 0]]
    assert list(multiset_permutations([0, 2, 1],
                                      2)) == [[0, 1], [0, 2], [1, 0], [1, 2],
                                              [2, 0], [2, 1]]
    assert len(list(multiset_permutations('a', 0))) == 1
    assert len(list(multiset_permutations('a', 3))) == 0

    def test():
        for i in range(1, 7):
            print(i)
            for p in multiset_permutations([0, 0, 1, 0, 1], i):
                print(p)

    assert capture(lambda: test()) == dedent('''\
        1
        [0]
        [1]
        2
        [0, 0]
        [0, 1]
        [1, 0]
        [1, 1]
        3
        [0, 0, 0]
        [0, 0, 1]
        [0, 1, 0]
        [0, 1, 1]
        [1, 0, 0]
        [1, 0, 1]
        [1, 1, 0]
        4
        [0, 0, 0, 1]
        [0, 0, 1, 0]
        [0, 0, 1, 1]
        [0, 1, 0, 0]
        [0, 1, 0, 1]
        [0, 1, 1, 0]
        [1, 0, 0, 0]
        [1, 0, 0, 1]
        [1, 0, 1, 0]
        [1, 1, 0, 0]
        5
        [0, 0, 0, 1, 1]
        [0, 0, 1, 0, 1]
        [0, 0, 1, 1, 0]
        [0, 1, 0, 0, 1]
        [0, 1, 0, 1, 0]
        [0, 1, 1, 0, 0]
        [1, 0, 0, 0, 1]
        [1, 0, 0, 1, 0]
        [1, 0, 1, 0, 0]
        [1, 1, 0, 0, 0]
        6\n''')
Exemplo n.º 3
0
 def __new__(cls, *args, **options):
     args = [_sympify(arg) for arg in args]
     argset = multiset(args)  # dictionary
     args_final=[]
     # xor is commutative and is false if count of x is even and x
     # if count of x is odd. Here x can be True, False or any Symbols
     for x, freq in argset.items():
         if freq % 2 == 0:
             argset[x] = false
         else:
             argset[x] = x
     for _, z in argset.items():
         args_final.append(z)
     argset = set(args_final)
     truecount = 0
     for x in args:
         if isinstance(x, Number) or x in [True, False]: # Includes 0, 1
             argset.discard(x)
             if x:
                 truecount += 1
     if len(argset) < 1:
         return true if truecount % 2 != 0 else false
     if truecount % 2 != 0:
         return Not(Xor(*argset))
     _args = frozenset(argset)
     obj = super(Xor, cls).__new__(cls, *_args, **options)
     if isinstance(obj, Xor):
         obj._argset = _args
     return obj
Exemplo n.º 4
0
def test_multiset_combinations():
    ans = [
        "iii",
        "iim",
        "iip",
        "iis",
        "imp",
        "ims",
        "ipp",
        "ips",
        "iss",
        "mpp",
        "mps",
        "mss",
        "pps",
        "pss",
        "sss",
    ]
    assert ["".join(i) for i in list(multiset_combinations("mississippi", 3))] == ans
    M = multiset("mississippi")
    assert ["".join(i) for i in list(multiset_combinations(M, 3))] == ans
    assert ["".join(i) for i in multiset_combinations(M, 30)] == []
    assert list(multiset_combinations([[1], [2, 3]], 2)) == [[[1], [2, 3]]]
    assert len(list(multiset_combinations("a", 3))) == 0
    assert len(list(multiset_combinations("a", 0))) == 1
    assert list(multiset_combinations("abc", 1)) == [["a"], ["b"], ["c"]]
Exemplo n.º 5
0
def test_multiset_permutations():
    ans = ["abby", "abyb", "aybb", "baby", "bayb", "bbay", "bbya", "byab", "byba", "yabb", "ybab", "ybba"]
    assert ["".join(i) for i in multiset_permutations("baby")] == ans
    assert ["".join(i) for i in multiset_permutations(multiset("baby"))] == ans
    assert list(multiset_permutations([0, 0, 0], 2)) == [[0, 0]]
    assert list(multiset_permutations([0, 2, 1], 2)) == [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
    assert len(list(multiset_permutations("a", 0))) == 1
    assert len(list(multiset_permutations("a", 3))) == 0

    def test():
        for i in range(1, 7):
            print(i)
            for p in multiset_permutations([0, 0, 1, 0, 1], i):
                print(p)

    assert capture(lambda: test()) == dedent(
        """\
        1
        [0]
        [1]
        2
        [0, 0]
        [0, 1]
        [1, 0]
        [1, 1]
        3
        [0, 0, 0]
        [0, 0, 1]
        [0, 1, 0]
        [0, 1, 1]
        [1, 0, 0]
        [1, 0, 1]
        [1, 1, 0]
        4
        [0, 0, 0, 1]
        [0, 0, 1, 0]
        [0, 0, 1, 1]
        [0, 1, 0, 0]
        [0, 1, 0, 1]
        [0, 1, 1, 0]
        [1, 0, 0, 0]
        [1, 0, 0, 1]
        [1, 0, 1, 0]
        [1, 1, 0, 0]
        5
        [0, 0, 0, 1, 1]
        [0, 0, 1, 0, 1]
        [0, 0, 1, 1, 0]
        [0, 1, 0, 0, 1]
        [0, 1, 0, 1, 0]
        [0, 1, 1, 0, 0]
        [1, 0, 0, 0, 1]
        [1, 0, 0, 1, 0]
        [1, 0, 1, 0, 0]
        [1, 1, 0, 0, 0]
        6\n"""
    )
Exemplo n.º 6
0
def test_multiset_permutations():
    ans = ['abby', 'abyb', 'aybb', 'baby', 'bayb', 'bbay', 'bbya', 'byab',
           'byba', 'yabb', 'ybab', 'ybba']
    assert [''.join(i) for i in multiset_permutations('baby')] == ans
    assert [''.join(i) for i in multiset_permutations(multiset('baby'))] == ans
    assert list(multiset_permutations([0, 0, 0], 2)) == [[0, 0]]
    assert list(multiset_permutations([0, 2, 1], 2)) == [
        [0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
    assert len(list(multiset_permutations('a', 0))) == 1
    assert len(list(multiset_permutations('a', 3))) == 0

    def test():
        for i in range(1, 7):
            print i
            for p in multiset_permutations([0, 0, 1, 0, 1], i):
                print p
    assert capture(lambda: test()) == dedent('''\
        1
        [0]
        [1]
        2
        [0, 0]
        [0, 1]
        [1, 0]
        [1, 1]
        3
        [0, 0, 0]
        [0, 0, 1]
        [0, 1, 0]
        [0, 1, 1]
        [1, 0, 0]
        [1, 0, 1]
        [1, 1, 0]
        4
        [0, 0, 0, 1]
        [0, 0, 1, 0]
        [0, 0, 1, 1]
        [0, 1, 0, 0]
        [0, 1, 0, 1]
        [0, 1, 1, 0]
        [1, 0, 0, 0]
        [1, 0, 0, 1]
        [1, 0, 1, 0]
        [1, 1, 0, 0]
        5
        [0, 0, 0, 1, 1]
        [0, 0, 1, 0, 1]
        [0, 0, 1, 1, 0]
        [0, 1, 0, 0, 1]
        [0, 1, 0, 1, 0]
        [0, 1, 1, 0, 0]
        [1, 0, 0, 0, 1]
        [1, 0, 0, 1, 0]
        [1, 0, 1, 0, 0]
        [1, 1, 0, 0, 0]
        6\n''')
Exemplo n.º 7
0
def test_multiset_combinations():
    ans = ["iii", "iim", "iip", "iis", "imp", "ims", "ipp", "ips", "iss", "mpp", "mps", "mss", "pps", "pss", "sss"]
    assert ["".join(i) for i in list(multiset_combinations("mississippi", 3))] == ans
    M = multiset("mississippi")
    assert ["".join(i) for i in list(multiset_combinations(M, 3))] == ans
    assert ["".join(i) for i in multiset_combinations(M, 30)] == []
    assert list(multiset_combinations([[1], [2, 3]], 2)) == [[[1], [2, 3]]]
    assert len(list(multiset_combinations("a", 3))) == 0
    assert len(list(multiset_combinations("a", 0))) == 1
    assert list(multiset_combinations("abc", 1)) == [["a"], ["b"], ["c"]]
Exemplo n.º 8
0
def test_multiset_combinations():
    ans = ['iii', 'iim', 'iip', 'iis', 'imp', 'ims', 'ipp', 'ips',
           'iss', 'mpp', 'mps', 'mss', 'pps', 'pss', 'sss']
    assert [''.join(i) for i in
            list(multiset_combinations('mississippi', 3))] == ans
    M = multiset('mississippi')
    assert [''.join(i) for i in
            list(multiset_combinations(M, 3))] == ans
    assert [''.join(i) for i in list(multiset_combinations(M, 30))] == []
    assert list(multiset_combinations([[1], [2, 3]], 2)) == [[[1], [2, 3]]]
    assert len(list(multiset_combinations('a', 3))) == 0
    assert list(multiset_combinations('abc', 1)) == [['a'], ['b'], ['c']]
Exemplo n.º 9
0
def test_nD_derangements():
    from sympy.utilities.iterables import (partitions, multiset,
                                           multiset_derangements,
                                           multiset_permutations)
    from sympy.functions.combinatorial.numbers import nD

    got = []
    for i in partitions(8, k=4):
        s = []
        it = 0
        for k, v in i.items():
            for i in range(v):
                s.extend([it] * k)
                it += 1
        ms = multiset(s)
        c1 = sum(1 for i in multiset_permutations(s)
                 if all(i != j for i, j in zip(i, s)))
        assert c1 == nD(ms) == nD(ms, 0) == nD(ms, 1)
        v = [tuple(i) for i in multiset_derangements(s)]
        c2 = len(v)
        assert c2 == len(set(v))
        assert c1 == c2
        got.append(c1)
    assert got == [
        1, 4, 6, 12, 24, 24, 61, 126, 315, 780, 297, 772, 2033, 5430, 14833
    ]

    assert nD('1112233456', brute=True) == nD('1112233456') == 16356
    assert nD('') == nD([]) == nD({}) == 0
    assert nD({1: 0}) == 0
    raises(ValueError, lambda: nD({1: -1}))
    assert nD('112') == 0
    assert nD(i='112') == 0
    assert [nD(n=i) for i in range(6)] == [0, 0, 1, 2, 9, 44]
    assert nD((i for i in range(4))) == nD('0123') == 9
    assert nD(m=(i for i in range(4))) == 3
    assert nD(m={0: 1, 1: 1, 2: 1, 3: 1}) == 3
    assert nD(m=[0, 1, 2, 3]) == 3
    raises(TypeError, lambda: nD(m=0))
    raises(TypeError, lambda: nD(-1))
    assert nD({-1: 1, -2: 1}) == 1
    assert nD(m={0: 3}) == 0
    raises(ValueError, lambda: nD(i='123', n=3))
    raises(ValueError, lambda: nD(i='123', m=(1, 2)))
    raises(ValueError, lambda: nD(n=0, m=(1, 2)))
    raises(ValueError, lambda: nD({1: -1}))
    raises(ValueError, lambda: nD(m={-1: 1, 2: 1}))
    raises(ValueError, lambda: nD(m={1: -1, 2: 1}))
    raises(ValueError, lambda: nD(m=[-1, 2]))
    raises(TypeError, lambda: nD({1: x}))
    raises(TypeError, lambda: nD(m={1: x}))
    raises(TypeError, lambda: nD(m={x: 1}))
Exemplo n.º 10
0
def test_multiset_combinations():
    ans = ['iii', 'iim', 'iip', 'iis', 'imp', 'ims', 'ipp', 'ips',
           'iss', 'mpp', 'mps', 'mss', 'pps', 'pss', 'sss']
    assert [''.join(i) for i in
            list(multiset_combinations('mississippi', 3))] == ans
    M = multiset('mississippi')
    assert [''.join(i) for i in
            list(multiset_combinations(M, 3))] == ans
    assert [''.join(i) for i in multiset_combinations(M, 30)] == []
    assert list(multiset_combinations([[1], [2, 3]], 2)) == [[[1], [2, 3]]]
    assert len(list(multiset_combinations('a', 3))) == 0
    assert len(list(multiset_combinations('a', 0))) == 1
    assert list(multiset_combinations('abc', 1)) == [['a'], ['b'], ['c']]
Exemplo n.º 11
0
 def check(*args):
     # not using _value_check since there is a
     # suggestion for the user
     if len(set(args)) != len(args):
         weights = multiset(args)
         n = Integer(len(args))
         for k in weights:
             weights[k] /= n
         raise ValueError(filldedent("""
             Repeated args detected but set expected. For a
             distribution having different weights for each
             item use the following:""") + (
             '\nS("FiniteRV(%s, %s)")' % ("'X'", weights)))
Exemplo n.º 12
0
def count_digits(n, base=10):
    """
    Returs an ordered dict. frequency counter for the digits of a decimal
    integer ``n`` representing an integer in a base ``base``, where the
    base digits are mapped to the positive decimal integers, e.g. the
    base ``11`` integer ``10321``, with digits ``10``, ``3``, ``2``, ``1``
    (from most significant to least), is equal to the decimal integer
    ``13696``, as ``10 * 11**3 + 3 * 11**2 + 2 * 11**1 + 1 * 11**0 = 13696``.

    For binary, octal or hexadecimal bases the integer ``n``
    can also be specified as a numeric literal in those bases, e.g. ``0xF321``.

    Examples
    ========

    >>> from sympy.ntheory import count_digits

    >>> count_digits(1903413094, 10)
    {0: 2, 1: 2, 3: 2, 4: 2, 9: 2}

    >>> count_digits(122333, 10)
    {1: 1, 2: 2, 3: 3}

    >>> count_digits(0b1001001, 2)
    {0: 4, 1: 3}

    >>> count_digits(0xF321, 16)
    {1: 1, 2: 1, 3: 1, 15: 1}

    The base 11 number 10321, with digits 10, 3, 2, 1 (from most significant digit)
    >>> count_digits(13696, 11)
    {1: 1, 2: 1, 3: 1, 10: 1}

    The base 100 number 990512, with digits 99, 0, 51, 2 (from most significant digit)
    >>> count_digits(99005102, 100)
    {0: 1, 2: 1, 51: 1, 99: 1}

    """
    return defaultdict(int, multiset(_digits(n, base)[1:]).items())
Exemplo n.º 13
0
def count_digits(n, b=10):
    """
    Return a dictionary whose keys are the digits of ``n`` in the
    given base, ``b``, with keys indicating the digits appearing in the
    number and values indicating how many times that digit appeared.

    Examples
    ========

    >>> from sympy.ntheory import count_digits, digits

    >>> count_digits(1111339)
    {1: 4, 3: 2, 9: 1}

    The digits returned are always represented in base-10
    but the number itself can be entered in any format that is
    understood by Python; the base of the number can also be
    given if it is different than 10:

    >>> n = 0xFA; n
    250
    >>> count_digits(_)
    {0: 1, 2: 1, 5: 1}
    >>> count_digits(n, 16)
    {10: 1, 15: 1}

    The default dictionary will return a 0 for any digit that did
    not appear in the number. For example, which digits appear 7
    times in ``77!``:

    >>> from sympy import factorial
    >>> c77 = count_digits(factorial(77))
    >>> [i for i in range(10) if c77[i] == 7]
    [1, 3, 7, 9]
    """
    rv = defaultdict(int, multiset(digits(n, b)).items())
    rv.pop(b) if b in rv else rv.pop(-b)  # b or -b is there
    return rv
Exemplo n.º 14
0
def test_multiset_permutations():
    ans = [
        'abby', 'abyb', 'aybb', 'baby', 'bayb', 'bbay', 'bbya', 'byab', 'byba',
        'yabb', 'ybab', 'ybba'
    ]
    assert [''.join(i) for i in multiset_permutations('baby')] == ans
    assert [''.join(i) for i in multiset_permutations(multiset('baby'))] == ans
    assert list(multiset_permutations([0, 0, 0], 2)) == [[0, 0]]
    assert list(multiset_permutations([0, 2, 1],
                                      2)) == [[0, 1], [0, 2], [1, 0], [1, 2],
                                              [2, 0], [2, 1]]
    assert len(list(multiset_permutations('a', 0))) == 1
    assert len(list(multiset_permutations('a', 3))) == 0
    for nul in ([], {}, ''):
        assert list(multiset_permutations(nul)) == [[]]
    assert list(multiset_permutations(nul, 0)) == [[]]
    # impossible requests give no result
    assert list(multiset_permutations(nul, 1)) == []
    assert list(multiset_permutations(nul, -1)) == []

    def test():
        for i in range(1, 7):
            print(i)
            for p in multiset_permutations([0, 0, 1, 0, 1], i):
                print(p)

    assert capture(lambda: test()) == dedent('''\
        1
        [0]
        [1]
        2
        [0, 0]
        [0, 1]
        [1, 0]
        [1, 1]
        3
        [0, 0, 0]
        [0, 0, 1]
        [0, 1, 0]
        [0, 1, 1]
        [1, 0, 0]
        [1, 0, 1]
        [1, 1, 0]
        4
        [0, 0, 0, 1]
        [0, 0, 1, 0]
        [0, 0, 1, 1]
        [0, 1, 0, 0]
        [0, 1, 0, 1]
        [0, 1, 1, 0]
        [1, 0, 0, 0]
        [1, 0, 0, 1]
        [1, 0, 1, 0]
        [1, 1, 0, 0]
        5
        [0, 0, 0, 1, 1]
        [0, 0, 1, 0, 1]
        [0, 0, 1, 1, 0]
        [0, 1, 0, 0, 1]
        [0, 1, 0, 1, 0]
        [0, 1, 1, 0, 0]
        [1, 0, 0, 0, 1]
        [1, 0, 0, 1, 0]
        [1, 0, 1, 0, 0]
        [1, 1, 0, 0, 0]
        6\n''')
    raises(ValueError, lambda: list(multiset_permutations({0: 3, 1: -1})))
Exemplo n.º 15
0
def test_multiset_permutations():
    ans = [
        "abby",
        "abyb",
        "aybb",
        "baby",
        "bayb",
        "bbay",
        "bbya",
        "byab",
        "byba",
        "yabb",
        "ybab",
        "ybba",
    ]
    assert ["".join(i) for i in multiset_permutations("baby")] == ans
    assert ["".join(i) for i in multiset_permutations(multiset("baby"))] == ans
    assert list(multiset_permutations([0, 0, 0], 2)) == [[0, 0]]
    assert list(multiset_permutations([0, 2, 1], 2)) == [
        [0, 1],
        [0, 2],
        [1, 0],
        [1, 2],
        [2, 0],
        [2, 1],
    ]
    assert len(list(multiset_permutations("a", 0))) == 1
    assert len(list(multiset_permutations("a", 3))) == 0

    def test():
        for i in range(1, 7):
            print(i)
            for p in multiset_permutations([0, 0, 1, 0, 1], i):
                print(p)

    assert capture(lambda: test()) == dedent(
        """\
        1
        [0]
        [1]
        2
        [0, 0]
        [0, 1]
        [1, 0]
        [1, 1]
        3
        [0, 0, 0]
        [0, 0, 1]
        [0, 1, 0]
        [0, 1, 1]
        [1, 0, 0]
        [1, 0, 1]
        [1, 1, 0]
        4
        [0, 0, 0, 1]
        [0, 0, 1, 0]
        [0, 0, 1, 1]
        [0, 1, 0, 0]
        [0, 1, 0, 1]
        [0, 1, 1, 0]
        [1, 0, 0, 0]
        [1, 0, 0, 1]
        [1, 0, 1, 0]
        [1, 1, 0, 0]
        5
        [0, 0, 0, 1, 1]
        [0, 0, 1, 0, 1]
        [0, 0, 1, 1, 0]
        [0, 1, 0, 0, 1]
        [0, 1, 0, 1, 0]
        [0, 1, 1, 0, 0]
        [1, 0, 0, 0, 1]
        [1, 0, 0, 1, 0]
        [1, 0, 1, 0, 0]
        [1, 1, 0, 0, 0]
        6\n"""
    )