Jama y Commons Math no dan el resultado deseado para una matriz relativamente grande

I am using Jama and Apache Commons Math to solve linear algebra equations. They both work fine when the matrix is small (4x4). But not when it is relatively big (25x25). Below are my implemenations:

Jama:

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

Esto funciona bien para:

    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);

But it fails for:

    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);
    }

Esto funciona bien para:

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

But fails for:

    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} ,

    };

What I mean by "fails" is that, I know there is at least one solution available which has all integer values but my implementation gives me fraction values.

preguntado el 04 de julio de 12 a las 06:07

I suggest you put the claimed solution into Jama and Common Maths and check that it is indeed a solution. It seems unlikely that two different programs make the same mistake. -

Thanks @Jitse Niesen.. I'm sure it is one of the solutions. So if I put it back it will prove to be a solution. But I know there is one solution with all integer values. Can I force Jama or Commons Math to get that solution or get all possible solutions? -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.