|
1 | | -import java.util.*; |
2 | | - |
3 | 1 | /** |
4 | 2 | * |
5 | 3 | * Java implementation of Matrix fast power |
|
14 | 12 | * other Matrix basic operator is based on @author Kyler Smith, 2017 |
15 | 13 | * |
16 | 14 | * @author DDullahan, 2018 |
17 | | - * |
| 15 | + * |
18 | 16 | */ |
19 | 17 |
|
| 18 | +class MatrixFastPower { |
| 19 | + |
| 20 | + /** |
| 21 | + * Matrix Fast Power |
| 22 | + * |
| 23 | + * @param matrix : square Matrix |
| 24 | + * @param k : power of Matrix |
| 25 | + * @return product |
| 26 | + */ |
| 27 | + public static Matrix FastPower(Matrix matrix, int k) throws RuntimeException { |
| 28 | + |
| 29 | + if(matrix.getColumns() != matrix.getRows()) |
| 30 | + throw new RuntimeException("Matrix is not square Matrix."); |
| 31 | + |
| 32 | + int[][] newData = new int[matrix.getColumns()][matrix.getRows()]; |
| 33 | + |
| 34 | + for(int i = 0; i < matrix.getColumns(); i++) |
| 35 | + newData[i][i] = 1; |
| 36 | + |
| 37 | + Matrix newMatrix = new Matrix(newData), |
| 38 | + coMatrix = new Matrix(matrix.data); |
| 39 | + |
| 40 | + while(k != 0) { |
| 41 | + |
| 42 | + if((k & 1) != 0) |
| 43 | + newMatrix = newMatrix.multiply(coMatrix); |
| 44 | + |
| 45 | + k >>= 1; |
| 46 | + coMatrix = coMatrix.multiply(coMatrix); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + return newMatrix; |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] argv) { |
| 54 | + |
| 55 | + int[][] data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 56 | + Matrix matrix = new Matrix(data); |
| 57 | + |
| 58 | + System.out.println("original matrix : "); |
| 59 | + System.out.println(matrix.toString()); |
| 60 | + |
| 61 | + matrix = MatrixFastPower.FastPower(matrix, 5); |
| 62 | + |
| 63 | + System.out.println("after power : "); |
| 64 | + System.out.println(matrix.toString()); |
| 65 | + |
| 66 | + matrix = MatrixFastPower.FastPower(matrix, 1000000); |
| 67 | + |
| 68 | + System.out.println("notice, large power may cause overflow : "); |
| 69 | + System.out.print(matrix.toString()); |
| 70 | + System.out.println("you can use mod to fix that :-) "); |
| 71 | + |
| 72 | + } |
| 73 | +} |
20 | 74 | class Matrix { |
21 | | - private int[][] data; |
| 75 | + public int[][] data; |
22 | 76 |
|
23 | 77 | /** |
24 | 78 | * Constructor for the matrix takes in a 2D array |
@@ -106,34 +160,32 @@ public Matrix multiply(Matrix other) throws RuntimeException { |
106 | 160 | } |
107 | 161 |
|
108 | 162 | /** |
109 | | - * Matrix Fast Power |
110 | | - * |
111 | | - * @param k : power of Matrix |
112 | | - * @return product |
113 | | - */ |
114 | | - public Matrix MatrixFastPower(int k) throws RuntimeException { |
115 | | - |
116 | | - if(this.getColumns() != this.getRows()) |
117 | | - throw new RuntimeException("Matrix is not square Matrix."); |
118 | | - |
119 | | - int[][] newData = new int[this.getColumns()][this.getRows()]; |
120 | | - |
121 | | - for(int i = 0; i < this.getColumns(); i++) |
122 | | - newData[i][i] = 1; |
123 | | - |
124 | | - Matrix newMatrix = new Matrix(newData), |
125 | | - coMatrix = new Matrix(this.data); |
126 | | - |
127 | | - while(k != 0) { |
128 | | - |
129 | | - if((k & 1) != 0) |
130 | | - newMatrix = newMatrix.multiply(coMatrix); |
131 | | - |
132 | | - k >>= 1; |
133 | | - coMatrix = coMatrix.multiply(coMatrix); |
| 163 | + * Returns the Matrix as a String in the following format |
| 164 | + * |
| 165 | + * [ a b c ] ... |
| 166 | + * [ x y z ] ... |
| 167 | + * [ i j k ] ... |
| 168 | + * ... |
| 169 | + * |
| 170 | + * @return Matrix as String |
| 171 | + * TODO: Work formatting for different digit sizes |
| 172 | + */ |
| 173 | + public String toString() { |
| 174 | + String str = ""; |
| 175 | + |
| 176 | + for(int i = 0; i < this.data.length; i++) { |
| 177 | + str += "[ "; |
| 178 | + |
| 179 | + for(int j = 0; j < this.data[0].length; j++) { |
| 180 | + str += data[i][j]; |
| 181 | + str += " "; |
| 182 | + } |
134 | 183 |
|
| 184 | + str += "]"; |
| 185 | + str += "\n"; |
135 | 186 | } |
136 | 187 |
|
137 | | - return newMatrix; |
| 188 | + return str; |
138 | 189 | } |
| 190 | + |
139 | 191 | } |
0 commit comments