Jama和Commons Math对于相对较大的矩阵没有给出期望的结果

时间:2012-07-04 05:58:43

标签: java matrix linear-algebra jama apache-commons-math

我正在使用Jama和Apache Commons Math来解决线性代数方程。当矩阵很小(4x4)时,它们都能很好地工作。但不是当它相对较大(25x25)时。以下是我的实施:

贾马:

    public double[][] solve(double[][] startState, double[][] input){
    double[][] answer = new double[1][input.length-1];
    Matrix A = new Matrix(input);
    Matrix B = new Matrix(startState);
    Matrix X = A.solve(B);
    answer = X.getArray();
    return answer;
}

这适用于:

    double[][] startState = {{1},{2},{1},{2}};
    double[][] input = {{1,1,0,0},{1,1,1,0},{0,1,1,1,},{0,0,1,1}};
    double[][] answer = new JamaSolution().solve(startState, input);

但它失败了:

    double[][] startState = { {0},{1},{2},{0},{2}, {0},{2},{1},{1},{0}, {0},{2},{1},{2},{2}, {1},{1},{1},{1},{2}, {1},{2},{1},{0},{0} };
    double[][] input={
             {1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,1,1, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,

             {1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,1,0,0,0, 1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,1,1, 0,0,0,0,1, 0,0,0,0,0, 0,0,0,0,0} ,

             {0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,1,0,0,0, 1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,1, 0,0,0,1,1, 0,0,0,0,1, 0,0,0,0,0} ,

             {0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,1,0,0,0, 1,1,1,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 0,0,0,1,1, 0,0,0,0,1} ,

             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,1,1,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 0,0,0,0,1} ,

    };

Commons Math:

    public void solve(double[] startState, double[][] input){
        RealMatrix coefficients = new Array2DRowRealMatrix(input, false);
        DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
        RealVector constants = new ArrayRealVector(startState, false);
        RealVector solution = solver.solve(constants);
    }

这适用于:

    double[] startState = {1,2,1,2};
    double[][] input = {{1,1,0,0},{1,1,1,0},{0,1,1,1,},{0,0,1,1}};

但是失败了:

    double[] startState = { 0,1,2,0,2, 0,2,1,1,0, 0,2,1,2,2, 1,1,1,1,2, 1,2,1,0,0 };
    double[][] input={
             {1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,1,1, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,

             {1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,1,0,0,0, 1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,1,1, 0,0,0,0,1, 0,0,0,0,0, 0,0,0,0,0} ,

             {0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,1,0,0,0, 1,1,1,0,0, 0,1,0,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,1, 0,0,0,1,1, 0,0,0,0,1, 0,0,0,0,0} ,

             {0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0, 1,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,1,0,0,0, 1,1,1,0,0, 0,0,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0, 0,0,1,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,1, 0,0,0,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 0,0,0,1,1, 0,0,0,0,1} ,

             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,0,0,0,0, 1,1,0,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 1,1,1,0,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,1,1,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,1,1,0} ,
             {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 0,0,0,0,1} ,

    };

我的意思是“失败”是因为,我知道至少有一个解决方案可用,它具有所有整数值,但我的实现给出了分数值。

0 个答案:

没有答案