Open In App

Implementing Coppersmith Winograd Algorithm in Java

Last Updated : 15 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Coppersmith Winograd Algorithm is asymptotically the fastest known algorithm to date for the matrix multiplication algorithm. Algorithms with a preferred asymptotic running time over the Strassen calculation are rarely utilized practically, on the grounds that the huge steady factors in their running occasions make them inappropriate. 

It can multiply two n × n matrices in O(n^{2.375477}) time. It is used to check matrix multiplication.

It decides if the matrices are equivalent to a chosen value of k with an expectation value of failure under 2^-k in O(kn^2).

Example

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

Algorithm

// 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

Below is the implementation of the above approach.

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");
    }
}


Output

Resultant matrix is Matching

Time Complexity: O(n^{2.375477})



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads