Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficient way to represent a lower/upper triangular matrix

Tags:

I am working on my data in a C/C++ program, which is 2 dimensional. Here my value is calculated for pair-wise and here values would be same for foo[i][j] and foo[j][i].

Thus if I implement it by using a simple 2 dimensional array, half of my space would be wasted. So what would be best data structure to represent this lower/upper triangular matrix.

Regards,

like image 428
shampa Avatar asked Oct 30 '11 15:10

shampa


People also ask

How do you represent a upper triangular matrix?

What is an Upper Triangular Matrix? An n × n square matrix A = [aij] is said to be an upper triangular matrix if and only if aij = 0, for all i > j. This implies that all elements below the main diagonal of a square matrix are zero in an upper triangular matrix.

Can an upper triangular matrix be a lower triangular matrix?

Yes. Diagonal matrices are both upper and lower triangular. Notice that the definition for upper triangular says that entries below the diagonal are all zero. It doesn't matter what the entries above the diagonal are.

How do you turn a matrix into a lower triangular matrix?

To convert given matrix to lower triangular matrix, set the elements of the array to 0 where (j > i) that is, the column number is greater than row number. Display the resulting matrix.

How can you store an upper triangular matrix in one dimensional array?

The upper triangle is packed by columns. The elements are packed sequentially, column by column, in n(n+1)/2 elements of a one-dimensional array. To calculate the location of each element of the triangular matrix in the array, use the technique described in Upper-Packed Storage Mode.


2 Answers

If you have N items then a lower triangular array without the main diagonal will have (N - 1) * N / 2 elements, or (N + 1) * N / 2 elements with the main diagonal. Without the main diagonal, (I, J) (I,J ∈ 0..N-1, I > J) ⇒ (I * (I - 1) / 2 + J). With the main diagonal, (I,J ∈ 0..N-1, I ≥ J) ⇒ ((I + 1) * I / 2 + J).

(And yes, when you're allocating 4 gigabytes on a 2.5 gigabyte machine, cutting it half does make a huge difference.)

like image 122
prosfilaes Avatar answered Sep 21 '22 18:09

prosfilaes


Really, you're best off just using a regular two dimensional matrix. RAM is pretty cheap. If you really don't want to do that, then you can build a one-dimensional array with the right number of elements and then figure out how to access each element. For example, if the array is structured like this:

    j     1234 i 1 A   2 BC   3 DEF   4 GHIJ 

and you have it stored as a one dimensional array, left to right, you'd access element C (2, 2) with array[3]. You can work out a function to go from [i][j] to [n] but I won't spoil your fun. But you don't have to do this unless the triangular array in question is really huge or you're very concerned about space.

like image 21
Dan Avatar answered Sep 18 '22 18:09

Dan