UNPKG

@rayyamhk/matrix

Version:

A professional, comprehensive and high-performance library for you to manipulate matrices.

52 lines (40 loc) 1.14 kB
"use strict"; var _require = require('../../Error'), INVALID_MATRIX = _require.INVALID_MATRIX, INVALID_SQUARE_MATRIX = _require.INVALID_SQUARE_MATRIX, INVALID_EXPONENT = _require.INVALID_EXPONENT; /** * Calculates the power of any square matrix. * The algorithm is implemented recursively. * @memberof Matrix * @static * @param {Matrix} A - Any square Matrix * @param {number} exponent - Any Non-negative integer * @returns {Matrix} The power of A */ function pow(A, exponent) { if (!(A instanceof this)) { throw new Error(INVALID_MATRIX); } if (!A.isSquare()) { throw new Error(INVALID_SQUARE_MATRIX); } if (!Number.isInteger(exponent) || exponent < 0) { throw new Error(INVALID_EXPONENT); } var size = A.size()[0]; if (exponent === 0) { return this.identity(size); } if (exponent === 1) { return this.clone(A); } if (exponent % 2 === 0) { var _temp = this.pow(A, exponent / 2); return this.multiply(_temp, _temp); } var temp = this.pow(A, (exponent - 1) / 2); return this.multiply(this.multiply(temp, temp), A); } ; module.exports = pow;