nat_to_sub: nat_to_sub

Description Usage Arguments Details Value References Examples

View source: R/combinadic.R

Description

Computes the x^th (n choose 2) combination.

Usage

1
nat_to_sub(x, n, k)

Arguments

x

non-negative integers in the range c(1, choose(n, k))

n

numerator of the binomial coefficient

k

denominator of the binomial coefficient

Details

The mapping is done via an lexicographically-ordered combinadic mapping.
In general, this function is not intended to be used to generate all (n choose k) combinations in the combinadic mapping.

Value

integer matrix whose columns give the combinadics of x.

References

McCaffrey, J. D. "Generating the mth lexicographical element of a mathematical combination." MSDN Library (2004).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
library(simplextree)
all(nat_to_sub(seq(choose(100,2)), n = 100, k = 2) == combn(100,2))

## Generating pairwise combinadics is particularly fast
## Below: test to generate ~ 45k combinadics (note: better to use microbenchmark) 
system.time({
  x <- seq(choose(300,2))
  nat_to_sub(x, n = 300, k = 2L)
})

## Compare with generating raw combinations
system.time(combn(300,2))

simplextree documentation built on Sept. 13, 2020, 5:06 p.m.