Bug 14473 - combn anomalies for m=0
combn anomalies for m=0
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Misc
R 2.11.1
All All
: P4 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-01-14 17:55 UTC by Christian Brechbuehler
Modified: 2011-01-17 09:56 UTC (History)
1 user (show)

See Also:


Attachments
Reduce special casing of m==0; use normal return. (1.45 KB, patch)
2011-01-14 19:28 UTC, Christian Brechbuehler
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Brechbuehler 2011-01-14 17:55:21 UTC
BACKGROUND

In general, combn(x, m) constructs all possible outcomes of sample(x, m).
(If SIMPLIFY, it returns them as columns of a matrix; if !SIMPLIFY as a list.)

For any positive integer n, dim(combn(n,m)) == c(m, choose(n,m)).


PROBLEM

For m=0, combn yields 0 combinations.  There should be choose(n,0) == 1.  Reproduce:

   > combn(6,0)                    # expect  matrix(integer(),0,1)
   numeric(0)
   > combn(3,0, deparse, FALSE)    # expect  list("integer(0)")
   list()

These results violate the general identities stated above.  They also get in the way of easy use of combn.

Please help: I tried classifying this issue.  I cannot map the file name (trunk/src/library/utils/R/combn.R) to any of the listed categories.


SCOPE

I found this issue in "R version 2.11.1 Patched (2010-07-27 r52627)", but it still affects trunk, revision 53979, R 2.13.0-to-be.


FIX

I propose a slight simplification of src/library/utils/R/combn.R to reduce the special-casing of m==0.  In my tests, this corrects the problem.  I can put together a a patch relative to rev. 53979.


SEARCH

Nothing relevant in bugzilla.  (R 2.9.2 contained combn.Rd~; Mismatched braces in the documentation cause bad formatting.)
Comment 1 Christian Brechbuehler 2011-01-14 19:28:22 UTC
Created attachment 1165 [details]
Reduce special casing of m==0; use normal return.

Most of the included patch covers indentation.  The actual changes are few (for your eyes only, white space changes ignored):


--- combn-trunk-53978.R 2011-01-14 10:21:06.000000000 -0500
+++ combn.R     2011-01-14 12:51:13.000000000 -0500
@@ -32,6 +32,8 @@
     stopifnot(length(m) == 1L)
     if(m < 0)
        stop("m < 0")
-    if(m == 0)
-       return(if(simplify) vector(mode(x), 0L) else list())
     if(is.numeric(x) && length(x) == 1L && x > 0 && trunc(x) == x)
        x <- seq_len(x)
     n <- length(x)
@@ -40,7 +42,7 @@
     m <- as.integer(m)
     e <- 0
     h <- m
-    a <- 1L:m
+    a <- seq_len(m)
     nofun <- is.null(FUN)
     if(!nofun && !is.function(FUN))
        stop("'FUN' must be a function or NULL")
@@ -80,7 +82,6 @@
        out[[1L]] <- r
     }
 
+    if(m > 0) {
        i <- 2L
        nmmp1 <- n - m + 1L # using 1L to keep integer arithmetic
        while(a[1L] != nmmp1) {
@@ -100,7 +101,6 @@
                out[, i] <- r else out[[i]] <- r
            i <- i + 1L
        }
+    }
     if(simplify) ##S if(use.arr)
        array(out, dim.use) else out
 }
Comment 2 Martin Maechler 2011-01-17 09:56:12 UTC
thank you; this fixed in R-devel and R-patched.