通過對列表進行分區,我指的是列表元素的一組子集,使得任何不同的子集對的交集都是空的,並且所有子集的並集等於原始列表。如何獲得Mathematica中列表的所有分區?
例如,如果我的輸入列表是{1,π,x}
那麼我想返回
{ {{1},{π},{x}}, {{1,π},{x}}, {{1,x},{π}}, {{1},{x,π}}, {{1,π,x}} }
通過對列表進行分區,我指的是列表元素的一組子集,使得任何不同的子集對的交集都是空的,並且所有子集的並集等於原始列表。如何獲得Mathematica中列表的所有分區?
例如,如果我的輸入列表是{1,π,x}
那麼我想返回
{ {{1},{π},{x}}, {{1,π},{x}}, {{1,x},{π}}, {{1},{x,π}}, {{1,π,x}} }
使用適合代碼http://mathforum.org/advanced/robertd/bell.html
BellList[1] = {{{1}}};
BellList[n_Integer?Positive] := Join @@
(ReplaceList[#,
{{b___, {S__}, a___} :> {b, {S, n}, a},
{S__} :> {S, {n}}}
] & /@ BellList[n - 1])
s = {a, b, c, d, e};
bell = [email protected]@s /. n_Integer :> s[[n]]
或者,勿庸置疑,在Combinatorica包有此功能(SetPartitions)了!
Needs["Combinatorica`"]
comb = SetPartitions[{a, b, c, d, e}]
檢查,他們都返回相同的結果(但在不同的順序)
Complement[bell, comb] == {}
[email protected] == [email protected]
(* Both of the above return True *)
@Wizard先生,這將需要我一點時間精神分析,但據我可以告訴它做的工作,謝謝! – Michael
@Michael,我忘了檢查標準庫中的這個函數。查看我剛纔提供的更新。 –
@Simon,感謝您的編輯。 –
@yoda:該OP可能與術語混淆。我同意這些不是分區。 – Blender
@Blender是的,我只是意識到他可能會得到別的東西。 – abcd
@Blender,yoda:這些是[集合意義上的分區](http://en.wikipedia.org/wiki/Partition_of_a_set),不是Mathematica命令[Partition](http:// reference .wolfram.com /數學/ REF/Partition.html)。 – Simon