javasparse-matrixojalgo

How to find the index of maximum element in ojalgo?


How to find the index of maximum element in ojalgo?

To find maximum I can use double res = mat.aggregateAll(Aggregator.MAXIMUM);, but how to find the maximum's indices?

PS: the mat is type of SparseStore<Double>


Solution

  • You don't specify what type mat is?

    There is a method indexOfLargest() defined in the Access1D.Aggregatable interface – implemented by a lot of different classes. But, this corresponds to Aggregator.LARGEST rather than Aggregator.MAXIMUM.

    Since you tagged the question with sparse-matrix I assume the type is SparseStore, and that could be problematic, using indexOfLargest(), as I believe its implementation of that method does not exploit sparsity. (It's just some inherited default method that would perform terribly on a sparse matrix.)

    You can, quite efficiently, iterate over all nonzero elements in a SparseStore:

        for (ElementView2D<N, ?> nz : mat.nonzeros()) {
            nz.row();
            nz.column();
            nz.index();
            nz.doubleValue();
        }