3
$\begingroup$
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.

$\endgroup$
6
$\begingroup$

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], {}]

| improve this answer | |
$\endgroup$
  • $\begingroup$ Brilliant ! Thank you very much. You are very polite - you avoided pointing out that I am indeed dumb ! $\endgroup$ – Simon May 25 at 19:48
  • 3
    $\begingroup$ 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. $\endgroup$ – flinty May 25 at 20:03
4
$\begingroup$

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...

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much ! It seems, per flinty's answer, that Complement "setifies" the list. $\endgroup$ – Simon May 25 at 19:51
  • 1
    $\begingroup$ 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. =) $\endgroup$ – Henrik Schumacher May 25 at 19:55
  • 3
    $\begingroup$ 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}]] $\endgroup$ – flinty May 25 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.