Hi
I am new on this forum. I am searching for a function in R which provides
all partitions of a set, say for the set
{1,2,3}
you get
{{1,2,3}}
{1,{2,3}}
{2,{1,3}}
{3,{1,2}}
{{1},{2},{3}}
. The number of partitions of a set is given by Bellsche`s number. The
number of possibilities of length (this is the number of subsets included in
the partition) of the partition is given by the partition function.
But I need a list containg all possible partitions of an index set
{1,...,d}, d arbitrary.
I have already looked at the FAQs but I did not found. Sorry.
Can anybody help me?
Thanks a lot!
Diana Tichy
--
View this message in context:
http://r.789695.n4.nabble.com/Partition-of-a-set-tp3039291p3039291.html
Sent from the R help mailing list archive at Nabble.com.
Hi Diana, Have a look at the setparts function in the partitions package. Michael On 12 November 2010 20:03, Diana <d.tichy at mathematik.uni-wuerzburg.de> wrote:> > Hi > > I am new on this forum. I am searching for a function in R which provides > all partitions of a set, say for the set > {1,2,3} > you get > {{1,2,3}} > {1,{2,3}} > {2,{1,3}} > {3,{1,2}} > {{1},{2},{3}} > . The number of partitions of a set is given by Bellsche`s number. The > number of possibilities of length (this is the number of subsets included in > the partition) of the partition is given by the partition function. > But I need a list containg all possible partitions of an index set > {1,...,d}, d arbitrary. > > I have already looked at the FAQs but I did not found. Sorry. > > Can anybody help me? > > Thanks a lot! > Diana Tichy > -- > View this message in context: http://r.789695.n4.nabble.com/Partition-of-a-set-tp3039291p3039291.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi Diane,
Does this do what you want ?
listParts <- function(n) {
# Generates a list of lists representing the partitions
# of an integer n
require(partitions)
x <- 1:n
apply(setparts(n), 2, function(pp) tapply(x, pp, function(xx) list(xx)))
}
Michael
On 12 November 2010 21:15, Diana Tichy
<d.tichy at mathematik.uni-wuerzburg.de> wrote:> Hello Michael
> once again. Now I can use print.partition which tells me, how to generate,
> e.g., all possible 5 partitions of {1,2,3}. But this generation is still
> complicated. Does there already exist a function doing that for me, e.g.
> returns a list, where the first element contains the one-elemnt partion,
the
> second to fourth the three 2-element-partions and the fifth the 3-element
> partition?
> Do you know or is it my job to implement this by myself (o.k. but
> time-consuming ..)
>
> Best wishes
> Diana
>
> Am 12.11.2010 10:32, schrieb Michael Bedward:
>>
>> Hi Diana,
>>
>> Have a look at the setparts function in the partitions package.
>>
>> Michael
>>
>> On 12 November 2010 20:03, Diana<d.tichy at
mathematik.uni-wuerzburg.de>
>> ?wrote:
>>>
>>> Hi
>>>
>>> I am new on this forum. I am searching for a function in R which
provides
>>> all partitions of a set, say for the set
>>> {1,2,3}
>>> you get
>>> {{1,2,3}}
>>> {1,{2,3}}
>>> {2,{1,3}}
>>> {3,{1,2}}
>>> {{1},{2},{3}}
>>> . The number of partitions of a set is given by Bellsche`s number.
The
>>> number of possibilities of length (this is the number of subsets
included
>>> in
>>> the partition) of the partition is given by the partition function.
>>> But I need a list containg all possible partitions of an index set
>>> {1,...,d}, d arbitrary.
>>>
>>> I have already looked at the FAQs but I did not found. Sorry.
>>>
>>> Can anybody help me?
>>>
>>> Thanks a lot!
>>> Diana Tichy
>>> --
>>> View this message in context:
>>>
http://r.789695.n4.nabble.com/Partition-of-a-set-tp3039291p3039291.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>
> --
> Diana Tichy (geb. St?hr)
> Lehrstuhl f?r Statistik
> Institut f?r Mathematik
> Universit?t W?rzburg
> Am Hubland
> 97074 W?rzburg
> Raum R110, Mathematikgeb?ude
> Tel.: 0931-31 85027
> d.tichy at mathematik.uni-wuerzburg.de
> http://statistik.mathematik.uni-wuerzburg.de/~tichy
>
>
Hi Diana,
Yes, this seems to be a little bug in the setparts function. The
following is a modified version which should work for any x > 0.
You'll see I've just changed a couple of lines...
setparts2 <- function (x)
{
if (length(x) == 1) {
if (x < 1)
stop("if single value, x must be >= 1")
else if (x == 1)
out <- matrix(1, 1, 1)
else
return(Recall(parts(x)))
}
if (is.matrix(x)) {
out <- apply(x, 2, setparts)
if (is.list(out)) out <- do.call("cbind", out)
}
else {
x <- sort(x[x > 0], decreasing = TRUE)
num.of.parts <- factorial(sum(x))/(prod(c(factorial(x),
factorial(table(x)))))
out <- .C("wrap", as.integer(x), as.integer(length(x)),
ans = integer(sum(x) * num.of.parts), PACKAGE =
"partitions")$ans
dim(out) <- c(sum(x), num.of.parts)
}
return(as.partition(out))
}
If that works OK you might like to contact the package author about it.
Michael
On 17 November 2010 01:59, Diana Tichy
<d.tichy at mathematik.uni-wuerzburg.de> wrote:> Hello Michael
>
> I am once again contacting you, since I found out, that your function
> listParts does not work for index set of length lower or equal than 2. This
> is not your fault, since the function setparts does not work correct for a
> value n<= 2. But I do not know why.
> Do you have an answer for that problem?
> If not, do not spend to much time, since I can solve the problem by
> implementing list with all partitions of set {1,2} and {1}. (only two
> partitions for {1,2} ...)
>
> Best wishes
> Diana
>
> Am 12.11.2010 12:06, schrieb Michael Bedward:
>>
>> You're welcome Diana - glad to help
>>
>> Michael
>>
>>
>> On 12 November 2010 22:00, Diana Tichy
>> <d.tichy at mathematik.uni-wuerzburg.de> ?wrote:
>>>
>>> Hi Michael
>>>
>>> Wow, yes this is exactly what I need. I want to sum up over all
>>> partitions
>>> of a set of length n. Now I can use a for loop which calls the
elements
>>> of
>>> the list which is returned by your function listParts!
>>> Hence, you helped me a lot with your experience!
>>> Thank you!
>>> Diana
>>>
>>> Am 12.11.2010 11:42, schrieb Michael Bedward:
>>>>
>>>> listParts<- function(n) {
>>>> # Generates a list of lists representing the partitions
>>>> # of an integer n
>>>> ? require(partitions)
>>>> ? x<- 1:n
>>>> ? apply(setparts(n), 2, function(pp) tapply(x, pp, function(xx)
>>>> list(xx)))
>>>> }
>>>
>>> --
>>> Diana Tichy (geb. St?hr)
>>> Lehrstuhl f?r Statistik
>>> Institut f?r Mathematik
>>> Universit?t W?rzburg
>>> Am Hubland
>>> 97074 W?rzburg
>>> Raum R110, Mathematikgeb?ude
>>> Tel.: 0931-31 85027
>>> d.tichy at mathematik.uni-wuerzburg.de
>>> http://statistik.mathematik.uni-wuerzburg.de/~tichy
>>>
>>>
>
> --
> Diana Tichy (geb. St?hr)
> Lehrstuhl f?r Statistik
> Institut f?r Mathematik
> Universit?t W?rzburg
> Am Hubland
> 97074 W?rzburg
> Raum R110, Mathematikgeb?ude
> Tel.: 0931-31 85027
> d.tichy at mathematik.uni-wuerzburg.de
> http://statistik.mathematik.uni-wuerzburg.de/~tichy
>
>