算法三角矩阵系数的索引号 [英] algorithm for index numbers of triangular matrix coefficients
问题描述
我想这一定是简单的,但我无法得到它的权利......
I think this must be simple but I can't get it right...
我有一个M×M个三角矩阵,其系数由行存储在向量,行。 例如:
I have an MxM triangular matrix, the coefficients of which are stored in a vector, row by row. For example:
M = [ m00 m01 m02 m03 ]
[ m11 m12 m12 ]
[ m22 m23 ]
[ m33 ]
存储为
coef[ m00 m01 m02 m03 m11 m12 m13 m22 m23 m33 ]
现在我正在寻找一个非递归算法,让我为矩阵大小 M
和系数数组索引我
Now I'm looking for a non-recursive algorithm that gives me for matrix size M
and coefficient array index i
unsigned int row_index(i,M)
和
unsigned int column_index(i,M)
矩阵元素的,它指的是。所以,
row_index(9,4)== 3
,与Column_Index(7,4)== 2
等,如果索引计数从零开始的。
of the matrix element that it refers to. So,
row_index(9,4) == 3
, column_index(7,4) == 2
etc. if the index counting is zero-based.
编辑:用迭代一些答复已经给出。有谁知道代数EX pressions?
Several replies using an iteration have been given. Does anyone know of algebraic expressions?
推荐答案
下面是一个代数(主要)解决方案:
Here's an 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 i - M * row + row*(row+1) / 2;
}
编辑:固定row_index()
fixed row_index()
这篇关于算法三角矩阵系数的索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!