0

In Java, given a 2D array of double values with dim as 6000*6000, is there an efficient way to query the row max and the row sum?

I am using the data structure double[][] and a two-layer loop to get the row max and sum, but it is not sufficiently efficient, as this function is called frequently.

double MinRowMax = Double.POSITIVE_INFINITY;
int num = 6000;
double[][] array2DDist = new double[num][num];
Random rand = new Random();

// initialising the array2DDist
for(int i=0;i<num;++i)
    for(int j=0;j<num;++j)
        array2DDist[i][j] = rand.nextDouble();

// get the row.max and row.sum
for(int i=0;i<num;++i) {
    double maxDist = Double.NEGATIVE_INFINITY;
    double sumDist = 0;
    for(int j=0;j<num;++j) {
        double dist = array2DDist[i][j];
        maxDist = Double.max(maxDist, dist);
        sumDist+=dist;
    }
    if(maxDist < MinRowMax) {
        MinRowMax = maxDist;
    }
}

Is there any Java library that provides more efficient solutions? Is there any Java library that is similar to Matrix class in Python or R?

Thanks!

Huanfa Chen
  • 577
  • 6
  • 15

3 Answers3

1

To compute the sum of an array, or largest value in an array, you have to visit every element of the array. You cannot speed that up.

However, if the array is not going to change, and you are going to need the sum and max for the array multiple times, then you can compute them once and then look them up. There are two approaches:

  • Compute the required values for all rows of your 2-D array at the start and store them in a lookup table. This is a form or eager cache.

  • Use (say) a HashMap<Integer, CacheEntry> (where CacheEntry represents the sum and max), and then use this to lazily cache the required values for each row (indexed by the key).

(Or some variation on the implementation of the above.)


Is there any Java library that provides more efficient solutions? Is there any Java library that is similar to Matrix class in Python or R?

Not to my knowledge. Certainly, not in the standard Java class libraries.

However, if you use eager or lazy caching, you should not need a library ... for this problem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I don't know if more efficient but way shorter using Stream. Here is a demo using 4x4 array :

    double MinRowMax = Double.POSITIVE_INFINITY;
    int num = 4;
    double[][] array2DDist = new double[num][num];
    Random rand = new Random();

    // initializing the array2DDist
    for(int i=0;i<num;++i) {
        for(int j=0;j<num;++j) {
            array2DDist[i][j] = rand.nextDouble();
        }
    }

    // get the row.max and row.sum
    for(int row=0;row<num;++row) {

        double maxDist = Double.NEGATIVE_INFINITY;
        double sumDist = 0;

        for(int col=0;col<num;++col) {

            double dist = array2DDist[row][col];
            maxDist = Double.max(maxDist, dist);
            sumDist+=dist;
        }

        //System.out.println(Arrays.toString(array2DDist[row]));
        System.out.println("row sum - max " + sumDist +" - " + maxDist);
        System.out.println("row sum - max " + Arrays.stream(array2DDist[row]).parallel().sum()
                +" - " + Arrays.stream(array2DDist[row]).parallel() .max().getAsDouble());

        if(maxDist < MinRowMax) {
            MinRowMax = maxDist;
        }
    }
c0der
  • 18,467
  • 6
  • 33
  • 65
0
// Programme to get sum of rows value and column values seprately.

    int[] colSum =new int[array[0].length];
    for (int i = 0; i < array.length; i++){   
        for (int j = 0; j < array[i].length; j++){                
            sum += array[i][j];
            colSum[j] += array[i][j];
        }
        System.out.println("Print the sum of rows =" + sum);
    }  
    for(int k=0;k<colSum.length;k++){
        System.out.println("Print the sum of columns =" + colSum[k]);
    } 


// Programme to get maximum in 2D array.

map<int, int> temp;
int currentMax= -999999,maxCount=0;
for(i=0; i< numberOflines ;i++)
{
        for(j=0;j< array[i].length;j++)
        {
            int newCount = ++temp[array[i][j]];
            if (maxCount < newCount) {
                 maxCount = newCount;
                 currentMax = array[i][j];
            }
        }
}
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35