实现了一个C++版本的lanczos算法,搞了两天哦。如下:
class CDArray
{
public:
CDArray();
CDArray(int);
CDArray(CDArray&o);
CDArray(int size, double*value);
~CDArray();
void Zero();
void Value(double v=1.);
void Random();
void SetSize(int size);
int GetSize();
double Norm2();
double Normalize();
double*GetData();
void SetData(int size, double*value);
double&operator()(int i);
void Add(CDArray&array_arg, double coef_arg=1., double coef_self=1.);
void Minus(CDArray&array_arg, double coef_arg=1., double coef_self=1.);
void Multiply(double coef_self);
double Multiply(CDArray&array_arg, double coef_arg=1., double coef_self=1.);
private:
int m_size;
double*m_value;
int m_selfResource;
double error_index_value;//0.
};
class CDMatrix
{
public:
CDMatrix();
CDMatrix(int r, int c);
CDMatrix(int r, int c, double*v);
double&operator()(int i, int j);
void Zero();
int Row();
int Col();
double*GetData();
void SetSize(int r, int c);
void GetRowArray(int row, CDArray&rowValues, double coef=1.);
void GetColArray(int col, CDArray&colValues, double coef=1.);
void SetRowArray(int row, CDArray&rowValues, double coef=1.);
void SetColArray(int col, CDArray&colValues, double coef=1.);
void MatrixMultiplyArray(CDArray&array_arg, CDArray&array_res);
void MatrixMultiplyMatrix(CDMatrix&matrix_arg, CDMatrix&matrix_res);
void TransMatrix(CDMatrix&T);
void lanczos(int m, CDArray&eignvalues);
void general_lanczos(CDMatrix&B, int m, CDArray&eignvalues);
private:
int m_row, m_col;
double* m_matixvalue;
int m_selfResource;
double error_index_value;//0.
};
//////////////////////////////////////////////////////////////////////////////////////////
double matric_value[36]=
{
3.0291,2.6136,2.5156,2.3668,3.2222,3.3113,
2.6136,3.9099,3.7357,3.1730,3.5164,4.2731,
2.5156,3.7357,5.1312,3.8987,3.6089,4.5110,
2.3668,3.1730,3.8987,4.0152,3.1374,3.6548,
3.2222,3.5164,3.6089,3.1374,4.7553,4.5013,
3.3113,4.2731,4.5110,3.6548,4.5013,5.5402
};
CDMatrix matrixHelper(6, 6, matric_value);
for (int i=3; i<6; i++)
{
CDArray eignvalues;
matrixHelper.lanczos(i, eignvalues);
}
- (eignvalues).m_value,6 0x003a3a40 double *
[0] 22.097831308726644 double
[1] 0.91418994723270364 double
[2] 1.9930155692273419 double
[3] 0.29363700351981309 double
[4] 0.47020395540170512 double
[5] 0.61202221589179351 double
参考解为:
您输入的矩阵如下:
第1列 第2列 第3列 第4列 第5列 第6列
3.02910000 2.61360000 2.51560000 2.36680000 3.22220000 3.31130000
2.61360000 3.90990000 3.73570000 3.17300000 3.51640000 4.27310000
2.51560000 3.73570000 5.13120000 3.89870000 3.60890000 4.51100000
2.36680000 3.17300000 3.89870000 4.01520000 3.13740000 3.65480000
3.22220000 3.51640000 3.60890000 3.13740000 4.75530000 4.50130000
3.31130000 4.27310000 4.51100000 3.65480000 4.50130000 5.54020000
您所输入问题的解如下:
特征值:
特征值1: 22.09783131
特征值2: 1.99301557
特征值3: 0.91418995
特征值4: 0.61202222
特征值5: 0.47020396
特征值6: 0.29363700
特征向量:
向量1 向量2 向量3 向量4 向量5 向量6
-0.31392917 0.47451939 0.41426786 -0.28690244 -0.63534859 -0.13667892
-0.39577520 -0.02486736 -0.35708223 -0.56869765 0.31706331 -0.53971263
-0.43824511 -0.56274262 -0.15619994 0.43979973 -0.44518867 -0.27431149
-0.37588160 -0.44566339 0.66028586 -0.21429215 0.32125919 0.27384514
-0.42294572 0.48873042 0.13821522 0.58813826 0.43470794 -0.16816194
-0.48226717 0.14163484 -0.47052645 -0.09496540 -0.07369952 0.71520120
|