(A, n)
| 142 | */ |
| 143 | // A is a square matrix |
| 144 | const 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 | |
| 161 | const FibonacciMatrixExpo = (num) => { |
| 162 | const isBigInt = typeof num === 'bigint' |
no test coverage detected