UNPKG

deept.js

Version:

Eine TypeScript/JavaScript Library für ML im Browser

220 lines (219 loc) 7.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * ### Matrix Library von [Daniel Shiffmann](https://github.com/shiffmann) * - mit eigenen Erweiterungen * @constructor Erstellt eine Matrix mit gegebener Anzahl an Rows & Columns. * @method add: (_static_) Addiert Zwei Matrizen. * @method subtract: (_static_) Subtrahiert Zwei Matrizen von einander, * @method multiply: (_static_) Multipliziert Zwei Matrizen. * @method fromArray: (_static_) Gibt eine Matrix von einem gegebenen Array zurück. * @method toArray: (_static_) Gibt ein Array mit allen Inhalten der Matrix zurück. * @method toArray2D: (_static_) Gibt ein zweidimensionales Array mit allen Inhalten der Matrix zurück. * @method transpose: (_static_) Transponiert eine gegebene Matrix. * @method copy: (_static_) Kopiert eine gegebene Matrix. * @method map: (_static_) Wendet eine gegebene Funktion auf jedes Element der Matrix an. * @method add: Addiert eine Zahl oder Matrix. * @method subtract: Subtrahiert eine Zahl oder Matrix. * @method multiply: Multipliziert eine Zahl oder Matrix. * @method map: Wendet auf jedes Element der Matrix eine gegebene Funktion an. * @method copy: Kopiert die Matrix. * @method randomize: Weißt jedem Element in der Matrix eine pseudo-zufällige Zahl zw. -1.0 & 1.0 zu. * @method print: Loggt die Matrix in anschaulicher Form zur Konsole. * @method fill: Weißt jedem Element der Matrix eine gegebene Zahl zu (sonst 0). */ var Matrix = /** @class */ (function () { function Matrix(r, c, n) { this.rows = r; this.cols = c; this.data = []; if (!n) { n = 0; } for (var i = 0; i < this.rows; i++) { this.data[i] = []; for (var j = 0; j < this.cols; j++) { this.data[i][j] = n; } } } // === STATIC MATRIX-MATH === Matrix.add = function (a, b) { var result = new Matrix(a.rows, a.cols); for (var i = 0; i < a.rows; i++) { for (var j = 0; j < a.cols; j++) { result.data[i][j] = a.data[i][j] + b.data[i][j]; } } return result; }; Matrix.subtract = function (a, b) { var result = new Matrix(a.rows, a.cols); for (var i = 0; i < a.rows; i++) { for (var j = 0; j < a.cols; j++) { result.data[i][j] = a.data[i][j] - b.data[i][j]; } } return result; }; Matrix.multiply = function (a, b) { // Matrix Product if (a.cols !== b.rows) { console.error('Cols of A must match rows of B.'); return new Matrix(0, 0); } else { var result = new Matrix(a.rows, b.cols); for (var i = 0; i < result.rows; i++) { for (var j = 0; j < result.cols; j++) { // Dot Product of values in col var sum = 0; for (var k = 0; k < a.cols; k++) { sum += a.data[i][k] * b.data[k][j]; } result.data[i][j] = sum; } } return result; } }; // === STATIC MATRIX-UTILS === Matrix.fromArray = function (arr) { var m = new Matrix(arr.length, 1); for (var i = 0; i < arr.length; i++) { m.data[i][0] = arr[i]; } return m; }; Matrix.toArray = function (m) { var result = []; for (var i = 0; i < m.rows; i++) { for (var j = 0; j < m.cols; j++) { result.push(m.data[i][j]); } } return result; }; Matrix.toArray2D = function (m) { return m.data; }; Matrix.transpose = function (matrix) { var result = new Matrix(matrix.cols, matrix.rows); for (var i = 0; i < matrix.rows; i++) { for (var j = 0; j < matrix.cols; j++) { result.data[j][i] = matrix.data[i][j]; } } return result; }; Matrix.copy = function (m) { var result = new Matrix(m.rows, m.cols); for (var i = 0; i < m.rows; i++) { for (var j = 0; j < m.cols; j++) { result.data[i][j] = m.data[i][j]; } } return result; }; Matrix.map = function (matrix, func) { var result = new Matrix(matrix.rows, matrix.cols); for (var i = 0; i < matrix.rows; i++) { for (var j = 0; j < matrix.cols; j++) { var val = matrix.data[i][j]; result.data[i][j] = func(val); } } return result; }; // === INSTANCE MATRIX-MATH === Matrix.prototype.add = function (n) { if (n instanceof Matrix) { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] += n.data[i][j]; } } } else { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] += n; } } } }; Matrix.prototype.subtract = function (n) { if (n instanceof Matrix) { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] -= n.data[i][j]; } } } else { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] -= n; } } } }; Matrix.prototype.multiply = function (n) { if (n instanceof Matrix) { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] *= n.data[i][j]; } } } else { // Scalar product for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] *= n; } } } }; // === INSTANCE MATRIX-UTILS === Matrix.prototype.map = function (func) { // Apply a function to every object in the Matrix for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { var val = this.data[i][j]; this.data[i][j] = func(val); } } }; Matrix.prototype.copy = function () { var m = new Matrix(this.rows, this.cols); for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { m.data[i][j] = this.data[i][j]; } } return m; }; Matrix.prototype.randomize = function () { for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] = Math.random() * 2 - 1; } } }; Matrix.prototype.print = function () { console.table(this.data); }; Matrix.prototype.fill = function (n) { if (!n) { n = 0; } for (var i = 0; i < this.rows; i++) { for (var j = 0; j < this.cols; j++) { this.data[i][j] = n; } } }; return Matrix; }()); exports.default = Matrix;