Re: rules on vector sequence
- To: mathgroup at smc.vnet.net
- Subject: [mg98089] Re: rules on vector sequence
- From: Raffy <raffy at mac.com>
- Date: Sun, 29 Mar 2009 03:22:57 -0500 (EST)
- References: <gqkurj$47q$1@smc.vnet.net>
On Mar 28, 3:40 am, Filippo Miatto <sottosupr... at gmail.com> wrote: > Dear all, > I need to generate all the vectors, of a given length N, that have > these three properties: > > 1- the entries have to be among -2,-1,0,1,2 > 2- the sum of all the entries has to be 0 > 3- only two or four of the entries can be different from 0 > > do you have any suggestions on how i can do that? i tried something > but without success.. expecially i don't know how to implement the > third rule.. > thank you in advance! > Filippo filippo[n_Integer, nz_Integer] := Join @@ (Permutations[PadRight[#, n]] & /@ Select[Tuples[{-2, -1, 0, 1, 2}, nz], Total[#] == 0 &]); n : length of vector nz : number of entries not explicitly set to 0 filippo[4, 2] ==> {{-2,2,0,0},{-2,0,2,0},{-2,0,0,2},{2,-2,0,0}, {2,0,-2,0},{2,0,0,-2},{0,-2,2,0},{0,-2,0,2},{0,2,-2,0},{0,2,0,-2}, {0,0,-2,2},{0,0,2,-2},{-1,1,0,0},{-1,0,1,0},{-1,0,0,1},{1,-1,0,0}, {1,0,-1,0},{1,0,0,-1},{0,-1,1,0},{0,-1,0,1},{0,1,-1,0},{0,1,0,-1}, {0,0,-1,1},{0,0,1,-1},{0,0,0,0},{1,-1,0,0},{1,0,-1,0},{1,0,0,-1}, {-1,1,0,0},{-1,0,1,0},{-1,0,0,1},{0,1,-1,0},{0,1,0,-1},{0,-1,1,0}, {0,-1,0,1},{0,0,1,-1},{0,0,-1,1},{2,-2,0,0},{2,0,-2,0},{2,0,0,-2}, {-2,2,0,0},{-2,0,2,0},{-2,0,0,2},{0,2,-2,0},{0,2,0,-2},{0,-2,2,0}, {0,-2,0,2},{0,0,2,-2},{0,0,-2,2}}