F94ed31bc99b74602392d052ee5bf901

I want to modify this code to work for subtractions, multiplications and divisions. The only thing that needs to change is the add operator really. I don't want to copy and paste this code. I know there are library functions but this is experimentation

public static Matrices add(Matrices a, Matrices b)
    {
        int rows = a.getRows();
        int cols = a.getCols();
        
        if (rows != b.getRows() || cols != b.getCols() ) {
            return null;
        }
        
        int[][] aData = a.getData();
        int[][] bData = b.getData();
        
        int[][] newData = new int[rows][cols];
        
        for (int row = 0 ; row < rows; row++) {
            for (int col = 0 ; col < cols; col++) {
                newData[row][col] = aData[row][col] + bData[row][col];
            }
        }
        
        return new Matrices(a.getName() + b.getName(), newData);
            
    }

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Brian

April 29, 2009, April 29, 2009 04:10, permalink

No rating. Login to rate!

First, matrix multiplication and division do not work like matrix addition: you do not operate cell-by-cell. The matrix product is the sum of the products of each row entry of the first matrix and column entry of the second matrix.

[[1, 2][3, 4]] * [[5, 6][7, 8]] = [[(1*5 + 2*7), (1*6 + 2*8)][(3*5 + 4*7), (3*6 + 4*8)]]

To answer your question, probably not. In other languages like Python, you can pass a function as a formal parameter to another function. You can later use that passed function to preform addition, subtraction, or whatever. However, Java does not let you do that (easily).

I would just copy/paste/modify the code if I were you.

BTW, I think the class name should be singular, FWIW.
BTW, throw (or print) an error if the rows/cols don't match. [1] + [2 3] != null; it is impossible.

Here is an idea, but I think it will just make the problem worse...

public class Operation {          // this class represents a generic operation on two numbers
  public int exec(int a, int b) { // This is a 'dummy' function, your add() will call this
    return 0;                     // This part will be overwritten by another operation later
  }
}

public class Matrix {
  // Other stuff...

  // This will preform the generic operations on matrices
  protected static Matrix operate(Matrix a, Matrix b, Operation func) {
    // All the setup stuff (like creating your matrices, getting data, etc...)

    // In the nested for loops
      newData[row][col] = func.exec(a[row][col], b[row][col]); // Operate with every element
                                                               // using the provided function

    // Return the new matrix
    return new Matrix(a.getName(), b.getName(), newData);
  }

  // The addition will use the above operate() method
  public static Matrix add(Matrix a, Matrix b) {
    Operation add = new Operation() { // Construct a custom Operation object
      public int exec(int a, int b) { // Override the existing exec() method
        return a + b;                 // ...with one specific to addition
      }
    }

    return operate(a, b, add);
  }

  // The multiplication will use the above operate() method
  public static Matrix multiply(Matrix a, Matrix b) { // cell-by-cell multiplication
    Operation multiply = new Operation() { // Construct a custom operation object
      public int exec(int a, int b) {      // Override the existing exec() method
        return a * b;                      // ...with one specific to multiplication
      }
    }

    return operate(a, b, multiply);
  }

  // And so on...
}
F94ed31bc99b74602392d052ee5bf901

Sambob

April 29, 2009, April 29, 2009 15:06, permalink

No rating. Login to rate!

That's exactly what I thought. (I forgot about the non-cell processes, I had not got that far in my book)

Thanks.

Your refactoring





Format Copy from initial code

or Cancel