fprintf(pReadFile,\ //printf(\ //printf(\ fprintf(pReadFile,\ } } /******************************************* 带双步位移的QR分解 将所有求出的特征值传入参量lambda[]中 返回值为整型 ********************************************/ intTwoStepDisplacement_QR(double a[][N],complex lambda[]){ inti,j,m,l,flag = 1,count = 0,k = 0; double mk[N][N],detD,b,s,t,sum,delta; struct complex s1,s2; m = N - 1;
//(3)到(9)的循环 while (1) { k++;
//(3)到(7)的循环 while (1) {
//(3)到(4)的循环 while (1) { if (flag){ //(3) if (abs(a[m][m-1]) <= err){ lambda[count].rpart = a[m][m]; lambda[count].ipart = 0; count++; m--; } else break; } flag = 1; //(4) if (m == 0){ lambda[count].rpart = a[m][m]; lambda[count].ipart = 0; print_lambda(lambda); return 0; } else if (m == -1){
print_lambda(lambda); return 0; } } //(5)
detD = a[m-1][m-1] * a[m][m] - a[m-1][m] * a[m][m-1]; b = -(a[m-1][m-1] + a[m][m]); delta = b * b - 4 * detD; if (delta >= 0){ s1.rpart = -(b + sqrt(delta)) / 2; s1.ipart = 0; s2.rpart = -(b - sqrt(delta)) / 2; s2.ipart = 0; } else{ s1.rpart = -b / 2; s1.ipart = -sqrt(-delta) / 2; s2.rpart = -b / 2; s2.ipart = sqrt(-delta) / 2; }
//(6) if (m == 1){ lambda[count] = s1; count++; lambda[count] = s2; print_lambda(lambda); return 0; } //(7) if (abs(a[m-1][m-2]) <= err){ m = m - 2; lambda[count] = s1; count++; lambda[count] = s2; count++; } else break; flag = 0; } //(8)
if (k == L){ fprintf(pReadFile,\在规定的迭代次数 L=%d 内未能得到全部特征值\ //printf(\在规定的迭代次数 L=%d 内未能得到全部特征值\ //scanf(\ return 0; } //(9)
}
}
s = a[m-1][m-1] + a[m][m];
t = a[m-1][m-1] * a[m][m] - a[m][m-1] * a[m-1][m]; //计算矩阵Mk
for (i = 0; i <= m; i ++){ for (j = 0; j <= m; j++){ sum = 0; for (l = 0; l <= m; l++) sum += a[i][l] * a[l][j]; if (i == j) mk[i][j] = sum - s * a[i][j] + t; else mk[i][j] = sum - s * a[i][j]; } }
QR_decompositionMk(mk,m,a);
/*******************************************
矩阵乘法 a[][]与b[][]的乘积传入c[][]中 无返回值 ********************************************/ void multi_matrix(double a[][N],double b[][N],double c[][N]){ inti,j,t; double sum; for (i = 0; i < N; i++){ for (j = 0; j < N; j++){ sum = 0; for (t = 0; t < N; t++) sum += a[i][t] * b[t][j]; c[i][j] = sum; } } }
三、 运算结果:
1. 拟上三角化之后的矩阵A[10][10]:
-8.827516758830e-001 -2.347878362416e+000 0.000000000000e+000 0.000000000000e+000
-9.933136491826e-002 2.372370104937e+000 1.728274599968e+000 0.000000000000e+000
-1.103349285994e+000 1.819290822208e+000 -1.171467642785e+000 -1.291669534130e+000
-7.600443585637e-001 3.237804101546e-001 -1.243839262699e+000 -1.111603513396e+000
1.549101079914e-001 2.205798440320e-001 -6.399758341743e-001 1.171346824096e+000 8.125049397515e-001 -7.707773755194e-001
-1.946591862872e+000 -8.782436382927e-002 -9.255889387184e-001 6.032599440534e-001 1.518860956469e-001 2.102692662546e+000 1.816138086098e-001 1.278839089990e+000 -6.380578124405e-001 -4.154075603804e-001 -2.002833079037e+000 2.924947206124e-001 -6.412830068395e-001 9.783997621285e-002 2.557763574160e-001 -1.307356030021e+000 1.803699177750e-001 -4.246385358369e-001 7.988955239304e-002 1.608819928069e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+000
1.560126298527e+000
4.421756832923e-001 -3.588616128137e-002 4.691742313671e-001 -2.736595050092e-001 -7.359334657750e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000
-1.583051425742e+000 -3.042843176799e-001 2.528712446035e-001 -6.709925401449e-001 2.544619929082e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 -7.463453456938e-001 -2.708365157019e-002 -9.486521893682e-001 1.195871081495e-001 1.929265617952e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 -7.701801374364e-001 -4.697623990618e-001 4.988259468008e-001 1.137691603776e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0007.013167092107e-001 1.582180688475e-001 3.862594614233e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0004.843807602783e-001 3.992777995177e-001
2. QR分解之后拟上三角化之后的矩阵A[10][10]的结果Q[10][10]:
-3.519262579534e-001 -9.360277287361e-001 0.000000000000e+000
4.427591982245e-001 -1.664679186545e-001 -8.810520554692e-001
-6.955982513607e-001 2.615299548560e-001 -3.989762796959e-001 -5.371806806439e-001
6.486200753651e-002 -2.438671728934e-002 3.720308728479e-002 -1.234945854205e-001 9.892235468621e-001
3.709718861896e-001 -1.394774360893e-001 2.127794064090e-001 -7.063151608719e-001 -1.239411731211e-001 5.323610690264e-001
1.855847143605e-001 -1.628942319628e-002 -1.181053169648e-001 -5.255375383724e-002 -5.486582943568e-002 -6.977585391241e-002 6.124472142963e-003 4.440505443139e-002 1.975907909728e-002 2.062836970533e-002 1.064463557221e-001 -9.343171079758e-003 -6.774200464527e-002 -3.014340698675e-002 -3.146955080444e-002
0.000000000000e+0000.000000000000e+000
-3.533456368496e-001 3.101438948264e-002 2.248676491598e-001 1.000601783527e-001 1.044622748702e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+000
-6.200358589825e-002 5.442272839461e-003 3.945881637235e-002 1.755813350011e-002 1.833059462907e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000
-6.733900344896e-001 5.910581205868e-002 4.285425323867e-001 1.906901343193e-001 1.990794495295e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 -6.059761505747e-001 -9.165783032819e-002 -6.645586508974e-001 -2.957110877580e-001 -3.087207462557e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 9.933396625117e-001 -9.690440311939e-002 -4.311990584470e-002 -4.501694411183e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 5.410088006061e-001 -5.817540838226e-001 -6.073480580545e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 -7.221591336735e-001 6.917269588876e-001
3. QR分解之后拟上三角化之后的矩阵A[10][10]的结果R[10][10]:
2.508342744917e+000 0.000000000000e+000 0.000000000000e+000 0.000000000000e+000 0.000000000000e+000 0.000000000000e+000 0.000000000000e+000
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000-7.753441914209e-001 -3.464514508821e-001 4.312226803504e-001 1.234643696237e-001
0.000000000000e+000
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0001.296312940612e+000 -4.288053318338e-001 2.737334158165e-001
0.000000000000e+000
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 -6.707396440648e-001 -4.842320121884e-001
0.000000000000e+000
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 7.168323926323e-002
-2.185646885493e+000 -1.961603277854e+000 0.000000000000e+000
-1.314609070786e+000 2.407523727633e-001 2.404534601993e+000
-3.558787493835e-002 7.054714572823e-001 1.706758096328e+000 1.577122080722e+000
-2.609857850388e-001 5.957204318279e-001 -4.239566704091e-001 6.399535133956e-001 -1.447846997770e+000
-1.283121847090e+000 -1.390878610606e-001 -8.712897972161e-001 3.849367902971e-001 3.353802899665e-001 5.526978774676e-001 -3.268209924413e-001 -5.769498668364e-002 2.871129330189e-001 -8.895128754189e-002 3.405332305815e+000 -1.050017655852e-001 1.462257102734e+000 -6.684487469283e-001 -4.027646209664e-001
0.000000000000e+0000.000000000000e+000
3.468127872427e-001 -5.701786649768e-002 4.014788054433e-001 -2.222476176311e-001 -6.317059236442e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+000
-1.415724007744e+000 -2.806139044665e-001 -2.817910521892e-001 -4.611434881851e-002 1.996629079956e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000
1.231641451542e+000 1.619701003419e-001 1.962638275504e-001 5.350035621760e-001 -1.509273424767e-001
4. QR分解之后拟上三角化之后的矩阵A[10][10]的结果Q、R乘积矩阵为:
1.163074414164e+000 1.836115060851e+000 0.000000000000e+000
2.632670934508e+000 1.144286420080e-001 -2.118520153533e+000
-1.772796003272e+000 -9.880381403133e-001 -1.876189745783e+000 -8.471995127808e-001
-8.668899138521e-002 5.589725694767e-001 -5.407071940597e-001 4.382910468318e-001 -1.432244342447e+000
3.300503471047e-001 4.694190067101e-002 1.171538359721e+000 -1.008632199185e+000 -5.742284908055e-001 6.556779598004e-001
1.455162371214e+000 -9.730650448593e-001 -4.873031174655e-001 -7.756411630489e-001 -3.249201979113e-001 -2.978478237007e-001 1.617130577649e-002 6.936977702522e-001 1.367670571405e-001 1.419099231519e-002 -2.550323020223e+000 1.691577936540e+000 1.229951613262e+000 1.387947777212e+000 8.667502917242e-001
0.000000000000e+0000.000000000000e+000
-7.959374261495e-001 4.769258865577e-001 4.072683083890e-001 4.096390493527e-001 3.363378940862e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+000
1.213151477723e+000 -3.457508625575e-001 -4.749853573124e-001 -3.176158274191e-001 -4.294507015032e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000
-9.275250974463e-001 2.529079844053e-001 6.905949216976e-001 -2.374430675823e-002 -2.429781119781e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 4.698400884876e-001 -2.730776009527e-001 7.821296259798e-001 -9.580964936399e-002 7.846239841323e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000 1.287679058937e+000 -3.576058900348e-001 -4.116725408806e-003 3.914268216423e-001
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+000-3.628760503545e-001 7.398980975354e-0017.241608309576e-002
0.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000e+0000.000000000000
e+0000.000000000000e+0000.000000000000e+000 -5.176670596524e-002 4.958522909877e-002
5. 矩阵的所有特征值λ为:
λ1 =5.650488993501e-002 λ2 =6.360627875745e-001 λ3 =9.355889078188e-001
λ4 =-9.805309562902e-001 + 1.139489127430e-001*i λ5 =-9.805309562902e-001 + 1.139489127430e-001*i λ6 =-1.484039822259e+000 λ7 =1.577548557113e+000
λ8 =-2.323496210212e+000 + 8.930405177200e-001*i λ9 =-2.323496210212e+000 + 8.930405177200e-001*i λ10=3.383039617436e+000
6. 矩阵的所有实特征值对应的特征向量为:
矩阵与特征值λ1=5.650488993501e-002对应的特征向量为X1[10]: -5.105003830620e+000 -4.886319842355e+000 9.505161576142e+000 -9.604334110489e+000 -3.045741988151e+000 1.574873885600e+001 -7.109654943654e+000 1.000000000000e+000
矩阵与特征值λ2=6.360627875745e-001对应的特征向量为X2[10]: 4.745031936534e+000 3.157868541747e+000 1.729946912419e+001 -3.187521973521e+001 7.794009603193e+000 -1.004255685844e+001 1.310524273052e+001 1.000000000000e+000
矩阵与特征值λ3=9.355889078188e-001对应的特征向量为X3[10]: 2.792418944526e+000 1.598236841510e+000 -5.207507040906e-001 -1.225708535858e+001 7.241214790770e+000 -5.398214501428e+000 -1.216518754415e+0011.000000000000e+000
矩阵与特征值λ6=-1.484039822259e+000对应的特征向量为X6[10]: -1.730784582110e+001 2.408192534965e+001 4.133852365114e-001 9.287334657923e-002 -7.832726042765e-002 -6.374274025708e-001 -3.784865409365e-001 1.000000000000e+000
矩阵与特征值λ7=1.577548557113e+000对应的特征向量为X7[10]: 6.217350824575e-001 -1.115111815225e-001 -2.483773580801e+000 -3.815605442529e+000 8.117305509387e+000 -1.239170883674e+000 2.691900144837e+000 1.000000000000e+000
矩阵与特征值λ10=3.383039617436e+000对应的特征向量为X10[10]: -4.367398732970e-001 -9.063574014237e-001 -1.963340418326e+000 -1.269297783865e+000 -1.072448834413e+000 3.625108238697e-001 2.112744711308e+000 1.000000000000e+000
-6.788331788207e-001 -7.395037126270e+000
-1.980049331453e+000 1.670757770479e+001
-1.667886451700e+000 2.841008912972e+001
-8.572044074529e+000 -3.403204760828e-001
-1.306860840419e+000 -6.800309586190e-001
-1.082857447819e+000 1.682905297743e+000