12.4 Multidimensional enumeration

Module: sage.misc.mrange

Multidimensional enumeration

Author Log:

Module-level Functions

cartesian_product_iterator( X)

Iterate over the Cartesian product.

Input:

X
- list or tuple of lists
Output: iterator over the cartesian product of the elements of X

sage: list(cartesian_product_iterator([[1,2], ['a','b']]))
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

mrange( sizes, [typ=<type 'list'>])

Return the multirange list with given sizes and type.

This is the list version of xmrange. Use xmrange for the iterator.

More precisely, return the iterator over all objects of type typ of n-tuples of Python ints with entries between 0 and the integers in the sizes list. The iterator is empty if sizes is empty or contains any non-positive integer.

Input:

sizes
- a list of nonnegative integers
typ
- (default: list) a type or class; more generally, something that can be called with a list as input.

Output: a list

sage: mrange([3,2])
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
sage: mrange([3,2], tuple)
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
sage: mrange([3,2], sum)
[0, 1, 1, 2, 2, 3]

Examples that illustrate empty multi-ranges.

sage: mrange([])
[]
sage: mrange([5,3,-2])
[]
sage: mrange([5,3,0])
[]

Author Log:

mrange_iter( iter_list, [typ=<type 'list'>])

Return the multirange list derived from the given list of iterators.

This is the list version of xmrange_iter. Use xmrange_iter for the iterator.

More precisely, return the iterator over all objects of type typ of n-tuples of Python ints with entries between 0 and the integers in the sizes list. The iterator is empty if sizes is empty or contains any non-positive integer.

Input:

sizes
- a list of nonnegative integers
typ
- (default: list) a type or class; more generally, something that can be called with a list as input.

Output: a list

sage: mrange_iter([range(3),[0,2]])
[[0, 0], [0, 2], [1, 0], [1, 2], [2, 0], [2, 2]]
sage: mrange_iter([['Monty','Flying'],['Python','Circus']], tuple)
[('Monty', 'Python'), ('Monty', 'Circus'), ('Flying', 'Python'), ('Flying',
'Circus')]
sage: mrange_iter([[2,3,5,7],[1,2]], sum)
[3, 4, 4, 5, 6, 7, 8, 9]

Examples that illustrate empty multi-ranges.

sage: mrange_iter([])
[]
sage: mrange_iter([range(5),xrange(3),xrange(-2)])
[]
sage: mrange_iter([range(5),range(3),range(0)])
[]

Author: Joel B. Mohler

Class: xmrange

class xmrange
Return the multirange iterate with given sizes and type.

More precisely, return the iterator over all objects of type typ of n-tuples of Python ints with entries between 0 and the integers in the sizes list. The iterator is empty if sizes is empty or contains any non-positive integer.

Use mrange for the non-iterator form.

Input:

sizes
- a list of nonnegative integers
typ
- (default: list) a type or class; more generally, something that can be called with a list as input.

Output: a generator

We create multi-range iterators, print them and also iterate through a tuple version.

sage: z = xmrange([3,2]);z
xmrange([3, 2])
sage: z = xmrange([3,2], tuple);z
xmrange([3, 2], <type 'tuple'>)
sage: for a in z:
...    print a
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)

We illustrate a few more iterations.

sage: list(xmrange([3,2]))
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
sage: list(xmrange([3,2], tuple))
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]

Here we compute the sum of each element of the multi-range iterator:

sage: list(xmrange([3,2], sum))
[0, 1, 1, 2, 2, 3]

Next we compute the product:

sage: list(xmrange([3,2], prod))
[0, 0, 0, 1, 0, 2]

Examples that illustrate empty multi-ranges.

sage: list(xmrange([]))
[]
sage: list(xmrange([5,3,-2]))
[]
sage: list(xmrange([5,3,0]))
[]

We use a multi-range iterator to iterate through the cartesian product of sets.

sage: X = ['red', 'apple', 389]
sage: Y = ['orange', 'horse']
sage: for i,j in xmrange([len(X), len(Y)]):
...    print (X[i], Y[j])
('red', 'orange')
('red', 'horse')
('apple', 'orange')
('apple', 'horse')
(389, 'orange')
(389, 'horse')

Author Log:

xmrange( self, sizes, [typ=<type 'list'>])

Special Functions: __init__,$ \,$ __iter__,$ \,$ __len__,$ \,$ __repr__

Class: xmrange_iter

class xmrange_iter
Return the multirange iterate derived from the given iterators and type.

NOTE: This basically gives you the cartesian product of sets.

More precisely, return the iterator over all objects of type typ of n-tuples of Python ints with entries between 0 and the integers in the sizes list. The iterator is empty if sizes is empty or contains any non-positive integer.

Use mrange_iter for the non-iterator form.

Input:

list_iter
- a list of objects usable as iterators (possibly lists)
typ
- (default: list) a type or class; more generally, something that can be called with a list as input.

Output: a generator

We create multi-range iterators, print them and also iterate through a tuple version.

sage: z = xmrange_iter([xrange(3),xrange(2)]);z
xmrange_iter([xrange(3), xrange(2)])
sage: z = xmrange_iter([range(3),range(2)], tuple);z
xmrange_iter([[0, 1, 2], [0, 1]], <type 'tuple'>)
sage: for a in z:
...    print a
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)

We illustrate a few more iterations.

sage: list(xmrange_iter([range(3),range(2)]))
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
sage: list(xmrange_iter([range(3),range(2)], tuple))
[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]

Here we compute the sum of each element of the multi-range iterator:

sage: list(xmrange_iter([range(3),range(2)], sum))
[0, 1, 1, 2, 2, 3]

Next we compute the product:

sage: list(xmrange_iter([range(3),range(2)], prod))
[0, 0, 0, 1, 0, 2]

Examples that illustrate empty multi-ranges.

sage: list(xmrange_iter([]))
[]
sage: list(xmrange_iter([xrange(5),xrange(3),xrange(-2)]))
[]
sage: list(xmrange_iter([xrange(5),xrange(3),xrange(0)]))
[]

We use a multi-range iterator to iterate through the cartesian product of sets.

sage: X = ['red', 'apple', 389]
sage: Y = ['orange', 'horse']
sage: for i,j in xmrange_iter([X, Y], tuple):
...    print (i, j)
('red', 'orange')
('red', 'horse')
('apple', 'orange')
('apple', 'horse')
(389, 'orange')
(389, 'horse')

Author: Joel B. Mohler

xmrange_iter( self, iter_list, [typ=<type 'list'>])

Special Functions: __init__,$ \,$ __iter__,$ \,$ __repr__

See About this document... for information on suggesting changes.