algorithm for index numbers of triangular matrix coefficients -


i think must simple can't right...

i have mxm triangular matrix, coefficients of stored in vector, row row. example:

m =   [ m00 m01 m02 m03 ]        [     m11 m12 m12 ]       [         m22 m23 ]       [             m33 ] 

is stored

coef[ m00 m01 m02 m03 m11 m12 m13 m22 m23 m33 ] 

now i'm looking non-recursive algorithm gives me matrix size m , coefficient array index i

unsigned int row_index(i,m) 

and

unsigned int column_index(i,m) 

of matrix element refers to. so, row_index(9,4) == 3, column_index(7,4) == 2 etc. if index counting zero-based.

edit: several replies using iteration have been given. know of algebraic expressions?

here's algebraic (mostly) solution:

unsigned int row_index( unsigned int i, unsigned int m ){     double m = m;     double row = (-2*m - 1 + sqrt( (4*m*(m+1) - 8*(double)i - 7) )) / -2;     if( row == (double)(int) row ) row -= 1;     return (unsigned int) row; }   unsigned int column_index( unsigned int i, unsigned int m ){     unsigned int row = row_index( i, m);     return  - m * row + row*(row+1) / 2; } 

edit: fixed row_index()


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -