Skip to content

Commit 15d0ae1

Browse files
add docs
1 parent 6dec65b commit 15d0ae1

33 files changed

+268
-22
lines changed

src/_apply.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { _transpose } from './_transpose' ;
22

3+
/**
4+
* Applies a given sequence (in the given order) of transpositions (given as
5+
* index tuples) to a given permutation. The permutation is modified in place.
6+
*
7+
* @param transpositions The given transpositions to apply.
8+
* @param sigma The permutation to apply the transpositions to.
9+
*/
310
export function _apply ( transpositions , sigma ) {
411

5-
for ( const [ s , t ] of transpositions ) {
6-
7-
_transpose( s , t , sigma ) ;
8-
9-
}
12+
for ( const [ s , t ] of transpositions ) _transpose( s , t , sigma ) ;
1013

1114
}
1215

src/_bitreversal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
/**
3+
* Fills an input array with the bitreversal permutation for input
4+
* <code>n</code> items. The array is filled starting at index <code>0</code>
5+
* and ending at index <code>n-1</code>. Note that <code>n</code> MUST be a
6+
* power of <code>2</code>.
7+
*
8+
* @param {Array} array The array to fill.
9+
* @param {Number} n The size of the permutation, must be a power of 2.
10+
*/
211
export function _bitreversal ( array , n ) {
312

413
let i = 1 ;

src/_compose.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
/**
3+
* Compose two input permutations. The resulting permutation is output as an
4+
* iterator of indices instead of an array of indices.
5+
*
6+
* @param {Array} sigma The first input permutation.
7+
* @param {Array} tau The second input permutation.
8+
* @returns {Iterator} An iterator over the items of the resulting permutation.
9+
*/
210
export function* _compose ( sigma , tau ) {
311

412
for ( const t of tau ) yield sigma[t] ;

src/_copy.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

2+
/**
3+
* Copy an input permutation to an output array.
4+
*
5+
* @param {Array} sigma The input permutation.
6+
* @param {Number} n The size of the input permutation to copy.
7+
* @param {Array} tau The output array.
8+
*/
29
export function _copy ( sigma , n , tau ) {
310

4-
for ( let i = 0 ; i < n ; ++i ) {
5-
6-
tau[i] = sigma[i] ;
7-
8-
}
11+
for ( let i = 0 ; i < n ; ++i ) tau[i] = sigma[i] ;
912

1013
}
1114

src/_cycles.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11

2+
/**
3+
* Computes a {@link https://en.wikipedia.org/wiki/Permutation#Cycle_notation
4+
* cycle decomposition} of an input permutation. This algorithm is
5+
* deterministic; the algorithm will procedes in a sequential manner, first
6+
* yielding the cycle containing the first (in input order) index of the
7+
* permutation, then yielding the cycle containing the first (in input order)
8+
* index of the permutation that is not in the first cycle, etc. The output is
9+
* in the form of an iterator over cycles <code>[a, [b, c, ...]]</code> where
10+
* <code>a</code> is the first element of the cycle and the list <code>[b, c,
11+
* ...]</code> contains the second, third, etc. elements of the cycle. The
12+
* algorithm uses an helper array to remember which elements have already been
13+
* encountered.
14+
* @param {Array} sigma The input permutation.
15+
* @param {Array} used The helper array.
16+
* @returns {Iterator} The cycles <code>[a, [b, c, ...]]</code> for sigma.
17+
*/
218
export function* _cycles ( sigma , used ) {
319

420
for ( const s of sigma ) {

src/_identity.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

2+
/**
3+
* Fills an input array with the identity permutation on input <code>n</code>
4+
* elements.
5+
*
6+
* @param sigma The input array.
7+
* @param n The size to use for the permutation.
8+
*/
29
export function _identity ( sigma , n ) {
310

4-
for ( let i = 0 ; i < n ; ++i ) {
5-
6-
sigma[i] = i ;
7-
8-
}
11+
for ( let i = 0 ; i < n ; ++i ) sigma[i] = i ;
912

1013
}
1114

src/_invert.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
1+
/**
2+
* Fills an input array with the inverse <code>rho</code> of the input
3+
* permutation <code>sigma</code>, that is, the permutation such that
4+
* <code>compose(rho, sigma)</code> returns
5+
* <code>identity(sigma.length)</code>.
6+
*
7+
* @param {Array} sigma The input permutation.
8+
* @param {Number} n The size of the input permutation.
9+
* @param {Array} tau The array where to put the inverse of the input permutation.
10+
*/
211
export function _invert ( sigma , n , tau ) {
312

4-
for ( let i = 0 ; i < n ; ++i ) {
5-
6-
tau[sigma[i]] = i ;
7-
8-
}
13+
for ( let i = 0 ; i < n ; ++i ) tau[sigma[i]] = i ;
914

1015
}
1116

src/_invertcycles.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { _transpose } from './_transpose' ;
22

3+
/**
4+
* No idea what this does. Probably inverts the cycles of a given permutation.
5+
*
6+
* @param {Iterable} cycles The cycles to invert.
7+
* @param {Array} tau The given permutation.
8+
*/
39
export function _invertcycles ( cycles , tau ) {
410

511
for ( const [ s , cycle ] of cycles ) {

src/_itranspositions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
/**
3+
* Really no idead what this does.
4+
*
5+
* @param {Array} sigma Input permutation.
6+
* @param {Array} used Helper array.
7+
* @return {Iterator} ?
8+
*/
29
export function* _itranspositions ( sigma , used ) {
310

411
for ( const s of sigma ) {

src/_next.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { _transpose } from './_transpose' ;
22
import { _reverse } from './_reverse' ;
33

4+
/**
5+
* Updates the input permutation to the next one. Returns true unless the input
6+
* permutation is the last for its elements. In that case, the input permutation
7+
* remains untouched.
8+
*
9+
* @param {Array} sigma The input permutation.
10+
* @param {Number} n The size of the input permutation.
11+
* @returns {Boolean} Whether the input permutation is
12+
* <underline>NOT</underline> the last for its elements.
13+
*/
414
export function _next ( sigma , n ) {
515

616
let i = n - 1 ;

src/_permutations.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { _next } from './_next' ;
22

3+
/**
4+
* Yields all permutations starting from a given one and ending at the last
5+
* permutation.
6+
*
7+
* @param {Array} sigma The starting permutation.
8+
* @param {Number} n The size of the permutation.
9+
* @returns {Iterator} Iterator over all permutations between the starting one
10+
* and the last permutation on its elements.
11+
*/
312
export function* _permutations ( sigma , n ) {
413

514
do { yield sigma ; } while ( _next( sigma , n ) ) ;

src/_reverse.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { _transpose } from './_transpose' ;
22

3+
/**
4+
* Reverses input permutation in place from input index <code>i</code>
5+
* (include) to input index <code>j</code> (excluded).
6+
*
7+
* @param {Array} sigma The input permutation to reverse.
8+
* @param {Number} i The left bound (included).
9+
* @param {Number} j The right bound (excluded).
10+
*/
311
export function _reverse ( sigma , i , j ) {
412

513
while ( i <-- j ) _transpose( i++ , j , sigma ) ;

src/_transpose.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11

2+
/**
3+
* Transpose elements of input index <code>a</code> and <code>b</code> in the
4+
* input permutation.
5+
*
6+
* @param {Number} a The first input index.
7+
* @param {Number} b The second input index.
8+
* @param {Array} sigma The input permutation.
9+
*/
210
export function _transpose ( a , b , sigma ) {
311

412
const tmp = sigma[a] ;

src/_transposition.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
/**
3+
* Helper method for {@link transposition}.
4+
* Transposes <code>a</code> with <code>b</code> in <code>sigma</code> provided
5+
* <code>sigma[a] === a</code> and <code>sigma[b] === b</code>.
6+
*
7+
* @param {Number} a First index.
8+
* @param {Number} b Second index.
9+
* @param {Array} sigma Input permutation.
10+
*/
211
export function _transposition ( a , b , sigma ) {
312

413
sigma[a] = b ;

src/_transpositions.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* Computes the transposition decomposition of some permutation given its cycle
3+
* decomposition.
4+
*
5+
* @param {Array} cycles The cycle decomposition of some permutation.
6+
* @returns {Iterator} The transposition decomposition of the permutation
7+
* defined by the input cycles.
8+
*/
19
export function* _transpositions ( cycles ) {
210

311
for ( const [ s , cycle ] of cycles ) {
@@ -7,4 +15,3 @@ export function* _transpositions ( cycles ) {
715
}
816

917
}
10-

src/_used.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11

2+
/**
3+
* For a given size <code>n</code>, fills an input array with
4+
* <code>false</code>. Starting at index <code>0</code>, ending at index
5+
* <code>n-1</code>. This array is used as a helper in other function. For
6+
* example, for computing the cycle decomposition of an input permutation (see
7+
* {@link _cycles}, {@link cycles}).
8+
*
9+
* @param n The given size.
10+
* @param array The input array.
11+
*/
212
export function _used ( n , array ) {
313

414
for ( let i = 0 ; i < n ; ++i ) array[i] = false ;

src/apply.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { identity } from './identity' ;
22
import { _apply } from './_apply' ;
33

4+
/**
5+
* Apply a given sequence (in the given order) of transpositions (given as
6+
* index tuples) to the identity permutation over input <code>n</code> elements.
7+
*
8+
* @param n The size of the identity permutation to apply the transpositions
9+
* to.
10+
* @param transpositions The given transpositions to apply.
11+
* @returns {Array} The resulting permutation.
12+
*/
413
export function apply ( n , transpositions ) {
514

615
const rho = identity( n ) ;

src/bitreversal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { permutation } from './permutation' ;
22
import { _bitreversal } from './_bitreversal' ;
33

4+
/**
5+
* Returns a newly allocated array containing the bitreversal permutation for
6+
* input <code>n</code> items. Note that <code>n</code> MUST be a power of
7+
* <code>2</code>.
8+
*
9+
* @param {Number} n The size of the permutation, must be a power of
10+
* <code>2</code>.
11+
* @returns {Array} The bitreversal permutation of size <code>n</code>.
12+
*/
413
export function bitreversal ( n ) {
514

615
const sigma = permutation( n ) ;

src/compose.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { permutation } from './permutation' ;
22
import { _compose } from './_compose' ;
33

4+
/**
5+
* Compose two input permutations. The resulting permutation is output as an
6+
* array of indices.
7+
*
8+
* @param {Array} sigma The first input permutation.
9+
* @param {Array} tau The second input permutation.
10+
* @returns {Array} The resulting permutation as an array.
11+
*/
412
export function compose ( sigma , tau ) {
513

14+
// TODO replace with Array.from( _compose...
15+
616
const rho = permutation( sigma.length ) ;
717

818
let i = 0 ;

src/copy.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { permutation } from './permutation' ;
22
import { _copy } from './_copy' ;
33

4+
/**
5+
* Make a copy of the input permutation.
6+
*
7+
* @param {Array} sigma The input permutation.
8+
* @returns {Array} The copy.
9+
*/
410
export function copy ( sigma ) {
511

612
const rho = permutation( sigma.length ) ;

src/cycles.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { _cycles } from './_cycles' ;
22
import { used } from './used' ;
33

4+
5+
/**
6+
* Computes the cycle decomposition of the input permutation.
7+
* See {@link _cycles} for implementation.
8+
*
9+
* @param {Array} sigma The input permutation.
10+
* @returns {Iterator} The cycles <code>[a, [b, c, ...]]</code> for sigma.
11+
*/
412
export function* cycles ( sigma ) {
513

614
yield* _cycles( sigma , used( sigma.length ) ) ;

src/identity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { permutation } from './permutation' ;
22
import { _identity } from './_identity' ;
33

4+
/**
5+
* Returns the identity permutation of a given size.
6+
*
7+
* @param {Number} n The size of the permutation to return.
8+
* @returns {Array} The identity permutation on <code>n</code> elements.
9+
*/
410
export function identity ( n ) {
511

612
const rho = permutation( n ) ;

src/invert.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { permutation } from './permutation' ;
22
import { _invert } from './_invert' ;
33

4+
/**
5+
* Computes the inverse <code>rho</code> of the input permutation
6+
* <code>sigma</code>, that is, the permutation such that <code>compose(rho,
7+
* sigma)</code> returns <code>identity(sigma.length)</code>.
8+
*
9+
* @param {Array} sigma The input permutation.
10+
* @returns {Array} The inverse of the input permutation.
11+
*/
412
export function invert ( sigma ) {
513

614
const rho = permutation( sigma.length ) ;

src/itranspositions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { _itranspositions } from './_itranspositions' ;
22
import { used } from './used' ;
33

4+
/**
5+
* Really no idead what this does, see also {@link _itranspositions}.
6+
*
7+
* @param {Array} sigma Input permutation.
8+
* @return {Iterator} ?
9+
*/
410
export function* itranspositions ( sigma ) {
511

612
yield* _itranspositions( sigma , used( sigma.length ) ) ;

src/next.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { copy } from './copy' ;
22
import { _next } from './_next' ;
33
import { reverse } from './reverse' ;
44

5+
/**
6+
* Computes the permutation that follows the input permutation. If the input
7+
* permutation is the last for its elements, return the first for its elements.
8+
* The input permutation is not altered.
9+
*
10+
* @param {Array} sigma The input permutation.
11+
* @returns {Array} The next permutation.
12+
*/
513
export function next ( sigma ) {
614

715
const rho = copy( sigma ) ;

0 commit comments

Comments
 (0)