UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

150 lines (149 loc) 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../../exceptions/index"); const Matrix_1 = require("./Matrix"); class NumericMatrix extends Matrix_1.Matrix { constructor(rowSize = 2, colSize = 2, ...rows) { super(rowSize, colSize, ...rows); } negative() { const arr2 = []; for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = -this.arr[i][j]; } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } add(other) { const arr2 = []; if (typeof other === 'number') { for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] + other : this.arr[i][j]; } } } else { this.checkSchemaEqual(other); for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] + other.arr[i][j] : this.arr[i][j]; } } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } subtract(other) { const arr2 = []; if (typeof other === 'number') { for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] - other : this.arr[i][j]; } } } else { this.checkSchemaEqual(other); for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] - other.arr[i][j] : this.arr[i][j]; } } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } multiply(other) { const arr2 = []; if (typeof other === 'number') { for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] * other : this.arr[i][j]; } } } else { this.checkSchemaEqual(other); for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] * other.arr[i][j] : this.arr[i][j]; } } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } divide(other) { const arr2 = []; if (typeof other === 'number') { for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] / other : this.arr[i][j]; } } } else { this.checkSchemaEqual(other); for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] / other.arr[i][j] : this.arr[i][j]; } } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } power(other) { const arr2 = []; if (typeof other === 'number') { for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] ** other : this.arr[i][j]; } } } else { this.checkSchemaEqual(other); for (let i = 0; i < this.RowSize; i++) { arr2.push([]); for (let j = 0; j < this.ColSize; j++) { arr2[i][j] = this.arr[i][j] ? this.arr[i][j] ** other.arr[i][j] : this.arr[i][j]; } } } return new NumericMatrix(this.RowSize, this.ColSize, ...arr2); } dotProduct(other) { this.checkDotProduct(other); const arr2 = []; for (let srow = 0; srow < this.RowSize; srow++) { arr2.push([]); for (let ocol = 0; ocol < other.ColSize; ocol++) { arr2[srow][ocol] = 0; for (let orow = 0; orow < other.RowSize; orow++) { arr2[srow][ocol] += this.arr[srow][orow] * other.arr[orow][ocol]; } } } return new NumericMatrix(this.RowSize, other.ColSize, ...arr2); } checkSchemaEqual(other) { if (this.RowSize !== other.RowSize || this.ColSize !== other.ColSize) { throw new index_1.Exception('The matrixes schema is not equals'); } } checkDotProduct(other) { if (this.RowSize !== other.ColSize || this.ColSize !== other.RowSize) { throw new index_1.Exception('The matrixes schema is not equals'); } } } exports.NumericMatrix = NumericMatrix;