Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 24-04-2008, 11:59 PM   #1 (permalink)
Broken In
 
Tushar.bar's Avatar
 
Join Date: Jan 2006
Posts: 123
Question Matrix multiplication in java


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?
Quote:
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 by Tushar.bar; 25-04-2008 at 12:01 AM. Reason: Mistake
Tushar.bar is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 01-05-2008, 11:52 AM   #2 (permalink)
Wise Old Owl
 
JGuru's Avatar
 
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
Default Re: Matrix multiplication in java

@Tushar,

Here goes the code for Matrix Multiplication in Java language.

Code:
 
 //MatrixMultiplication.java

/*************************************************************************
 *  
 *  Compilation:  javac MatrixMultiplication.java
 *  Execution:    java MatrixMultiplication
 * 
 *  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!!!
__________________
* Imagination is more important than knowledge.
-Albert Einstein
JGuru is offline  
Old 01-05-2008, 11:58 AM   #3 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Matrix multiplication in java

But JGuru, he didn't ask for a program, but just an explanation of that code his friend gave him.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 01-05-2008, 05:59 PM   #4 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: Matrix multiplication in java

Quote:
Originally Posted by JGuru View Post
* @author JGuru 05-02-2006
Exact replica of

http://www.cs.princeton.edu/introcs/...tion.java.html

and added your name onto it.

Please mention sources next time, and try not to take credits for other people's work.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 01-05-2008, 06:05 PM   #5 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default Re: Matrix multiplication in java

Quote:
Originally Posted by Tushar.bar
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 ??
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is offline  
Old 07-05-2008, 09:51 AM   #6 (permalink)
Broken In
 
Tushar.bar's Avatar
 
Join Date: Jan 2006
Posts: 123
Red face Re: Matrix multiplication in java

^^^ya i think so.
Tushar.bar is offline  
Old 07-05-2008, 02:30 PM   #7 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: Matrix multiplication in java

Oh thats right, so how about storing a 2D array in a 1D array? Like for a n-column matrix, [i][j] = [n*i + j] Does anyone get what I'm talking about?
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you want in Matrix 4 ? Batistabomb Chit-Chat 7 28-01-2008 01:03 PM
THE MATRIX TRILOGY RETURNS - Hi-def Matrix trilogy goes to HD DVD this spring, Blu-ra techtronic Technology News 19 29-03-2007 04:17 PM
The Matrix: PON moshel Gamerz 4 16-10-2006 06:22 PM
Speed Multiplication on Dial-Up? How's it possible? DKant QnA (read only) 4 14-04-2005 12:47 PM
The Matrix: Path of Neo tarey_g Gamerz 20 21-03-2005 07:09 PM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 10:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2