MrThoe

Matrix in p5

May 31st, 2022
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Matrix {
  2.   constructor(rows, cols) {
  3.     this.rows = rows;
  4.     this.cols = cols;
  5.     this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0));
  6.   }
  7.  
  8.   copy() {
  9.     let m = new Matrix(this.rows, this.cols);
  10.     for (let i = 0; i < this.rows; i++) {
  11.       for (let j = 0; j < this.cols; j++) {
  12.         m.data[i][j] = this.data[i][j];
  13.       }
  14.     }
  15.     return m;
  16.   }
  17.  
  18.   static fromArray(arr) {
  19.     return new Matrix(arr.length, 1).map((e, i) => arr[i]);
  20.   }
  21.  
  22.   static subtract(a, b) {
  23.     if (a.rows !== b.rows || a.cols !== b.cols) {
  24.       console.log('Columns and Rows of A must match Columns and Rows of B.');
  25.       return;
  26.     }
  27.  
  28.     // Return a new Matrix a-b
  29.     return new Matrix(a.rows, a.cols)
  30.       .map((_, i, j) => a.data[i][j] - b.data[i][j]);
  31.   }
  32.  
  33.   toArray() {
  34.     let arr = [];
  35.     for (let i = 0; i < this.rows; i++) {
  36.       for (let j = 0; j < this.cols; j++) {
  37.         arr.push(this.data[i][j]);
  38.       }
  39.     }
  40.     return arr;
  41.   }
  42.  
  43.   randomize() {
  44.     return this.map(e => Math.random() * 2 - 1);
  45.   }
  46.  
  47.   add(n) {
  48.     if (n instanceof Matrix) {
  49.       if (this.rows !== n.rows || this.cols !== n.cols) {
  50.         console.log('Columns and Rows of A must match Columns and Rows of B.');
  51.         return;
  52.       }
  53.       return this.map((e, i, j) => e + n.data[i][j]);
  54.     } else {
  55.       return this.map(e => e + n);
  56.     }
  57.   }
  58.  
  59.   static transpose(matrix) {
  60.     return new Matrix(matrix.cols, matrix.rows)
  61.       .map((_, i, j) => matrix.data[j][i]);
  62.   }
  63.  
  64.   static multiply(a, b) {
  65.     // Matrix product
  66.     if (a.cols !== b.rows) {
  67.       console.log('Columns of A must match rows of B.')
  68.       return;
  69.     }
  70.  
  71.     return new Matrix(a.rows, b.cols)
  72.       .map((e, i, j) => {
  73.         // Dot product of values in col
  74.         let sum = 0;
  75.         for (let k = 0; k < a.cols; k++) {
  76.           sum += a.data[i][k] * b.data[k][j];
  77.         }
  78.         return sum;
  79.       });
  80.   }
  81.  
  82.   multiply(n) {
  83.     if (n instanceof Matrix) {
  84.       if (this.rows !== n.rows || this.cols !== n.cols) {
  85.         console.log('Columns and Rows of A must match Columns and Rows of B.');
  86.         return;
  87.       }
  88.  
  89.       // hadamard product
  90.       return this.map((e, i, j) => e * n.data[i][j]);
  91.     } else {
  92.       // Scalar product
  93.       return this.map(e => e * n);
  94.     }
  95.   }
  96.  
  97.   map(func) {
  98.     // Apply a function to every element of matrix
  99.     for (let i = 0; i < this.rows; i++) {
  100.       for (let j = 0; j < this.cols; j++) {
  101.         let val = this.data[i][j];
  102.         this.data[i][j] = func(val, i, j);
  103.       }
  104.     }
  105.     return this;
  106.   }
  107.  
  108.   static map(matrix, func) {
  109.     // Apply a function to every element of matrix
  110.     return new Matrix(matrix.rows, matrix.cols)
  111.       .map((e, i, j) => func(matrix.data[i][j], i, j));
  112.   }
  113.  
  114.   print() {
  115.     console.table(this.data);
  116.     return this;
  117.   }
  118.  
  119.   serialize() {
  120.     return JSON.stringify(this);
  121.   }
  122.  
  123.   static deserialize(data) {
  124.     if (typeof data == 'string') {
  125.       data = JSON.parse(data);
  126.     }
  127.     let matrix = new Matrix(data.rows, data.cols);
  128.     matrix.data = data.data;
  129.     return matrix;
  130.   }
  131. }
  132.  
  133. if (typeof module !== 'undefined') {
  134.   module.exports = Matrix;
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment