Pergunta

How does one create the Cartesian product of a variable number of sets in sage?

For example, if A is a list of integers having length n, we want to create the Cartesian Product whose ith set is the set of all permutations of the set {1,...,A[i-1]};

def PermProd(A):
    n=len(A)
    X=Permutations(A[0]) * Permutations(A[1]) * ... * Permutations(A[n-1])
    return X

where * denotes the Cartesian Product operator.

Foi útil?

Solução

CartesianProduct takes a variable number of arguments, each argument must be an iterable. So, if your A is a list of permutation groups, CartesianProduct(*A) does what you want. Here's an example:

sage: list(CartesianProduct(*(Permutations(i) for i in range(4))))
[[[], [1], [1, 2], [1, 2, 3]],
 [[], [1], [1, 2], [1, 3, 2]],
 [[], [1], [1, 2], [2, 1, 3]],
 [[], [1], [1, 2], [2, 3, 1]],
 [[], [1], [1, 2], [3, 1, 2]],
 [[], [1], [1, 2], [3, 2, 1]],
 [[], [1], [2, 1], [1, 2, 3]],
 [[], [1], [2, 1], [1, 3, 2]],
 [[], [1], [2, 1], [2, 1, 3]],
 [[], [1], [2, 1], [2, 3, 1]],
 [[], [1], [2, 1], [3, 1, 2]],
 [[], [1], [2, 1], [3, 2, 1]]]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top