0

i have to write matrix multiply function using generic class with float and double parameters, then compare results and times. so far i have this:

 public MyMatrix<T> multiply(MyMatrix<T> secondMatrix) {
    MyMatrix<T> resultMatrix = new MyMatrix<T>(classType, this.rows, secondMatrix.columns);
           if (classType.equals(Float.class)) {
            float sum = 0;
            for (int c = 0 ; c < rows; c++ ) {
             for (int d = 0 ; d < secondMatrix.columns; d++ ) {
                  for (int k = 0 ; k < secondMatrix.rows; k++ ) {
                 sum = sum + this.matrix[c][k].floatValue() * secondMatrix.matrix[k][d].floatValue(); }
                 Float wynik = sum;
                resultMatrix.matrix[c][d] = (T)result; 
                sum=0;   }       }    }
              else if (classType.equals(Double.class)  (...) same but double instead float

results are probably correct, but the problem is that the function is faster with double parameter.

here is how i measure the time:

start = System.nanoTime();
MyMatrix<Float> matrixFloat = matrixAfloat.multiply(vectorFloat); 
end = System.nanoTime() - start;
double timeFloat = end / 1000000000.0;

start = System.nanoTime();
MyMatrix<Double> matrixDouble = matrixAdouble.multiply(vectorDouble); 
end = System.nanoTime() - start;
double timeDouble = end / 1000000000.0;

any ideas where i could make a mistake?

bardamu
  • 57
  • 2
  • 8

0 Answers0