jblas-project / jblas

Linear Algebra for Java
http://jblas.org
BSD 3-Clause "New" or "Revised" License
590 stars 149 forks source link

Inverted rows/columns in matrix representation #42

Closed m0nzderr closed 9 years ago

m0nzderr commented 10 years ago

Not a big deal, but a bit confusing bug, either in a code or in docs.

According to your javadoc, we got the following API:

DoubleMatrix(int newRows, int newColumns, double... newData) saying: "DoubleMatrix(m,n, [value1, value2, value3...]) Values are filled in row by row."

So, I expected the following test to pass, but it didn't:

    final DoubleMatrix x1=new DoubleMatrix(2,3,new double[]{1,2,3,4,5,6});
    final DoubleMatrix x2=new DoubleMatrix(new double[][]{{1,2,3},{4,5,6}});

    System.out.println(x1);
    System.out.println(x2);

    assertTrue(x1.equals(x2));

The output is:

[1,000000, 3,000000, 5,000000; 2,000000, 4,000000, 6,000000] [1,000000, 2,000000, 3,000000; 4,000000, 5,000000, 6,000000]

It looks like DoubleMatrix(m,n, [value1, value2, value3...]) is filled "column by column" instead. "Column by column" may also make some sense, since for example, Matlab does so in memory, but I didn't expect such behavior here ;)

Thanx!

P.S: I uses jblass-1.2.3 from maven.

mikiobraun commented 9 years ago

Hi m0nzderr,

thanks for reporting this. Yeah, the docs are wrong.

So internally I'm using column major (column by column) because that is what Fortran expects, but when you do the nested double[][] thing, it just looks like filling in the data row after row, so this is why there's a difference.

-M