Using generics in matrix complement / multiplication

I am trying to create a custom class that creates matrices and, among other things, does operations that add all the cells or multiply them all. However, I want to use generics, so the matrix can be any number type: float, double, int, etc. Thus, I created a class like this:

public class Matrix<num>

      

Upon initialization, an instance of this class creates a matrix based on user-supplied data stored in the instance's .matrix variable. Now, in the code where I want to add all the cells, I do something like this:

public num addMatrices(num[][] toAdd){
        num result;
        if (toAdd.length != this.rows && toAdd[0].length != this.columns){
            System.out.println("Matrix mismatch. Try Again.");
            return toAdd[0][0];
        }
        for (int i=0; i<rows; i++)
            for (int j=0; j<rows; j++){
                result = this.matrix[i][j] + toAdd[i][j];
            }
    }

      

However, I am facing several problems. First of all, I cannot initialize the result to zero, which makes it difficult to do the + = operations. Secondly, when I try to add cells of two matrices, the compiler tells me that the + operator is undefined for type num.

I thought the whole point of generics was to be of type catchall, so I could do things like use floats in one case and ints in the other, but if I need to specify a type for operators like +, I don't sure where the advantage comes from ...

+3


source to share


2 answers


You can not perform operations such as +

and -

objects (excluding some special cases). Generics are all object types, so your use case is not ideal for them.

Thus, you can turn your ad into something like public class Matrix<num extends Number>

that will allow you to pass through Integer

, Double

, BigInteger

and so on, then you can use something like num.longValue()

or num.doubleValue()

to obtain long or double represent your numbers. Then you would need to return Double

either long

or something else from your method instead of your generic type.



another option is to create a custom container class that has methods for adding, subtracting, etc. Then the class declaration can be public class Matrix<num extends Custom>

. You will need to figure out how to account for the addition of long doubles and custom type returns.

+1


source


The problem is, you num

can be anything. So, for example, what should you do if you created Matrix<Object>

?

You can try to make your number from Number

, but even then you won't be able to use +

because java doesn't know what to do with this generic.

You can create a method similar to yours Matrix

with a generic one that does this. Something like



public static <NUM extends Number> NUM add(NUM[][] matrixA, NUM[][] matrixB, NumFunction<NUM> function) {
    // all equals to your add method, but the result you get with
    result = function.apply(this.matrix[i][j], toAdd[i][j]);
}

public interface NumFunction<NUM extends Number> {
    NUM apply(NUM operA, NUM operB);
}

      

You will need to duplicate NumFunction

for each type, Number

or create one generic type that checks what type of number the "instance" has. It's not very pretty: P

0


source







All Articles