高斯消除Java

时间:2013-11-12 21:57:14

标签: java matrix gaussian

我有这个示例矩阵:

[4,1,3]
[2,1,3]
[4,-1,6]

我想解决exuotions:

4x1+1x2+3x3=v
2x1+1x2+2x3=v
4x1-1x2+6x3=v

x1+x2+x3=1

它将是:4x1 + 1x2 + 3x3 = 2x1 + 1x2 + 2x3 = 4x1-1x2 + 6x3

-2x1 + x2-5x3 = 0

我使用代码:

import java.util.*;

public class GaussianElimination {

    // This is the problem we solved in class
    private static double[][] problem1 = {
        // x = 1, y = 2, z = 3
        { 1,  2, 3, 14 },  // 1x + 2y + 3z = 14
        { 1, -1, 1,  2 },  // 1x - 1y + 1z = 2
        { 4, -2, 1,  3 }   // 4x - 2y + 1z = 3
    };



    public static void solve(double[][] c, int row) {
        int rows = c.length;
        int cols = rows + 1;
        // 1. set c[row][row] equal to 1
        double factor = c[row][row];
        for (int col=0; col<cols; col++)
            c[row][col] /= factor;

        // 2. set c[row][row2] equal to 0
        for (int row2=0; row2<rows; row2++)
            if (row2 != row) {
                factor = -c[row2][row];
                for (int col=0; col<cols; col++)
                    c[row2][col] += factor * c[row][col];
            }
    }

    public static void solve(double[][] c) {
        int rows = c.length;
        for (int row=0; row<rows; row++)
            solve(c,row);
    }

    public static void print(double[][] c) {
        int rows = c.length;
        int cols = rows + 1;
        for (int row=0; row<rows; row++) {
            for (int col=0; col<cols; col++)
                System.out.printf("%5.1f ",c[row][col]);
            System.out.println();
        }
        System.out.println();
    }

    public static void printSolution(double[][] c) {
        int rows = c.length, cols = rows + 1;
        char variable = (char)((rows > 3) ? ('z' - (rows-1)) : 'x');
        System.out.println("Solution:\n");
        for (int row=0; row<rows; row++)
            System.out.printf("  %c = %1.1f\n",(char)variable++,c[row][cols-1]);
        System.out.println();
    }

    public static void doProblem(double[][] problem, String description) {
        System.out.printf("******* %s ********\n",description);
        System.out.println("Original Equations:");
        print(problem);
        solve(problem);
        System.out.println("Solved (reduced row echelon form):");
        print(problem);
        printSolution(problem);
    }

    public static void main(String[] args) {
        doProblem(problem1,"Problem 1 (from class)");

    }
}

如何在private static double[][] problem1中设置矩阵,以便得到x1,x2,x3?

2 个答案:

答案 0 :(得分:1)

我真的不明白你的问题或问题。但是我在行减少梯队形式求解方法中看到了一些错误。我最近也写了这个方法。我的工作。因为我不怀疑这是一个Java家庭作业,而是对编程数学算法的兴趣,我将抛出我的代码。我建议看看如何在数学世界中实际定义rref方法。

我发现的错误是您使用的factor错误。看看我的代码(请注意,它不会在矩阵的底部放置零行):

public static double[][] rref(double[][] mat)
{
    double[][] rref = new double[mat.length][mat[0].length];

    /* Copy matrix */
    for (int r = 0; r < rref.length; ++r)
    {
        for (int c = 0; c < rref[r].length; ++c)
        {
            rref[r][c] = mat[r][c];
        }
    }

    for (int p = 0; p < rref.length; ++p)
    {
        /* Make this pivot 1 */
        double pv = rref[p][p];
        if (pv != 0)
        {
            double pvInv = 1.0 / pv;
            for (int i = 0; i < rref[p].length; ++i)
            {
                rref[p][i] *= pvInv;
            }
        }

        /* Make other rows zero */
        for (int r = 0; r < rref.length; ++r)
        {
            if (r != p)
            {
                double f = rref[r][p];
                for (int i = 0; i < rref[r].length; ++i)
                {
                    rref[r][i] -= f * rref[p][i];
                }
            }
        }
    }

    return rref;
}

答案 1 :(得分:0)

以下代码adapted from Rosettacode.org也考虑了向上/向下移动行:

static public void rref(double [][] m)
{
    int lead = 0;
    int rowCount = m.length;
    int colCount = m[0].length;
    int i;
    boolean quit = false;

    for(int row = 0; row < rowCount && !quit; row++)
    {
        print(m);
        println();

        if(colCount <= lead)
        {
            quit = true;
            break;
        }

        i=row;

        while(!quit && m[i][lead] == 0)
        {
            i++;
            if(rowCount == i)
            {
                i=row;
                lead++;

                if(colCount == lead)
                {
                    quit = true;
                    break;
                }
            }
        }

        if(!quit)
        {
            swapRows(m, i, row);

            if(m[row][lead] != 0)
                multiplyRow(m, row, 1.0f / m[row][lead]);

            for(i = 0; i < rowCount; i++)
            {
                if(i != row)
                    subtractRows(m, m[i][lead], row, i);
            }
        }
    }
}

// swaps two rows
static void swapRows(double [][] m, int row1, int row2)
{
    double [] swap = new double[m[0].length];

    for(int c1 = 0; c1 < m[0].length; c1++)
        swap[c1] = m[row1][c1];

    for(int c1 = 0; c1 < m[0].length; c1++)
    {
        m[row1][c1] = m[row2][c1];
        m[row2][c1] = swap[c1];
    }
}

static void multiplyRow(double [][] m, int row, double scalar)
{
    for(int c1 = 0; c1 < m[0].length; c1++)
        m[row][c1] *= scalar;
}

static void subtractRows(double [][] m, double scalar, int subtract_scalar_times_this_row, int from_this_row)
{
    for(int c1 = 0; c1 < m[0].length; c1++)
        m[from_this_row][c1] -= scalar * m[subtract_scalar_times_this_row][c1];
}

static public void print(double [][] matrix)
{
    for(int c1 = 0; c1 < matrix.length; c1++)
    {
        System.out.print("[ ");

        for(int c2 = 0; c2 < matrix[0].length-1; c2++)
            System.out.print(matrix[c1][c2] + ", ");

        System.out.println(matrix[c1][matrix[c1].length-1] + " ]");
    }
}

static public void println()
{
    System.out.println();
}