@rayyamhk/matrix
Version:
A professional, comprehensive and high-performance library for you to manipulate matrices.
45 lines (36 loc) • 1.1 kB
JavaScript
var Matrix = require('../..');
var _require = require('../../Error'),
INVALID_P_NORM = _require.INVALID_P_NORM,
SINGULAR_MATRIX = _require.SINGULAR_MATRIX,
INVALID_SQUARE_MATRIX = _require.INVALID_SQUARE_MATRIX;
/**
* Calculations the condition number of square Matrix
* with respect to the choice of Matrix norm.
* If the Matrix is singular, returns Infinity.<br><br>
* The condition number is not cached.
* @memberof Matrix
* @instance
* @param {(1|2|Infinity|'F')} p - Type of Matrix norm
* @returns {number} The condition number of Matrix
*/
function cond() {
var p = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
if (p !== 1 && p !== 2 && p !== Infinity && p !== 'F') {
throw new Error(INVALID_P_NORM);
}
if (!this.isSquare()) {
throw new Error(INVALID_SQUARE_MATRIX);
}
try {
var inverse = Matrix.inverse(this);
return inverse.norm(p) * this.norm(p);
} catch (error) {
if (error.message === SINGULAR_MATRIX) {
return Infinity;
}
throw error;
}
}
;
module.exports = cond;
;