Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,772 people online right now. Registration is fast and FREE... Join Now!




Reduced Echelon Form

 
Reply to this topicStart new topic

Reduced Echelon Form

Cobra3232
10 Apr, 2007 - 12:57 PM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
I am trying to write a program to take a 3X4 matrix and change it to reduced echelon form using Gauss-Jordan elimination. I have worked it out by pencil and have the correct answers, but im stumped on how to go about writing the program. I have no examples in my book at all and everything I read seems way too long or confusing to be helpful. Every other example program i see has no numbers and tons of weird functions defined that i have never seen or heard of. Can anybody get me on the right track or get me going. Im as much of a beginner as they come. Thanks.

l 30 40 50 30500 l
l 80 70 70 55000 l
l 32 25 20 19000 l


That's what the matrix looks like with the answers:
x1 = 80
x2 = 70
x3 = 70

This post has been edited by Cobra3232: 10 Apr, 2007 - 04:26 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Reduced Echelon Form
10 Apr, 2007 - 09:35 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE
reduced echelon form using Gauss-Jordan elimination


well I don't like mathematical calculations much but if you put the steps for this then I think we can solve it.

And it would be even better if you put your code [whatever you did till now.] that will help us.
User is online!Profile CardPM
+Quote Post

NickDMax
RE: Reduced Echelon Form
10 Apr, 2007 - 09:41 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well... the good news is that the algorithm is not all that hard conceptually. The bad news is that it is rather long and involved.

The first thing you need to do is decide on a data format for your matrix... an NxM array works nicely. Then work out a few functions to do row manipulations. That is swap two rows, multiply a row by a constant, add 1 row to another, and add a multiple of one row to another row (this can also be used to make the "subtract a row" by using -1 as a multiplier). Note that that it is possible to make all of these function using only the "multiply by a constant" and "add two rows" though you may wish to make each funciton explicitly.

Once you have the functions then work on converting the logic of the GJ elimination.

so, to get your started. Lets look at a function for adding two rows of an N by M array:
CODE

#include <iostream>

using std::cout;
using std::endl;

typedef struct
{
    int Rows;
    int Cols;
    int *Mat; //array to hold the matrix... Cell(col, row) = Mat[col+row*Cols]
} Matrix;

bool addRow(Matrix Mat, int row1, int row2);
void printMat(Matrix Max);

int main()
{
    Matrix m;
    m.Mat = new int(4*5); //rows * cols
    m.Rows=4;
    m.Cols=5;
    
    //hard coded matrix...
    // | 1  2  3  4  5|
    // | 2  2  2  2  2|
    // | 2  4  6  8 10|
    // |-1 -2 -3 -4 -5|
    m.Mat[0+0]=1;  m.Mat[1+0]=2;  m.Mat[2+0]=3;  m.Mat[3+0]=4;  m.Mat[4+0]=5;
    m.Mat[0+5]=2;  m.Mat[1+5]=2;  m.Mat[2+5]=2;  m.Mat[3+5]=2;  m.Mat[4+5]=2;
    m.Mat[0+10]=2; m.Mat[1+10]=4; m.Mat[2+10]=6; m.Mat[3+10]=8; m.Mat[4+10]=10;
    m.Mat[0+15]=-1;m.Mat[1+15]=-2;m.Mat[2+15]=-3;m.Mat[3+15]=-4;m.Mat[4+15]=-5;
    
    printMat(m);

    addRow(m, 2, 3); //note that in the computer the rows are 0 to 3, to humans they would be 1 to 4.
    cout << "Added row 4 to row 3\n";
    printMat(m);

    addRow(m, 0, 2);
    cout << "Added row 3 to row 1\n";
    printMat(m);

    addRow(m, 3, 0);
    cout << "Added row 1 to row 4\n";
    printMat(m);


    return 0;
}

//Row1 <-- Row1 + Row2
bool addRow(Matrix Mat, int row1, int row2)
{
    //lets assume that the function fails, so our return value is flase.
    bool retValue = false; //Value to be passed by return

    //First lets make sure the user of this function is using it right...
    if ((row1 < Mat.Rows && row1 >= 0) && (row2 < Mat.Rows && row2 >= 0))
    {
        //Ok the user knows what they are doing.
        int i; // counter...
        
        //The next two lines help simplify calculations inside the loop.
        int row1Ptr = row1*Mat.Cols; //Offset to the begining of row1.
        int row2Ptr = row2*Mat.Cols; //Offset to the begining of row2.

        
        //We need to scroll though the
        for (i=0; i<Mat.Cols; ++i)
        {
            //For each cell in row1, add in the corisponding cell in row2.
            Mat.Mat[i+row1Ptr] += Mat.Mat[i + row2Ptr];
        }
        //That should do it...
        retValue = true;
    }
    
    return retValue;
}

void printMat(Matrix Mat)
{
    int i, j;
    for (i=0; i<Mat.Rows; ++i)
    {
        for (j=0; j<Mat.Cols; ++j)
        {
            cout << Mat.Mat[j+i*Mat.Cols] << '\t';
        }
        cout << '\n';
    }
    cout << endl;
    return;
}


Extending that to the other functions is not hard. I would note that this would be much better implemented in a class rather than with a struct. I only wanted to offer an example.


User is offlineProfile CardPM
+Quote Post

Cobra3232
RE: Reduced Echelon Form
11 Apr, 2007 - 05:52 AM
Post #4

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 12


My Contributions
OK...after reviewing that example...it makes more sense to me. I understand that i have to define each individual function i use to solve the system of equartions. For example, you just solved the "add a row to another row" correct? So i need to create the rest of them to cover all possible cases of addition, subtraction, multiplication....and so forth right? The next question i have is do i need to solve the matrix so it is in redcued row echelon form after i have defined these functions? Or does the program solve it for me because i have told it exactly how the matrix is supposed to be? I want to thank you in advance for your help. I am a beginnner, but i feel like what im being asked to do requires a little more knowledge than what i been taught. Thanks.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Reduced Echelon Form
11 Apr, 2007 - 07:21 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well just defining the functions will not reduce the matrix, you actually have to define a function to do that too. The good news is that once you have all of the row operations, the process of GJ-elimination is easily automated, meaning that it follows an easy pattern that will solve EVERY REDUCABLE matrix.

Step 1, Make Mat[0][0] a 1, you do this by multiplying row 0 but 1/Mat[0][0] (which points out that the matrix will have to be doubles not ints).

Step 2, zero all Mat[i][0] for i>0... this is easy, use addMultiple() to add a multiple of row 1 to each row below 1 (might look like: addMultiple(Mat, i, 1, -Mat[i][0])) that is -1 * Mat[i][0] is your multiple, so that it subtracts Max[i][0] from itsself (leaving 0).

Now you have a choice, you can make your program recusive (this makes for easier logic for the GJ-Elim function, but is harder over all) or you can you can continue on iterativly, modifying step 1 & 2 to work in a loop for Mat[1][1], then Mat[2][2] etc...

For the recursive method you need to take your NxM matrix and make it an (N-1)x(M-1) matrix by removing Row[0] and Collum[0], then just recall you function which again makes NewMat[0][0] = 1 and NewMat[i][0]=0. The problem here is putting the matrix back together once you are done.

For the interative method, just add step 1 and step 2 to a loop.

After you are done with Steps 1 and 2 all the way to Mat[N][N] then you can either have the program calculate the solutions directly (This is actually the faster way and if you are doing this for large arrays here is where you want to stop), Or you can now make Mat[i][j]=0 for all i>j. This leaves you with the right side of the matrix an NxN identity matrix, the Left most column the solution for the system of equations.

As I said, the Algorithm is not hard, but coding it can be tedious.

Note that the algorithm I discribed is not complete if you are working with multiple solutions. That is if you expect a row of zeros. This algorithm will have to make row swaps to ensure that the zero rows all end up at the bottom of the matrix.

Make sure you can do it on paper. Then do it in code.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month