>> From: Helmut Kahovec "helmut.kahovec"
naser hosseini wrote:
>| Dear Lady or Gentleman: I am using Maple V, generating a cartesian
>| product. I would like to access the members of the product like I can
>| access the members of a list. I kindly appreciate your help with this
>| problem. Thanks, N. Hosseini
Well, besides Carl Devore's procedure you may also try to use my
Cartesian product operator, which I teach my students:
> restart;
> `&x`:=proc()
option `Copyright (C) 2001, Helmut Kahovec. All rights reserved.`;
if type([args],list(list)) then
if nargs>2 then
procname(
procname(args[1],args[2]),
args[3..-1]
)
else
[
seq(
seq(
[
`if`(type(i,list),op(i),i),
`if`(type(j,list),op(j),j)
],
j=args[2]
),
i=args[1]
)
]
end if
else
error "sequence of lists expected"
end if
end proc:
> [1,2,3]&x[1,2,3];
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2],
[3, 3]]
> time(`&x`([1,2,3]$11));
44.053
> restart;
> CartProdAsList:= proc(L::list({list,set}))
option `Copyright (C) 2002, Carl DeVore. All rights reserved.`;
local q,i,n,N;
n:= nops(L);
N:= map(nops, L);
[seq([seq(L[i][1+irem(q,N[i],'q')], i= 1..n)], q= 0..`*`(N[])-1)]
end:
> CartProdAsList([[1,2,3]$2]);
[[1, 1], [2, 1], [3, 1], [1, 2], [2, 2], [3, 2], [1, 3], [2, 3],
[3, 3]]
> time(CartProdAsList([[1,2,3]$11]));
64.742
Kind regards,
Helmut
|