# Adding fractions from subsets of variables

Clear[exp]
exp[dist_] := Module[{nonemptysubsets, fractions, signs},
nonemptysubsets = Complement[Subsets[dist], {{}}];
fractions = 1/Map[Total, nonemptysubsets];
signs = Map[(-1)^(Length[#] + 1) &, nonemptysubsets];
Dot[signs, fractions]
]
exp[{x,y}]


gives the output

1/x + 1/y - 1/(x + y)


as expected. Yet

exp[{1,1}]


gives the output

1/2


rather than

3/2


as expected.

It may or may not be a bug depending on how you look at it. Perhaps it's just undocumented behaviour. Subsets[{1,1}] returns {{},{1},{1},{1,1}} but Complement[Subsets[{1, 1}], {{}}] returns {1},{1,1} because Complement will turn the list into a set and a duplicate {1} disappears.

Instead of using Complement just remove cases of {} using DeleteCases[Subsets[dist], {}]

• Brilliant ! Thank you very much. You are very polite - you avoided pointing out that I am indeed dumb ! – Simon May 25 at 19:48
• Well you aren't dumb because your code is at least well formatted! You should consider changing the title of the question though. Choose something appropriate like Adding fractions from subsets of variables as it will be easier for others to search for should they need to. – flinty May 25 at 20:03

The problem might be caused by the fact that Subsets does not treat its first argument as set. In the concrete case, Subsets[{1, 1}] returns

{{}, {1}, {1}, {1, 1}}

which actually shocked me because the subsets should be just

{{}, {1}}

You can apply Union to resolve this. You should also get rid of the empty set with Rest:

Rest[Subsets[Union[dist]]]


{{1}}

But I am not sure whether that is what you wanted, because exp[{1, 1}] would return just 1...

• Thank you very much ! It seems, per flinty's answer, that Complement "setifies" the list. – Simon May 25 at 19:51
• Partially, it "setifies". But it cannot take care of {1,1} which I would either not consider a mathematical subset of the "set" $\{1,1\}$ or I had to identify $\{1,1\}$ with $\{ 1 \}$ - and then the list of subsets (with duplicates removed) would be again $\{1\}$... But what is "right" here depends of course on your application. =) – Henrik Schumacher May 25 at 19:55
• Yes, it's more like Sub-multisets and I reckon it's like that because it's more useful for programming (think things like all possible groupings ignoring order) than the strict mathematical definition which you can get with Subsets[DeleteDuplicates[{1, 1}]] – flinty May 25 at 19:59