Skip to content

Commit 55c179b

Browse files
committed
Added MatrixFastPower.java with changes
1 parent f713a5f commit 55c179b

File tree

1 file changed

+82
-30
lines changed

1 file changed

+82
-30
lines changed

DataStructures/Matrix/MatrixFastPower.java

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.util.*;
2-
31
/**
42
*
53
* Java implementation of Matrix fast power
@@ -14,11 +12,67 @@
1412
* other Matrix basic operator is based on @author Kyler Smith, 2017
1513
*
1614
* @author DDullahan, 2018
17-
*
15+
*
1816
*/
1917

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+
}
2074
class Matrix {
21-
private int[][] data;
75+
public int[][] data;
2276

2377
/**
2478
* Constructor for the matrix takes in a 2D array
@@ -106,34 +160,32 @@ public Matrix multiply(Matrix other) throws RuntimeException {
106160
}
107161

108162
/**
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+
}
134183

184+
str += "]";
185+
str += "\n";
135186
}
136187

137-
return newMatrix;
188+
return str;
138189
}
190+
139191
}

0 commit comments

Comments
 (0)