enhancedmath
Version:
This package contains some enhanced mathematical operations
25 lines (24 loc) • 739 B
JavaScript
import { getDimensions, multiplyMatrix } from '../Helpers/index';
import cofactorMatrix from './cofactor';
import determinant from './determinant';
import transpose from './transpose';
/**
* Calculates the inverse of a given matrix
* @param matrix The matrix
* @returns The inverse of the matrix
*/
const matrixInverse = (matrix) => {
const { rows, cols } = getDimensions(matrix);
if (rows !== cols) {
return undefined;
}
const det = determinant(matrix);
if (det === 0) {
return undefined;
}
const cofactor = cofactorMatrix(matrix);
// TODO: Clean this up
const adjointMatrix = transpose(cofactor);
return multiplyMatrix(adjointMatrix, 1 / det);
};
export default matrixInverse;