Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getOwnPropertyDescriptors(obj) {
- var descriptors = {};
- for (var prop in obj) {
- if (obj.hasOwnProperty(prop)) {
- descriptors[prop] = Object.getOwnPropertyDescriptor(obj, prop);
- }
- }
- return descriptors;
- }
- function extend(subclass, superclass, proto) {
- subclass.prototype = Object.create(superclass.prototype, getOwnPropertyDescriptors(proto));
- subclass.prototype.constructor = subclass;
- return subclass;
- }
- function Vector(v) {
- if (v instanceof Vector) {
- return v;
- }
- if(!Array.isArray(v)) {
- throw new TypeError();
- }
- v = v.concat();
- v.__proto__ = Vector.prototype;
- return v;
- }
- extend(Vector, Array, {
- x: function (b) {
- var length = this.length,
- i = 0,
- result = 0;
- if (length != b.length) {
- throw new Error("vectors have different lengths");
- }
- for (; i < length; i++) {
- result += this[i] * b[i];
- }
- return result;
- }
- });
- /**
- * @class
- * @param {Array} m An array with the matrix's elements
- * @extends Array
- */
- function Matrix(m) {
- if (m instanceof Matrix) {
- return m;
- }
- if(!Array.isArray(m)) {
- throw new TypeError();
- }
- m = m.map(function (row) {
- return row instanceof Vector ? row : Vector(row);
- });
- m.__proto__ = Matrix.prototype;
- return m;
- }
- extend(Matrix, Array, {
- /**
- * @returns {String} String representation of the matrix
- */
- toString: function () {
- return this.map(function (a) { return a.join('\t'); }).join('\n');
- },
- /**
- * Multiplies
- * @param {Matrix|Array} m Matrix to multiply to
- * @return {Matrix}
- */
- x: function (m) {
- var result = [],
- size = this.size(),
- i, j;
- if (!(m instanceof Matrix)) {
- m = new Matrix(m);
- }
- if (size[1] != m.size()[0]) {
- throw new Error('Wrong matrix sizes for multiplication');
- }
- for (i = 0; i < size[0]; i++) {
- result[i] = [];
- for (j = 0; j < size[1]; j++) {
- result[i][j] = this[i].x(m.col(j));
- }
- }
- return new Matrix(result);
- },
- /**
- * Returns a vector representation of the given row
- * @param {Number} index
- * @returns {Vector}
- */
- row: function (index) {
- return this[index];
- },
- /**
- * Returns a vector representation of the given column
- * @param {Number} index
- * @returns {Vector}
- */
- col: function (index) {
- var result = [];
- if (this.size()[1] >= index) {
- result = this.map(function (row) {
- return row[index];
- });
- }
- return new Vector(result);
- },
- /**
- * Returns an array with the number of rows and columns of the matrix
- * @returns {Array}
- */
- size: function () {
- return [this.length, this.length > 0 ? this[0].length : 0];
- },
- /**
- * Returns a new matrix by transposing this one
- * @returns {Matrix}
- */
- transpose: function () {
- var result = [],
- size = this.size(),
- i, j;
- for (i = 0; i < size[0]; i++) {
- result[i] = [];
- }
- for (i = 0; i < size[0]; i++) {
- for (j = 0; j < size[1]; j++) {
- result[j][i] = this[i][j];
- }
- }
- return new Matrix(result);
- },
- /**
- * Returns a submatrix by deleting a row and a column from the original matrix
- * @param {Number} x The row to delete (counting from 0)
- * @param {Number} y The column to delete
- */
- sub: function (x, y) {
- return Matrix(this.filter(function (el, i) { return i !== x; }).map(function (row) {
- return row.filter(function (el, j) { return j !== y; });
- }));
- },
- /**
- * Calculates the determinant of the matrix
- * @returns {Number}
- */
- det: function () {
- var result = 0,
- size = this.size(),
- i;
- if (size[0] != size[1]) {
- throw new Error("Can't calculate the determinant for a rectangular matrix");
- }
- if (size[0] == 2) {
- return this[0][0] * this[1][1] - this[0][1] * this[1][0];
- } else if (size[0] > 2) {
- for (i = 0; i < size[0]; i++) {
- result += Math.pow(-1, i) * this[0][i] * this.sub(0, i).det();
- }
- }
- return result;
- },
- eigenvalues: function () {
- }
- });
Add Comment
Please, Sign In to add comment