# [R] Partition of a set

Michael Bedward michael.bedward at gmail.com
Wed Nov 17 02:12:17 CET 2010

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