perm {lazy.tools}R Documentation

Generate all m-permutations of n without repetition

Description

Generate all m-permutations of n without repetition

Usage

perm(n, m, vals = 1:n, noreverse = 0)

Arguments

n

total # of elements

m

# of elements to choose

vals

a vector of values to be permuted

noreverse

= 1 to remove reverse-equivalent rows
= 2 to check the result

Details

The recursive algorithm below is used.

  res=NULL
  for( i in 1:n ){
    res <- rbind(res, cbind(vals[i], Recall(n - 1, m - 1, vals[-i])))
  }

The method of removal of reverse-equivalent rows comes from the answer to stack overflow question:
"How to remove reverse rows in a permutation matrix?"
by obchardon at Sep 15, 2020 at 7:46
https://stackoverflow.com/questions/63896177/how-to-remove-reverse-rows -in-a-permutation-matrix

Value

A matrix of the size P(n,m) x m.

Examples

n <- 5
for( m in 1:n )
 Print( m, perm(n,m), comb(n,m) )
 
 Print( perm(3,3), perm(3,3,,1) )


[Package lazy.tools version 0.1.4 ]