# Matrix multiplication in java



## Tushar.bar (Apr 24, 2008)

HI all , one of my friend was challenged by me that to make a program to multiply two matrix and display result.
but rule is  using only one one dimensional array.
he wright the prog in java, but i don't know java(only c,c++)

so can any check that
            1>Is the code is working?
             2>How many array he takes for it?
             3>how many array he use?


> void forkJoinMatrixMultiply(
> final double[][] a,
> final double[][] b,
> final double[][] c) {
> ...


----------



## JGuru (May 1, 2008)

@Tushar,

           Here goes the code for *Matrix Multiplication* in Java language.


```
//MatrixMultiplication.java

/*************************************************************************
 *  
 *  Compilation:  [b]javac MatrixMultiplication.java[/b]
 *  Execution:    [b]java MatrixMultiplication[/b]
 * 
 *  6 different ways to multiply two N-by-N matrices.
 *  Illustrates importance of row-major vs. column-major ordering.
 *
 *  $ java MatrixMultiplication 400
 *  
 *   @author JGuru  05-02-2006
 *
 *  Generating input:  0.765 seconds
 *  Order ijk:   7.875 seconds
 *  Order ikj:   14.542 seconds
 *  Order jik:   7.838 seconds
 *  Order jki:   20.86 seconds
 *  Order kij:   13.132 seconds
 *  Order kji:   20.813 seconds
 *
 *************************************************************************/

public class MatrixMultiplication {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        long start, stop;
        double elapsed;


        // generate input
        start = System.currentTimeMillis(); 

        double[][] A = new double[N][N];
        double[][] B = new double[N][N];
        double[][] C;

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                A[i][j] = Math.random();

        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                B[i][j] = Math.random();

        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Generating input:  " + elapsed + " seconds");


        // order 1: ijk = dot product version
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                for (int k = 0; k < N; k++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order ijk:   " + elapsed + " seconds");


        // order 2: ikj
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int i = 0; i < N; i++)
            for (int k = 0; k < N; k++)
                for (int j = 0; j < N; j++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order ikj:   " + elapsed + " seconds");

        // order 3: jik
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int j = 0; j < N; j++)
            for (int i = 0; i < N; i++)
                for (int k = 0; k < N; k++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order jik:   " + elapsed + " seconds");

        // order 4: jki = GAXPY version
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int j = 0; j < N; j++)
            for (int k = 0; k < N; k++)
                for (int i = 0; i < N; i++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order jki:   " + elapsed + " seconds");

        // order 5: kij
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int k = 0; k < N; k++)
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order kij:   " + elapsed + " seconds");

        // order 6: kji = outer product version
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        for (int k = 0; k < N; k++)
            for (int j = 0; j < N; j++)
                for (int i = 0; i < N; i++)
                    C[i][k] += A[i][j] * B[j][k];
        stop = System.currentTimeMillis();
        elapsed = (stop - start) / 1000.0;
        System.out.println("Order kji:   " + elapsed + " seconds");


        // order 7: jik optimized ala JAMA
        C = new double[N][N];
        start = System.currentTimeMillis(); 
        double[] bj = new double[N];
        for (int j = 0; j < N; j++) {
           for (int k = 0; k < N; k++) bj[k] = B[k][j];
              for (int i = 0; i < N; i++) {
                 double[] ai = A[i];
                 double s = 0;
                 for (int k = 0; k < N; k++) {
                     s += ai[k] * bj[k];
                 }
                 C[i][j] = s;
             }
         }
         stop = System.currentTimeMillis();
         elapsed = (stop - start) / 1000.0;
         System.out.println("Order kji:   " + elapsed + " seconds");


    }

}
```

  Java is very similar to C/C++ (by intent). If you know C/C++, you should be very
 comfortable in Java in no time. Java goes a step higher ,compared to these
 languages in terms of simplicity, OOPS, reusability, inheritance, Core Classes,
  platform independence etc., 

  You can get a Java book like *Java 2: The Complete Reference*  to  get familiar
 with Java in no time!!!


----------



## QwertyManiac (May 1, 2008)

But JGuru, he didn't ask for a program, but just an explanation of that code his friend gave him.


----------



## dheeraj_kumar (May 1, 2008)

JGuru said:


> *   @author JGuru  05-02-2006



Exact replica of

*www.cs.princeton.edu/introcs/95linear/MatrixMultiplication.java.html

and added your name onto it.

Please mention sources next time, and try not to take credits for other people's work.


----------



## Garbage (May 1, 2008)

Tushar.bar said:
			
		

> but rule is  using only one one dimensional array.



Look at this code..

```
final double[][] a,
final double[][] b,
final double[][] c
```

Aren't they 3 two-dimensional arrays ??


----------



## Tushar.bar (May 7, 2008)

^^^ya i think so.


----------



## dheeraj_kumar (May 7, 2008)

Oh thats right, so how about storing a 2D array in a 1D array? Like for a n-column matrix, _[j] = [n*i + j] Does anyone get what I'm talking about?_


----------

