MathGroup Archive 2006

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

Search the Archive

Re: need to make a special function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68325] Re: need to make a special function
  • From: "Scout" <Scout at nodomain.com>
  • Date: Tue, 1 Aug 2006 06:59:56 -0400 (EDT)
  • References: <eakec7$qvd$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Nabeel Butt" <nabeel.butt at gmail.com>
news:eakec7$qvd$1 at smc.vnet.net...
>
> Dear Users,
>        I need help on making a special type of function which has the
> following properties.
>   f[1]={{0},{1}}
>   f[2]={{0,0},{0,1},{1,0},{1,1}}
>   f[3]={{0,0,0},{0,0,1},{0,1,0},{0,1,1},{1,0,0},{1,0,1},{1,1,0},{1,1,1}}
> .............so on you can see the pattern emerging.
> I need a very efficient code to perform the above evaluation in some
> optimization problem.
>    Thanks in advance.
>     regards,
>        Nabeel
> -- 
> Nabeel Butt
> LUMS,Lahore
>
>

Hi,
take a look at these two functions:

    f[0]={{}};
    f[n_Integer]:=Table[IntegerDigits[i,2,n],{i,0,2^n -1}]/;n>0


    g[0]={{}};
    g[n_Integer]:=(g[n]=Module[{tmp,tmp1,tmp2},
              tmp=g[n-1];
              tmp1=Prepend[#,0]&/@tmp;
              tmp2=Prepend[#,1]&/@tmp;
              Join[tmp1,tmp2] ])/;n>0

I didn't compare which of the two is faster.
The second is implemented with a recursion.
The growth of the list is exponential, so they are all valid for small 
integers.

HTH,
    ~Scout~




  • Prev by Date: Re: x=2;Composition[f,FindMinimum][x+1,{x,a}]
  • Next by Date: RE: need to make a special function
  • Previous by thread: Re: need to make a special function
  • Next by thread: RE: need to make a special function