const Matrix = require('../..');
const { INVALID_P_NORM, SINGULAR_MATRIX, INVALID_SQUARE_MATRIX } = require('../../Error');
/**
* 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(p = 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 {
const 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;