[Rd] combn implementation

Martin Maechler maechler at stat.math.ethz.ch
Fri Jan 19 14:45:27 CET 2007


>>>>> "Giampiero" == Giampiero Salvi <giampi at kth.se>
>>>>>     on Fri, 19 Jan 2007 14:21:24 +0100 (CET) writes:

    Giampiero> Hi,
    Giampiero> I was checking the source code to the function combn that "generates
    Giampiero> all combinations of the elements of 'x' taken 'm' at a time.",
    Giampiero> because I wished to modify it. I have a doubt about a statement.
    Giampiero> This is the main loop.

  >     ._1 <- 1:1
  >     nmmp1 <- n - m + ._1
  >     while (a[1] != nmmp1) {
  >         if (e < n - h) {
  >             h <- ._1
  >             e <- a[m]
  >             j <- ._1
  >         }
  >         else {
  >             e <- a[m - h]
  >             h <- h + ._1
  >             j <- 1:h
  >         }
  >         a[m - h + j] <- e + j
  >         r <- if (nofun)
  >             x[a]
  >         else FUN(x[a], ...)
  >         if (simplify)
  >             out[, i] <- r
  >         else out[[i]] <- r
  >         i <- i + 1
  >     }

Not really!  
You are not showing all the comments that go with it!
 --> https://svn.R-project.org/R/trunk/src/library/utils/R/combn.R
is the current source file of the development version whereas
     https://svn.r-project.org/R/tags/R-2-4-1/src/library/utils/R/combn.R
is the R-2.4.1 version

    Giampiero> I wonder what is the meaning of the statement "._1 <- 1:1".

The "real" source in 2.4.1 says
   ._1 <- 1:1 # to keep integer arithmetic
	      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So: It assigns  "1:1" {the infamous short form for as.integer(1)}
    to the variable '._1'

The variable name is a bit unusual and I had a little chuckle when I
invented it to put there.

However with '1L' in R-devel, i.e. from R-2.5.0 on,
code like the above will be definitely be simplified to use '1L'
in such places, and then assigning it does not make sense
anymore (of course it's questionable if it ever did make sense,
but then, as I said, a had a chuckle and I think I found back
then that it *did* increase speed slightly) ....


    Giampiero> First question: what is the difference with "._1 <- 1"?
explained in the comment

    Giampiero> Second question (perhaps answerd by the answer to the first): 

indeed it is.

    Giampiero> why not using "1" directly? E.g.:

    Giampiero> [...]
    Giampiero> h <- 1
    Giampiero> e <- a[m]
    Giampiero> e <- 1
    Giampiero> [...]

    Giampiero> Sorry if this is covered by the manuals, but it's not easy to search
    Giampiero> for the string "._1" with the usual search engines.
    Giampiero> Thank you!
    Giampiero> Giampiero

You're welcome.

Martin Maechler, ETH Zurich



More information about the R-devel mailing list