简体   繁体   English

递归数组方法在Java中返回0

[英]Recursive array method returning 0 in Java

I have taken the tip I got from my last question and went deep into debugging my code. 我已经从上一个问题中得到了提示,并深入调试了我的代码。 I'm still trying to make a method that takes two multidimensional arrays n*n and multiplies them, as if they were matrices. 我仍在尝试制作一种方法,将两个多维数组n * n乘以它们,就好像它们是矩阵一样。 But I have encountered an odd obstacle. 但是我遇到了一个奇怪的障碍。 This is my code: 这是我的代码:

public class subMatrix {

    public int[][] divcon(int[][] a, int[][] b, int sub) {

        int[][] matrixC = new int[a.length][a.length];

        if (sub == 1) {
            matrixC[0][0] = a[0][0] * b[0][0];

        } else {
            sub = sub / 2;

            int[][] smalla11 = new int[sub][sub];
            int[][] smalla12 = new int[sub][sub];
            int[][] smalla21 = new int[sub][sub];
            int[][] smalla22 = new int[sub][sub];
            int[][] smallb11 = new int[sub][sub];
            int[][] smallb12 = new int[sub][sub];
            int[][] smallb21 = new int[sub][sub];
            int[][] smallb22 = new int[sub][sub];
            int[][] smallc11 = new int[sub][sub];
            int[][] smallc12 = new int[sub][sub];
            int[][] smallc21 = new int[sub][sub];
            int[][] smallc22 = new int[sub][sub];

            for (int i = 0; i < sub; i++) {
                for (int j = 0; j < sub; j++) {

                    smalla11[i][j] = a[i][j];
                    smalla12[i][j] = a[sub + i][j];
                    smalla21[i][j] = a[i][sub + j];
                    smalla22[i][j] = a[sub + i][sub + j];

                    smallb11[i][j] = b[i][j];
                    smallb12[i][j] = b[sub + i][j];
                    smallb21[i][j] = b[i][sub + j];
                    smallb22[i][j] = b[sub + i][sub + j];

                }
            }
            smallc11 = addMatrix(divcon(smalla11, smallb11, sub), divcon(smalla12, smallb21, sub), sub);
            smallc12 = addMatrix(divcon(smalla11, smallb12, sub), divcon(smalla12, smallb12, sub), sub);
            smallc21 = addMatrix(divcon(smalla21, smallb11, sub), divcon(smalla22, smallb21, sub), sub);
            smallc22 = addMatrix(divcon(smalla21, smallb12, sub), divcon(smalla22, smallb22, sub), sub);
        }
        return matrixC;
    }

    public int[][] addMatrix(int[][] aadd, int[][] badd, int size) {
        int[][] c = new int[size][size];

        for (int d = 0; d < size; d++) {
            for (int e = 0; e < size; e++) {

                c[d][e] = aadd[d][e] + badd[d][e];
            }
        }
        return c;
    }
}

The matrices a and b are turned into quarters. 矩阵a和b变成四分之一。 I have debugged my code with two matrice inputs 4x4 with the value 2 in each field. 我已经使用两个矩阵输入4x4(每个字段中值为2)调试了我的代码。 Until a certain point, things are going the right way. 直到某个时候,事情都朝着正确的方向发展。 But if I add a System.out.println() to see the return values from smallc11 , 12, 21 and 22, it sometimes shows as 0. How come? 但是,如果我添加System.out.println()来查看smallc11和22的返回值,则有时会显示为0。为什么呢? I'm never inputting any 0, and the arrays should not go lower than 1 in length which is 2. 我从不输入任何0,并且数组的长度不应小于1,即2。

You are returning matrixC but you are not altering it anywhere, so it will be full of 0 . 您正在返回matrixC但未在任何地方进行更改,因此它将充满0

You can copy the results from all your sub-matrixes, but the simpler thing to do is not create them in the first place. 您可以复制所有子矩阵的结果,但是最简单的方法不是首先创建它们。 Just add all the elements of the two original matrixes together will be simpler and faster. 只需将两个原始矩阵的所有元素加在一起,就会更简单,更快。

Something like 就像是

public double[][] multiply(double[][] a, double[][] b) {
     assert a[0].length == b.length;
     double[][]c = new double[a.length][b[0].length];
     for(int i = 0, len1 = a.length; i < len1; a++) {
         for(int j = 0, len2 = b[0].length; j < len2; j++) {
              double sum = 0.0;
              for(int k = 0, len3 = b.length; k < len3; k++)
                  sum += a[i][k] * b[k][j];
              c[i][j] = sum;
         }
     }
}

The reason this is much faster is the cost of creating new objects and copying values exceeds the cost of the * and + . 之所以快得多,是因为创建新对象和复制值的成本超过了*+的成本。

You can optimise this further by transposing b first to improve cache friendliness. 您可以通过首先移调b来进一步优化此效果,以提高缓存友好性。

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

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