Comparing version 2.1.0 to 2.2.0
@@ -0,1 +1,20 @@ | ||
<a name="2.2.0"></a> | ||
# [2.2.0](https://github.com/mljs/matrix/compare/v2.1.0...v2.2.0) (2016-12-14) | ||
### Bug Fixes | ||
* Matrix and Lu circular dependency ([ab706b9](https://github.com/mljs/matrix/commit/ab706b9)) | ||
* styling issues picked up by Travis CI ([f211a1f](https://github.com/mljs/matrix/commit/f211a1f)) | ||
### Features | ||
* **det:** add 2x2 and 3x3 determinants ([04ae195](https://github.com/mljs/matrix/commit/04ae195)) | ||
* **det:** add determinant based on LU decomposition ([90532ef](https://github.com/mljs/matrix/commit/90532ef)) | ||
* **det:** add determinant synonym ([5395b56](https://github.com/mljs/matrix/commit/5395b56)) | ||
* **sum:** sum by 'row' or 'column' ([bf5d070](https://github.com/mljs/matrix/commit/bf5d070)) | ||
<a name="2.1.0"></a> | ||
@@ -2,0 +21,0 @@ # [2.1.0](https://github.com/mljs/matrix/compare/v2.0.0...v2.1.0) (2016-10-07) |
{ | ||
"name": "ml-matrix", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Matrix manipulation and computation library", | ||
@@ -14,3 +14,6 @@ "main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha --require should-approximately-deep --reporter mocha-better-spec-reporter --recursive" | ||
"eslint": "eslint src test", | ||
"eslint-fix": "npm run eslint -- --fix", | ||
"test": "npm run test-mocha && npm run eslint", | ||
"test-mocha": "mocha --require should-approximately-deep --reporter mocha-better-spec-reporter --recursive" | ||
}, | ||
@@ -50,2 +53,5 @@ "repository": { | ||
"csv-parse": "^1.0.0", | ||
"eslint": "^3.8.0", | ||
"eslint-config-cheminfo": "^1.5.2", | ||
"eslint-plugin-no-only-tests": "^1.1.0", | ||
"mathjs": "^3.2.1", | ||
@@ -56,3 +62,3 @@ "mocha": "^3.0.0", | ||
"pretty-hrtime": "^1.0.0", | ||
"should": "^10.0.0", | ||
"should": "^11.1.1", | ||
"should-approximately-deep": "^1.1.0" | ||
@@ -59,0 +65,0 @@ }, |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var LuDecomposition = require('./dc/lu'); | ||
var arrayUtils = require('ml-array-utils'); | ||
@@ -36,3 +37,3 @@ var util = require('./util'); | ||
* @param {Array} newData - A 1D array containing data for the matrix | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -56,3 +57,3 @@ static from1DArray(newRows, newColumns, newData) { | ||
* @param {Array} newData - A 1D array containing data for the vector | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -70,3 +71,3 @@ static rowVector(newData) { | ||
* @param {Array} newData - A 1D array containing data for the vector | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -85,3 +86,3 @@ static columnVector(newData) { | ||
* @param {number} columns - Number of columns | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -96,3 +97,3 @@ static empty(rows, columns) { | ||
* @param {number} columns - Number of columns | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -107,3 +108,3 @@ static zeros(rows, columns) { | ||
* @param {number} columns - Number of columns | ||
* @returns {Matrix} - The new matrix | ||
* @return {Matrix} - The new matrix | ||
*/ | ||
@@ -119,3 +120,3 @@ static ones(rows, columns) { | ||
* @param {function} [rng=Math.random] - Random number generator | ||
* @returns {Matrix} The new matrix | ||
* @return {Matrix} The new matrix | ||
*/ | ||
@@ -139,3 +140,3 @@ static rand(rows, columns, rng) { | ||
* @param {function} [rng=Math.random] - Random number generator | ||
* @returns {Matrix} The new matrix | ||
* @return {Matrix} The new matrix | ||
*/ | ||
@@ -158,8 +159,9 @@ static randInt(rows, columns, maxValue, rng) { | ||
* @param {number} rows - Number of rows | ||
* @param {number} [columns] - Number of columns (Default: rows) | ||
* @returns {Matrix} - The new identity matrix | ||
* @param {number} [columns=rows] - Number of columns | ||
* @param {number} [value=1] - Value to fill the diagonal with | ||
* @return {Matrix} - The new identity matrix | ||
*/ | ||
static eye(rows, columns, value) { | ||
if (value === undefined) value = 1 | ||
if (columns === undefined) columns = rows; | ||
if (value === undefined) value = 1; | ||
var min = Math.min(rows, columns); | ||
@@ -178,3 +180,3 @@ var matrix = this.zeros(rows, columns); | ||
* @param {number} [columns] - Number of columns (Default: rows) | ||
* @returns {Matrix} - The new diagonal matrix | ||
* @return {Matrix} - The new diagonal matrix | ||
*/ | ||
@@ -195,5 +197,5 @@ static diag(data, rows, columns) { | ||
* Returns a matrix whose elements are the minimum between matrix1 and matrix2 | ||
* @param matrix1 | ||
* @param matrix2 | ||
* @returns {Matrix} | ||
* @param {Matrix} matrix1 | ||
* @param {Matrix} matrix2 | ||
* @return {Matrix} | ||
*/ | ||
@@ -216,5 +218,5 @@ static min(matrix1, matrix2) { | ||
* Returns a matrix whose elements are the maximum between matrix1 and matrix2 | ||
* @param matrix1 | ||
* @param matrix2 | ||
* @returns {Matrix} | ||
* @param {Matrix} matrix1 | ||
* @param {Matrix} matrix2 | ||
* @return {Matrix} | ||
*/ | ||
@@ -237,4 +239,4 @@ static max(matrix1, matrix2) { | ||
* Check that the provided value is a Matrix and tries to instantiate one if not | ||
* @param value - The value to check | ||
* @returns {Matrix} | ||
* @param {*} value - The value to check | ||
* @return {Matrix} | ||
*/ | ||
@@ -247,3 +249,3 @@ static checkMatrix(value) { | ||
* Returns true if the argument is a Matrix, false otherwise | ||
* @param value - The value to check | ||
* @param {*} value - The value to check | ||
* @return {boolean} | ||
@@ -256,3 +258,3 @@ */ | ||
/** | ||
* @property {number} - The number of elements in the matrix. | ||
* @prop {number} size - The number of elements in the matrix. | ||
*/ | ||
@@ -266,3 +268,3 @@ get size() { | ||
* @param {function} callback - Function that will be called with two parameters : i (row) and j (column) | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -285,3 +287,3 @@ apply(callback) { | ||
* Returns a new 1D array filled row by row with the matrix values | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -300,3 +302,3 @@ to1DArray() { | ||
* Returns a 2D array containing a copy of the data | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -315,3 +317,3 @@ to2DArray() { | ||
/** | ||
* @returns {boolean} true if the matrix has one row | ||
* @return {boolean} true if the matrix has one row | ||
*/ | ||
@@ -323,3 +325,3 @@ isRowVector() { | ||
/** | ||
* @returns {boolean} true if the matrix has one column | ||
* @return {boolean} true if the matrix has one column | ||
*/ | ||
@@ -331,3 +333,3 @@ isColumnVector() { | ||
/** | ||
* @returns {boolean} true if the matrix has one row or one column | ||
* @return {boolean} true if the matrix has one row or one column | ||
*/ | ||
@@ -339,3 +341,3 @@ isVector() { | ||
/** | ||
* @returns {boolean} true if the matrix has the same number of rows and columns | ||
* @return {boolean} true if the matrix has the same number of rows and columns | ||
*/ | ||
@@ -347,3 +349,3 @@ isSquare() { | ||
/** | ||
* @returns {boolean} true if the matrix is square and has the same values on both sides of the diagonal | ||
* @return {boolean} true if the matrix is square and has the same values on both sides of the diagonal | ||
*/ | ||
@@ -366,8 +368,9 @@ isSymmetric() { | ||
* Sets a given element of the matrix. mat.set(3,4,1) is equivalent to mat[3][4]=1 | ||
* @abstract | ||
* @param {number} rowIndex - Index of the row | ||
* @param {number} columnIndex - Index of the column | ||
* @param {number} value - The new value for the element | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
set(rowIndex, columnIndex, value) { | ||
set(rowIndex, columnIndex, value) { // eslint-disable-line no-unused-vars | ||
throw new Error('set method is unimplemented'); | ||
@@ -378,7 +381,8 @@ } | ||
* Returns the given element of the matrix. mat.get(3,4) is equivalent to matrix[3][4] | ||
* @abstract | ||
* @param {number} rowIndex - Index of the row | ||
* @param {number} columnIndex - Index of the column | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
get(rowIndex, columnIndex) { | ||
get(rowIndex, columnIndex) { // eslint-disable-line no-unused-vars | ||
throw new Error('get method is unimplemented'); | ||
@@ -392,2 +396,3 @@ } | ||
* @param {number} colRep - Number of times the columns should be re | ||
* @return {Matrix} | ||
* @example | ||
@@ -412,3 +417,3 @@ * var matrix = new Matrix([[1,2]]); | ||
* @param {number} value - New value | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -426,3 +431,3 @@ fill(value) { | ||
* Negates the matrix. All elements will be multiplied by (-1) | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -436,3 +441,3 @@ neg() { | ||
* @param {number} index - Row index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -451,3 +456,3 @@ getRow(index) { | ||
* @param {number} index - Row index | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -462,3 +467,3 @@ getRowVector(index) { | ||
* @param {Array|Matrix} array - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -478,3 +483,3 @@ setRow(index, array) { | ||
* @param {number} row2 - Second row index | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -495,3 +500,3 @@ swapRows(row1, row2) { | ||
* @param {number} index - Column index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -510,3 +515,3 @@ getColumn(index) { | ||
* @param {number} index - Column index | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -521,3 +526,3 @@ getColumnVector(index) { | ||
* @param {Array|Matrix} array - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -537,3 +542,3 @@ setColumn(index, array) { | ||
* @param {number} column2 - Second column index | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -554,3 +559,3 @@ swapColumns(column1, column2) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -570,3 +575,3 @@ addRowVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -586,3 +591,3 @@ subRowVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -602,3 +607,3 @@ mulRowVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -618,3 +623,3 @@ divRowVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -634,3 +639,3 @@ addColumnVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -650,3 +655,3 @@ subColumnVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -666,3 +671,3 @@ mulColumnVector(vector) { | ||
* @param {Array|Matrix} vector - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -683,3 +688,3 @@ divColumnVector(vector) { | ||
* @param {number} value | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -698,3 +703,3 @@ mulRow(index, value) { | ||
* @param {number} value | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -706,2 +711,3 @@ mulColumn(index, value) { | ||
} | ||
return this; | ||
} | ||
@@ -711,3 +717,3 @@ | ||
* Returns the maximum value of the matrix | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -728,3 +734,3 @@ max() { | ||
* Returns the index of the maximum value | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -748,3 +754,3 @@ maxIndex() { | ||
* Returns the minimum value of the matrix | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -765,3 +771,3 @@ min() { | ||
* Returns the index of the minimum value | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -786,3 +792,3 @@ minIndex() { | ||
* @param {number} row - Row index | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -803,3 +809,3 @@ maxRow(row) { | ||
* @param {number} row - Row index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -822,3 +828,3 @@ maxRowIndex(row) { | ||
* @param {number} row - Row index | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -839,3 +845,3 @@ minRow(row) { | ||
* @param {number} row - Row index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -858,3 +864,3 @@ minRowIndex(row) { | ||
* @param {number} column - Column index | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -875,3 +881,3 @@ maxColumn(column) { | ||
* @param {number} column - Column index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -894,3 +900,3 @@ maxColumnIndex(column) { | ||
* @param {number} column - Column index | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -911,3 +917,3 @@ minColumn(column) { | ||
* @param {number} column - Column index | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -929,3 +935,3 @@ minColumnIndex(column) { | ||
* Returns an array containing the diagonal values of the matrix | ||
* @returns {Array} | ||
* @return {Array} | ||
*/ | ||
@@ -942,13 +948,16 @@ diag() { | ||
/** | ||
* Returns the sum of all elements of the matrix | ||
* @returns {number} | ||
* Returns the sum by the argument given, if no argument given, | ||
* it returns the sum of all elements of the matrix. | ||
* @param {string} by - sum by 'row' or 'column'. | ||
* @return {Matrix|number} | ||
*/ | ||
sum() { | ||
var v = 0; | ||
for (var i = 0; i < this.rows; i++) { | ||
for (var j = 0; j < this.columns; j++) { | ||
v += this.get(i, j); | ||
} | ||
sum(by) { | ||
switch (by) { | ||
case 'row': | ||
return util.sumByRow(this); | ||
case 'column': | ||
return util.sumByColumn(this); | ||
default: | ||
return util.sumAll(this); | ||
} | ||
return v; | ||
} | ||
@@ -958,3 +967,3 @@ | ||
* Returns the mean of all elements of the matrix | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -967,3 +976,3 @@ mean() { | ||
* Returns the product of all elements of the matrix | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -982,3 +991,3 @@ prod() { | ||
* Computes the cumulative sum of the matrix elements (in place, row by row) | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -999,3 +1008,3 @@ cumulativeSum() { | ||
* @param {Matrix} vector2 vector | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -1018,8 +1027,10 @@ dot(vector2) { | ||
* @param {Matrix} other | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
mmul(other) { | ||
other = this.constructor.checkMatrix(other); | ||
if (this.columns !== other.rows) | ||
if (this.columns !== other.rows) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Number of columns of left matrix are not equal to number of rows of right matrix.'); | ||
} | ||
@@ -1050,112 +1061,110 @@ var m = this.rows; | ||
strassen_2x2(other){ | ||
strassen2x2(other) { | ||
var result = new this.constructor[Symbol.species](2, 2); | ||
const a11 = this.get(0,0); | ||
const b11 = other.get(0,0); | ||
const a12 = this.get(0,1); | ||
const b12 = other.get(0,1); | ||
const a21 = this.get(1,0); | ||
const b21 = other.get(1,0); | ||
const a22 = this.get(1,1); | ||
const b22 = other.get(1,1); | ||
const a11 = this.get(0, 0); | ||
const b11 = other.get(0, 0); | ||
const a12 = this.get(0, 1); | ||
const b12 = other.get(0, 1); | ||
const a21 = this.get(1, 0); | ||
const b21 = other.get(1, 0); | ||
const a22 = this.get(1, 1); | ||
const b22 = other.get(1, 1); | ||
// Compute intermediate values. | ||
const m1 = (a11+a22)*(b11+b22); | ||
const m2 = (a21+a22)*b11; | ||
const m3 = a11*(b12-b22); | ||
const m4 = a22*(b21-b11); | ||
const m5 = (a11+a12)*b22; | ||
const m6 = (a21-a11)*(b11+b12); | ||
const m7 = (a12-a22)*(b21+b22); | ||
const m1 = (a11 + a22) * (b11 + b22); | ||
const m2 = (a21 + a22) * b11; | ||
const m3 = a11 * (b12 - b22); | ||
const m4 = a22 * (b21 - b11); | ||
const m5 = (a11 + a12) * b22; | ||
const m6 = (a21 - a11) * (b11 + b12); | ||
const m7 = (a12 - a22) * (b21 + b22); | ||
// Combine intermediate values into the output. | ||
const c00 =m1+m4-m5+m7; | ||
const c01 = m3+m5; | ||
const c10 = m2+m4; | ||
const c11 = m1-m2+m3+m6; | ||
const c00 = m1 + m4 - m5 + m7; | ||
const c01 = m3 + m5; | ||
const c10 = m2 + m4; | ||
const c11 = m1 - m2 + m3 + m6; | ||
result.set(0,0,c00); | ||
result.set(0,1,c01); | ||
result.set(1,0,c10); | ||
result.set(1,1,c11); | ||
result.set(0, 0, c00); | ||
result.set(0, 1, c01); | ||
result.set(1, 0, c10); | ||
result.set(1, 1, c11); | ||
return result; | ||
} | ||
strassen_3x3(other){ | ||
strassen3x3(other) { | ||
var result = new this.constructor[Symbol.species](3, 3); | ||
const a00 = this.get(0,0); | ||
const a01 = this.get(0,1); | ||
const a02 = this.get(0,2); | ||
const a10 = this.get(1,0); | ||
const a11 = this.get(1,1); | ||
const a12 = this.get(1,2); | ||
const a20 = this.get(2,0); | ||
const a21 = this.get(2,1); | ||
const a22 = this.get(2,2); | ||
const a00 = this.get(0, 0); | ||
const a01 = this.get(0, 1); | ||
const a02 = this.get(0, 2); | ||
const a10 = this.get(1, 0); | ||
const a11 = this.get(1, 1); | ||
const a12 = this.get(1, 2); | ||
const a20 = this.get(2, 0); | ||
const a21 = this.get(2, 1); | ||
const a22 = this.get(2, 2); | ||
const b00 = other.get(0,0); | ||
const b01 = other.get(0,1); | ||
const b02 = other.get(0,2); | ||
const b10 = other.get(1,0); | ||
const b11 = other.get(1,1); | ||
const b12 = other.get(1,2); | ||
const b20 = other.get(2,0); | ||
const b21 = other.get(2,1); | ||
const b22 = other.get(2,2); | ||
const b00 = other.get(0, 0); | ||
const b01 = other.get(0, 1); | ||
const b02 = other.get(0, 2); | ||
const b10 = other.get(1, 0); | ||
const b11 = other.get(1, 1); | ||
const b12 = other.get(1, 2); | ||
const b20 = other.get(2, 0); | ||
const b21 = other.get(2, 1); | ||
const b22 = other.get(2, 2); | ||
const m1 = (a00+a01+a02-a10-a11-a21-a22)*b11; | ||
const m2 = (a00-a10)*(-b01+b11); | ||
const m3 = a11*(-b00+b01+b10-b11-b12-b20+b22); | ||
const m4 = (-a00+a10+a11)*(b00-b01+b11); | ||
const m5 = (a10+a11)*(-b00+b01); | ||
const m6 = a00*b00; | ||
const m7 = (-a00+a20+a21)*(b00-b02+b12); | ||
const m8 = (-a00+a20)*(b02-b12); | ||
const m9 = (a20+a21)*(-b00+b02); | ||
const m10 = (a00+a01+a02-a11-a12-a20-a21)*b12; | ||
const m11 = a21*(-b00+b02+b10-b11-b12-b20+b21); | ||
const m12 = (-a02+a21+a22)*(b11+b20-b21); | ||
const m13 = (a02-a22)*(b11-b21); | ||
const m14 = a02*b20; | ||
const m15 = (a21+a22)*(-b20+b21); | ||
const m16 = (-a02+a11+a12)*(b12+b20-b22); | ||
const m17 = (a02-a12)*(b12-b22); | ||
const m18 = (a11+a12)*(-b20+b22); | ||
const m19= a01*b10; | ||
const m20 = a12*b21; | ||
const m21 = a10*b02; | ||
const m22 = a20*b01; | ||
const m23 = a22*b22; | ||
const m1 = (a00 + a01 + a02 - a10 - a11 - a21 - a22) * b11; | ||
const m2 = (a00 - a10) * (-b01 + b11); | ||
const m3 = a11 * (-b00 + b01 + b10 - b11 - b12 - b20 + b22); | ||
const m4 = (-a00 + a10 + a11) * (b00 - b01 + b11); | ||
const m5 = (a10 + a11) * (-b00 + b01); | ||
const m6 = a00 * b00; | ||
const m7 = (-a00 + a20 + a21) * (b00 - b02 + b12); | ||
const m8 = (-a00 + a20) * (b02 - b12); | ||
const m9 = (a20 + a21) * (-b00 + b02); | ||
const m10 = (a00 + a01 + a02 - a11 - a12 - a20 - a21) * b12; | ||
const m11 = a21 * (-b00 + b02 + b10 - b11 - b12 - b20 + b21); | ||
const m12 = (-a02 + a21 + a22) * (b11 + b20 - b21); | ||
const m13 = (a02 - a22) * (b11 - b21); | ||
const m14 = a02 * b20; | ||
const m15 = (a21 + a22) * (-b20 + b21); | ||
const m16 = (-a02 + a11 + a12) * (b12 + b20 - b22); | ||
const m17 = (a02 - a12) * (b12 - b22); | ||
const m18 = (a11 + a12) * (-b20 + b22); | ||
const m19 = a01 * b10; | ||
const m20 = a12 * b21; | ||
const m21 = a10 * b02; | ||
const m22 = a20 * b01; | ||
const m23 = a22 * b22; | ||
const c00 = m6+m14+m19; | ||
const c01 = m1+m4+m5+m6+m12+m14+m15; | ||
const c02 = m6+m7+m9+m10+m14+m16+m18; | ||
const c10 = m2+m3+m4+m6+m14+m16+m17; | ||
const c11 = m2+m4+m5+m6+m20; | ||
const c12 = m14+m16+m17+m18+m21; | ||
const c20 = m6+m7+m8+m11+m12+m13+m14; | ||
const c21 = m12+m13+m14+m15+m22; | ||
const c22 = m6+m7+m8+m9+m23; | ||
const c00 = m6 + m14 + m19; | ||
const c01 = m1 + m4 + m5 + m6 + m12 + m14 + m15; | ||
const c02 = m6 + m7 + m9 + m10 + m14 + m16 + m18; | ||
const c10 = m2 + m3 + m4 + m6 + m14 + m16 + m17; | ||
const c11 = m2 + m4 + m5 + m6 + m20; | ||
const c12 = m14 + m16 + m17 + m18 + m21; | ||
const c20 = m6 + m7 + m8 + m11 + m12 + m13 + m14; | ||
const c21 = m12 + m13 + m14 + m15 + m22; | ||
const c22 = m6 + m7 + m8 + m9 + m23; | ||
result.set(0,0,c00); | ||
result.set(0,1,c01); | ||
result.set(0,2,c02); | ||
result.set(1,0,c10); | ||
result.set(1,1,c11); | ||
result.set(1,2,c12); | ||
result.set(2,0,c20); | ||
result.set(2,1,c21); | ||
result.set(2,2,c22); | ||
result.set(0, 0, c00); | ||
result.set(0, 1, c01); | ||
result.set(0, 2, c02); | ||
result.set(1, 0, c10); | ||
result.set(1, 1, c11); | ||
result.set(1, 2, c12); | ||
result.set(2, 0, c20); | ||
result.set(2, 1, c21); | ||
result.set(2, 2, c22); | ||
return result; | ||
} | ||
/** | ||
* Returns the matrix product between x and y. More efficient than mmul(other) only when we multiply squared matrix and when the size of the matrix is > 1000. | ||
* @param {Matrix} x | ||
* @param {Matrix} y | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
mmul_strassen(y){ | ||
mmulStrassen(y) { | ||
var x = this.clone(); | ||
@@ -1166,4 +1175,5 @@ var r1 = x.rows; | ||
var c2 = y.columns; | ||
if(c1 != r2){ | ||
console.log(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`) | ||
if (c1 !== r2) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Multiplying ${r1} x ${c1} and ${r2} x ${c2} matrix: dimensions do not match.`); | ||
} | ||
@@ -1173,9 +1183,8 @@ | ||
// `rows` and `cols` are the dimensions of the output matrix. | ||
function embed(mat, rows, cols){ | ||
function embed(mat, rows, cols) { | ||
var r = mat.rows; | ||
var c = mat.columns; | ||
if((r==rows) && (c==cols)){ | ||
if ((r === rows) && (c === cols)) { | ||
return mat; | ||
} | ||
else{ | ||
} else { | ||
var resultat = Matrix.zeros(rows, cols); | ||
@@ -1194,9 +1203,9 @@ resultat = resultat.setSubMatrix(mat, 0, 0); | ||
var c = Math.max(c1, c2); | ||
var x = embed(x, r, c); | ||
var y = embed(y, r, c); | ||
x = embed(x, r, c); | ||
y = embed(y, r, c); | ||
// Our recursive multiplication function. | ||
function block_mult(a, b, rows, cols){ | ||
function blockMult(a, b, rows, cols) { | ||
// For small matrices, resort to naive multiplication. | ||
if (rows <= 512 || cols <= 512){ | ||
if (rows <= 512 || cols <= 512) { | ||
return a.mmul(b); // a is equivalent to this | ||
@@ -1206,11 +1215,9 @@ } | ||
// Apply dynamic padding. | ||
if ((rows % 2 == 1) && (cols % 2 == 1)) { | ||
if ((rows % 2 === 1) && (cols % 2 === 1)) { | ||
a = embed(a, rows + 1, cols + 1); | ||
b = embed(b, rows + 1, cols + 1); | ||
} | ||
else if (rows % 2 == 1){ | ||
} else if (rows % 2 === 1) { | ||
a = embed(a, rows + 1, cols); | ||
b = embed(b, rows + 1, cols); | ||
} | ||
else if (cols % 2 == 1){ | ||
} else if (cols % 2 === 1) { | ||
a = embed(a, rows, cols + 1); | ||
@@ -1220,25 +1227,25 @@ b = embed(b, rows, cols + 1); | ||
var half_rows = parseInt(a.rows / 2); | ||
var half_cols = parseInt(a.columns / 2); | ||
var halfRows = parseInt(a.rows / 2); | ||
var halfCols = parseInt(a.columns / 2); | ||
// Subdivide input matrices. | ||
var a11 = a.subMatrix(0, half_rows -1, 0, half_cols - 1); | ||
var b11 = b.subMatrix(0, half_rows -1, 0, half_cols - 1); | ||
var a11 = a.subMatrix(0, halfRows - 1, 0, halfCols - 1); | ||
var b11 = b.subMatrix(0, halfRows - 1, 0, halfCols - 1); | ||
var a12 = a.subMatrix(0, half_rows -1, half_cols, a.columns - 1); | ||
var b12 = b.subMatrix(0, half_rows -1, half_cols, b.columns - 1); | ||
var a12 = a.subMatrix(0, halfRows - 1, halfCols, a.columns - 1); | ||
var b12 = b.subMatrix(0, halfRows - 1, halfCols, b.columns - 1); | ||
var a21 = a.subMatrix(half_rows, a.rows - 1, 0, half_cols - 1); | ||
var b21 = b.subMatrix(half_rows, b.rows - 1, 0, half_cols - 1); | ||
var a21 = a.subMatrix(halfRows, a.rows - 1, 0, halfCols - 1); | ||
var b21 = b.subMatrix(halfRows, b.rows - 1, 0, halfCols - 1); | ||
var a22 = a.subMatrix(half_rows, a.rows - 1, half_cols, a.columns - 1); | ||
var b22 = b.subMatrix(half_rows, b.rows - 1, half_cols, b.columns - 1); | ||
var a22 = a.subMatrix(halfRows, a.rows - 1, halfCols, a.columns - 1); | ||
var b22 = b.subMatrix(halfRows, b.rows - 1, halfCols, b.columns - 1); | ||
// Compute intermediate values. | ||
var m1 = block_mult(Matrix.add(a11,a22), Matrix.add(b11,b22), half_rows, half_cols); | ||
var m2 = block_mult(Matrix.add(a21,a22), b11, half_rows, half_cols); | ||
var m3 = block_mult(a11, Matrix.sub(b12, b22), half_rows, half_cols); | ||
var m4 = block_mult(a22, Matrix.sub(b21,b11), half_rows, half_cols); | ||
var m5 = block_mult(Matrix.add(a11,a12), b22, half_rows, half_cols); | ||
var m6 = block_mult(Matrix.sub(a21, a11), Matrix.add(b11, b12), half_rows, half_cols); | ||
var m7 = block_mult(Matrix.sub(a12,a22), Matrix.add(b21,b22), half_rows, half_cols); | ||
var m1 = blockMult(Matrix.add(a11, a22), Matrix.add(b11, b22), halfRows, halfCols); | ||
var m2 = blockMult(Matrix.add(a21, a22), b11, halfRows, halfCols); | ||
var m3 = blockMult(a11, Matrix.sub(b12, b22), halfRows, halfCols); | ||
var m4 = blockMult(a22, Matrix.sub(b21, b11), halfRows, halfCols); | ||
var m5 = blockMult(Matrix.add(a11, a12), b22, halfRows, halfCols); | ||
var m6 = blockMult(Matrix.sub(a21, a11), Matrix.add(b11, b12), halfRows, halfCols); | ||
var m7 = blockMult(Matrix.sub(a12, a22), Matrix.add(b21, b22), halfRows, halfCols); | ||
@@ -1249,5 +1256,5 @@ // Combine intermediate values into the output. | ||
c11.add(m7); | ||
var c12 = Matrix.add(m3,m5); | ||
var c21 = Matrix.add(m2,m4); | ||
var c22 = Matrix.sub(m1,m2); | ||
var c12 = Matrix.add(m3, m5); | ||
var c21 = Matrix.add(m2, m4); | ||
var c22 = Matrix.sub(m1, m2); | ||
c22.add(m3); | ||
@@ -1257,5 +1264,5 @@ c22.add(m6); | ||
//Crop output to the desired size (undo dynamic padding). | ||
var resultat = Matrix.zeros(2*c11.rows, 2*c11.columns); | ||
var resultat = Matrix.zeros(2 * c11.rows, 2 * c11.columns); | ||
resultat = resultat.setSubMatrix(c11, 0, 0); | ||
resultat = resultat.setSubMatrix(c12, c11.rows, 0) | ||
resultat = resultat.setSubMatrix(c12, c11.rows, 0); | ||
resultat = resultat.setSubMatrix(c21, 0, c11.columns); | ||
@@ -1265,11 +1272,10 @@ resultat = resultat.setSubMatrix(c22, c11.rows, c11.columns); | ||
} | ||
var resultat_final = block_mult(x, y, r, c); | ||
return resultat_final; | ||
}; | ||
return blockMult(x, y, r, c); | ||
} | ||
/** | ||
* Returns a row-by-row scaled matrix | ||
* @param {Number} [min=0] - Minimum scaled value | ||
* @param {Number} [max=1] - Maximum scaled value | ||
* @returns {Matrix} - The scaled matrix | ||
* @param {number} [min=0] - Minimum scaled value | ||
* @param {number} [max=1] - Maximum scaled value | ||
* @return {Matrix} - The scaled matrix | ||
*/ | ||
@@ -1292,5 +1298,5 @@ scaleRows(min, max) { | ||
* Returns a new column-by-column scaled matrix | ||
* @param {Number} [min=0] - Minimum scaled value | ||
* @param {Number} [max=1] - Maximum scaled value | ||
* @returns {Matrix} - The new scaled matrix | ||
* @param {number} [min=0] - Minimum scaled value | ||
* @param {number} [max=1] - Maximum scaled value | ||
* @return {Matrix} - The new scaled matrix | ||
* @example | ||
@@ -1347,3 +1353,3 @@ * var matrix = new Matrix([[1,2],[-1,0]]); | ||
* Transposes the matrix and returns a new one containing the result | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -1363,3 +1369,3 @@ transpose() { | ||
* @param {function} compareFunction - usual Array.prototype.sort comparison function | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -1377,3 +1383,3 @@ sortRows(compareFunction) { | ||
* @param {function} compareFunction - usual Array.prototype.sort comparison function | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -1394,3 +1400,3 @@ sortColumns(compareFunction) { | ||
* @param {number} endColumn - Last column index | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -1413,3 +1419,3 @@ subMatrix(startRow, endRow, startColumn, endColumn) { | ||
* @param {number} [endColumn = this.columns-1] - Last column index | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -1440,3 +1446,3 @@ subMatrixRow(indices, startColumn, endColumn) { | ||
* @param {number} [endRow = this.rows-1] - Last row index | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -1465,5 +1471,5 @@ subMatrixColumn(indices, startRow, endRow) { | ||
* @param {Matrix|Array< Array >} matrix - The source matrix from which to extract values. | ||
* @param startRow - The index of the first row to set | ||
* @param startColumn - The index of the first column to set | ||
* @returns {Matrix} | ||
* @param {number} startRow - The index of the first row to set | ||
* @param {number} startColumn - The index of the first column to set | ||
* @return {Matrix} | ||
*/ | ||
@@ -1487,3 +1493,3 @@ setSubMatrix(matrix, startRow, startColumn) { | ||
* @param {Array<number>} columnIndices - The column indices to select. Order matters and an index can be use more than once. | ||
* @returns {Matrix} The new matrix | ||
* @return {Matrix} The new matrix | ||
*/ | ||
@@ -1505,3 +1511,3 @@ selection(rowIndices, columnIndices) { | ||
* Returns the trace of the matrix (sum of the diagonal elements) | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -1523,3 +1529,3 @@ trace() { | ||
* Returns a view of the transposition of the matrix | ||
* @returns {MatrixTransposeView} | ||
* @return {MatrixTransposeView} | ||
*/ | ||
@@ -1533,3 +1539,3 @@ transposeView() { | ||
* @param {number} row - row index of the vector | ||
* @returns {MatrixRowView} | ||
* @return {MatrixRowView} | ||
*/ | ||
@@ -1544,3 +1550,3 @@ rowView(row) { | ||
* @param {number} column - column index of the vector | ||
* @returns {MatrixColumnView} | ||
* @return {MatrixColumnView} | ||
*/ | ||
@@ -1554,3 +1560,3 @@ columnView(column) { | ||
* Returns a view of the matrix flipped in the row axis | ||
* @returns {MatrixFlipRowView} | ||
* @return {MatrixFlipRowView} | ||
*/ | ||
@@ -1563,3 +1569,3 @@ flipRowView() { | ||
* Returns a view of the matrix flipped in the column axis | ||
* @returns {MatrixFlipColumnView} | ||
* @return {MatrixFlipColumnView} | ||
*/ | ||
@@ -1576,3 +1582,3 @@ flipColumnView() { | ||
* @param {number} endColumn - last column index of the submatrix | ||
* @returns {MatrixSubView} | ||
* @return {MatrixSubView} | ||
*/ | ||
@@ -1590,3 +1596,3 @@ subMatrixView(startRow, endRow, startColumn, endColumn) { | ||
* @param {Array<number>} columnIndices | ||
* @returns {MatrixSelectionView} | ||
* @return {MatrixSelectionView} | ||
*/ | ||
@@ -1596,2 +1602,41 @@ selectionView(rowIndices, columnIndices) { | ||
} | ||
/** | ||
* Calculates and returns the determinant of a matrix as a Number | ||
* @example | ||
* new Matrix([[1,2,3], [4,5,6]]).det() | ||
* @return {number} | ||
*/ | ||
det() { | ||
if (this.isSquare()) { | ||
var a, b, c, d; | ||
if (this.columns === 2) { | ||
// 2 x 2 matrix | ||
a = this.get(0, 0); | ||
b = this.get(0, 1); | ||
c = this.get(1, 0); | ||
d = this.get(1, 1); | ||
return a * d - (b * c); | ||
} else if (this.columns === 3) { | ||
// 3 x 3 matrix | ||
var subMatrix0, subMatrix1, subMatrix2; | ||
subMatrix0 = this.selectionView([1, 2], [1, 2]); | ||
subMatrix1 = this.selectionView([1, 2], [0, 2]); | ||
subMatrix2 = this.selectionView([1, 2], [0, 1]); | ||
a = this.get(0, 0); | ||
b = this.get(0, 1); | ||
c = this.get(0, 2); | ||
return a * subMatrix0.det() - b * subMatrix1.det() + c * subMatrix2.det(); | ||
} else { | ||
// general purpose determinant using the LU decomposition | ||
return new LuDecomposition(this).determinant; | ||
} | ||
} else { | ||
throw Error('Determinant can only be calculated for a square matrix.'); | ||
} | ||
} | ||
} | ||
@@ -1607,3 +1652,3 @@ | ||
*/ | ||
function checkDimensions(matrix, otherMatrix) { | ||
function checkDimensions(matrix, otherMatrix) { // eslint-disable-line no-unused-vars | ||
if (matrix.rows !== otherMatrix.rows || | ||
@@ -1629,2 +1674,3 @@ matrix.columns !== otherMatrix.columns) { | ||
Matrix.prototype.tensorProduct = Matrix.prototype.kroneckerProduct; | ||
Matrix.prototype.determinant = Matrix.prototype.det; | ||
@@ -1758,2 +1804,4 @@ /* | ||
var i; | ||
for (var operator of operators) { | ||
@@ -1764,3 +1812,3 @@ var inplaceOp = eval(fillTemplateFunction(inplaceOperator, {name: operator[1], op: operator[0]})); | ||
var staticOp = eval(fillTemplateFunction(staticOperator, {name: operator[1]})); | ||
for (var i = 1; i < operator.length; i++) { | ||
for (i = 1; i < operator.length; i++) { | ||
Matrix.prototype[operator[i]] = inplaceOp; | ||
@@ -1788,3 +1836,3 @@ Matrix.prototype[operator[i] + 'S'] = inplaceOpS; | ||
var staticMeth = eval(fillTemplateFunction(staticMethod, {name: method[1]})); | ||
for (var i = 1; i < method.length; i++) { | ||
for (i = 1; i < method.length; i++) { | ||
Matrix.prototype[method[i]] = inplaceMeth; | ||
@@ -1801,3 +1849,3 @@ Matrix[method[i]] = staticMeth; | ||
var args = 'arg0'; | ||
for (var i = 1; i < methodWithArg[1]; i++) { | ||
for (i = 1; i < methodWithArg[1]; i++) { | ||
args += `, arg${i}`; | ||
@@ -1812,3 +1860,3 @@ } | ||
var staticMethWithArgs = eval(fillTemplateFunction(staticMethodWithArgs, {name: methodWithArg[2], args: args})); | ||
for (var i = 2; i < methodWithArg.length; i++) { | ||
for (i = 2; i < methodWithArg.length; i++) { | ||
Matrix.prototype[methodWithArg[i]] = inplaceMethWithArgs; | ||
@@ -1823,11 +1871,11 @@ Matrix[methodWithArg[i]] = staticMethWithArgs; | ||
}; | ||
var inplaceMethod = eval(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); | ||
var inplaceMethod2 = eval(fillTemplateFunction(inplaceMethodWithOneArg, tmplVar)); | ||
var inplaceMethodS = eval(fillTemplateFunction(inplaceMethodWithOneArgScalar, tmplVar)); | ||
var inplaceMethodM = eval(fillTemplateFunction(inplaceMethodWithOneArgMatrix, tmplVar)); | ||
var staticMethod = eval(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); | ||
for (var i = 2; i < methodWithArg.length; i++) { | ||
Matrix.prototype[methodWithArg[i]] = inplaceMethod; | ||
var staticMethod2 = eval(fillTemplateFunction(staticMethodWithOneArg, tmplVar)); | ||
for (i = 2; i < methodWithArg.length; i++) { | ||
Matrix.prototype[methodWithArg[i]] = inplaceMethod2; | ||
Matrix.prototype[methodWithArg[i] + 'M'] = inplaceMethodM; | ||
Matrix.prototype[methodWithArg[i] + 'S'] = inplaceMethodS; | ||
Matrix[methodWithArg[i]] = staticMethod; | ||
Matrix[methodWithArg[i]] = staticMethod2; | ||
} | ||
@@ -1838,4 +1886,4 @@ } | ||
function fillTemplateFunction(template, values) { | ||
for (var i in values) { | ||
template = template.replace(new RegExp('%' + i + '%', 'g'), values[i]); | ||
for (var value in values) { | ||
template = template.replace(new RegExp('%' + value + '%', 'g'), values[value]); | ||
} | ||
@@ -1842,0 +1890,0 @@ return template; |
'use strict'; | ||
var Matrix = require('../matrix'); | ||
var Matrix = require('../matrix').Matrix; | ||
@@ -11,4 +11,5 @@ // https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs | ||
value = Matrix.checkMatrix(value); | ||
if (!value.isSymmetric()) | ||
if (!value.isSymmetric()) { | ||
throw new Error('Matrix is not symmetric'); | ||
} | ||
@@ -15,0 +16,0 @@ var a = value, |
'use strict'; | ||
const Matrix = require('../matrix'); | ||
const Matrix = require('../matrix').Matrix; | ||
const util = require('./util'); | ||
@@ -45,4 +45,3 @@ const hypotenuse = util.hypotenuse; | ||
tql2(n, e, d, V); | ||
} | ||
else { | ||
} else { | ||
var H = getFilled2DArray(n, n, 0), | ||
@@ -91,4 +90,3 @@ ort = new Array(n); | ||
X[i][i + 1] = e[i]; | ||
} | ||
else if (e[i] < 0) { | ||
} else if (e[i] < 0) { | ||
X[i][i - 1] = e[i]; | ||
@@ -777,4 +775,3 @@ } | ||
return [(xr + r * xi) / d, (xi - r * xr) / d]; | ||
} | ||
else { | ||
} else { | ||
r = yr / yi; | ||
@@ -781,0 +778,0 @@ d = yi + r * yr; |
@@ -10,4 +10,5 @@ 'use strict'; | ||
} | ||
matrix = Matrix.checkMatrix(matrix); | ||
matrix = Matrix.Matrix.checkMatrix(matrix); | ||
var lu = matrix.clone(), | ||
@@ -89,7 +90,9 @@ rows = lu.rows, | ||
var data = this.LU; | ||
if (!data.isSquare()) | ||
if (!data.isSquare()) { | ||
throw new Error('Matrix must be square'); | ||
} | ||
var determinant = this.pivotSign, col = data.columns; | ||
for (var j = 0; j < col; j++) | ||
for (var j = 0; j < col; j++) { | ||
determinant *= data[j][j]; | ||
} | ||
return determinant; | ||
@@ -101,3 +104,3 @@ }, | ||
columns = data.columns, | ||
X = new Matrix(rows, columns); | ||
X = new Matrix.Matrix(rows, columns); | ||
for (var i = 0; i < rows; i++) { | ||
@@ -120,3 +123,3 @@ for (var j = 0; j < columns; j++) { | ||
columns = data.columns, | ||
X = new Matrix(rows, columns); | ||
X = new Matrix.Matrix(rows, columns); | ||
for (var i = 0; i < rows; i++) { | ||
@@ -137,3 +140,3 @@ for (var j = 0; j < columns; j++) { | ||
solve: function (value) { | ||
value = Matrix.checkMatrix(value); | ||
value = Matrix.Matrix.checkMatrix(value); | ||
@@ -143,11 +146,13 @@ var lu = this.LU, | ||
if (rows !== value.rows) | ||
if (rows !== value.rows) { | ||
throw new Error('Invalid matrix dimensions'); | ||
if (this.isSingular()) | ||
} | ||
if (this.isSingular()) { | ||
throw new Error('LU matrix is singular'); | ||
} | ||
var count = value.columns, | ||
X = value.subMatrixRow(this.pivotVector, 0, count - 1), | ||
columns = lu.columns, | ||
i, j, k; | ||
var count = value.columns; | ||
var X = value.subMatrixRow(this.pivotVector, 0, count - 1); | ||
var columns = lu.columns; | ||
var i, j, k; | ||
@@ -154,0 +159,0 @@ for (k = 0; k < columns; k++) { |
'use strict'; | ||
var Matrix = require('../matrix'); | ||
var Matrix = require('../matrix').Matrix; | ||
var hypotenuse = require('./util').hypotenuse; | ||
@@ -57,11 +57,13 @@ | ||
if (value.rows !== m) | ||
if (value.rows !== m) { | ||
throw new Error('Matrix row dimensions must agree'); | ||
if (!this.isFullRank()) | ||
} | ||
if (!this.isFullRank()) { | ||
throw new Error('Matrix is rank deficient'); | ||
} | ||
var count = value.columns, | ||
X = value.clone(), | ||
n = qr.columns, | ||
i, j, k, s; | ||
var count = value.columns; | ||
var X = value.clone(); | ||
var n = qr.columns; | ||
var i, j, k, s; | ||
@@ -68,0 +70,0 @@ for (k = 0; k < n; k++) { |
'use strict'; | ||
var Matrix = require('../matrix'); | ||
var Matrix = require('../matrix').Matrix; | ||
var util = require('./util'); | ||
@@ -22,6 +22,4 @@ var hypotenuse = util.hypotenuse; | ||
var wantu = true, wantv = true; | ||
if (options.computeLeftSingularVectors === false) | ||
wantu = false; | ||
if (options.computeRightSingularVectors === false) | ||
wantv = false; | ||
if (options.computeLeftSingularVectors === false) wantu = false; | ||
if (options.computeRightSingularVectors === false) wantv = false; | ||
var autoTranspose = options.autoTranspose === true; | ||
@@ -34,2 +32,3 @@ | ||
a = value.clone(); | ||
// eslint-disable-next-line no-console | ||
console.warn('Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose'); | ||
@@ -105,4 +104,5 @@ } else { | ||
if (e[k] !== 0) { | ||
if (e[k + 1] < 0) | ||
e[k] = -e[k]; | ||
if (e[k + 1] < 0) { | ||
e[k] = 0 - e[k]; | ||
} | ||
for (i = k + 1; i < n; i++) { | ||
@@ -380,2 +380,3 @@ e[i] /= e[k]; | ||
} | ||
// no default | ||
} | ||
@@ -382,0 +383,0 @@ } |
'use strict'; | ||
exports.hypotenuse = function hypotenuse(a, b) { | ||
var r; | ||
if (Math.abs(a) > Math.abs(b)) { | ||
var r = b / a; | ||
r = b / a; | ||
return Math.abs(a) * Math.sqrt(1 + r * r); | ||
} | ||
if (b !== 0) { | ||
var r = a / b; | ||
r = a / b; | ||
return Math.abs(b) * Math.sqrt(1 + r * r); | ||
@@ -11,0 +12,0 @@ } |
'use strict'; | ||
var Matrix = require('./matrix'); | ||
var Matrix = require('./matrix').Matrix; | ||
@@ -5,0 +5,0 @@ var SingularValueDecomposition = require('./dc/svd'); |
'use strict'; | ||
module.exports = require('./matrix'); | ||
module.exports = require('./matrix').Matrix; | ||
module.exports.Decompositions = module.exports.DC = require('./decompositions'); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
constructor(nRows, nColumns) { | ||
var i; | ||
if (arguments.length === 1 && typeof nRows === 'number') { | ||
@@ -18,3 +19,3 @@ return new Array(nRows); | ||
if (Number.isInteger(nColumns) && nColumns > 0) { | ||
for (var i = 0; i < nRows; i++) { | ||
for (i = 0; i < nRows; i++) { | ||
this[i] = new Array(nColumns); | ||
@@ -26,3 +27,3 @@ } | ||
} else if (Array.isArray(nRows)) { // Copy the values from the 2D array | ||
var matrix = nRows; | ||
const matrix = nRows; | ||
nRows = matrix.length; | ||
@@ -34,3 +35,3 @@ nColumns = matrix[0].length; | ||
super(nRows); | ||
for (var i = 0; i < nRows; i++) { | ||
for (i = 0; i < nRows; i++) { | ||
if (matrix[i].length !== nColumns) { | ||
@@ -46,2 +47,3 @@ throw new RangeError('Inconsistent array dimensions'); | ||
this.columns = nColumns; | ||
return this; | ||
} | ||
@@ -60,3 +62,3 @@ | ||
* Creates an exact and independent copy of the matrix | ||
* @returns {Matrix} | ||
* @return {Matrix} | ||
*/ | ||
@@ -76,8 +78,9 @@ clone() { | ||
* @param {number} index - Row index | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
removeRow(index) { | ||
util.checkRowIndex(this, index); | ||
if (this.rows === 1) | ||
if (this.rows === 1) { | ||
throw new RangeError('A matrix cannot have less than one row'); | ||
} | ||
this.splice(index, 1); | ||
@@ -92,3 +95,3 @@ this.rows -= 1; | ||
* @param {Array|Matrix} array - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -110,8 +113,9 @@ addRow(index, array) { | ||
* @param {number} index - Column index | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
removeColumn(index) { | ||
util.checkColumnIndex(this, index); | ||
if (this.columns === 1) | ||
if (this.columns === 1) { | ||
throw new RangeError('A matrix cannot have less than one column'); | ||
} | ||
for (var i = 0; i < this.rows; i++) { | ||
@@ -128,3 +132,3 @@ this[i].splice(index, 1); | ||
* @param {Array|Matrix} array - Array or vector | ||
* @returns {Matrix} this | ||
* @return {Matrix} this | ||
*/ | ||
@@ -146,3 +150,3 @@ addColumn(index, array) { | ||
module.exports = Matrix; | ||
exports.Matrix = Matrix; | ||
Matrix.abstractMatrix = abstractMatrix; |
'use strict'; | ||
var Matrix = require('./matrix'); | ||
/** | ||
@@ -36,3 +38,3 @@ * @private | ||
* @param {Array|Matrix} vector | ||
* @returns {Array} | ||
* @return {Array} | ||
* @throws {RangeError} | ||
@@ -55,3 +57,3 @@ */ | ||
* @param {Array|Matrix} vector | ||
* @returns {Array} | ||
* @return {Array} | ||
* @throws {RangeError} | ||
@@ -80,3 +82,3 @@ */ | ||
if (rowOut || columnOut) { | ||
throw new RangeError('Indices are out of range') | ||
throw new RangeError('Indices are out of range'); | ||
} | ||
@@ -114,1 +116,31 @@ | ||
}; | ||
exports.sumByRow = function sumByRow(matrix) { | ||
var sum = Matrix.Matrix.zeros(matrix.rows, 1); | ||
for (var i = 0; i < matrix.rows; ++i) { | ||
for (var j = 0; j < matrix.columns; ++j) { | ||
sum.set(i, 0, sum.get(i, 0) + matrix.get(i, j)); | ||
} | ||
} | ||
return sum; | ||
}; | ||
exports.sumByColumn = function sumByColumn(matrix) { | ||
var sum = Matrix.Matrix.zeros(1, matrix.columns); | ||
for (var i = 0; i < matrix.rows; ++i) { | ||
for (var j = 0; j < matrix.columns; ++j) { | ||
sum.set(0, j, sum.get(0, j) + matrix.get(i, j)); | ||
} | ||
} | ||
return sum; | ||
}; | ||
exports.sumAll = function sumAll(matrix) { | ||
var v = 0; | ||
for (var i = 0; i < matrix.rows; i++) { | ||
for (var j = 0; j < matrix.columns; j++) { | ||
v += matrix.get(i, j); | ||
} | ||
} | ||
return v; | ||
}; |
'use strict'; | ||
var abstractMatrix = require('../abstractMatrix'); | ||
var Matrix; | ||
var Matrix = require('../matrix'); | ||
@@ -15,6 +15,3 @@ class BaseView extends abstractMatrix() { | ||
static get [Symbol.species]() { | ||
if (!Matrix) { | ||
Matrix = require('../matrix'); | ||
} | ||
return Matrix; | ||
return Matrix.Matrix; | ||
} | ||
@@ -21,0 +18,0 @@ } |
@@ -16,3 +16,3 @@ 'use strict'; | ||
get(rowIndex, columnIndex) { | ||
get(rowIndex) { | ||
return this.matrix.get(rowIndex, this.column); | ||
@@ -19,0 +19,0 @@ } |
@@ -15,3 +15,3 @@ 'use strict'; | ||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.rowIndices[rowIndex], this.columnIndices[columnIndex] , value); | ||
this.matrix.set(this.rowIndices[rowIndex], this.columnIndices[columnIndex], value); | ||
return this; | ||
@@ -18,0 +18,0 @@ } |
@@ -15,3 +15,3 @@ 'use strict'; | ||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.startRow + rowIndex, this.startColumn + columnIndex , value); | ||
this.matrix.set(this.startRow + rowIndex, this.startColumn + columnIndex, value); | ||
return this; | ||
@@ -18,0 +18,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134487
3646
12