9

When I'm initializing a new DMatrixRMaj in ejml (standard format for real matrix), it can store internally a double[][] matrix. Example

double[][] a = new double[][];
//init a
DMatrixRMaj d = new DMatrixRMaj(a);
//math operations on d

now, after the necessary calculations, how I can obtain back a double[][] form of d? With d.getData() i can only obtain the row form. I've also tried wrapping in SimpleMatrix, or creating a SimpleMatrix from doubles, but I haven't find any methods (or matrix format) to retrieve doubles back!

Do you know how I can do it? Or can you suggest a straigthforward workaround without having to write a personalized function?

Lore
  • 1,286
  • 1
  • 22
  • 57

1 Answers1

3

I am not very familiar with the library, but considering matrix is stored using data[ y*numCols + x ] = data[y][x] format, you could write your own function to retrieve data using the same format.

Example:.

import org.ejml.data.DMatrixRMaj;

public class Main {

    public static void main(String[] args) throws Exception {
        double [] [] doubles = new double[2][2];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                doubles[i][j] = 0.1;
            }
        }

        //init a
        DMatrixRMaj d = new DMatrixRMaj(doubles);

        double [] [] doublesAfter = getInitMatrix(d);

        System.out.println("Initial matrix: ");
        printMatrix(doubles);

        System.out.println("Same matrix after DMatrixRMaj: ");
        printMatrix(doublesAfter);
    }

    private static double [][] getInitMatrix(DMatrixRMaj dMatrixRMaj) {
        double [] [] doubles = new double[dMatrixRMaj.getNumRows()][dMatrixRMaj.getNumCols()];
        for (int x = 0; x < dMatrixRMaj.getNumRows(); x++) {
            for (int y = 0; y <dMatrixRMaj.getNumCols(); y++) {
                doubles[x][y] = dMatrixRMaj.getData()[y*dMatrixRMaj.getNumCols() + x];
            }
        }
        return doubles;
    }

    private static void printMatrix(double [][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}

**Print result:**

//Initial matrix: 
0.1 0.1 
0.1 0.1 
//Same matrix after DMatrixRMaj: 
0.1 0.1 
0.1 0.1 
CrazySabbath
  • 1,274
  • 3
  • 11
  • 33
  • Java is a great language, there's a reason it's used so widely. You just have to choose right tools for the right task... – CrazySabbath May 11 '18 at 07:04
  • There is no reason, but I don't think SO comments it's the right place to start a discussion about. – Lore May 11 '18 at 07:06
  • 1
    You started, not me. Anyway, I looked at the docs again (as you should have?), there's even a simpler way to do this. DMatrixRMaj class has methods getNumCols(), getNumRows() and get(row, col). Simple 2d loop and you get your data back. – CrazySabbath May 11 '18 at 07:09
  • I won't call writing by myself a function "simple". I call it a lack of structure in the library, and in Java it's the norm. – Lore May 11 '18 at 07:11