@rayyamhk/matrix
Version:
A professional, comprehensive and high-performance library for you to manipulate matrices.
102 lines (76 loc) • 3.63 kB
JavaScript
"use strict";
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/* eslint-disable prefer-destructuring */
var Matrix = require('../..');
var _require = require('../../Error'),
INVALID_SQUARE_MATRIX = _require.INVALID_SQUARE_MATRIX;
/**
* Calculates the determinant of square Matrix.
* If the Matrix size is larger than 3, it calculates the determinant using
* LU decomposition, otherwise, using Leibniz Formula.<br><br>
* The determinant is cached.
* @memberof Matrix
* @instance
* @returns {number} Returns the determinant of square matrirx
*/
function det() {
if (!this.isSquare()) {
throw new Error(INVALID_SQUARE_MATRIX);
}
if (this._det !== undefined) {
return this._det;
}
var matrix = this._matrix;
var size = matrix.length;
if (size === 0) {
this._det = 1;
return 1; // the determinant of 0x0 matrix must be 1
}
if (size === 1) {
this._det = matrix[0][0];
return this._det;
}
if (size === 2) {
this._det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return this._det;
}
if (size === 3) {
this._det = matrix[0][0] * matrix[1][1] * matrix[2][2] + matrix[0][1] * matrix[1][2] * matrix[2][0] + matrix[0][2] * matrix[1][0] * matrix[2][1] - matrix[0][2] * matrix[1][1] * matrix[2][0] - matrix[0][1] * matrix[1][0] * matrix[2][2] - matrix[0][0] * matrix[1][2] * matrix[2][1];
return this._det;
}
var _Matrix$LU = Matrix.LU(this, true),
_Matrix$LU2 = _slicedToArray(_Matrix$LU, 2),
P = _Matrix$LU2[0],
LU = _Matrix$LU2[1];
var matrixLU = LU._matrix; // count whether the number of permutations <swap> is odd or even
// O(n^2)
var swap = 0;
for (var i = 0; i < size; i++) {
if (P[i] === i) {
continue;
}
while (P[i] !== i) {
var target = P[i];
P[i] = P[target];
P[target] = target;
swap++;
}
}
var result = 1;
for (var _i2 = 0; _i2 < size; _i2++) {
result *= matrixLU[_i2][_i2];
}
if (swap % 2 === 1) {
this._det = result * -1;
return this._det;
}
this._det = result;
return result;
}
;
module.exports = det;