Although the combinatorics module contains Partition and IntegerPartition
classes for investigation and manipulation of partitions, there are a few
functions to generate partitions that can be used as low-level tools for
routines: partitions and multiset_partitions. The former gives
integer partitions, and the latter gives enumerated partitions of elements.
There is also a routine kbins that will give a variety of permutations
of partions.

Return permutations of [0, 1, ..., n - 1] such that each permutation
differs from the last by the exchange of a single pair of neighbors.
The n! permutations are returned as an iterator. In order to obtain
the next permutation from a random starting permutation, use the
next_trotterjohnson method of the Permutation class (which generates
the same sequence in a different manner).

This is the sort of permutation used in the ringing of physical bells,
and does not produce permutations in lexicographical order. Rather, the
permutations differ from each other by exactly one inversion, and the
position at which the swapping occurs varies periodically in a simple
fashion. Consider the first few permutations of 4 elements generated
by permutations and generate_bell:

Notice how the 2nd and 3rd lexicographical permutations have 3 elements
out of place whereas each “bell” permutation always has only two
elements out of place relative to the previous permutation (and so the
signature (+/-1) of a permutation is opposite of the signature of the
previous permutation).

How the position of inversion varies across the elements can be seen
by tracing out where the largest number appears in the permutations:

An oriented graph is a directed graph having no symmetric pair of directed
edges. A forest is an acyclic graph, i.e., it has no cycles. A forest can
also be described as a disjoint union of trees, which are graphs in which
any two vertices are connected by exactly one simple path.

Return a list of length bits corresponding to the binary value
of n with small bits to the right (last). If bits is omitted, the
length will be the number required to represent n. If the bits are
desired in reversed order, use the [::-1] slice of the returned list.

If a sequence of all bits-length lists starting from [0, 0,..., 0]
through [1, 1, ..., 1] are desired, pass a non-integer for bits, e.g.
‘all’.

The ordered flag which is either None (to give the simple partition
of the the elements) or is a 2 digit integer indicating whether the order of
the bins and the order of the items in the bins matters. Given:

When all the elements are the same in the multiset, the order
of the returned partitions is determined by the partitions
routine. If one is counting partitions then it is better to use
the nT function.

A routine to generate necklaces that may (free=True) or may not
(free=False) be turned over to be viewed. The “necklaces” returned
are comprised of n integers (beads) with k different
values (colors). Only unique necklaces are returned.

The “unrestricted necklace” is sometimes also referred to as a
“bracelet” (an object that can be turned over, a sequence that can
be reversed) and the term “necklace” is used to imply a sequence
that cannot be reversed. So ACB == ABC for a bracelet (rotate and
reverse) while the two are different for a necklace since rotation
alone cannot make the two sequences the same.

This generator recursively yields nodes that it has visited in a postorder
fashion. That is, it descends through the tree depth-first to yield all of
a node’s children’s postorder traversal before yielding the node itself.

Parameters :

node : sympy expression

The expression to traverse.

keys : (default None) sort key(s)

The key(s) used to sort args of Basic objects. When None, args of Basic
objects are processed in arbitrary order. If key is defined, it will
be passed along to ordered() as the only key(s) to use to sort the
arguments; if key is simply True then the default keys of
ordered will be used (node count and default_sort_key).

A k-subset of an n-element set is any subset of length exactly k. The
number of k-subsets of an n-element set is given by binomial(n, k),
whereas there are 2**n subsets all together. If k is None then all
2**n subsets will be returned from shortest to longest.

Examples

>>> fromsympy.utilities.iterablesimportsubsets

subsets(seq, k) will return the n!/k!/(n - k)! k-subsets (combinations)
without repetition, i.e. once an item has been removed, it can no
longer be “taken”: