name-undecided
Version:
A toy Neural Network library.
183 lines (182 loc) • 5.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Matrix = void 0;
var Matrix = /** @class */ (function () {
function Matrix(rows, cols) {
this.rows = rows;
this.cols = cols;
this.vals = [];
for (var i = 0; i < rows; i++) {
this.vals[i] = [];
for (var j = 0; j < cols; j++) {
this.vals[i][j] = 0;
}
}
}
Matrix.prototype.print = function () {
// tslint:disable-next-line:no-console
console.table(this.vals);
};
Matrix.prototype.set = function (row, col, val) {
this.vals[row][col] = val;
};
Matrix.prototype.map = function (f) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] = f(this.vals[i][j]);
}
}
};
Matrix.prototype.addMatrix = function (other) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] += other.vals[i][j];
}
}
};
Matrix.prototype.addScaler = function (num) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] += num;
}
}
};
Matrix.prototype.subMatrix = function (other) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] -= other.vals[i][j];
}
}
};
Matrix.prototype.subScaler = function (num) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] -= num;
}
}
};
Matrix.prototype.hadamard = function (other) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] *= other.vals[i][j];
}
}
};
Matrix.prototype.mult = function (other) {
// Matrix product
if (this.cols !== other.rows) {
throw new Error('Columns of A must match rows of B.');
}
var result = new Matrix(this.rows, other.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 < this.cols; k++) {
sum += this.vals[i][k] * other.vals[k][j];
}
result.vals[i][j] = sum;
}
}
this.vals = result.vals;
this.rows = result.rows;
this.cols = result.cols;
};
Matrix.prototype.scale = function (num) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.vals[i][j] *= num;
}
}
};
Matrix.prototype.transpose = function () {
var result = new Matrix(this.cols, this.rows);
for (var i = 0; i < result.rows; i++) {
for (var j = 0; j < result.cols; j++) {
result.vals[i][j] = this.vals[j][i];
}
}
this.vals = result.vals;
this.rows = result.rows;
this.cols = result.cols;
};
Matrix.prototype.toArray = function () {
return this.vals;
};
Matrix.fromArray = function (arr, rows, cols) {
var result = new Matrix(rows, cols);
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
result.vals[i][j] = arr[i * cols + j];
}
}
return result;
};
Matrix.print = function (m) {
m.print();
};
Matrix.set = function (m, row, col, val) {
var result = new Matrix(m.rows, m.cols);
result.set(row, col, val);
return result;
};
Matrix.map = function (m, f) {
var result = new Matrix(m.rows, m.cols);
result.addMatrix(m);
result.map(f);
return result;
};
Matrix.addMatrix = function (m1, m2) {
var result = new Matrix(m1.rows, m1.cols);
result.addMatrix(m1);
result.addMatrix(m2);
return result;
};
Matrix.addScaler = function (m, num) {
var result = new Matrix(m.rows, m.cols);
result.addScaler(num);
result.addMatrix(m);
return result;
};
Matrix.subMatrix = function (m1, m2) {
var result = new Matrix(m1.rows, m1.cols);
result.addMatrix(m1);
result.subMatrix(m2);
return result;
};
Matrix.subScaler = function (m, num) {
var result = new Matrix(m.rows, m.cols);
result.addMatrix(m);
result.subScaler(num);
return result;
};
Matrix.hadamard = function (m1, m2) {
var result = new Matrix(m1.rows, m1.cols);
result.addMatrix(m1);
result.hadamard(m2);
return result;
};
Matrix.mult = function (m1, m2) {
var result = new Matrix(m1.rows, m1.cols);
result.addMatrix(m1);
result.mult(m2);
return result;
};
Matrix.scale = function (m, num) {
var result = new Matrix(m.rows, m.cols);
result.addMatrix(m);
result.scale(num);
return result;
};
Matrix.transpose = function (m) {
var result = new Matrix(m.cols, m.rows);
for (var i = 0; i < result.rows; i++) {
for (var j = 0; j < result.cols; j++) {
result.vals[i][j] = m.vals[j][i];
}
}
return result;
};
return Matrix;
}());
exports.Matrix = Matrix;