From: Fabian Seoane <fab...@fseoane.net> variations is a routine that calculates all possible variations of size n (with or without repetition) of a given set. --- doc/src/modules.txt | 1 + doc/src/modules/utilities/index.txt | 11 ++++++++ doc/src/modules/utilities/iterables.txt | 39 +++++++++++++++++++++++++++++++ sympy/utilities/iterables.py | 36 ++++++++++++++++++++++++++++ sympy/utilities/tests/test_iterables.py | 6 ++++- 5 files changed, 92 insertions(+), 1 deletions(-) create mode 100644 doc/src/modules/utilities/index.txt create mode 100644 doc/src/modules/utilities/iterables.txt
diff --git a/doc/src/modules.txt b/doc/src/modules.txt index fca4eb6..6c46683 100644 --- a/doc/src/modules.txt +++ b/doc/src/modules.txt @@ -31,6 +31,7 @@ access any SymPy module, or use this contens: modules/statistics.txt modules/concrete.txt modules/solvers.txt + modules/utilities/index.txt Contributions to docs --------------------- diff --git a/doc/src/modules/utilities/index.txt b/doc/src/modules/utilities/index.txt new file mode 100644 index 0000000..3e1e692 --- /dev/null +++ b/doc/src/modules/utilities/index.txt @@ -0,0 +1,11 @@ +Utilities +========= + +This module contains some general purpose utilities that are used across SymPy + +Contents: + +.. toctree:: + :maxdepth: 2 + + iterables.txt \ No newline at end of file diff --git a/doc/src/modules/utilities/iterables.txt b/doc/src/modules/utilities/iterables.txt new file mode 100644 index 0000000..023a3bc --- /dev/null +++ b/doc/src/modules/utilities/iterables.txt @@ -0,0 +1,39 @@ +Iterables +========= + +cartes +------ + +Returns the cartesian product of two sequences + +Examples:: + >>> from sympy.utilities.iterables import cartes + >>> cartes([1,2,3], ['a', 'b', 'c']) + [[1, 'a'], + [1, 'b'], + [1, 'c'], + [2, 'a'], + [2, 'b'], + [2, 'c'], + [3, 'a'], + [3, 'b'], + [3, 'c']] + + + +variations +---------- + +variations(seq, n) Returns all the variations of the list of size n. + +Has an optional third argument. Must be a boolean value and makes the method +return the variations with repetition if set to True, or the variations +without repetition if set to False. + +Examples:: + >>> from sympy.utilities.iterables import variations + >>> variations([1,2,3], 2) + [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] + >>> variations([1,2,3], 2, True) + [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] + diff --git a/sympy/utilities/iterables.py b/sympy/utilities/iterables.py index 6179622..24ebcd0 100644 --- a/sympy/utilities/iterables.py +++ b/sympy/utilities/iterables.py @@ -173,3 +173,39 @@ def recursion(result, M, k): for elem in recursion([item], M[i + 1:], k - 1): yield elem + +def cartes(seq0, seq1, modus='pair'): + """Return the cartesian product of two sequences """ + if modus == 'pair': + return [[item0, item1] for item0 in seq0 for item1 in seq1] + elif modus == 'triple': + return [item0 + [item1] for item0 in seq0 for item1 in seq1] + +def variations(seq, n, repetition=False): + """Returns all the variations of the list of size n. + + variations(seq, n, True) will return all the variations of the list of + size n with repetitions + + variations(seq, n, False) will return all the variations of the list of + size n without repetitions + """ + def setrep(seq): # remove sets with duplicates (repetition is relevant) + def delrep(seq): # remove duplicates while maintaining order + result = [] + for item in seq: + if item not in result: + result.append(item) + return result + return [item for item in seq if item == delrep(item)] + + if n == 1: + return [[item] for item in seq] + result = range(len(seq)) + cartesmodus = 'pair' + for i in range(n-1): + result = cartes(result, range(len(seq)), cartesmodus) + if not repetition: + result = setrep(result) + cartesmodus = 'triple' + return [[seq[index] for index in indices] for indices in result] diff --git a/sympy/utilities/tests/test_iterables.py b/sympy/utilities/tests/test_iterables.py index 1eea497..959c900 100644 --- a/sympy/utilities/tests/test_iterables.py +++ b/sympy/utilities/tests/test_iterables.py @@ -1,6 +1,6 @@ from sympy import symbols from sympy.utilities.iterables import postorder_traversal, \ - preorder_traversal, flatten, subsets + preorder_traversal, flatten, subsets, variations w,x,y,z= symbols('wxyz') @@ -31,3 +31,7 @@ def test_subsets(): assert list(subsets([1, 2, 3], 2)) == [[1, 2], [1,3], [2, 3]] assert list(subsets([1, 2, 3], 3)) == [[1, 2, 3]] +def test_variations(): + assert variations([1,2,3], 2) == [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] + assert variations([1,2,3], 2, True) == [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], \ + [3,1], [3,2], [3,3]] -- 1.6.1.2 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patches@googlegroups.com To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en -~----------~----~----~----~------~----~------~--~---