Java Gauss gauss(double[] A, int m, int n)

Here you can find the source of gauss(double[] A, int m, int n)

Description

Performs gaussian elimination on the m by n matrix A.

License

Open Source License

Declaration

public static void gauss(double[] A, int m, int n) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Breakout Cave Survey Visualizer/*from w  w  w.j av  a  2  s  . c om*/
 *
 * Copyright (C) 2014 James Edwards
 *
 * jedwards8 at fastmail dot fm
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *******************************************************************************/

public class Main {
    /**
     * Performs gaussian elimination on the m by n matrix A.
     */
    public static void gauss(double[] A, int m, int n) {
        int i = 0;
        int j = 0;

        while (i < m && j < n) {
            int rowstart = i * n;
            int maxi = i;
            double maxpivot = A[rowstart + j];

            // find the largest pivot in column j
            for (int k = i + 1; k < m; k++) {
                double newpivot = A[k * n + j];
                if (Math.abs(newpivot) > Math.abs(maxpivot)) {
                    maxpivot = newpivot;
                    maxi = k;
                }
            }
            if (maxpivot != 0) {
                int count = n - j;

                // swap the row with the largest pivot with row i
                if (i != maxi) {
                    double[] temp = new double[count];
                    System.arraycopy(A, rowstart + j, temp, 0, count);
                    System.arraycopy(A, maxi * n + j, A, rowstart + j, count);
                    System.arraycopy(temp, 0, A, maxi * n + j, count);
                }

                // divide row i by the pivot value
                for (int k = j; k < n; k++) {
                    A[rowstart + k] /= maxpivot;
                }

                // subtract row i from the rows below
                for (int u = i + 1; u < m; u++) {
                    int rowstart2 = u * n;
                    double multiplier = A[rowstart2 + j];

                    for (int k = j; k < n; k++) {
                        A[rowstart2 + k] -= multiplier * A[rowstart + k];
                    }
                }
                i += 1;
            }
            j += 1;
        }
    }

    /**
     * Performs gaussian elimination on the m by n matrix A. This method is faster than {@link #gauss(double[], int, int)} because it doesn't actually swap
     * rows. Instead of exchanging rows, row_perms is used to mark the positions of the rows in the reduced matrix. Row <code>i</code> of the reduced matrix is
     * row <code>row_perms[ i ]</code> of A.
     */
    public static void gauss(double[] A, int m, int n, int[] row_perms) {
        int i = 0;
        int j = 0;

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

        while (i < m && j < n) {
            int rowstart = row_perms[i] * n;
            int maxi = i;
            double maxpivot = A[rowstart + j];

            // find the largest pivot in column j
            for (int k = i + 1; k < m; k++) {
                double newpivot = A[row_perms[k] * n + j];
                if (Math.abs(newpivot) > Math.abs(maxpivot)) {
                    maxpivot = newpivot;
                    maxi = k;
                }
            }
            if (maxpivot != 0) {
                // swap the row with the largest pivot with row i
                if (i != maxi) {
                    int temp = row_perms[i];
                    row_perms[i] = row_perms[maxi];
                    row_perms[maxi] = temp;

                    rowstart = row_perms[i] * n;
                }

                // divide row i by the pivot value
                for (int k = j; k < n; k++) {
                    A[rowstart + k] /= maxpivot;
                }

                // subtract row i from the rows below
                for (int u = i + 1; u < m; u++) {
                    int rowstart2 = row_perms[u] * n;
                    double multiplier = A[rowstart2 + j];

                    for (int k = j; k < n; k++) {
                        A[rowstart2 + k] -= multiplier * A[rowstart + k];
                    }
                }
                i += 1;
            }
            j += 1;
        }
    }

    /**
     * Performs gaussian elimination on the m by n matrix A. Instead of exchanging rows, row_perms is used to mark the positions of the rows in the reduced
     * matrix. Row <code>i</code> of the reduced matrix is row <code>row_perms[ i ]</code> of A.
     */
    public static void gauss(double[][] A, int[] row_perms) {
        int i = 0;
        int j = 0;

        int m = A.length;
        int n = A.length == 0 ? 0 : A[0].length;

        if (row_perms.length != m) {
            throw new IllegalArgumentException("row_perms.length must equal A.length");
        }

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

        while (i < m && j < n) {
            int maxi = i;
            double maxpivot = A[row_perms[i]][j];

            // find the largest pivot in column j
            for (int k = i + 1; k < m; k++) {
                double newpivot = A[row_perms[k]][j];
                if (Math.abs(newpivot) > Math.abs(maxpivot)) {
                    maxpivot = newpivot;
                    maxi = k;
                }
            }
            if (maxpivot != 0) {
                // swap the row with the largest pivot with row i
                if (i != maxi) {
                    int temp = row_perms[i];
                    row_perms[i] = row_perms[maxi];
                    row_perms[maxi] = temp;
                }

                // divide row i by the pivot value
                for (int k = j; k < n; k++) {
                    A[row_perms[i]][k] /= maxpivot;
                }

                // subtract row i from the rows below
                for (int u = i + 1; u < m; u++) {
                    double multiplier = A[row_perms[u]][j];

                    for (int k = j; k < n; k++) {
                        A[row_perms[u]][k] -= multiplier * A[row_perms[i]][k];
                    }
                }
                i++;
            }
            j++;
        }
    }

    /**
     * Performs gaussian elimination on the m by n matrix A. Instead of exchanging rows, row_perms is used to mark the positions of the rows in the reduced
     * matrix. Row <code>i</code> of the reduced matrix is row <code>row_perms[ i ]</code> of A.
     */
    public static void gauss(float[][] A, int[] row_perms) {
        int i = 0;
        int j = 0;

        int m = A.length;
        int n = A.length == 0 ? 0 : A[0].length;

        if (row_perms.length != m) {
            throw new IllegalArgumentException("row_perms.length must equal A.length");
        }

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

        while (i < m && j < n) {
            int maxi = i;
            float maxpivot = A[row_perms[i]][j];

            // find the largest pivot in column j
            for (int k = i + 1; k < m; k++) {
                float newpivot = A[row_perms[k]][j];
                if (Math.abs(newpivot) > Math.abs(maxpivot)) {
                    maxpivot = newpivot;
                    maxi = k;
                }
            }
            if (maxpivot != 0) {
                // swap the row with the largest pivot with row i
                if (i != maxi) {
                    int temp = row_perms[i];
                    row_perms[i] = row_perms[maxi];
                    row_perms[maxi] = temp;
                }

                // divide row i by the pivot value
                for (int k = j; k < n; k++) {
                    A[row_perms[i]][k] /= maxpivot;
                }

                // subtract row i from the rows below
                for (int u = i + 1; u < m; u++) {
                    float multiplier = A[row_perms[u]][j];

                    for (int k = j; k < n; k++) {
                        A[row_perms[u]][k] -= multiplier * A[row_perms[i]][k];
                    }
                }
                i++;
            }
            j++;
        }
    }
}

Related

  1. gauss(double mean, double deviation, double x)
  2. gauss(final double mean, final double sigma, final double x)
  3. gauss(int N, long seed)
  4. GaussElimination(double a[][])
  5. gaussian(double a[][], int index[])