daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Sep 22nd, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getOwnPropertyDescriptors(obj) {
  2.     var descriptors = {};
  3.     for (var prop in obj) {
  4.         if (obj.hasOwnProperty(prop)) {
  5.             descriptors[prop] = Object.getOwnPropertyDescriptor(obj, prop);
  6.         }
  7.     }
  8.     return descriptors;
  9. }
  10.  
  11. function extend(subclass, superclass, proto) {
  12.     subclass.prototype = Object.create(superclass.prototype, getOwnPropertyDescriptors(proto));
  13.     subclass.prototype.constructor = subclass;
  14.     return subclass;
  15. }
  16.  
  17. function Vector(v) {
  18.     if (v instanceof Vector) {
  19.         return v;
  20.     }
  21.     if(!Array.isArray(v)) {
  22.         throw new TypeError();
  23.     }
  24.     v = v.concat();
  25.     v.__proto__ = Vector.prototype;
  26.     return v;
  27. }
  28. extend(Vector, Array, {
  29.     x: function (b) {
  30.         var length = this.length,
  31.             i = 0,
  32.             result = 0;
  33.         if (length != b.length) {
  34.             throw new Error("vectors have different lengths");
  35.         }
  36.         for (; i < length; i++) {
  37.             result += this[i] * b[i];
  38.         }
  39.         return result;
  40.     }
  41. });
  42.    
  43. /**
  44.  * @class
  45.  * @param {Array} m An array with the matrix's elements
  46.  * @extends Array
  47.  */
  48. function Matrix(m) {
  49.     if (m instanceof Matrix) {
  50.         return m;
  51.     }
  52.     if(!Array.isArray(m)) {
  53.         throw new TypeError();
  54.     }
  55.     m = m.map(function (row) {
  56.         return row instanceof Vector ? row : Vector(row);
  57.     });
  58.     m.__proto__ = Matrix.prototype;
  59.     return m;
  60. }
  61. extend(Matrix, Array, {
  62.     /**
  63.      * @returns {String} String representation of the matrix
  64.      */
  65.     toString: function () {
  66.         return this.map(function (a) { return a.join('\t'); }).join('\n');
  67.     },
  68.     /**
  69.      * Multiplies
  70.      * @param {Matrix|Array} m Matrix to multiply to
  71.      * @return {Matrix}
  72.      */
  73.     x: function (m) {
  74.         var result = [],
  75.             size = this.size(),
  76.             i, j;
  77.         if (!(m instanceof Matrix)) {
  78.             m = new Matrix(m);
  79.         }
  80.         if (size[1] != m.size()[0]) {
  81.             throw new Error('Wrong matrix sizes for multiplication');
  82.         }
  83.         for (i = 0; i < size[0]; i++) {
  84.             result[i] = [];
  85.             for (j = 0; j < size[1]; j++) {
  86.                 result[i][j] = this[i].x(m.col(j));
  87.             }
  88.         }
  89.         return new Matrix(result);
  90.     },
  91.     /**
  92.      * Returns a vector representation of the given row
  93.      * @param {Number} index
  94.      * @returns {Vector}
  95.      */
  96.     row: function (index) {
  97.         return this[index];
  98.     },
  99.     /**
  100.      * Returns a vector representation of the given column
  101.      * @param {Number} index
  102.      * @returns {Vector}
  103.      */
  104.     col: function (index) {
  105.         var result = [];
  106.         if (this.size()[1] >= index) {
  107.             result = this.map(function (row) {
  108.                 return row[index];
  109.             });
  110.         }
  111.         return new Vector(result);
  112.     },
  113.     /**
  114.      * Returns an array with the number of rows and columns of the matrix
  115.      * @returns {Array}
  116.      */
  117.     size: function () {
  118.         return [this.length, this.length > 0 ? this[0].length : 0];
  119.     },
  120.     /**
  121.      * Returns a new matrix by transposing this one
  122.      * @returns {Matrix}
  123.      */
  124.     transpose: function () {
  125.         var result = [],
  126.             size = this.size(),
  127.             i, j;
  128.            
  129.         for (i = 0; i < size[0]; i++) {
  130.             result[i] = [];
  131.         }
  132.         for (i = 0; i < size[0]; i++) {
  133.             for (j = 0; j < size[1]; j++) {
  134.                 result[j][i] = this[i][j];
  135.             }
  136.         }
  137.         return new Matrix(result);
  138.     },
  139.     /**
  140.      * Returns a submatrix by deleting a row and a column from the original matrix
  141.      * @param {Number} x The row to delete (counting from 0)
  142.      * @param {Number} y The column to delete
  143.      */
  144.     sub: function (x, y) {
  145.         return Matrix(this.filter(function (el, i) { return i !== x; }).map(function (row) {
  146.             return row.filter(function (el, j) { return j !== y; });
  147.         }));
  148.     },
  149.     /**
  150.      * Calculates the determinant of the matrix
  151.      * @returns {Number}
  152.      */
  153.     det: function () {
  154.         var result = 0,
  155.             size = this.size(),
  156.             i;
  157.         if (size[0] != size[1]) {
  158.             throw new Error("Can't calculate the determinant for a rectangular matrix");
  159.         }
  160.         if (size[0] == 2) {
  161.             return this[0][0] * this[1][1] - this[0][1] * this[1][0];
  162.         } else if (size[0] > 2) {
  163.             for (i = 0; i < size[0]; i++) {
  164.                 result += Math.pow(-1, i) * this[0][i] * this.sub(0, i).det();
  165.             }
  166.         }
  167.         return result;
  168.     },
  169.     eigenvalues: function () {
  170.        
  171.     }
  172. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top