|
| 1 | +package MatrixExponentiation; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** @author Anirudh Buvanesh (https://github.com/anirudhb11) |
| 6 | + * For more information see https://www.geeksforgeeks.org/matrix-exponentiation/ |
| 7 | + * */ |
| 8 | +public class Fibonacci { |
| 9 | + // Exponentiation matrix for Fibonacci sequence |
| 10 | + private static final int [][] fibMatrix = {{1,1}, {1,0}}; |
| 11 | + private static final int [][] identityMatrix = {{1,0}, {0,1}}; |
| 12 | + //First 2 fibonacci numbers |
| 13 | + private static final int [][] baseFibNumbers = {{1}, {0}}; |
| 14 | + |
| 15 | + /** |
| 16 | + * Performs multiplication of 2 matrices |
| 17 | + * @param matrix1 |
| 18 | + * @param matrix2 |
| 19 | + * @return The product of matrix1 and matrix2 |
| 20 | + */ |
| 21 | + |
| 22 | + private static int[][] matrixMultiplication(int[][] matrix1, int[][] matrix2){ |
| 23 | + //Check if matrices passed can be multiplied |
| 24 | + int rowsInMatrix1 = matrix1.length; |
| 25 | + int columnsInMatrix1 = matrix1[0].length; |
| 26 | + |
| 27 | + int rowsInMatrix2 = matrix2.length; |
| 28 | + int columnsInMatrix2 = matrix2[0].length; |
| 29 | + |
| 30 | + assert columnsInMatrix1 == rowsInMatrix2; |
| 31 | + int [][] product = new int[rowsInMatrix1][columnsInMatrix2]; |
| 32 | + for (int rowIndex = 0; rowIndex < rowsInMatrix1; rowIndex ++){ |
| 33 | + for(int colIndex = 0; colIndex < columnsInMatrix2; colIndex++){ |
| 34 | + int matrixEntry = 0; |
| 35 | + for(int intermediateIndex = 0; intermediateIndex < columnsInMatrix1; intermediateIndex++){ |
| 36 | + matrixEntry += matrix1[rowIndex][intermediateIndex] * matrix2[intermediateIndex][colIndex]; |
| 37 | + } |
| 38 | + product[rowIndex][colIndex] = matrixEntry; |
| 39 | + } |
| 40 | + } |
| 41 | + return product; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Calculates the fibonacci number using matrix exponentiaition technique |
| 46 | + * @param n The input n for which we have to determine the fibonacci number Outputs the nth |
| 47 | + * * fibonacci number |
| 48 | + * @return a 2 X 1 array as { {F_n+1}, {F_n} } |
| 49 | + */ |
| 50 | + public static int[][] fib(int n){ |
| 51 | + if(n == 0){ |
| 52 | + return Fibonacci.identityMatrix; |
| 53 | + } |
| 54 | + else{ |
| 55 | + int [][] cachedResult = fib(n/2); |
| 56 | + int [][] matrixExpResult = matrixMultiplication(cachedResult, cachedResult); |
| 57 | + if(n%2 == 0){ |
| 58 | + return matrixExpResult; |
| 59 | + } |
| 60 | + else{ |
| 61 | + return matrixMultiplication(Fibonacci.fibMatrix, matrixExpResult); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + // Returns [0, 1, 1, 2, 3, 5 ..] for n = [0, 1, 2, 3, 4, 5.. ] |
| 68 | + Scanner sc = new Scanner(System.in); |
| 69 | + int n = sc.nextInt(); |
| 70 | + int [][] result = matrixMultiplication(fib(n), baseFibNumbers); |
| 71 | + System.out.println("Fib(" + n + ") = "+ result[1][0] ); |
| 72 | + sc.close(); |
| 73 | + } |
| 74 | +} |
0 commit comments