# Permutation

#####

declaration
Permutation

The permutation class for MathLib

    var Permutation = (function () {
    		function Permutation (p) {
    			var _this = this;
    			this.type = 'permutation';
    			var cycle, permutation;

    			if (Array.isArray(p[0])) {
    				cycle = p;
    				permutation = Permutation.cycleToList(cycle);
    			}
    			else {
    				permutation = p;
    				cycle = Permutation.listToCycle(permutation);
    			}

    			permutation.forEach(function (x, i) {
    				_this[i] = x;
    			});
    			this.length = permutation.length;
    			this.cycle = cycle;
    		}

# applyTo

#####

method
Permutation.prototype.applyTo() → number array Matrix Point Vector

Option name | Type | Description
--- | --- | ---
n | number array Matrix Point Vector | The object to apply the permutation to

Applies the permutation to a number or a array/matrix/point/vector

    Permutation.prototype.applyTo = function (n) {
    			var p, permutatedObj;
    			if (typeof n === 'number') {
    				if (n >= this.length) {
    					return n;
    				}
    				return this[n];
    			}
    			else {
    				p = this;
    				permutatedObj = n.map(function (x, i) {
    					return n[p.applyTo(i)];
    				});

    				return (n.type === undefined ? permutatedObj : new n.constructor(permutatedObj));
    			}
    		};

# compare

#####

method
Permutation.prototype.compare() → number

Option name | Type | Description
--- | --- | ---
p | Permutation | The permutation to compare

Compares two permutations.

    Permutation.prototype.compare = function (p) {
    			var i, ii;

    			if (this.length !== p.length) {
    				return MathLib.sign(this.length - p.length);
    			}

    			for (i = 0, ii = this.length; i < ii; i++) {
    				if (p[i] - this[i]) {
    					return MathLib.sign(this[i] - p[i]);
    				}
    			}

    			return 0;
    		};

# cycleToList

#####

method
Permutation.cycleToList() → array

Option name | Type | Description
--- | --- | ---
cycle | array | The cycle to be converted

Converts a cycle representation to a list representation

    Permutation.cycleToList = function (cycle) {
    			var index, list = [], cur, i, ii, j, jj, max;

    			max = cycle.map(function (b) {
    				return Math.max.apply(null, b);
    			});
    			max = Math.max.apply(null, max);

    			for (i = 0, ii = max; i <= ii; i++) {
    				cur = i;
    				for (j = 0, jj = cycle.length; j < jj; j++) {
    					index = cycle[j].indexOf(cur);
    					if (++index) {
    						cur = cycle[j][index % cycle[j].length];
    					}
    				}
    				list.push(cur);
    			}
    			return list;
    		};

# inverse

#####

method
Permutation.prototype.inverse() → Permutation

Calculates the inverse of the permutation

    Permutation.prototype.inverse = function () {
    			var cycle = this.cycle.slice(0);
    			cycle.reverse().forEach(function (e) {
    				e.reverse();
    			});
    			return new MathLib.Permutation(cycle);
    		};

# listToCycle

#####

method
Permutation.listToCycle() → array

Option name | Type | Description
--- | --- | ---
list | array | The list to be converted

Converts a list representation to a cycle representation

    Permutation.listToCycle = function (list) {
    			var finished = [], cur, i, ii, cycle, cycles = [];

    			for (i = 0, ii = list.length; i < ii; i++) {
    				cur = i;
    				cycle = [];
    				while (!finished[cur]) {
    					finished[cur] = true;
    					cycle.push(cur);
    					cur = list[cur];
    				}
    				if (cycle.length) {
    					cycles.push(cycle);
    				}
    			}
    			return cycles;
    		};

# map

#####

method
Permutation.prototype.map() → Permutation

Works like Array.prototype.map.

    Permutation.prototype.map = function () {
    			var args = [];
    			for (var _i = 0; _i < (arguments.length - 0); _i++) {
    				args[_i] = arguments[_i + 0];
    			}
    			return new MathLib.Permutation(Array.prototype.map.apply(this, args));
    		};

# sgn

#####

method
Permutation.prototype.sgn() → number

Calculates the signum of the permutation

    Permutation.prototype.sgn = function () {
    			var i, ii, count = 0;

    			for (i = 0, ii = this.cycle.length; i < ii; i++) {
    				count += this.cycle[i].length;
    			}
    			count += this.cycle.length;
    			return -2 * (count % 2) + 1;
    		};

# times

#####

method
Permutation.prototype.times() → Permutation

Option name | Type | Description
--- | --- | ---
p | Permutation | The permutation to multiply

Multiplies two permutations

    Permutation.prototype.times = function (p) {
    			var a = this;
    			return p.map(function (x) {
    				return a[x];
    			});
    		};

# toMatrix

#####

method
Permutation.prototype.toMatrix() → Matrix

Option name | Type | Description
--- | --- | ---
n | number | The size of the matrix

Converts the permuatation to a matrix.

    Permutation.prototype.toMatrix = function (n) {
    			var row = [], matrix = [], index, i, ii;
    			n = n || this.length;

    			for (i = 0, ii = n - 1; i < ii; i++) {
    				row.push(0);
    			}
    			row = row.concat([1]).concat(row);
    			for (i = 0, ii = n; i < ii; i++) {
    				index = n - this.applyTo(i) - 1;
    				matrix.push(row.slice(index, index + n));
    			}
    			return new MathLib.Matrix(matrix);
    		};

# toString

#####

method
Permutation.prototype.toString() → string

String representation of the permutation.

    Permutation.prototype.toString = function () {
    			var str = '';
    			this.cycle.forEach(function (elem) {
    				str += '(' + elem.toString() + ')';
    			});
    			return str;
    		};
    		Permutation.id = new Permutation([[]]);
    		return Permutation;
    	})();
    	MathLib.Permutation = Permutation;
    })(MathLib || (MathLib = {}));