简体   繁体   中英

Matrix Multiplication Java

I need help, I am trying to make use of Lattice Multiplication in java for use in a BigInt class I am having to write for a class.

Right now I have the code storing the digits needed for the adding part of the algorithm in a 2 dimensional array. From there though I am at a lose as to how to make a loop to go through the array and add the numbers in what would be a diagonal.

For instance here is the test numbers etc:

200 *311 = 62200

The array is holding:

6 0 0

2 0 0

2 0 0

6 is (2,2) in the array and the bottom right is (0,0)

I need to add in a diagonal, such as (1,0) + (0,1) = 0

The issue is how do I do this, since not only is it moving up and left in different ways, but it goes from 1 element to 2 elements to 3 elements, then back the other way, and of course this will get bigger the longer the number is.

This is the code that I have:

public int multiply(BigInt val){
        int[] bigger;
        int[] smaller;
        int[] dStore;

        int lengthMax = (val.getSize()+this.getSize()) - 1;
        int first = 0;
        int second = 0;

        int[][] tempResult;


        //Checks to see which is bigger and then adds that to bigger
        if(val.getSize() >= this.getSize()){
            bigger = val.getData();
            smaller = this.getData();
            dStore = new int[val.getSize()+this.getSize()];
        }else{
            bigger = this.getData();
            smaller = val.getData();
            dStore = new int[val.getSize()+this.getSize()];
        }

        tempResult = new int[smaller.length][bigger.length];

        for(int i=0;i < smaller.length;i++){
            for(int j = 0;j < bigger.length;j++){
                tempResult[i][j] = smaller[i] * bigger[j];
            }
        }

** there is the return statement etc below

This might help as to explain lattice multi better: Lattice Multi Video

To move diagonally, you'd increment both x and y:

// Loop though the diagonal of an array
x = 0;
y = 0;
while (x < ARR_X_SIZE && y < ARR_Y_SIZE) {
   // Do something with arr[x][y]
   x++;
   y++;
}

This is the basic loop; you can change the x and y increments to determine the direction you need to go. The key to going through the whole array is the value of the coordinates going into the loop. Array:

1 2 3
4 5 6
7 8 9

If you set x = 1; y=0 x = 1; y=0 at the beginning of the loop, you'll get 2 6 . Set x = 0, y = 1 and you'll get 4 8 .

I hope this helps you with your assignment. Good luck on the rest! That is definately an interesting algorithm to implement.

I would try a different approach. Look at the lattice in the video and imagine that you rotates the array a little bit to the left so that the diagonals becomes vertical. The array would then look like this:

2 3 5
  8 3
  2 4 0

Now, just summarize the columns and you would have the total.

You would of course have to split the numbers into arrays of digits first. The easiest way to do that (but not the most efficient) is to convert them into strings ...

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM