Matrix multiplication in java

Status
Not open for further replies.

Tushar.bar

Journeyman
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) {
check(a,b,c);
final int m = a.length;
final int n = b.length;
final ParallelDoubleArray cols = ParallelDoubleArray.createUsingHandoff(b[0],
ParallelDoubleArray.defaultExecutor());
cols.replaceWithMappedIndex(new Ops.IntAndDoubleToDouble() {

public double op(final int j, final double element) {
final double[] Bcolj = new double[n]; // allocated inside the op, not "outside the loop"
for (int k = 0; k < n; k++) {
Bcolj[k] = b[k][j];
}
for (int i = 0; i < m; i++) {
final double[] Arowi = a;
double s = 0;
for (int k = 0; k < n; k++) {
s += Arowi[k] * Bcolj[k];
}
c[j] = s;
}
return element;
}
});
}
 
Last edited:

JGuru

Wise Old Owl
@Tushar,

Here goes the code for Matrix Multiplication in Java language.

Code:
 //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!!!
 

dheeraj_kumar

Legen-wait for it-dary!
* @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

God of Mistakes...
Tushar.bar said:
but rule is using only one one dimensional array.

Look at this code..
Code:
final double[][] a,
final double[][] b,
final double[][] c

Aren't they 3 two-dimensional arrays ?? :confused:
 

dheeraj_kumar

Legen-wait for it-dary!
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?
 
Status
Not open for further replies.
Top Bottom