Skip to content

Commit 65a919f

Browse files
authored
Merge pull request #501 from DDullahan/master
Added MatrixFastPower.java
2 parents 826e7b5 + 55c179b commit 65a919f

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
*
3+
* Java implementation of Matrix fast power
4+
* It can calculate the high power of constant Matrix with O( log(K) )
5+
* where K is the power of the Matrix
6+
*
7+
* In order to do that, Matrix must be square Matrix ( columns equals rows)
8+
*
9+
* Notice : large power of Matrix may cause overflow
10+
*
11+
*
12+
* other Matrix basic operator is based on @author Kyler Smith, 2017
13+
*
14+
* @author DDullahan, 2018
15+
*
16+
*/
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+
}
74+
class Matrix {
75+
public int[][] data;
76+
77+
/**
78+
* Constructor for the matrix takes in a 2D array
79+
*
80+
* @param pData
81+
*/
82+
public Matrix(int[][] pData) {
83+
84+
/** Make a deep copy of the data */
85+
if(pData.length != 0) {
86+
int[][] newData = new int[pData.length][pData[0].length];
87+
88+
for(int i = 0; i < pData.length; i++)
89+
for(int j = 0; j < pData[0].length; j++)
90+
newData[i][j] = pData[i][j];
91+
92+
this.data = newData;
93+
} else {
94+
this.data = null;
95+
}
96+
}
97+
98+
/**
99+
* Returns the element specified by the given location
100+
*
101+
* @param x : x cooridinate
102+
* @param y : y cooridinate
103+
* @return int : value at location
104+
*/
105+
public int getElement(int x, int y) {
106+
return data[x][y];
107+
}
108+
109+
/**
110+
* Returns the number of rows in the Matrix
111+
*
112+
* @return rows
113+
*/
114+
public int getRows() {
115+
if(this.data == null)
116+
return 0;
117+
118+
return data.length;
119+
}
120+
121+
/**
122+
* Returns the number of rows in the Matrix
123+
*
124+
* @return columns
125+
*/
126+
public int getColumns() {
127+
if(this.data == null)
128+
return 0;
129+
130+
return data[0].length;
131+
}
132+
133+
/**
134+
* Multiplies this matrix with another matrix.
135+
*
136+
* @param other : Matrix to be multiplied with
137+
* @return product
138+
*/
139+
public Matrix multiply(Matrix other) throws RuntimeException {
140+
141+
int[][] newData = new int[this.data.length][other.getColumns()];
142+
143+
if(this.getColumns() != other.getRows())
144+
throw new RuntimeException("The two matrices cannot be multiplied.");
145+
146+
int sum;
147+
148+
for (int i = 0; i < this.getRows(); ++i)
149+
for(int j = 0; j < other.getColumns(); ++j) {
150+
sum = 0;
151+
152+
for(int k = 0; k < this.getColumns(); ++k) {
153+
sum += this.data[i][k] * other.getElement(k, j);
154+
}
155+
156+
newData[i][j] = sum;
157+
}
158+
159+
return new Matrix(newData);
160+
}
161+
162+
/**
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+
}
183+
184+
str += "]";
185+
str += "\n";
186+
}
187+
188+
return str;
189+
}
190+
191+
}

0 commit comments

Comments
 (0)