For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > Re: Algorithm for multiplting two int









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Algorithm for multiplting two int
John

2005-02-28, 4:01 pm

bunallo wrote:
> I am trying to make the product of two int[][] (matrices).
>
> If I have the two matrices a and b I would like to calculate matrix c which
> is the product of a and b:
> In this example I use two matrices with length 2 that contains the same
> numbers.
>
> a: 1 2 b:1 2 = c: 7 10
> 3 4 3 4 15 22
>
> Here I create matrix a and matrix b:
>
> int[][] a = new int[2][2];
> int[][] b = new int[2][2];
>
> int fill = 1;
> for (int i = 0; i < 2; i++){
> for (int k = 0; k < 2; k++){
> a[i][k] = fill;
> b[i][k] = fill;
> fill++;
> }
> }
>
> Here I try yo make the calculation:
>
> int result = 0;
> int result2 = 0;
> int[]store = new int[4]; // Where I store the values of the product
> matrix x.
>
> for (int m = 0; m < 2; m++){
> for (int n = 0; n < 2; n++){
> result = result + (a[m][n] * b[n][m]);
> }
> result2 = result;
> store[m*m] = result2; // 7 and 22 get stored in my array.
> result = 0;
> }
> }
> }
>
> But I can only seem to calculate "7" and "22"! Does anybody have a hint for
> how to get the other two result of c calculated??
>
>

There is no need to reinvent the wheel.

http://math.nist.gov/javanumerics/jama/

John
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com