求矩阵的最大特征值的幂法
还有,我在写一些程序段的时候对待外部传进的指针采用如下处理手段(纯C条件下)
除非这个函数有特殊的作用,如申请内存,或要读入外部文本内容到二维指针等。其余的情况,一律不对外部指针进行任何申请或释放内存的处理。
对于要保护数据的外部传入指针,则在函数内部再做一个局部指针,在函数结尾释放.
对局部指针的操作,也仅限于赋值,而绝对不要用外部传入针指去指向它(即赋一个临时区的地址给外部的指针变量),这当然是错误的。
do
{
assertF(listNotZero(listU,size),"in the core of powerMethodForLamda list U is NULL\n");
assertF(listNotZero(listV,size),"in the core of powerMethodForLamda list V is NULL\n");
{
FILE *inputFile;/*input file*/
FILE *outputFile;/*output file*/
double startTime,endTime,tweenTime;/*time callopsed info*/
int rowNum,colNum;
Type** wArr;
{
/*variable declare*/
int i,k;/*iterator number*/
Type sum;
/*assertion*/
assertF(matrixA!=NULL,"in twoMatrixBy matrixA is null\n");
assertF(matrixListIn!=NULL,"in twoMatrixBy matrixB is null\n");
outputListArrFloat(listV,0,size,outputFile);
/*End of the Core Program*/
maxLamda=maxAbsValInList(listU,size);
fprintf(outputFile,"the max lamda is:\r\n %f.\r\n",maxLamda);
fprintf(outputFile,"%-16d%-16f\r\n",iteratorNum,maxAbsValInList(listU,size));
}
while(fabs(preMax-maxAbsValInList(listU,size))>=e);
fprintf(outputFile,"charactstic vector is:\r\n");
/*argu prepare*/
assertF(colNum==rowNum,"in test colNum!=rowNum");
n=rowNum;/*get the size of square matrix*/
maxLamda=powerMethodForLamda(wArr,n,"output2.txt");
好了,下面是程序段:
/*for max lamda resolve*/
Type powerMethodForLamda(Type** matrixA,int size,char* outputFileName)
{
Type maxLamda;
Type* listV;
Type* listU;
FILE* outputFile;/*the outputFile for the data output*/
printf("output file open success\n");
/*This function,automatically fullfill the task of
apply the mem for the 2d pointers. Perfect,right? :)*/
read2DArrFloat(&wArr,&rowNum,&colNum,"inputData2.txt");
Type preMax;/*a tween data*/
float e=(float)0.0001;/*the precise controller*/
Type tmpData;/*temp data for program*/
int i=0;/*iterator times*/
int iteratorNum=0;/*iterator number*/
printf("input file open success\n");
/*default outpout file open*/
if(argc>2)strcpy(outFileName,argv[2]);
assertF((outputFile=fopen(outFileName,"wb"))!=NULL,"output file error");
assertF(matrixListAns!=NULL,"in twoMatrixBy matrixAns is null\n");
/*core program*/
for(i=0;i<rowNum;i++)
{
sum=0;
for(k=0;k<mNum;k++)
sum+=matrixA[i][k]*matrixListIn[k];
{
int i=0;/*iterator value*/
assertF((*inArr)!=NULL,"in 2d arr mem free,in arr is null\n");
for(i=0;i<rowNum;i++)
free((*inArr)[i]);
free((*inArr));
}
这两个函数的作用相信大家一看就明白,是实现二维指针的申请内存和释放内存的,这样,以后再主程序里的工作量就会小多了。
而maxLamda所对应的特征向量由最后一次迭代的Vk所决定.
主要的想法就是先选一个不为0的初始向量U0!=0,然后按下面的式子迭代。
U0=V0!=0
Do
{
Uk=AVk-1
Vk=Uk/max(Uk)
}while(abs(max(Uk)-max(Uk-1))>=e)//e为精度.
好了,就这样,更多的细节请去参考相关的数值算法书籍.
maxData=(Type)fabs(inList[0]);
for(i=1;i<len;i++)
if(fabs(inList[i])>maxData)maxData=(Type)fabs(inList[i]);
return maxData;
}
/*test program*/
/*maxLamda resolve test program*/
assertF((outputFile=fopen(outputFileName,"wb"))!=NULL,"output file open error\n");
/*mem apply*/
listArrMemApply(&listV,size);
listArrMemApply(&listU,size);
/*assertion*/
assertF(matrixA!=NULL,"in powerMethodFor lamda,matrixA is null\n");
assertF(outputFileName!=NULL,"in readList,listFileName is null\n");
/*open file*/
for(i=0;i<rowNum;i++)
(*inArr)[i]=(Type*)malloc(sizeof(Type)*colNum);
assertF(*inArr!=NULL,"in twoDArrMemApply,inArr at last is null\n");
}
void twoDArrMemFree(Type*** inArr,int rowNum)
printf("the max lamda is:%f.\r\n",maxLamda);
fprintf(outputFile,"the max lamda is:%f.\r\n",maxLamda);
/*
input data specification
row,col;
//Arr
a11,a12,...;
, , , ,
, , , ,
an1,an2,...;
}
*/
char *outFileName="outputData.txt";
#define DEBUG 1
void main(int argc,char* argv[])
在贴出程序之前,先对一部分我新增加的实用函数进行说明:
如:
void twoDArrMemApply(Type*** inArr,int rowNum,int colNum)
{
int i=0;/*iterator vaule*/
(*inArr)=(Type**)malloc(sizeof(Type*)*rowNum);
Type maxLamda;
int n;/*arr deminision for squre matrix*/