forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixFastPower.java
191 lines (149 loc) · 4.49 KB
/
MatrixFastPower.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
*
* Java implementation of Matrix fast power
* It can calculate the high power of constant Matrix with O( log(K) )
* where K is the power of the Matrix
*
* In order to do that, Matrix must be square Matrix ( columns equals rows)
*
* Notice : large power of Matrix may cause overflow
*
*
* other Matrix basic operator is based on @author Kyler Smith, 2017
*
* @author DDullahan, 2018
*
*/
class MatrixFastPower {
/**
* Matrix Fast Power
*
* @param matrix : square Matrix
* @param k : power of Matrix
* @return product
*/
public static Matrix FastPower(Matrix matrix, int k) throws RuntimeException {
if(matrix.getColumns() != matrix.getRows())
throw new RuntimeException("Matrix is not square Matrix.");
int[][] newData = new int[matrix.getColumns()][matrix.getRows()];
for(int i = 0; i < matrix.getColumns(); i++)
newData[i][i] = 1;
Matrix newMatrix = new Matrix(newData),
coMatrix = new Matrix(matrix.data);
while(k != 0) {
if((k & 1) != 0)
newMatrix = newMatrix.multiply(coMatrix);
k >>= 1;
coMatrix = coMatrix.multiply(coMatrix);
}
return newMatrix;
}
public static void main(String[] argv) {
int[][] data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Matrix matrix = new Matrix(data);
System.out.println("original matrix : ");
System.out.println(matrix.toString());
matrix = MatrixFastPower.FastPower(matrix, 5);
System.out.println("after power : ");
System.out.println(matrix.toString());
matrix = MatrixFastPower.FastPower(matrix, 1000000);
System.out.println("notice, large power may cause overflow : ");
System.out.print(matrix.toString());
System.out.println("you can use mod to fix that :-) ");
}
}
class Matrix {
public int[][] data;
/**
* Constructor for the matrix takes in a 2D array
*
* @param pData
*/
public Matrix(int[][] pData) {
/** Make a deep copy of the data */
if(pData.length != 0) {
int[][] newData = new int[pData.length][pData[0].length];
for(int i = 0; i < pData.length; i++)
for(int j = 0; j < pData[0].length; j++)
newData[i][j] = pData[i][j];
this.data = newData;
} else {
this.data = null;
}
}
/**
* Returns the element specified by the given location
*
* @param x : x cooridinate
* @param y : y cooridinate
* @return int : value at location
*/
public int getElement(int x, int y) {
return data[x][y];
}
/**
* Returns the number of rows in the Matrix
*
* @return rows
*/
public int getRows() {
if(this.data == null)
return 0;
return data.length;
}
/**
* Returns the number of rows in the Matrix
*
* @return columns
*/
public int getColumns() {
if(this.data == null)
return 0;
return data[0].length;
}
/**
* Multiplies this matrix with another matrix.
*
* @param other : Matrix to be multiplied with
* @return product
*/
public Matrix multiply(Matrix other) throws RuntimeException {
int[][] newData = new int[this.data.length][other.getColumns()];
if(this.getColumns() != other.getRows())
throw new RuntimeException("The two matrices cannot be multiplied.");
int sum;
for (int i = 0; i < this.getRows(); ++i)
for(int j = 0; j < other.getColumns(); ++j) {
sum = 0;
for(int k = 0; k < this.getColumns(); ++k) {
sum += this.data[i][k] * other.getElement(k, j);
}
newData[i][j] = sum;
}
return new Matrix(newData);
}
/**
* Returns the Matrix as a String in the following format
*
* [ a b c ] ...
* [ x y z ] ...
* [ i j k ] ...
* ...
*
* @return Matrix as String
* TODO: Work formatting for different digit sizes
*/
public String toString() {
String str = "";
for(int i = 0; i < this.data.length; i++) {
str += "[ ";
for(int j = 0; j < this.data[0].length; j++) {
str += data[i][j];
str += " ";
}
str += "]";
str += "\n";
}
return str;
}
}