Solving systems of linear equations

In the Java Apache Commons Math library there is a package, linear, that is about Linear Algebra. There we could find support to manage systems of linear equation.

A very simple linear system, as shown in the above quoted wikipedia page, features two equations and two variables:
2x + 3y = 6
4x + 9y = 15
Let's solve it through Apache:
RealMatrix coeffs = new Array2DRowRealMatrix(
        new double[][] {{2, 3}, {4, 9}}, false ); // 1
DecompositionSolver ds = new
        LUDecompositionImpl(coeffs).getSolver(); // 2

RealVector consts = new ArrayRealVector(
        new double[] {6, 15}, false ); // 3
RealVector sol = ds.solve(consts); // 4
System.out.println(sol);
1. We create an Array2DRowRealMatrix object passing to it our coefficients as a bidimensional array of double. The second parameter, set to false, says to the ctor not to bother making a local copy of the array, but use it directly. We won't care of the actual matrix type, so we store the result in the interface RealMatrix, root of a hierarchy of matrices including also an implementation for sparse matrix (OpenMapRealMatrix).
2. We should decide which algorithm use to solve the system. Here the chosen one is the LU decomposition. See the Apache documentation for the available alternatives (Cholesky, QR ...). Notice that here we need just to get a solver from the decompositor, so it is created and then forgotten at the end of the same line. But its solver survives and it is kept as a DecompositionSolver interface.
3. Very similarly to (1), we create an ArrayRealVector object for the constants, without copying the double raw array created on the fly, and keep it as a RealVector interface.
4. Calling DecompositionSolver.solve() for the constants vector we get a vector containing the solution. If there is no solution we get an exception instead. So it would be better to try/catch all this code (also the Array2DRowRealMatrix ctor throws unchecked exceptions) to avoid unpleasant surprises. It is easy to make this code crashing. Just change the coefficients and constants so that the system has no solution - just think to the geometrical interpretation of this system, remember that there is no intersection between parallel lines, and try to solve a system where:
RealMatrix coeffs = new Array2DRowRealMatrix(
        new double[][] {{2, 3}, {4, 6}}, false );
RealVector consts = new ArrayRealVector(
        new double[] {6, 12}, false );
You will get instead of a solution a SingularMatrixException.

We can extract from a RealVector the underlying raw double array:
double[] dv = sol.getData();
for(double d : dv)
    System.out.print(d + " ");
System.out.println();
But we can even get rid of all the RealVector objects above, and use directly just raw arrays:
// ...
double[] consts = new double[] {6, 15};
double[] sol = ds.solve(consts);

for(double d : sol)
    System.out.print(d + " ");
System.out.println();

3 comments:

  1. I am not getting right result for large number like:

    double [][]matrixPoint= new double[][]{{1,80,6400,512000,4.096*Math.pow(10, 7)},{1,100,10000,1000000,1.0*Math.pow(10, 8)},{1,120,14400,1728000,2.073*Math.pow(10, 8)},{1,160,25600,4096000,6.553*Math.pow(10, 8)},{1,200,40000,8000000,1.6*Math.pow(10, 9)}};
    double [] matrixVector=new double[]{300,350,300,350,250};

    Is there any restriction?

    Thanks
    Rakesh

    ReplyDelete
    Replies
    1. Hi Rakesh, usual caveat applies.

      Have a look at this project for details: code.google.com/p/java-matrix-benchmark

      Delete
  2. I have three equations like the following ones:
    x + y + z = 100;
    x + y - z = 50;
    x - y - z = 10;
    How can I find the values of x, y, and z with Java?
    String equation1="x+y+z=100;";
    String equation2="x+y-z=50;";
    String equation3="x-y-z=10;";

    int[] SolveEquations(equation1,equation2,equation3) {
    // to do
    // how to do?
    }
    Do you have any possible solutions or other common frameworks?




    Labels: Java Codes Linear Equations Apache Commons Math

    ReplyDelete