链矩阵乘法:乘法算法不起作用

时间:2017-04-13 21:02:16

标签: java algorithm matrix data-structures

我实现了链矩阵乘法。虽然我得到了正确的链序,但在进行实际乘法时,给定的乘法算法无法正常工作。我的Java代码: -

package algopackage;
import java.util.ArrayList;
import java.util.List;

public class ChainMatrixMultiplication {

    static int s[][];

    static int x[][];

    static int y[][];

    public ChainMatrixMultiplication() {
    }

    static void chainMatrixMultiplication(int[] p) {
        boolean isFirst = false;
        int n = p.length-1;
        int m[][] = new int[n][n];
        s = new int[n][n];
        for (int c = 0; c < n; c++) 
            m[c][c] = 0; 
        for (int counter = 1; counter < n; counter++) {
            for (int i = 0;i < n; i++) {
                isFirst = false;
                int j = i + counter;
                for (int k = i; k < j && j < n; k++) {
                    int result = m[i][k] + m[k+1][j] + p[i] * p[k+1] * p[j+1];
                    if (!isFirst) {
                        m[i][j] = result;
                        s[i][j] = k;
                        isFirst = true;
                    } else if (m[i][j] > result) {
                        m[i][j] = result;
                        s[i][j] = k;
                    }
                }
            }
        }
    }

    static int[][] matrixMultiply(List<Matrix> matrices, int s[][], int i ,int j) {
        if (i ==j) return matrices.get(i).getMatrix();
         else {
             int k = s[i][j];
             x = matrixMultiply(matrices, s, i, k);
             y = matrixMultiply(matrices, s, k+1, j);
             return mult(x,y);
        }
    }

    static int[][] mult(int[][] x, int[][] y) {
        int [][] result = new int[x.length][y[0].length];

        /* Loop through each and get product, then sum up and store the value */
        for (int i = 0; i < x.length; i++) { 
            for (int j = 0; j < y[0].length; j++) { 
                for (int k = 0; k < x[0].length; k++) { 
                    result[i][j] += x[i][k] * y[k][j];
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
        int p[] = {5,4,6,2};
        List<Matrix> matrices = new ArrayList<Matrix>();
        Matrix m1 = new Matrix(5,4);
        int arr[] = {1,2,40,2,3,29,10,21,11,120,23,90,24,12,11,1,11,45,23,21};
        m1.addElementsToMatrix(5, 4, arr);
        Matrix m2 = new Matrix(4,6);
        int arr1[] = {1,1,1,2,3,12,12,3,10,12,12,29,22,11,22,11,11,11,13,1,2,12,4,2};
        m2.addElementsToMatrix(4, 6, arr1);
        Matrix m3 = new Matrix(6,2);
        int arr2[] = {1,1,12,3,22,11,13,1,2,12,12,12};
        m3.addElementsToMatrix(6, 2, arr2);
        matrices.add(m1);
        matrices.add(m2);
        matrices.add(m3);
        chainMatrixMultiplication(p);
        matrixMultiply(matrices,s,0,2);
    }

}

class Matrix {

       private final int[][] matrix;

       public Matrix(int rows, int cols) {
            this.matrix = new int[rows][cols];
       }

       public void addElementsToMatrix(int rows, int cols, int[] arr) {
           int counter = 0;
           for (int i = 0; i < rows; i++) {
               for (int j = 0; j < cols; j++) {
                   matrix[i][j] = arr[counter++];
               }
           }

       }

       public int[][] getMatrix() {
           return matrix;
       }
}

在上面的代码matrixMultiply()函数中抛出异常。由于矩阵没有按预期成倍增加,因此导致异常。由于递归行为不符合预期。我在这段代码中遗漏了一些东西。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

我不确定你的代码的正确性,但是当你乘以维数a * b和c * d的矩阵时,查看矩阵乘法输出,你得到一个维数a * d的矩阵改变

for (int k = 0; k < x[0].length; k++) { 

for (int k = 0; k < y[0].length; k++) { 

应解决问题。

答案 1 :(得分:0)

此:

 x = matrixMultiply(matrices, s, i, k);   // call A
 y = matrixMultiply(matrices, s, k+1, j); // call B
 return mult(x,y);

看起来它应该将调用A的结果乘以调用B的结果。但一般来说,它不会。

x不是本地的,呼叫B通常会覆盖它,除非呼叫B立即触及基本情况。

当您将数据传递到“侧面”而不是函数参数或函数返回值时,会发生这种情况。一直避免这种数据流是不合理的,但你应该在这里避免它,特别是因为它甚至不是你想要的。