MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Generate all k-tuples

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36241] Re: Generate all k-tuples
  • From: Szikla <szszikla at dpg.hu>
  • Date: Wed, 28 Aug 2002 04:16:01 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Selwyn Hollis  <slhollis at earthlink.net> wrote:

> Here's my contestant:
> 
> <<DiscreteMath`Combinatorica`
> 
> KTuples[k_Integer, vals_List] :=
>    Union[KSubsets[PadRight[vals, k*Length[vals], vals], k]]

Dear mathgroup,

Here is another solution; probably less elegant, but I found it much faster (DiscreteMath's Subsets and KSubsets are too slow. Besides, it produces each tuple only once):

KTuples2[n_Integer, L_List] :=
    Flatten[Outer[Append, KTuples2[n-1, L], L, 1], 1] /; n > 1;
KTuples2[1, L_List] := Transpose[{L}]

(Both function yields the tuples in lexicographic order.)

Best Regards,

Sz. Szikla



  • Prev by Date: MathCanvas JInternalFrame
  • Next by Date: Re: Can you help me to solve this Integrate using Mathematica ?
  • Previous by thread: Re: Generate all k-tuples
  • Next by thread: Export and ImageSize