📜  用Java实现 Coppersmith Winograd 算法

📅  最后修改于: 2022-05-13 01:54:32.205000             🧑  作者: Mango

用Java实现 Coppersmith Winograd 算法

Coppersmith Winograd 算法是迄今为止已知最快的矩阵乘法算法。具有优先于Strassen计算的渐近运行时间的算法在实践中很少被使用,理由是其运行场合中巨大的稳定因素使它们不合适。

它可以在 O(n^{2.375477}) 时间内将两个 n × sn 矩阵相乘。它用于检查矩阵乘法。

它决定矩阵是否等于选定的 k 值,并且在 O(kn^2) 中,失败的期望值低于 2^-k。

例子

Input:M1={{1,2},
      {3,4}}
      M2={{3,2},
          {5,1}}
      Result={{13,4},
              {29,10}}
Output:Resultant matrix is matching

算法

// Task is to verify matrix multiplication as M1*M2=M3 or not.
1. Start
2. Take Matrices M1, M2, M3 as an input of (n*n).
3. Choose matrix a[n][1] randomly to which component will be 0 or 1.
4. Calculate M2 * a, M3 * a and then M1 * (M2 * a) for computing the expression,
   M1 * (M2 * a) - M3 * a.
5. Verify if M1 * (M2 * a) - M3 * a = 0 or not.
6. If it is zero or false, then matrix multiplication is correct otherwise not.
7. End

下面是上述方法的实现。

Java
// Implementing Coppersmith Winograd Algorithm in Java
import java.io.*;
import java.util.Random;
  
class GFG {
  
    public static boolean coppersmithWinograd(double[][] M1,
                                       double[][] M2,
                                       double[][] M3, int n)
    {
        double[][] a = new double[n][1];
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            a[i][0] = rand.nextInt() % 2;
        }
  
        double[][] M2a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M2a[i][j]
                        = M2a[i][j] + M2[i][k] * a[k][j];
                }
            }
        }
  
        double[][] M3a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M3a[i][j]
                        = M3a[i][j] + M3[i][k] * a[k][j];
                }
            }
        }
  
        double[][] M12a = new double[n][1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 1; j++) {
                for (int k = 0; k < n; k++) {
                    M12a[i][j]
                        = M12a[i][j] + M1[i][k] * M2a[k][j];
                }
            }
        }
        for (int i = 0; i < n; i++) {
            M12a[i][0] -= M3a[i][0];
        }
        boolean sameResultantMatrix = true;
        for (int i = 0; i < n; i++) {
            if (M12a[i][0] == 0)
                continue;
            else
                sameResultantMatrix = false;
        }
        return sameResultantMatrix;
    }
  
    // Driver's Function
    public static void main(String[] args)
    {
  
        /// "Input the dimension of the matrices: "
        int n;
        n = 2;
        // "Input the 1st or M1 matrix: "
        double[][] M1 = { { 1, 2 }, { 3, 4 } };
        // "Input the 2nd or M2 matrix: "
  
        double[][] M2 = { { 2, 0 }, { 1, 2 } };
  
        // "Input the result or M3 matrix: "
        double[][] M3 = { { 4, 4 }, { 10, 8 } };
  
        if (coppersmithWinograd(M1, M2, M3, n))
            System.out.println("Resultant matrix is Matching");
        else
            System.out.println("Resultant matrix is not Matching");
    }
}


输出
Resultant matrix is Matching

时间复杂度: O(n^{2.375477})