UNPKG

ml-matrix

Version:

Matrix manipulation and computation library

824 lines (718 loc) 23.4 kB
export function installMathOperations(AbstractMatrix, Matrix) { AbstractMatrix.prototype.add = function add(value) { if (typeof value === 'number') return this.addS(value); return this.addM(value); }; AbstractMatrix.prototype.addS = function addS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) + value); } } return this; }; AbstractMatrix.prototype.addM = function addM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) + matrix.get(i, j)); } } return this; }; AbstractMatrix.add = function add(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.add(value); }; AbstractMatrix.prototype.sub = function sub(value) { if (typeof value === 'number') return this.subS(value); return this.subM(value); }; AbstractMatrix.prototype.subS = function subS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) - value); } } return this; }; AbstractMatrix.prototype.subM = function subM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) - matrix.get(i, j)); } } return this; }; AbstractMatrix.sub = function sub(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.sub(value); }; AbstractMatrix.prototype.subtract = AbstractMatrix.prototype.sub; AbstractMatrix.prototype.subtractS = AbstractMatrix.prototype.subS; AbstractMatrix.prototype.subtractM = AbstractMatrix.prototype.subM; AbstractMatrix.subtract = AbstractMatrix.sub; AbstractMatrix.prototype.mul = function mul(value) { if (typeof value === 'number') return this.mulS(value); return this.mulM(value); }; AbstractMatrix.prototype.mulS = function mulS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) * value); } } return this; }; AbstractMatrix.prototype.mulM = function mulM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) * matrix.get(i, j)); } } return this; }; AbstractMatrix.mul = function mul(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.mul(value); }; AbstractMatrix.prototype.multiply = AbstractMatrix.prototype.mul; AbstractMatrix.prototype.multiplyS = AbstractMatrix.prototype.mulS; AbstractMatrix.prototype.multiplyM = AbstractMatrix.prototype.mulM; AbstractMatrix.multiply = AbstractMatrix.mul; AbstractMatrix.prototype.div = function div(value) { if (typeof value === 'number') return this.divS(value); return this.divM(value); }; AbstractMatrix.prototype.divS = function divS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) / value); } } return this; }; AbstractMatrix.prototype.divM = function divM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) / matrix.get(i, j)); } } return this; }; AbstractMatrix.div = function div(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.div(value); }; AbstractMatrix.prototype.divide = AbstractMatrix.prototype.div; AbstractMatrix.prototype.divideS = AbstractMatrix.prototype.divS; AbstractMatrix.prototype.divideM = AbstractMatrix.prototype.divM; AbstractMatrix.divide = AbstractMatrix.div; AbstractMatrix.prototype.mod = function mod(value) { if (typeof value === 'number') return this.modS(value); return this.modM(value); }; AbstractMatrix.prototype.modS = function modS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) % value); } } return this; }; AbstractMatrix.prototype.modM = function modM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) % matrix.get(i, j)); } } return this; }; AbstractMatrix.mod = function mod(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.mod(value); }; AbstractMatrix.prototype.modulus = AbstractMatrix.prototype.mod; AbstractMatrix.prototype.modulusS = AbstractMatrix.prototype.modS; AbstractMatrix.prototype.modulusM = AbstractMatrix.prototype.modM; AbstractMatrix.modulus = AbstractMatrix.mod; AbstractMatrix.prototype.and = function and(value) { if (typeof value === 'number') return this.andS(value); return this.andM(value); }; AbstractMatrix.prototype.andS = function andS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) & value); } } return this; }; AbstractMatrix.prototype.andM = function andM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) & matrix.get(i, j)); } } return this; }; AbstractMatrix.and = function and(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.and(value); }; AbstractMatrix.prototype.or = function or(value) { if (typeof value === 'number') return this.orS(value); return this.orM(value); }; AbstractMatrix.prototype.orS = function orS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) | value); } } return this; }; AbstractMatrix.prototype.orM = function orM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) | matrix.get(i, j)); } } return this; }; AbstractMatrix.or = function or(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.or(value); }; AbstractMatrix.prototype.xor = function xor(value) { if (typeof value === 'number') return this.xorS(value); return this.xorM(value); }; AbstractMatrix.prototype.xorS = function xorS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) ^ value); } } return this; }; AbstractMatrix.prototype.xorM = function xorM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) ^ matrix.get(i, j)); } } return this; }; AbstractMatrix.xor = function xor(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.xor(value); }; AbstractMatrix.prototype.leftShift = function leftShift(value) { if (typeof value === 'number') return this.leftShiftS(value); return this.leftShiftM(value); }; AbstractMatrix.prototype.leftShiftS = function leftShiftS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) << value); } } return this; }; AbstractMatrix.prototype.leftShiftM = function leftShiftM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) << matrix.get(i, j)); } } return this; }; AbstractMatrix.leftShift = function leftShift(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.leftShift(value); }; AbstractMatrix.prototype.signPropagatingRightShift = function signPropagatingRightShift(value) { if (typeof value === 'number') return this.signPropagatingRightShiftS(value); return this.signPropagatingRightShiftM(value); }; AbstractMatrix.prototype.signPropagatingRightShiftS = function signPropagatingRightShiftS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) >> value); } } return this; }; AbstractMatrix.prototype.signPropagatingRightShiftM = function signPropagatingRightShiftM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) >> matrix.get(i, j)); } } return this; }; AbstractMatrix.signPropagatingRightShift = function signPropagatingRightShift(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.signPropagatingRightShift(value); }; AbstractMatrix.prototype.rightShift = function rightShift(value) { if (typeof value === 'number') return this.rightShiftS(value); return this.rightShiftM(value); }; AbstractMatrix.prototype.rightShiftS = function rightShiftS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) >>> value); } } return this; }; AbstractMatrix.prototype.rightShiftM = function rightShiftM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) >>> matrix.get(i, j)); } } return this; }; AbstractMatrix.rightShift = function rightShift(matrix, value) { const newMatrix = new Matrix(matrix); return newMatrix.rightShift(value); }; AbstractMatrix.prototype.zeroFillRightShift = AbstractMatrix.prototype.rightShift; AbstractMatrix.prototype.zeroFillRightShiftS = AbstractMatrix.prototype.rightShiftS; AbstractMatrix.prototype.zeroFillRightShiftM = AbstractMatrix.prototype.rightShiftM; AbstractMatrix.zeroFillRightShift = AbstractMatrix.rightShift; AbstractMatrix.prototype.not = function not() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, ~(this.get(i, j))); } } return this; }; AbstractMatrix.not = function not(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.not(); }; AbstractMatrix.prototype.abs = function abs() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.abs(this.get(i, j))); } } return this; }; AbstractMatrix.abs = function abs(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.abs(); }; AbstractMatrix.prototype.acos = function acos() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.acos(this.get(i, j))); } } return this; }; AbstractMatrix.acos = function acos(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.acos(); }; AbstractMatrix.prototype.acosh = function acosh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.acosh(this.get(i, j))); } } return this; }; AbstractMatrix.acosh = function acosh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.acosh(); }; AbstractMatrix.prototype.asin = function asin() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.asin(this.get(i, j))); } } return this; }; AbstractMatrix.asin = function asin(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.asin(); }; AbstractMatrix.prototype.asinh = function asinh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.asinh(this.get(i, j))); } } return this; }; AbstractMatrix.asinh = function asinh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.asinh(); }; AbstractMatrix.prototype.atan = function atan() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.atan(this.get(i, j))); } } return this; }; AbstractMatrix.atan = function atan(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.atan(); }; AbstractMatrix.prototype.atanh = function atanh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.atanh(this.get(i, j))); } } return this; }; AbstractMatrix.atanh = function atanh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.atanh(); }; AbstractMatrix.prototype.cbrt = function cbrt() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.cbrt(this.get(i, j))); } } return this; }; AbstractMatrix.cbrt = function cbrt(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.cbrt(); }; AbstractMatrix.prototype.ceil = function ceil() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.ceil(this.get(i, j))); } } return this; }; AbstractMatrix.ceil = function ceil(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.ceil(); }; AbstractMatrix.prototype.clz32 = function clz32() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.clz32(this.get(i, j))); } } return this; }; AbstractMatrix.clz32 = function clz32(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.clz32(); }; AbstractMatrix.prototype.cos = function cos() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.cos(this.get(i, j))); } } return this; }; AbstractMatrix.cos = function cos(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.cos(); }; AbstractMatrix.prototype.cosh = function cosh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.cosh(this.get(i, j))); } } return this; }; AbstractMatrix.cosh = function cosh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.cosh(); }; AbstractMatrix.prototype.exp = function exp() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.exp(this.get(i, j))); } } return this; }; AbstractMatrix.exp = function exp(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.exp(); }; AbstractMatrix.prototype.expm1 = function expm1() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.expm1(this.get(i, j))); } } return this; }; AbstractMatrix.expm1 = function expm1(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.expm1(); }; AbstractMatrix.prototype.floor = function floor() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.floor(this.get(i, j))); } } return this; }; AbstractMatrix.floor = function floor(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.floor(); }; AbstractMatrix.prototype.fround = function fround() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.fround(this.get(i, j))); } } return this; }; AbstractMatrix.fround = function fround(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.fround(); }; AbstractMatrix.prototype.log = function log() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.log(this.get(i, j))); } } return this; }; AbstractMatrix.log = function log(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.log(); }; AbstractMatrix.prototype.log1p = function log1p() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.log1p(this.get(i, j))); } } return this; }; AbstractMatrix.log1p = function log1p(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.log1p(); }; AbstractMatrix.prototype.log10 = function log10() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.log10(this.get(i, j))); } } return this; }; AbstractMatrix.log10 = function log10(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.log10(); }; AbstractMatrix.prototype.log2 = function log2() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.log2(this.get(i, j))); } } return this; }; AbstractMatrix.log2 = function log2(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.log2(); }; AbstractMatrix.prototype.round = function round() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.round(this.get(i, j))); } } return this; }; AbstractMatrix.round = function round(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.round(); }; AbstractMatrix.prototype.sign = function sign() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.sign(this.get(i, j))); } } return this; }; AbstractMatrix.sign = function sign(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.sign(); }; AbstractMatrix.prototype.sin = function sin() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.sin(this.get(i, j))); } } return this; }; AbstractMatrix.sin = function sin(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.sin(); }; AbstractMatrix.prototype.sinh = function sinh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.sinh(this.get(i, j))); } } return this; }; AbstractMatrix.sinh = function sinh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.sinh(); }; AbstractMatrix.prototype.sqrt = function sqrt() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.sqrt(this.get(i, j))); } } return this; }; AbstractMatrix.sqrt = function sqrt(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.sqrt(); }; AbstractMatrix.prototype.tan = function tan() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.tan(this.get(i, j))); } } return this; }; AbstractMatrix.tan = function tan(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.tan(); }; AbstractMatrix.prototype.tanh = function tanh() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.tanh(this.get(i, j))); } } return this; }; AbstractMatrix.tanh = function tanh(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.tanh(); }; AbstractMatrix.prototype.trunc = function trunc() { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, Math.trunc(this.get(i, j))); } } return this; }; AbstractMatrix.trunc = function trunc(matrix) { const newMatrix = new Matrix(matrix); return newMatrix.trunc(); }; AbstractMatrix.pow = function pow(matrix, arg0) { const newMatrix = new Matrix(matrix); return newMatrix.pow(arg0); }; AbstractMatrix.prototype.pow = function pow(value) { if (typeof value === 'number') return this.powS(value); return this.powM(value); }; AbstractMatrix.prototype.powS = function powS(value) { for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) ** value); } } return this; }; AbstractMatrix.prototype.powM = function powM(matrix) { matrix = Matrix.checkMatrix(matrix); if (this.rows !== matrix.rows || this.columns !== matrix.columns) { throw new RangeError('Matrices dimensions must be equal'); } for (let i = 0; i < this.rows; i++) { for (let j = 0; j < this.columns; j++) { this.set(i, j, this.get(i, j) ** matrix.get(i, j)); } } return this; }; }