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
|
public class StrassenMatrix {
public void printMatrix(Matrix matrix, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(matrix.matrix[i][j] + " "); } System.out.println(); } }
public void Divide(Matrix M, Matrix M11, Matrix M12, Matrix M21, Matrix M22, int n) {
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M11.matrix[i][j] = M.matrix[i][j]; M12.matrix[i][j] = M.matrix[i][j + n]; M21.matrix[i][j] = M.matrix[i + n][j]; M22.matrix[i][j] = M.matrix[i + n][j + n]; } } }
public Matrix MergeMatrix(Matrix M11, Matrix M12, Matrix M21, Matrix M22, int n) { Matrix _return = new Matrix(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { _return.matrix[i][j] = M11.matrix[i][j]; _return.matrix[i][j + n] = M12.matrix[i][j]; _return.matrix[i + n][j] = M21.matrix[i][j]; _return.matrix[i + n][j + n] = M22.matrix[i][j]; } } return _return; }
public Matrix MatrixMultiplication(Matrix x, Matrix y) { int M1, M2, M3, M4, M5, M6, M7; M1 = x.matrix[0][0] * (y.matrix[0][1] - y.matrix[1][1]); M2 = y.matrix[1][1] * (x.matrix[0][0] + x.matrix[0][1]); M3 = y.matrix[0][0] * (x.matrix[1][0] + x.matrix[1][1]); M4 = x.matrix[1][1] * (y.matrix[1][0] - y.matrix[0][0]); M5 = (x.matrix[0][0] * y.matrix[0][0] + x.matrix[0][0] * y.matrix[1][1]) + (x.matrix[1][1] * y.matrix[0][0] + x.matrix[1][1] * y.matrix[1][1]); M6 = (x.matrix[0][1] * y.matrix[1][0] + x.matrix[0][1] * y.matrix[1][1]) - (x.matrix[1][1] * y.matrix[1][0] + x.matrix[1][1] * y.matrix[1][1]); M7 = (x.matrix[0][0] * y.matrix[0][0] + x.matrix[0][0] * y.matrix[0][1]) - (x.matrix[1][0] * y.matrix[0][0] + x.matrix[1][0] * y.matrix[0][1]); Matrix _return = new Matrix(); _return.matrix[0][0] = M5 + M4 - M2 + M6; _return.matrix[1][0] = M1 + M2; _return.matrix[0][1] = M3 + M4; _return.matrix[1][1] = M5 + M1 - M3 - M7; return _return; }
public Matrix MatrixMultiplication(Matrix x, Matrix y, int n) { Matrix A11 = new Matrix(); Matrix A12 = new Matrix(); Matrix A21 = new Matrix(); Matrix A22 = new Matrix(); Matrix B11 = new Matrix(); Matrix B12 = new Matrix(); Matrix B21 = new Matrix(); Matrix B22 = new Matrix(); Matrix C11 = new Matrix(); Matrix C12 = new Matrix(); Matrix C21 = new Matrix(); Matrix C22 = new Matrix(); Matrix M1, M2, M3, M4, M5, M6, M7; if (n == 2) { return MatrixMultiplication(x, y); } else { Matrix C = null; Divide(x, A11, A12, A21, A22, n / 2); Divide(y, B11, B12, B21, B22, n / 2); n /= 2; M1 = MatrixMultiplication(A11, MatrixModified(B12, B22, n, false), n); M2 = MatrixMultiplication(MatrixModified(A11, A12, n, true), B22, n); M3 = MatrixMultiplication(MatrixModified(A21, A22, n, true), B11, n); M4 = MatrixMultiplication(A22, MatrixModified(B21, B11, n, false), n); M5 = MatrixMultiplication(MatrixModified(A11, A22, n, true), MatrixModified(B11, B22, n, true), n); M6 = MatrixMultiplication(MatrixModified(A12, A22, n, false), MatrixModified(B21, B22, n, true), n); M7 = MatrixMultiplication(MatrixModified(A11, A21, n, false), MatrixModified(B11, B12, n, true), n); C11 = MatrixModified(MatrixModified(M5, M4, n, true), MatrixModified(M2, M6, n, false), n, false); C12 = MatrixModified(M1, M2, n, true); C21 = MatrixModified(M3, M4, n, true); C22 = MatrixModified(MatrixModified(M5, M1, n, true), MatrixModified(M3, M7, n, true), n, false); C = MergeMatrix(C11, C12, C21, C22, n); return C; } }
public Matrix MatrixModified(Matrix x, Matrix y, int n, Boolean isPlus) { Matrix _return = new Matrix(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (isPlus) { _return.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j]; } else { _return.matrix[i][j] = x.matrix[i][j] - y.matrix[i][j]; } return _return; }
public static void main(String[] args) { StrassenMatrix strassenMatrix = new StrassenMatrix(); int[][] M_X = { { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 } }; int[][] M_Y = { { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 } }; Matrix x = new Matrix(); Matrix y = new Matrix(); x.matrix = M_X; y.matrix = M_Y; Matrix result = strassenMatrix.MatrixMultiplication(x, y, 8); strassenMatrix.printMatrix(result, 8); }
}
class Matrix { public int[][] matrix = new int[32][32]; public Matrix() { } }
|