1
votes

I have this example matrix:

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

and i want to solve exuotions:

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

x1+x2+x3=1

it will be: 4x1+1x2+3x3 = 2x1+1x2+2x3 = 4x1-1x2+6x3

-2x1+x2-5x3 =0

and I use the code:

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

    }
}

How do I set the matrix in private static double[][] problem1 so that I get x1,x2,x3?

2

2 Answers

1
votes

I don't really understand your question or problem. However I see some bugs in the row reduction echelon form solving method. I recently wrote this method as well. Mine works. Since I don't suspect this to be a Java homework assignment but rather an interest in programming mathematical algorithms, I will just throw in my code. I recommend taking a look at how the rref method is actually defined in the world of maths.

The bug I spotted is that the factor you use is wrong. Take a look at my code (note that it doesn't put zero rows to the bottom of the matrix):

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

The following code adapted from Rosettacode.org takes into account moving rows up/down as well:

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