Java Gauss GaussElimination(double a[][])

Here you can find the source of GaussElimination(double a[][])

Description

Implements a Gaussian elimination on the given matrix.

License

Open Source License

Parameter

Parameter Description
a the matrix to be solved.

Declaration

static double[] GaussElimination(double a[][]) 

Method Source Code

//package com.java2s;
//  License as published by the Free Software Foundation; either

public class Main {
    /**//from w  w w .  j av  a  2  s . c  om
     * Implements a Gaussian elimination on the given matrix. The last column
     * being the Right Hand Side values. All rows in the matrix are used.
     * @param a the matrix to be solved.
     */
    static double[] GaussElimination(double a[][]) {
        int n = a.length;

        return GaussElimination(n, a);
    }

    /**
     * Implements a Gaussian elimination on the given matrix. The matrix
     * <code>a</code> should be n rows by n+1 columns. Column <code>n+1</code>
     * being the Right Hand Side values.
     * @param n the number of rows in the matrix.
     * @param a the matrix to be solved.
     */
    static double[] GaussElimination(int n, double a[][]) {
        // Forward elimination
        for (int k = 0; k < n - 1; k++) {
            for (int i = k + 1; i < n; i++) {
                double qt = a[i][k] / a[k][k];
                for (int j = k + 1; j < n + 1; j++)
                    a[i][j] -= qt * a[k][j];

                a[i][k] = 0.0;
            }
        }

        /*
        // DEBUG
        for ( int i=0; i<n; i++ )
        for ( int j=0; j<n+1; j++ )
        System.out.println( "After forward elimination, a["+i+"]["+j+"]="+a[i][j] );
        */

        double x[] = new double[n];

        // Back-substitution
        x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
        for (int k = n - 2; k >= 0; k--) {
            double sum = 0.0;
            for (int j = k + 1; j < n; j++)
                sum += a[k][j] * x[j];

            x[k] = (a[k][n] - sum) / a[k][k];
        }

        /*
        // DEBUG
        for ( int k=0; k<n; k++ )
        System.out.println( "After back-substitution, x["+k+"]="+x[k] );
        */

        return x;
    }
}

Related

  1. gauss(double mean, double deviation, double x)
  2. gauss(double[] A, int m, int n)
  3. gauss(final double mean, final double sigma, final double x)
  4. gauss(int N, long seed)
  5. gaussian(double a[][], int index[])
  6. gaussian(double mu, double sigma, double x)
  7. gaussian(double mu, double sigma, double x)
  8. gaussian(double t)