MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / matrixExpo

Function matrixExpo

Maths/Fibonacci.js:144–159  ·  view source on GitHub ↗
(A, n)

Source from the content-addressed store, hash-verified

142 */
143// A is a square matrix
144const matrixExpo = (A, n) => {
145 A = copyMatrix(A)
146 const isBigInt = typeof n === 'bigint'
147 const ZERO = isBigInt ? 0n : 0
148 const TWO = isBigInt ? 2n : 2
149
150 // Just like Binary exponentiation mentioned in ./BinaryExponentiationIterative.js
151 let result = Identity((isBigInt ? BigInt : Number)(A.length)) // Identity matrix
152 while (n > ZERO) {
153 if (n % TWO !== ZERO) result = matrixMultiply(result, A)
154 n /= TWO
155 if (!isBigInt) n = Math.floor(n)
156 if (n > ZERO) A = matrixMultiply(A, A)
157 }
158 return result
159}
160
161const FibonacciMatrixExpo = (num) => {
162 const isBigInt = typeof num === 'bigint'

Callers 1

FibonacciMatrixExpoFunction · 0.85

Calls 3

copyMatrixFunction · 0.85
matrixMultiplyFunction · 0.85
IdentityFunction · 0.70

Tested by

no test coverage detected