ujmp / universal-java-matrix-package

A Java Library for sparse and dense matrices, linear algebra, visualization and big data
https://ujmp.org
GNU Lesser General Public License v3.0
111 stars 23 forks source link

select on multi-dimensional arrays not supported #15

Open bigerl opened 7 years ago

bigerl commented 7 years ago

The current implementation doesn't support select on sparse non-2D-matrices. This limits the usability of 3 and higher dimensional matrices significantly.

The following test code illustrates the problem:

    public void test3DSelect() {
        long n = 1000;
        // init sparse 3D matrix
        SparseMatrix tensor = Matrix.Factory.sparse(ValueType.BOOLEAN, n, n, n);
        tensor.setAsBoolean(true, 1L, 3L, 5L);
        tensor.setAsBoolean(true, 30L, 3L, 5L);
        tensor.setAsBoolean(true, 700L, 3L, 5L);

        // define selections to execute
        String[] selectStrings = new String[]{"*;3;5", "1;*;5", "1;3;*"};

        // select
        for (String selectString : selectStrings) {
            System.out.println("selection: " + selectString);

            // select a 1D vector
            Matrix select = tensor.select(Ret.LINK, selectString); // Exception is thrown here
            Iterable<long[]> result = select.nonZeroCoordinates();
            // take only non-zero values
            Iterator<long[]> resultIt = result.iterator();
            // print result
            while(resultIt.hasNext()){
                long[] entry = resultIt.next();
                for (int i = 0; i < entry.length; i++) {
                    System.out.print(entry[i] + " ");
                }
                System.out.print("\n");
            }
        }
    } 

This code throws a RuntimeException("only supported for 2d matrices") at org.ujmp.core.objectmatrix.calculation.Selection.createAnnotation():82. The whole method createAnnotation() is widely commented out. If I comment line 82 out that throws the exception the both programs runs but returns false results.

With select on sparse 3D+ matrices ujmp would be awesome. :)

holger-arndt commented 7 years ago

I completely agree. However, it's difficult to iterate over multidimensional sparse matrices an efficient way. That's why it's not implemented yet.

There are also many instances where annotation is not modified accordingly when functions are executed which change the size of a matrix.

I believe this issue will remain open for a longer time...