简体   繁体   English

有效地获取Java 2D数组的row.max和row.sum

[英]Efficiently get the row.max and row.sum of a Java 2D array

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? 在Java中,给定double值的2D数组,dim为6000 * 6000,是否有一种有效的方法来查询行最大值和行总和?

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 [] []和两层循环来获取行的最大值和总和,但效率不高,因为经常调用此函数。

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? 有没有提供更有效解决方案的Java库? Is there any Java library that is similar to Matrix class in Python or R? 是否有任何类似于Python或R中的Matrix类的Java库?

Thanks! 谢谢!

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). 使用(例如)一个HashMap<Integer, CacheEntry> (其中CacheEntry表示总和和最大值),然后使用它来懒惰地缓存每行所需的值(由键索引)。

(Or some variation on the implementation of the above.) (或上述实现的一些变体。)


Is there any Java library that provides more efficient solutions? 有没有提供更有效解决方案的Java库? Is there any Java library that is similar to Matrix class in Python or R? 是否有任何类似于Python或R中的Matrix类的Java库?

Not to my knowledge. 据我所知。 Certainly, not in the standard Java class libraries. 当然,不是在标准Java类库中。

However, if you use eager or lazy caching, you should not need a library ... for this problem. 但是,如果您使用急切延迟缓存,则不需要库...来解决此问题。

I don't know if more efficient but way shorter using Stream . 我不知道是否更有效,但使用Stream更短。 Here is a demo using 4x4 array : 这是一个使用4x4数组的演示:

    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;
        }
    }
// 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];
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM