Re: Combination/Permutation questions
- To: mathgroup at smc.vnet.net
- Subject: [mg37450] Re: [mg37436] Combination/Permutation questions
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Wed, 30 Oct 2002 00:51:00 -0500 (EST)
- References: <200210290509.AAA27962@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I think your problem may be solved like this. I consider only the case with
2 out of 4 elements. Then
In[1]:=
Needs["DiscreteMath`Combinatorica`"];
cset = KSubsets[{a, b, c, d}, 2]
Out[2]=
{{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}
Define the function f (a simpler version of what you had already surmised):
In[3]:=
f[{x_, y_}] := Flatten[Outer[Plus, {-x, x}, {-y, y}]]
so that, for example,
In[4]:=
f[a, b]
Out[4]=
{-a - b, -a + b, a - b, a + b}
Then, map f over cset and Flatten:
In[5]:=
newSet = Flatten[f /@ cset]
Out[5]=
{-a - b, -a + b, a - b, a + b, -a - c, -a + c, a - c,
a + c, -a - d, -a + d, a - d, a + d, -b - c, -b + c,
b - c, b + c, -b - d, -b + d, b - d, b + d, -c - d,
-c + d, c - d, c + d}
You now get all the elements where the first term comes with a minus sign:
In[6]:=
Cases[newSet, -x_ + y_]
Out[6]=
{-a - b, -a + b, a - b, -a - c, -a + c, a - c, -a - d,
-a + d, a - d, -b - c, -b + c, b - c, -b - d, -b + d,
b - d, -c - d, -c + d, c - d}
and filter out those elements which have a second minus sign:
In[7]:=
DeleteCases[Cases[newSet, -x_ + y_], -x_ - y_]
Out[7]=
{-a + b, a - b, -a + c, a - c, -a + d, a - d, -b + c,
b - c, -b + d, b - d, -c + d, c - d}
I guess this is what you were looking for. The logic may be extended to
cover more general cases.
Tomas Garza
Mexico City
----- Original Message -----
From: "Michael Chang" <michael_chang86 at hotmail.com>
To: mathgroup at smc.vnet.net
Subject: [mg37450] [mg37436] Combination/Permutation questions
> Hi everyone,
>
> I'm running Mathematica 4.1 on Windoze XP, and have some newbie
> combination/permutation questions, and am hoping that the collective
> wisdom of this newsgroup can help/guide me out! ;)
>
> First, suppose that I have 4 objects, a, b, c, and d, respectively.
> How can I generate the permutation set when chosing, say, only *2*
> elements.
>
> In[1]:= Permutations[{a,b,c,d}]
>
> gives me the permutation set when choosing *all* 4 objects ... :(
>
> Second, for the same four objects (a,b,c,d), to generate the
> *combination* set (with 2 elements), I use:
>
> In[2]:= Needs["DiscreteMath`Combinatorica`"];
> In[3]:= cset=KSubsets[{a,b,c,d},2]
>
> and this generates the expected 6 combinatorial pairs
> ({{a,b},{a,c},{a,d},{b,c},{b,d},{c,d}}).
>
> What I'd like to do next is add each combinatorial set, and I am able
> to do this correctly via:
>
> In[4]:= Plus@@Transpose[cset]
>
> to obtain {a+b,a+c,a+d,b+c,b+d,c+d}.
>
> My problem lies in the fact that now, I'd like to be able to allow
> *each* (a,b,c,d) element to be either plus, or minus (in reality, I'm
> trying to generate combinatorial adds/minuses for *functions*
> (a,b,c,d)), and to still generate the 'sum' of the cominatorial set.
> So for instance, in my current example, for the *first* {a,b}
> combinatorial pair, I'd like to be able to generate
>
> In[5]:= Plus@@Transpose[Partition[Flatten[Outer[List,{a,-a},{b,-b}]],2]]
>
> (which generates {a+b,a-b,-a+b,-a-b}) ... only I'd like this to be
> somehow done automatically for *each* combinatorial pair (and, for the
> more general case when I generate combinatorial sets involving only
> 'k' elements). I've struggled with this for a while, and am only able
> to generate such a list *manually* for each of my six original
> combinatorial pairs ... a tedious, and somewhat tiresome procedure! :(
> Is there a way of 'easily' doing this?!?
>
> With my newly generated list (having 4*6 'elements'), how can I also
> go about finding which expressions involve/use only, say, 1 Minus? Is
> there an 'easy' way of doing this too? (For instance, I'd like to
> then 'filter' for (a-b) and (b-a) ...) (Perhaps the count of 1 Minus
> is a little contrived for this example here, but for the more general
> case I'll probably be considering, I might need to filter for, say,
> (longer) expressions involving 2 Minuses (say).)
>
> My apologies in advance for my rather long email, but as always, any
> feedback and help would be most welcome and appreciated!
>
> Thanks,
>
> Michael
>
>
- References:
- Combination/Permutation questions
- From: michael_chang86@hotmail.com (Michael Chang)
- Combination/Permutation questions