@sgratzl/science
Version:
Scientific and statistical computing in JavaScript.
29 lines (22 loc) • 621 B
JavaScript
import gaussjordan from './gaussjordan';
// Find matrix inverse using Gauss-Jordan.
export default function inverse(m) {
var n = m.length,
i = -1;
// Check if the matrix is square.
if (n !== m[0].length) return;
// Augment with identity matrix I to get AI.
m = m.map(function(row, i) {
var identity = new Array(n),
j = -1;
while (++j < n) identity[j] = i === j ? 1 : 0;
return row.concat(identity);
});
// Compute IA^-1.
gaussjordan(m);
// Remove identity matrix I to get A^-1.
while (++i < n) {
m[i] = m[i].slice(n);
}
return m;
};