# Set

#####

declaration
Set

The Implementation of sets in MathLib

To generate the set {1, 2, 3, 4, 5} you simply need to type

    new MathLib.Set([1, 2, 3, 4, 5])

    var Set = (function () {
    		function Set (elements) {
    			var _this = this;
    			this.type = 'set';

# intersect

#####

property
this.intersect → Set

Option name | Type | Description
--- | --- | ---
set | Set | The set to intersect the current set with.

Returns the intersection of two sets.

    this.intersect = Set.createSetOperation(false, true, false);

# union

#####

property
this.union → Set

Option name | Type | Description
--- | --- | ---
set | Set | The set to join the current set with.

Returns the union of two sets.

    this.union = Set.createSetOperation(true, true, true);

# without

#####

property
this.without → Set

Option name | Type | Description
--- | --- | ---
set | Set | The set whose elements should be removed from the current set.

Returns all elements, which are in the first set, but not in the second.

    this.without = Set.createSetOperation(true, false, false);

# xor

#####

property
this.xor → Set

Option name | Type | Description
--- | --- | ---
set | Set | The second set.

Returns all elements which are in either the first or the second set.

    this.xor = Set.createSetOperation(true, false, true);
    			if (!elements) {
    				elements = [];
    			}

    			elements = elements.sort(MathLib.compare).filter(function (x, i, a) {
    				return (a.length === i + 1) || !MathLib.isEqual(x, a[i + 1]) || (typeof x.isEqual !== 'undefined' && !x.isEqual(a[i + 1]));
    			});

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

# compare

#####

method
Set.prototype.compare() → number

Option name | Type | Description
--- | --- | ---
x | Set | The set to compare the current set to

Compare function for sets

    Set.prototype.compare = function (x) {
    			var a, i, ii;

    			if (this.card !== x.card) {
    				return MathLib.sign(this.card - x.card);
    			}
    			else {
    				for (i = 0, ii = this.card; i < ii; i++) {
    					a = MathLib.compare(this[i], x[i]);
    					if (a !== 0) {
    						return a;
    					}
    				}
    				return 0;
    			}
    		};

# evaluate

#####

method
Set.prototype.evaluate() → Set

Evaluates the elements of the set

    Set.prototype.evaluate = function () {
    			return this.map(MathLib.evaluate);
    		};

# every

#####

method
Set.prototype.every() → boolean

Works like the Array.prototype.every function

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

# filter

#####

method
Set.prototype.filter() → Set

Works like the Array.prototype.filter function

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

# forEach

#####

method
Set.prototype.forEach()

Works like the Array.prototype.forEach function

    Set.prototype.forEach = function () {
    			var args = [];
    			for (var _i = 0; _i < (arguments.length - 0); _i++) {
    				args[_i] = arguments[_i + 0];
    			}
    			Array.prototype.forEach.apply(this, args);
    		};

# indexOf

#####

method
Set.prototype.indexOf() → number

Works like the Array.prototype.indexOf function

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

# insert

#####

method
Set.prototype.insert() → Set

Option name | Type | Description
--- | --- | ---
x | any | The element to insert in the set.

Inserts an element into the set.

    Set.prototype.insert = function (x) {
    			var i = this.locate(x);
    			if (this[i] !== x) {
    				this.splice(i, 0, x);
    				this.card++;
    			}
    			return this;
    		};

# isEmpty

#####

method
Set.prototype.isEmpty() → boolean

Determines if the set is empty.

    Set.prototype.isEmpty = function () {
    			return this.card === 0;
    		};

# isEqual

#####

method
Set.prototype.isEqual() → boolean

Option name | Type | Description
--- | --- | ---
set | Set | The set to compare

Determines if the set is equal to an other set.

    Set.prototype.isEqual = function (set) {
    			if (this.card !== set.card) {
    				return false;
    			}
    			else {
    				return this.every(function (y, i) {
    					return MathLib.isEqual(y, set[i]);
    				});
    			}
    		};

# isSubsetOf

#####

method
Set.prototype.isSubsetOf() → boolean

Option name | Type | Description
--- | --- | ---
set | Set | The potential superset

Determines if the set is a subset of an other set.

    Set.prototype.isSubsetOf = function (set) {
    			return this.every(function (x) {
    				return set.indexOf(x) !== -1;
    			});
    		};

# locate

#####

method
Set.prototype.locate() → number

Option name | Type | Description
--- | --- | ---
x | Set | The element to locate

Array.prototype.indexOf() returns only the position of an element in the
array and not the position where one should be inserted.

    Set.prototype.locate = function (x) {
    			var left = 0, right = this.card - 1, middle, i = this.indexOf(x);

    			if (i !== -1) {
    				return i;
    			}

    			while (left <= right) {
    				middle = left + Math.floor((right - left) / 2);
    				if (this[middle] < x) {
    					left = middle + 1;
    				}
    				else if (this[middle] > x) {
    					right = middle - 1;
    				}
    				else {
    					return middle;
    				}
    			}
    			return left;
    		};

# map

#####

method
Set.prototype.map() → Set

Option name | Type | Description
--- | --- | ---
callback | function | - The mapping function
[thisArg] | object | - The value to use as this when executing the callback.

Works like the Array.prototype.map function

    Set.prototype.map = function (callback, thisArg) {
    			return new MathLib.Set(Array.prototype.map.call(this, callback, thisArg));
    		};

# plus

#####

method
Set.prototype.plus() → Set any

Option name | Type | Description
--- | --- | ---
object} | number MathLib | n The object to add to the elements in the set.

Adds the argument to all elements in the set,
or if no argument is provided adds up all the elements in the set.

    Set.prototype.plus = function (n) {
    			var sum = [];
    			if (!arguments.length) {
    				return MathLib.plus.apply(null, this.toArray());
    			}
    			else if (n.type === 'set') {
    				this.forEach(function (x) {
    					n.forEach(function (y) {
    						sum.push(MathLib.plus(x, y));
    					});
    				});

    				return new MathLib.Set(sum);
    			}
    			else {
    				return this.map(function (x) {
    					return MathLib.plus(x, n);
    				});
    			}
    		};

# powerset

#####

method
Set.prototype.powerset() → Set

Returns the powerset

    Set.prototype.powerset = function () {
    			var flag, subset, i, ii, j, jj, powerset = [];

    			for (i = 0, ii = Math.pow(2, this.card); i < ii; i++) {
    				flag = i.toString(2).split('').reverse();
    				subset = [];
    				for (j = 0, jj = this.card; j < jj; j++) {
    					if (flag[j] === '1') {
    						subset.push(this[j]);
    					}
    				}
    				powerset.push(new MathLib.Set(subset));
    			}

    			return new MathLib.Set(powerset);
    		};

# reduce

#####

method
Set.prototype.reduce() → any

Works like the Array.prototype.reduce function

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

# remove

#####

method
Set.prototype.remove() → Set

Option name | Type | Description
--- | --- | ---
element | any | The element to remove from the set.

Removes a element from a set

    Set.prototype.remove = function (element) {
    			var i = this.indexOf(element);
    			if (i !== -1) {
    				this.splice(i, 1);
    				this.card--;
    			}
    			return this;
    		};

# slice

#####

method
Set.prototype.slice() → array

Works like the Array.prototype.slice function

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

# some

#####

method
Set.prototype.some() → boolean

Works like the Array.prototype.some function

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

# splice

#####

method
Set.prototype.splice() → Set

Works like the Array.prototype.splice function

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

# times

#####

method
Set.prototype.times() → Set

Option name | Type | Description
--- | --- | ---
object} | number MathLib | n The object to multiply the elements with

Multiplies all elements in the set if no argument is passed.
Multiplies all elements by a argument if one is passed.

    Set.prototype.times = function (n) {
    			if (!arguments.length) {
    				return MathLib.times.apply(null, this.toArray());
    			}
    			else {
    				return this.map(function (x) {
    					return MathLib.times(x, n);
    				});
    			}
    		};

# toArray

#####

method
Set.prototype.toArray() → array

Converts the set to an array

    Set.prototype.toArray = function () {
    			return Array.prototype.slice.call(this);
    		};

# toContentMathML

#####

method
Set.prototype.toContentMathML() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the content MathML representation of the set

    Set.prototype.toContentMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			if (options.strict) {
    				if (this.isEmpty()) {
    					return '<csymbol cd="set1">emptyset</csymbol>';
    				}
    				else {
    					return this.reduce(function (old, cur) {
    						return old + MathLib.toContentMathML(cur, options);
    					}, '<apply><csymbol cd="set1">set</csymbol>') + '</apply>';
    				}
    			}
    			else {
    				if (this.isEmpty()) {
    					return '<emptyset/>';
    				}
    				else {
    					return this.reduce(function (old, cur) {
    						return old + MathLib.toContentMathML(cur, options);
    					}, '<set>') + '</set>';
    				}
    			}
    		};

# toLaTeX

#####

method
Set.prototype.toLaTeX() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the LaTeX representation of the set

    Set.prototype.toLaTeX = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			if (this.isEmpty()) {
    				return '\\emptyset';
    			}
    			else {
    				return this.reduce(function (old, cur) {
    					return old + MathLib.toLaTeX(cur, options) + ', ';
    				}, '\\left{').slice(0, -2) + '\\right}';
    			}
    		};

# toMathML

#####

method
Set.prototype.toMathML() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns the (presentation) MathML representation of the set

    Set.prototype.toMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			if (this.isEmpty()) {
    				return '<mi>&#x2205;</mi>';
    			}
    			else {
    				return this.reduce(function (old, cur) {
    					return old + MathLib.toMathML(cur, options) + '<mo>,</mo>';
    				}, '<mrow><mo>{</mo>').slice(0, -10) + '<mo>}</mo></mrow>';
    			}
    		};

# toString

#####

method
Set.prototype.toString() → string

Option name | Type | Description
--- | --- | ---
[options] | object | - Optional options to style the output

Returns a string representation of the set

    Set.prototype.toString = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			if (this.isEmpty()) {
    				return '∅';
    			}
    			else {
    				return this.reduce(function (old, cur) {
    					return old + MathLib.toString(cur, options) + ', ';
    				}, '{').slice(0, -2) + '}';
    			}
    		};
    		Set.fromTo = function (start, end, step) {
    			if (typeof step === 'undefined') { step = 1; }
    			var i, set = [];

    			if (start <= end) {
    				for (i = start; i <= end; i += step) {
    					set.push(i);
    				}
    				return new MathLib.Set(set);
    			}
    		};

    		Set.createSetOperation = function (left, both, right) {
    			return function (a) {
    				var set = [], i = 0, j = 0, tl = this.card, al = a.card;

    				while (i < tl && j < al) {
    					if (MathLib.compare(this[i], a[j]) < 0) {
    						if (left) {
    							set.push(this[i]);
    						}
    						i++;
    						continue;
    					}
    					if (MathLib.compare(this[i], a[j]) > 0) {
    						if (right) {
    							set.push(a[j]);
    						}
    						j++;
    						continue;
    					}
    					if (MathLib.isEqual(this[i], a[j])) {
    						if (both) {
    							set.push(this[i]);
    						}
    						i++;
    						j++;
    						continue;
    					}
    				}
    				if (left && j === al) {
    					set = set.concat(this.slice(i));
    				}
    				else if (right && i === tl) {
    					set = set.concat(a.slice(j));
    				}
    				return new MathLib.Set(set);
    			};
    		};
    		return Set;
    	})();
    	MathLib.Set = Set;
    })(MathLib || (MathLib = {}));