How to add all elements of an ojalgo SparseArray?

Andres Suarez :

I have a SparseArray and need to obtain the sum of all its elements

I'm currently doing a dot product of the array with a same-sized array filled with ones, but I feel there should be something more straightforward (and efficient?)

Can't find a proper method here: https://javadoc.scijava.org/ojAlgo/org/ojalgo/array/SparseArray.html

I have:

SparseArray<Double> lhCounts = SparseArray.factory(Primitive64Array.FACTORY, dim).make();

then do some operations with it, and finally want some method that returns the sum of all elements, something like:

array_sum = lhCounts.sum();
apete :

That class in primarily used as a delegate to other implementation, but it's perfectly fine to use it directly. Probably the most direct solution in this case is to do:

    array_sum = lhCounts.nonzeros().stream().mapToDouble(nz -> nz.doubleValue()).sum();

There is also the possibility to create an Array1D instead. It has a richer api. Then you can do it this way:

    Array1D<Double> lhCounts1D = Array1D.PRIMITIVE64.makeSparse(dim);
    array_sum = lhCounts1D.aggregateAll(Aggregator.SUM);

The nonzeros-stream is also available in this case

    array_sum = lhCounts1D.nonzeros().stream().mapToDouble(nz -> nz.doubleValue()).sum();

and if that "array" is actually something 2-D or N-D you can create an Array2D or ArrayAnyD instead.

    Array2D<Double> lhCounts2D = Array2D.PRIMITIVE64.makeSparse(dim, dim);
    ArrayAnyD<Double> lhCountsAnyD = ArrayAnyD.PRIMITIVE64.makeSparse(dim, dim, dim, dim);

The Array1D, Array2D and ArrayAnyD api:s were designed for dense structures. The ability to instantiate them as sparse was added at a later point. Some things you can do with them may not be very efficient in the sparse case. You can even do plain stupid things like hugeSparseArray.fillAll(1.0);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165552&siteId=1