10 Replies - 2200 Views - Last Post: 16 November 2013 - 08:02 AM Rate Topic: -----

#1 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 12:59 AM

I'm pasting here just a small part of the whole code that converts a matrix to upper triangular form by elementary oprations.
for (j=0;j<n;j++)
    {
        for (i=n-1;i>j;i--)
        {
            for (r=0;r<n;r++)
            {
                a[i][r]=a[i][r]-((a[i][j]/a[i-1][j])*a[i-1][r]);
                
            }
        }
    }
    printf("The operated matrix is\n");
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
            printf("%f\t",a[i][j]);
        printf("\n");
    }


The code compiles correctly but does not give the correct output, for instance if I enter 1,1,1,5 as a[0][0], a[0][1], a[1][0] & a[1][0] then I get 1,1,0,5 (instead of 1,1,0,4) as the output: a[0][0], a[0][1], a[1][0] & a[1][0]

I'm unable to detect why is the control not reaching out to the last element.. I guess it has got something to do with
for (i=n-1;i>j;i--)
but am not sure. pls help..

This post has been edited by Soumikbhat: 15 November 2013 - 01:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Conversion of Matrix to Upper triangular form

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 06:23 AM

How are your variables defined?

What is the value of n in that snippet?

Have you tried printing out the value of a[][] in the first loop?

You should also post the smallest possible complete program that illustrates your problem.

Jim

This post has been edited by jimblumberg: 15 November 2013 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 06:38 AM

Here's the full code..

#include <stdio.h>
#include <stdlib.h>

int main()
{   int i,j,n,r;
    printf ("Enter the order of the mtarix ");
    scanf("%d",&n);
    printf("\n");
    float a[n][n];
    for (i=0;i<=n-1;i++)
    {
        for (j=0;j<=(n-1);j++)
        {
            printf("enter the element of %d th row and %d th column ",i,j);
            scanf("%f",&a[i][j]);


            printf("\n"); }
    }
    printf("The matrix enetred is\n");
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
            printf("%f\t",a[i][j]);
        printf("\n");
    }

    for (j=0;j<n;j++)
    {
        for (i=n-1;i>j;i--)
        {
            for (r=0;r<n;r++)
            {
                a[i][r]=a[i][r]-((a[i][j]/a[i-1][j])*a[i-1][r]);

            }
        }
    }
    printf("The operated matrix is\n");
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
            printf("%f\t",a[i][j]);
        printf("\n");
    }

}


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 06:39 AM

What exactly did you input into this program?

And with that input what output does your program produce and what do you expect your output to be?

Edit: Does your compiler support VLA?


Jim

This post has been edited by jimblumberg: 15 November 2013 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 07:03 AM

View Postjimblumberg, on 15 November 2013 - 06:39 AM, said:

What exactly did you input into this program?


This program aims to take in the order and elements of a square matrix and produce the upper-triangular form of the matrix. What it does is make use of elementary operations to convert the matrix to a form where all elements below the diagonal are 0.

For instance, suppose I input the order as 2 and a[0][0],a[0][1],a[1][0],a[1][1] are inputted as 1,1,1,5, the upper triangular form of the matrix should be 1,1,0,4 (in the order of a[0][0],a[0][1],a[1][0],a[1][1] )... But the output that I get is 1,1,0,5 (in the order of a[0][0],a[0][1],a[1][0],a[1][1]).

Pardon me, but what is VLA? I use Codeblocks GCC.

This post has been edited by Soumikbhat: 15 November 2013 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 08:00 AM

Quote

Pardon me, but what is VLA? I use Codeblocks GCC.

A VLA is a Variable Length Array. These are arrays which have a size as a non compile time constant such as:

  int n;
    printf ("Enter the order of the mtarix ");
    scanf("%d",&n);
    float array[n];

Since the size of array is not a constant this is a VLA. VLA are only guaranteed to be supported by the C99 standard, all other versions of the standard either don't allow (C90) VLA or make there use optional (C11). Also C++ has no support for this concept in any of the current C++ standards.

I really recommend you not use this "feature" since there are several subtle limitations of this type. You should either use malloc/free or statically allocate the arrays with constant values.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 08:03 AM

Oh ok, my compiler supports VLA. I'll try to avoid this in the future. Am still not able to detect the flaw in my logic..
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 11:25 AM

It might be a good idea to put each of your elementary operations into a function. This way you can test and find out whats going wrong. It's also a lot easier to understand and debug if you do this. An example:
void multiplyRowByConstant(int rowId, int size, double constant, double **matrix) {
    for(int i = 0; i < size; i++) {   
        matrix[rowId][i] *= constant;
    }   
}

void swapRows(int rowI, int rowJ, int size, double** array) {
// ...
}

//Probably need a better names 
void addRowToAnotherTimesConstant(int row, int anotherRow, int size, double constant, double** matrix) {
// ...
}


It's also a bit more complicated than you think. What about the zero matix? You can't divide by 0!

Also a good idea to use jimblumberg's idea to not use variable length arrays:

int size = 2;

// allocate memory for a
float** a = malloc(size*sizeof(float*));
int i;
for(i = 0; i < size; i++) {
  a[i] = malloc(size * sizeof(float));
}

//your code


// MUST remember to free allocated memory!
for(i = 0; i < size; i++) {
  free(a[i]);
}
free(a);




This post has been edited by simeesta: 15 November 2013 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 15 November 2013 - 08:45 PM

Quote

It's also a bit more complicated than you think. What about the zero matrix? You can't divide by 0!

That can be sorted out (hopefully) by if-else type condition testing. I'll think about it once my code starts to work for the non-zero elements.

I kinda observed what's going wrong: like, I printed the matrix after each loop of operation and came out with this: The operation
 a[i][r]=a[i][r]-((a[i][j]/a[i-1][j])*a[i-1][r])
is being applied to the first element of the row ONLY. I am unable to know why. After that the control does not increment r anymore and straightway decrements i. So the control goes out to the previous row and again the same thing happens. For some reason or the other, r is not incremented at all.
lets deal only with non-zero matrix elements.

This post has been edited by Soumikbhat: 15 November 2013 - 08:47 PM

Was This Post Helpful? 0
  • +
  • -

#10 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 16 November 2013 - 07:34 AM

Am stuck and clueless... after all, how is
a[i][r]=a[i][r]-((a[i][j]/a[i-1][j])*a[i-1][r]);
not working for r>0 ?

ASSUME ALL MATRIX ELEMENTS ARE NON-ZERO.

This code gave me the problem as I said in my last post. Want the solution badly..

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n,r,s,t;
    printf ("Enter the order of the mtarix ");
    scanf("%d",&n);
    printf("\n");
    float a[n][n];
    for (i=0;i<=n-1;i++)
    {
        for (j=0;j<=(n-1);j++)
        {
            printf("enter the element of %d th row and %d th column ",i,j);
            scanf("%f",&a[i][j]);


            printf("\n"); }
    }
    printf("The matrix enetred is\n");
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
            printf("%f\t",a[i][j]);
        printf("\n");
    }

    fflush(stdin);
    for (j=0;j<n;j++)
    {
        for (i=n-1;i>j;i--)
        {
            for (r=0;r<n;r++)
            {
                a[i][r]=a[i][r]-((a[i][j]/a[i-1][j])*a[i-1][r]);

                /*printf("****************\n"); */
                printf("\n The value of i and r is %d and %d\n",i,r);
                printf("*****\n");



            for (s=0;s<n;s++)
    {
             for (t=0;t<n;t++)
             {


             printf("%f\t",a[s][t]); }
              printf("\n");

    }
            }
        }
    printf("\n");

    }
    printf("The operated matrix is\n");
    fflush(stdin);
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
            printf("%f\t",a[i][j]);
        printf("\n");
    }

}


This post has been edited by Soumikbhat: 16 November 2013 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 Soumikbhat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 13

Re: Conversion of Matrix to Upper triangular form

Posted 16 November 2013 - 08:02 AM

Issue resolved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1