2

I have been trying to write code that determines if a matrix is upper triangular or not, and print it.

I have tried while loops, double for loops, nothing. Here is the mess I currently have:

int i, j;
int count = 0;
bool upper;

while (upper = true;)
{
for (i=1; i<m; i++)
{
   for (j=i-1; j<n; j++)
   {
      if (a[i] > a[j] && a[i][j] == 0.0)
         upper = true;
      else if (a[i][j] != 0.0)
         upper = false;
   }
}
}
//   cout << "Matrix is upper triangular. " << count << endl;
user1824947
  • 21
  • 1
  • 2
  • Isn't `i` in for statement start from **0** and `upper = true` is an assignment and not a comparison. Also, if the program doesn't compile, let us know the error as well. – Mahesh Nov 14 '12 at 20:56
  • you have no need of the while loop. Also it should be `while (true == upper)` – andre Nov 14 '12 at 20:56
  • Break out when you reach upper = true. Or else it will most probably go into upper = false before it's done. – Martol1ni Nov 14 '12 at 20:56
  • Correct me otherwise, but in linear algebra, the only condition that is required for being an upper triangle is that A[i][j] = 0 for all i > j. The top triangle could be filled with zeros as well, but the bottom-left *must* be; correct ? – WhozCraig Nov 14 '12 at 21:02
  • I think "a[i] > a[j]" should not be in there. –  Nov 14 '12 at 21:03
  • And it shouldn't be `while (upper)`. It shouldn't be, period. All that this outer loop accomplishes is to create an infinite loop in the case of a matrix that is upper triangular. – David Hammen Nov 14 '12 at 22:53
  • @DavidHammen Correct you are. For OP, Careful you must be, not to assign when you mean to comparing. – andre Nov 15 '12 at 14:22

4 Answers4

1

Look at example:

 |0|1|2|3|4|5|
0| | | | | | |
1|X| | | | | |
2|X|X| | | | |
3|X|X|X| | | |
4|X|X|X|X| | |
5|X|X|X|X|X| |

This matrix is upper triangular is cells marked with X are all zero.
For i-th row - the cells {i,0},{i,1}, ... , {i,i-1} must be zero.

So it is easy task:

bool isUpperTriangle = true; // be optimistic!
for (int i = 1; i < SIZE && isUpperTriangle; ++i) // rows
    for (int j = 0; j < i && isUpperTriangle; ++j) // columns - see example
        if (m[i][j] != 0) 
            isUpperTriangle = false;
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
1

I think this will likely do what you're looking for. Note: this assume the matrix is square. If it is not (i.e. m!=n) you should return false immediately:

bool upper = true;
for (i=1; i<m && upper; ++i)
   for (j=0; j<i && (upper = (0 == a[i][j])); ++j);
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
1

Whether the matrix is upper triangular or not can only be determined by checking the whole lower part. If you encounter a non-zero element along the way, you know it's not upper triangular. You cannot make that determination until you checked the whole lower part. So your:

upper = true;

statement while you're still in the loop has no logical basis.

The problem is similar to a character search inside a string. You need to check the whole string. If you arrived at the end of the string and still didn't find the character you're looking for, then (and only then) do you know that the character isn't in the string. The only difference with the matrix problem is that you've now got one additional dimension. Or, in other words, multiple one-dimensional arrays, each one +1 in size compared to the previous array, and you got to search them all.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
0

Have you considered using a matrix library that has this function built in? I use the Eigen library quite often and I find the syntax very easy to use - they also have a short and useful tutorial to become familiar rather quickly.

http://eigen.tuxfamily.org/index.php?title=Main_Page

david
  • 580
  • 4
  • 12