Re: need to make a special function
- To: mathgroup at smc.vnet.net
- Subject: [mg68330] Re: need to make a special function
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Tue, 1 Aug 2006 07:00:02 -0400 (EDT)
- Organization: The University of Western Australia
- References: <eakec7$qvd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <eakec7$qvd$1 at smc.vnet.net>,
"Nabeel Butt" <nabeel.butt at gmail.com> wrote:
> 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.
Clearly the (padded) binary digits of 0, 1, ..., 2^n - 1.
f[n_Integer] := IntegerDigits[Range[0, 2^n - 1], 2, n]
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}}
Here is another solution, based on Outer:
f[n_Integer] := Flatten[Outer[List, Sequence @@ Table[{0,1},{n}]], n-1]
> I need a very efficient code to perform the above evaluation in some
> optimization problem.
It might be helpful to know how you will be using f, and what the
optimization problem is -- because there could be a way of avoiding
computing f.
Cheers,
Paul
_______________________________________________________________________
Paul Abbott Phone: 61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul