Thread: Partial pivoting a matrix with an upper triangulate function.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    Partial pivoting a matrix with an upper triangulate function.

    Hey all, I was given a triangulate function in order to convert a matrix into an upper-triangular matrix.

    Code:
    //code excerpt:
    #define ROWS 4
    #define COLS 4
    
    void triangulate(double a[][COLS+2], const size_t M, const size_t N)
    {
        for(size_t i=1; i<=N; i++)
        {
            for(size_t j=i+1; j<=N; j++)
            {
                for(size_t k=N+1; k>=i; k--)
                {
                    a[j][k] -= a[i][k] * a[j][i]/a[i][i];
                }
            }
        }
    }
    a = array, i = rows, j = columns.

    However, this code was intentionally written for specific matrices, and the triangulate function puts a[2][2] as 0.
    Code:
     
    double array1[ROWS+2][COLS+2] = {{0, 0, 0, 0, 0, 0}, {0, -1, 0, 0, 0, -0.08}, {0, 1, -1, 0, 1, 0}, {0, 0, 1, -1, 0, 0.02}, {0, 0, 0, 1, 0, 0.05}};
    The assignment is to "modify your triangulate function such that, when you are updating row i, search below row i in column i to find the row j containing the diagonal element of greatest nonzero absolute value, and then swap the entire row i with row j. After this row swap, resume your normal triangulation code."

    This is what I have so far, but it's not doing what I hoped it would:

    Code:
    void triangulate(double a[][COLS+2], const size_t M, const size_t N)
    {
        double swap;
    
    
        for(size_t i=1; i<=N; i++)
        {
            if(a[i+1][i] > a[i][i]){
                swap = a[i][i];
                a[i+1][i] = a[i][i];
                a[i+1][i] = swap;
            }
    
    
            for(size_t j=i+1; j<=N; j++)
            {
                for(size_t k=N+1; k>=i; k--)
                {
                    a[j][k] -= a[i][k] * a[j][i]/a[i][i];
                }
            }
        }
    }

    How would I go about executing this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would suggest creating two sub-functions.
    Code:
    void triangulate(double a[][COLS+2], const size_t M, const size_t N)
    {
        for(size_t i=1; i<=N; i++)
        {
            j = findLargestOnDiagonal(a,i);
            swapRows(a,i,j);
            for(size_t j=i+1; j<=N; j++)
            {
                for(size_t k=N+1; k>=i; k--)
                {
                    a[j][k] -= a[i][k] * a[j][i]/a[i][i];
                }
            }
        }
    }
    Each sub-function can be written and tested independently.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Upper Letter Function Doesnt Work
    By korayaykor in forum C Programming
    Replies: 4
    Last Post: 12-20-2016, 06:26 PM
  2. Replies: 4
    Last Post: 12-06-2014, 01:51 PM
  3. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  4. Replies: 11
    Last Post: 11-30-2011, 07:54 AM
  5. Templates - Function Partial Specialization?
    By Cat in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2003, 07:53 AM

Tags for this Thread