# Vector

#####

declaration
Vector

The vector implementation of MathLib makes calculations with vectors of
arbitrary size possible. The entries of the vector can be numbers and complex
numbers.

It is as easy as
`new MathLib.Vector([1, 2, 3])`
to create the following vector:
⎛ 1 ⎞
⎜ 2 ⎟
⎝ 3 ⎠

    var Vector = (function () {
    		function Vector (coords) {
    			var _this = this;
    			this.type = 'vector';
    			coords.forEach(function (x, i) {
    				_this[i] = x;
    			});
    			this.length = coords.length;
    		}

# compare

#####

method
Vector.prototype.compare() → number

Option name | Type | Description
--- | --- | ---
v | Vector | The vector to compare

Compares two vectors.

    Vector.prototype.compare = function (v) {
    			var i, ii;

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

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

    			return 0;
    		};

# evaluate

#####

method
Vector.prototype.evaluate() → Vector

Evaluates the entries of the vector

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

# every

#####

method
Vector.prototype.every() → boolean

Option name | Type | Description
--- | --- | ---
f | function | The function to be applied to all the values

Works like Array.prototype.every.

    Vector.prototype.every = function (f) {
    			return Array.prototype.every.call(this, f);
    		};

# forEach

#####

method
Vector.prototype.forEach()

Option name | Type | Description
--- | --- | ---
f | function | The function to be applied to all the values

Works like Array.prototype.forEach.

    Vector.prototype.forEach = function (f) {
    			Array.prototype.forEach.call(this, f);
    		};

# isEqual

#####

method
Vector.prototype.isEqual() → boolean

Option name | Type | Description
--- | --- | ---
v | Vector | The vector to compare

Determines if two vectors are equal

    Vector.prototype.isEqual = function (v) {
    			if (this.length !== v.length) {
    				return false;
    			}

    			return this.every(function (x, i) {
    				return MathLib.isEqual(x, v[i]);
    			});
    		};

# isZero

#####

method
Vector.prototype.isZero() → boolean

Determines if the vector is the zero vector.

    Vector.prototype.isZero = function () {
    			return this.every(MathLib.isZero);
    		};

# map

#####

method
Vector.prototype.map() → Vector

Option name | Type | Description
--- | --- | ---
f | function | The function to be applied to all the values

Works like Array.prototype.map.

    Vector.prototype.map = function (f) {
    			return new this.constructor(Array.prototype.map.call(this, f));
    		};

# minus

#####

method
Vector.prototype.minus() → Vector

Option name | Type | Description
--- | --- | ---
v | Vector | The vector to be subtracted.

Calculates the difference of two vectors.

    Vector.prototype.minus = function (v) {
    			if (this.length === v.length) {
    				return this.plus(v.negative());
    			}
    			else {
    				MathLib.error({message: 'Vector sizes not matching', method: 'Vector#minus'});
    				return;
    			}
    		};

# negative

#####

method
Vector.prototype.negative() → Vector

Returns the negative vector.

    Vector.prototype.negative = function () {
    			return this.map(function (entry) {
    				return MathLib.negative(entry);
    			});
    		};

# norm

#####

method
Vector.prototype.norm() → number

Option name | Type | Description
--- | --- | ---
p | number | The p for the p-norm

Calcultes the norm of the vector.

    Vector.prototype.norm = function (p) {
    			if (typeof p === 'undefined') { p = 2; }
    			if (p === 2) {
    				return MathLib.hypot.apply(null, this.toArray());
    			}
    			else if (p === Infinity) {
    				return Math.max.apply(null, this.map(Math.abs).toArray());
    			}
    			else {
    				return MathLib.root(this.reduce(function (prev, curr) {
    					return prev + Math.pow(Math.abs(curr), p);
    				}, 0), p);
    			}
    		};

# outerProduct

#####

method
Vector.prototype.outerProduct() → Matrix

Option name | Type | Description
--- | --- | ---
v | Vector | The second vector to calculate the outer product with.

Calculates the outer product of two vectors.

    Vector.prototype.outerProduct = function (v) {
    			return new MathLib.Matrix(this.map(function (x) {
    				return v.map(function (y) {
    					return MathLib.times(x, y);
    				});
    			}));
    		};

# plus

#####

method
Vector.prototype.plus() → Vector

Option name | Type | Description
--- | --- | ---
v | Vector | The vector to add to the current vector.

Calculates the sum of two vectors.

    Vector.prototype.plus = function (v) {
    			if (this.length === v.length) {
    				return new MathLib.Vector(this.map(function (x, i) {
    					return MathLib.plus(x, v[i]);
    				}));
    			}
    			else {
    				MathLib.error({message: 'Vector sizes not matching', method: 'Vector#plus'});
    				return;
    			}
    		};

# reduce

#####

method
Vector.prototype.reduce() → any

Works like Array.prototype.reduce.

    Vector.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, args);
    		};

# scalarProduct

#####

method
Vector.prototype.scalarProduct() → number Complex

Option name | Type | Description
--- | --- | ---
v | Vector | The second vector to calculate the scalar product with.

Calculates the scalar product of two vectors.

    Vector.prototype.scalarProduct = function (v) {
    			if (this.length === v.length) {
    				return this.reduce(function (old, cur, i, w) {
    					return MathLib.plus(old, MathLib.times(w[i], v[i]));
    				}, 0);
    			}
    			else {
    				MathLib.error({message: 'Vector sizes not matching', method: 'Vector#scalarProduct'});
    				return;
    			}
    		};

# slice

#####

method
Vector.prototype.slice() → array

Works like the Array.prototype.slice function

    Vector.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);
    		};

# times

#####

method
Vector.prototype.times() → Vector

Option name | Type | Description
--- | --- | ---
n | number Complex Matrix | The object to multiply to the vector

Multiplies the vector by a (complex) number or a matrix.
The vector is multiplied from left to the matrix.
If you want to multiply it from the right use
matrix.times(vector) instead of vector.times(matrix)

    Vector.prototype.times = function (n) {
    			var i, ii, colVectors, product = [];
    			if (n.type === 'rational') {
    				n = n.coerceTo('number');
    			}
    			if (typeof n === 'number' || n.type === 'complex') {
    				return this.map(function (x) {
    					return MathLib.times(n, x);
    				});
    			}
    			if (n.type === 'matrix') {
    				if (this.length === n.rows) {
    					colVectors = n.toColVectors();
    					for (i = 0, ii = colVectors.length; i < ii; i++) {
    						product[i] = this.scalarProduct(colVectors[i]);
    					}
    					return new MathLib.Vector(product);
    				}
    				else {
    					MathLib.error({message: 'Vector/Matrix sizes not matching', method: 'Vector#times'});
    					return;
    				}
    			}
    		};

# toArray

#####

method
Vector.prototype.toArray() → array

Converts the vector to an array.

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

# toContentMathML

#####

method
Vector.prototype.toContentMathML() → string

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

Returns the content MathML representation of the vector.

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

# toLaTeX

#####

method
Vector.prototype.toLaTeX() → string

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

Returns a LaTeX representation of the vector.

    Vector.prototype.toLaTeX = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			return '\\begin{pmatrix}\n\t' + this.reduce(function (old, cur) {
    				return old + '\\\\\n\t' + MathLib.toLaTeX(cur, options);
    			}) + '\n\\end{pmatrix}';
    		};

# toMathML

#####

method
Vector.prototype.toMathML() → string

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

Returns the (presentation) MathML representation of the vector.

    Vector.prototype.toMathML = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			return this.reduce(function (old, cur) {
    				return old + '<mtr><mtd>' + MathLib.toMathML(cur, options) + '</mtd></mtr>';
    			}, '<mrow><mo>(</mo><mtable>') + '</mtable><mo>)</mo></mrow>';
    		};

# toString

#####

method
Vector.prototype.toString() → string

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

Returns a string representation of the vector.

    Vector.prototype.toString = function (options) {
    			if (typeof options === 'undefined') { options = {}; }
    			return '(' + this.reduce(function (old, cur) {
    				return old + ', ' + MathLib.toString(cur, options);
    			}) + ')';
    		};

# vectorProduct

#####

method
Vector.prototype.vectorProduct() → Vector

Option name | Type | Description
--- | --- | ---
v | Vector | The second vector to calculate the vector product with.

Calculates the vector product of two vectors.

    Vector.prototype.vectorProduct = function (v) {

TODO: Implement vectorproduct for non three-dimensional vectors

    if (this.length === 3 && v.length === 3) {
    				return new MathLib.Vector([
    					MathLib.minus(MathLib.times(this[1], v[2]), MathLib.times(this[2], v[1])),
    					MathLib.minus(MathLib.times(this[2], v[0]), MathLib.times(this[0], v[2])),
    					MathLib.minus(MathLib.times(this[0], v[1]), MathLib.times(this[1], v[0]))
    				]);
    			}
    			else {
    				MathLib.error({message: 'Vectors are not three-dimensional', method: 'Vector#vectorProduct'});
    				return;
    			}
    		};
    		Vector.areLinearIndependent = function (vectors) {
    			var n = vectors.length, m = vectors[0].length;

    			if (n > m) {
    				return false;
    			}

    			if (!vectors.every(function (x) {
    				return x.length === m;
    			})) {
    				return undefined;
    			}

    			return (new MathLib.Matrix(vectors)).rank() === n;
    		};

    		Vector.zero = function (n) {
    			var vector = [], i;
    			for (i = 0; i < n; i++) {
    				vector.push(0);
    			}
    			return new MathLib.Vector(vector);
    		};
    		return Vector;
    	})();
    	MathLib.Vector = Vector;
    })(MathLib || (MathLib = {}));