我實作了兩種方陣乘法演算法。multiply_normal是傳統方式,它使用 3 個嵌套的 for 回圈并且multiply是一種遞回方法。
然而,即使該multiply_normal演算法給出了正確的輸出,其他演算法似乎也無法輸出正確的結果。
這是整個代碼:
public class App {
public static void main(String[] args) throws Exception {
int a[][] = { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } };
int b[][] = { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } };
print(multiply_normal(a, b));
System.out.println();
print(multiply(a, b));
}
public static int[][] multiply(int[][] A, int[][] B) {
int n = A.length;
int[][] R = new int[n][n];
if (n == 1)
R[0][0] = A[0][0] * B[0][0];
else {
int[][] A11 = new int[n / 2][n / 2];
int[][] A12 = new int[n / 2][n / 2];
int[][] A21 = new int[n / 2][n / 2];
int[][] A22 = new int[n / 2][n / 2];
int[][] B11 = new int[n / 2][n / 2];
int[][] B12 = new int[n / 2][n / 2];
int[][] B21 = new int[n / 2][n / 2];
int[][] B22 = new int[n / 2][n / 2];
split(A, A11, 0, 0);
split(A, A12, 0, n / 2);
split(A, A21, n / 2, 0);
split(A, A22, n / 2, n / 2);
split(B, B11, 0, 0);
split(B, B12, 0, n / 2);
split(B, B21, n / 2, 0);
split(B, B22, n / 2, n / 2);
// P:=M2 M3?M6?M7
int[][] C11 = add(multiply(A11, B11), multiply(A12, B21));
// Q:=M4 M6
int[][] C12 = add(multiply(A11, B12), multiply(A12, B22));
// R:=M5 M7
int[][] C21 = add(multiply(A21, B11), multiply(A22, B21));
// S:=M1?M3?M4?M5
int[][] C22 = add(multiply(A21, B12), multiply(A22, B22));
// Step 3: Join 4 halves into one result matrix
join(C11, R, 0, 0);
join(C12, R, 0, n / 2);
join(C21, R, n / 2, 0);
join(C22, R, n / 2, n / 2);
}
// Step 4: Return result
return R;
}
// Method 2
// Function to subtract two matrices
public int[][] sub(int[][] A, int[][] B) {
//
int n = A.length;
//
int[][] C = new int[n][n];
// Iterating over elements of 2D matrix
// using nested for loops
// Outer loop for rows
for (int i = 0; i < n; i )
// Inner loop for columns
for (int j = 0; j < n; j )
// Subtracting corresponding elements
// from matrices
C[i][j] = A[i][j] - B[i][j];
// Returning the resultant matrix
return C;
}
// Method 3
// Function to add two matrices
public static int[][] add(int[][] A, int[][] B) {
//
int n = A.length;
// Creating a 2D square matrix
int[][] C = new int[n][n];
// Iterating over elements of 2D matrix
// using nested for loops
// Outer loop for rows
for (int i = 0; i < n; i )
// Inner loop for columns
for (int j = 0; j < n; j )
// Adding corresponding elements
// of matrices
C[i][j] = A[i][j] B[i][j];
// Returning the resultant matrix
return C;
}
// Method 4
// Function to split parent matrix
// into child matrices
public static void split(int[][] P, int[][] C, int iB, int jB) {
// Iterating over elements of 2D matrix
// using nested for loops
// Outer loop for rows
for (int i1 = 0, i2 = iB; i1 < C.length; i1 , i2 )
// Inner loop for columns
for (int j1 = 0, j2 = jB; j1 < C.length; j1 , j2 )
C[i1][j1] = P[i2][j2];
}
// Method 5
// Function to join child matrices
// into (to) parent matrix
public static void join(int[][] C, int[][] P, int iB, int jB)
{
// Iterating over elements of 2D matrix
// using nested for loops
// Outer loop for rows
for (int i1 = 0, i2 = iB; i1 < C.length; i1 , i2 )
// Inner loop for columns
for (int j1 = 0, j2 = jB; j1 < C.length; j1 , j2 )
P[i2][j2] = C[i1][j1];
}
public static int[][] multiply_normal(int[][] A, int[][] B) {
int n = A.length;
int C[][] = new int[n][n];
for (int i = 0; i < n; i ) {
for (int j = 0; j < n; j ) {
C[i][j] = 0;
for (int k = 0; k < n; k ) {
C[i][j] = C[i][j] A[i][k] * B[k][j];
}
}
}
return C;
}
public static void print(int[][] a) {
for (int i = 0; i < a.length; i ) {
for (int k = 0; k < a.length; k ) {
System.out.print(a[i][k] " ");
}
System.out.println();
}
}
}
的輸出multiply_normal(輸入在 main 方法中):
6 6 6
12 12 12
18 18 18
的輸出multiply:
3 3 0
6 6 0
0 0 0
那么,我錯在哪里了?我該怎么做才能解決這個問題?如果您能幫助我,我將不勝感激。
uj5u.com熱心網友回復:
在簡要回顧了代碼之后:在給定的示例中,n/2 等于 1,這可能不是您想要的。
我希望這兩種方法都能為偶數大小(比如 4x4)矩陣產生相同的結果。
uj5u.com熱心網友回復:
嘗試使用替代遞回演算法實作。這是一個可以保證作業的。向下滾動以查看 Java 代碼示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346803.html
上一篇:從排序陣列邏輯中洗掉重復項在C#中拋出例外以獲得正確答案
下一篇:無交點連接二維多邊形的演算法
