matrix-inverse
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -1,36 +0,43 @@ | ||
var Sylvester = {}; | ||
var Sylvester = {} | ||
Sylvester.Matrix = function() {}; | ||
Sylvester.Matrix = function() {} | ||
Sylvester.Matrix.create = function(elements) { | ||
var M = new Sylvester.Matrix(); | ||
return M.setElements(elements); | ||
}; | ||
var M = new Sylvester.Matrix() | ||
return M.setElements(elements) | ||
} | ||
Sylvester.Matrix.I = function(n) { | ||
var els = [], i = n, j; | ||
while (i--) { j = n; | ||
els[i] = []; | ||
var els = [], | ||
i = n, | ||
j | ||
while (i--) { | ||
j = n | ||
els[i] = [] | ||
while (j--) { | ||
els[i][j] = (i === j) ? 1 : 0; | ||
els[i][j] = i === j ? 1 : 0 | ||
} | ||
} | ||
return Sylvester.Matrix.create(els); | ||
}; | ||
return Sylvester.Matrix.create(els) | ||
} | ||
Sylvester.Matrix.prototype = { | ||
dup: function() { | ||
return Sylvester.Matrix.create(this.elements); | ||
return Sylvester.Matrix.create(this.elements) | ||
}, | ||
isSquare: function() { | ||
var cols = (this.elements.length === 0) ? 0 : this.elements[0].length; | ||
return (this.elements.length === cols); | ||
var cols = this.elements.length === 0 ? 0 : this.elements[0].length | ||
return this.elements.length === cols | ||
}, | ||
toRightTriangular: function() { | ||
if (this.elements.length === 0) return Sylvester.Matrix.create([]); | ||
var M = this.dup(), els; | ||
var n = this.elements.length, i, j, np = this.elements[0].length, p; | ||
if (this.elements.length === 0) return Sylvester.Matrix.create([]) | ||
var M = this.dup(), | ||
els | ||
var n = this.elements.length, | ||
i, | ||
j, | ||
np = this.elements[0].length, | ||
p | ||
for (i = 0; i < n; i++) { | ||
@@ -40,6 +47,8 @@ if (M.elements[i][i] === 0) { | ||
if (M.elements[j][i] !== 0) { | ||
els = []; | ||
for (p = 0; p < np; p++) { els.push(M.elements[i][p] + M.elements[j][p]); } | ||
M.elements[i] = els; | ||
break; | ||
els = [] | ||
for (p = 0; p < np; p++) { | ||
els.push(M.elements[i][p] + M.elements[j][p]) | ||
} | ||
M.elements[i] = els | ||
break | ||
} | ||
@@ -50,4 +59,4 @@ } | ||
for (j = i + 1; j < n; j++) { | ||
var multiplier = M.elements[j][i] / M.elements[i][i]; | ||
els = []; | ||
var multiplier = M.elements[j][i] / M.elements[i][i] | ||
els = [] | ||
for (p = 0; p < np; p++) { | ||
@@ -58,48 +67,75 @@ // Elements with column numbers up to an including the number of the | ||
// loop over and correct rounding errors later | ||
els.push(p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier); | ||
els.push( | ||
p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier | ||
) | ||
} | ||
M.elements[j] = els; | ||
M.elements[j] = els | ||
} | ||
} | ||
} | ||
return M; | ||
return M | ||
}, | ||
determinant: function() { | ||
if (this.elements.length === 0) { return 1; } | ||
if (!this.isSquare()) { return null; } | ||
var M = this.toRightTriangular(); | ||
var det = M.elements[0][0], n = M.elements.length; | ||
if (this.elements.length === 0) { | ||
return 1 | ||
} | ||
if (!this.isSquare()) { | ||
return null | ||
} | ||
var M = this.toRightTriangular() | ||
var det = M.elements[0][0], | ||
n = M.elements.length | ||
for (var i = 1; i < n; i++) { | ||
det = det * M.elements[i][i]; | ||
det = det * M.elements[i][i] | ||
} | ||
return det; | ||
return det | ||
}, | ||
isSingular: function() { | ||
return (this.isSquare() && this.determinant() === 0); | ||
return this.isSquare() && this.determinant() === 0 | ||
}, | ||
augment: function(matrix) { | ||
if (this.elements.length === 0) { return this.dup(); } | ||
var M = matrix.elements || matrix; | ||
if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; } | ||
var T = this.dup(), cols = T.elements[0].length; | ||
var i = T.elements.length, nj = M[0].length, j; | ||
if (i !== M.length) { return null; } | ||
while (i--) { j = nj; | ||
if (this.elements.length === 0) { | ||
return this.dup() | ||
} | ||
var M = matrix.elements || matrix | ||
if (typeof M[0][0] === 'undefined') { | ||
M = Sylvester.Matrix.create(M).elements | ||
} | ||
var T = this.dup(), | ||
cols = T.elements[0].length | ||
var i = T.elements.length, | ||
nj = M[0].length, | ||
j | ||
if (i !== M.length) { | ||
return null | ||
} | ||
while (i--) { | ||
j = nj | ||
while (j--) { | ||
T.elements[i][cols + j] = M[i][j]; | ||
T.elements[i][cols + j] = M[i][j] | ||
} | ||
} | ||
return T; | ||
return T | ||
}, | ||
inverse: function() { | ||
if (this.elements.length === 0) { return null; } | ||
if (!this.isSquare() || this.isSingular()) { return null; } | ||
var n = this.elements.length, i= n, j; | ||
var M = this.augment(Sylvester.Matrix.I(n)).toRightTriangular(); | ||
var np = M.elements[0].length, p, els, divisor; | ||
var inverse_elements = [], new_element; | ||
if (this.elements.length === 0) { | ||
return null | ||
} | ||
if (!this.isSquare() || this.isSingular()) { | ||
return null | ||
} | ||
var n = this.elements.length, | ||
i = n, | ||
j | ||
var M = this.augment(Sylvester.Matrix.I(n)).toRightTriangular() | ||
var np = M.elements[0].length, | ||
p, | ||
els, | ||
divisor | ||
var inverse_elements = [], | ||
new_element | ||
// Sylvester.Matrix is non-singular so there will be no zeros on the | ||
@@ -109,52 +145,56 @@ // diagonal. Cycle through rows from last to first. | ||
// First, normalise diagonal elements to 1 | ||
els = []; | ||
inverse_elements[i] = []; | ||
divisor = M.elements[i][i]; | ||
els = [] | ||
inverse_elements[i] = [] | ||
divisor = M.elements[i][i] | ||
for (p = 0; p < np; p++) { | ||
new_element = M.elements[i][p] / divisor; | ||
els.push(new_element); | ||
new_element = M.elements[i][p] / divisor | ||
els.push(new_element) | ||
// Shuffle off the current row of the right hand side into the results | ||
// array as it will not be modified by later runs through this loop | ||
if (p >= n) { inverse_elements[i].push(new_element); } | ||
if (p >= n) { | ||
inverse_elements[i].push(new_element) | ||
} | ||
} | ||
M.elements[i] = els; | ||
M.elements[i] = els | ||
// Then, subtract this row from those above it to give the identity matrix | ||
// on the left hand side | ||
j = i; | ||
j = i | ||
while (j--) { | ||
els = []; | ||
els = [] | ||
for (p = 0; p < np; p++) { | ||
els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]); | ||
els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]) | ||
} | ||
M.elements[j] = els; | ||
M.elements[j] = els | ||
} | ||
} | ||
return Sylvester.Matrix.create(inverse_elements); | ||
return Sylvester.Matrix.create(inverse_elements) | ||
}, | ||
setElements: function(els) { | ||
var i, j, elements = els.elements || els; | ||
if (elements[0] && typeof(elements[0][0]) !== 'undefined') { | ||
i = elements.length; | ||
this.elements = []; | ||
while (i--) { j = elements[i].length; | ||
this.elements[i] = []; | ||
var i, | ||
j, | ||
elements = els.elements || els | ||
if (elements[0] && typeof elements[0][0] !== 'undefined') { | ||
i = elements.length | ||
this.elements = [] | ||
while (i--) { | ||
j = elements[i].length | ||
this.elements[i] = [] | ||
while (j--) { | ||
this.elements[i][j] = elements[i][j]; | ||
this.elements[i][j] = elements[i][j] | ||
} | ||
} | ||
return this; | ||
return this | ||
} | ||
var n = elements.length; | ||
this.elements = []; | ||
var n = elements.length | ||
this.elements = [] | ||
for (i = 0; i < n; i++) { | ||
this.elements.push([elements[i]]); | ||
this.elements.push([elements[i]]) | ||
} | ||
return this; | ||
return this | ||
}, | ||
} | ||
}; | ||
module.exports = function (elements) { | ||
return Sylvester.Matrix.create(elements).inverse().elements; | ||
}; | ||
module.exports = function(elements) { | ||
return Sylvester.Matrix.create(elements).inverse().elements | ||
} |
{ | ||
"name": "matrix-inverse", | ||
"version": "0.1.0", | ||
"description": "Matrix inverse function (from sylvester)", | ||
"version": "1.0.0", | ||
"description": "Matrix inverse (code from sylvester)", | ||
"main": "matrix-inverse.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test:js": "node test.js", | ||
"prettier": "prettier --write \"**/*.@(yml|json|js|md)\"", | ||
"prettier:check": "prettier --check \"**/*.@(yml|json|js|md)\"", | ||
"test": "npm run test && npm run prettier:check" | ||
}, | ||
"keywords": [ | ||
"matrix" | ||
"matrix", | ||
"inverse", | ||
"linear algebra", | ||
"sylvester" | ||
], | ||
"author": "Body Labs", | ||
"license": "MIT", | ||
"repository": "https://github.com/bodylabs/matrix-inverse-js", | ||
"repository": "metabolize/matrix-inverse", | ||
"devDependencies": { | ||
"chai": "~1.10.0", | ||
"chai-stats": "~0.3.0" | ||
} | ||
"chai": "~4.2.0", | ||
"chai-stats": "~0.3.0", | ||
"prettier": "^1.18.2" | ||
}, | ||
"files": [ | ||
"matrix-inverse.js" | ||
] | ||
} |
@@ -1,50 +0,55 @@ | ||
matrix-inverse-js | ||
================= | ||
# matrix-inverse | ||
Matrix inverse function packaged for CommonJS. Code is from [sylvester][] | ||
by [James Coglan][], with gratitude. | ||
[![version](https://img.shields.io/npm/v/matrix-inverse?style=flat-square)][npm] | ||
[![license](https://img.shields.io/npm/l/matrix-inverse?style=flat-square)][npm] | ||
[![build](https://img.shields.io/circleci/project/github/metabolize/matrix-inverse?style=flat-square)][build] | ||
[![bundle size](https://img.shields.io/bundlephobia/minzip/matrix-inverse?style=flat-square)][bundlephobia] | ||
[![code style](https://img.shields.io/badge/code_style-prettier-ff69b4?style=flat-square)][prettier] | ||
[npm]: https://npmjs.com/anafanafo | ||
[build]: https://circleci.com/gh/metabolize/anafanafo/tree/master | ||
[bundlephobia]: https://bundlephobia.com/result?p=anafanafo | ||
[prettier]: https://prettier.io/ | ||
Installation | ||
------------ | ||
Matrix inverse function. Code is from [sylvester][] by [James Coglan][], with | ||
gratitude. | ||
Install matrix-inverse-js by running: | ||
## Installation | ||
Install matrix-inverse by running: | ||
npm install matrix-inverse | ||
## Example | ||
Example | ||
------- | ||
```js | ||
const matrixInverse = require('matrix-inverse') | ||
var matrixInverse = require('./matrix-inverse'); | ||
const M = [[3, 3.2], [3.5, 3.6]] | ||
var M = [ | ||
[3, 3.2], | ||
[3.5, 3.6], | ||
]; | ||
const M_inv = matrixInverse(M) | ||
``` | ||
var M_inv = matrixInverse(M); | ||
## Origin | ||
Origin | ||
------ | ||
This code was copied and adapted from sylvester at commit | ||
5a2c61681e988d60bf0a4223640c636052946341. | ||
## Contribute | ||
Contribute | ||
---------- | ||
- Issue Tracker: https://github.com/metabolize/matrix-inverse/issues | ||
- Source Code: https://github.com/metabolize/matrix-inverse | ||
- Issue Tracker: github.com/bodylabs/matrix-inverse-js/issues | ||
- Source Code: github.com/bodylabs/matrix-inverse-js | ||
## Acknowledgements | ||
This project was packaged by [Paul Melnikow][] while at [Body Labs][]. Thanks | ||
to Body Labs for the repository transfer. | ||
License | ||
------- | ||
## License | ||
The project is licensed under the MIT license. | ||
[sylvester]: https://github.com/jcoglan/sylvester | ||
[James Coglan]: http://jcoglan.com/ | ||
[paul melnikow]: https://github.com/paulmelnikow | ||
[body labs]: https://github.com/bodylabs | ||
[james coglan]: http://jcoglan.com/ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8438
184
1
56
3
1