📄 agr3.cpp
字号:
/*for max lamda resolve*/
//求矩阵的最大特征值的幂法
double powerMethodForLamda(double** matrixA,int size,char* outputFileName)
{
double maxLamda;
double* listV;
double* listU;
FILE* outputFile;/*the outputFile for the data output*/
double preMax;/*a tween data*/
float e=(float)0.0001;/*the precise controller*/
double tmpData;/*temp data for program*/
int i=0;/*iterator times*/
int iteratorNum=0;/*iterator number*/
/*assertion*/
assertF(matrixA!=NULL,"in powerMethodFor lamda,matrixA is null\n");
assertF(outputFileName!=NULL,"in readList,listFileName is null\n");
/*open file*/
assertF((outputFile=fopen(outputFileName,"wb"))!=NULL,"output file open error\n");
/*mem apply*/
listArrMemApply(&listV,size);
listArrMemApply(&listU,size);
/*initialization*/
for(i=0;i<size;i++)
{
listV[i]=0;
listU[i]=0;
}
listV[size-1]=1;
listU[size-1]=1;
/*core program*/
fprintf(outputFile,"iteratorTime maxUk\r\n");
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");
preMax=maxAbsValInList(listU,size);
matirxBy2DWith1DCol(matrixA,listV,listU,size,size);
tmpData=1/maxAbsValInList(listU,size);
numByList(tmpData,listU,listV,size);
fprintf(outputFile,"%-16d%-16f\r\n",iteratorNum,maxAbsValInList(listU,size));
}while(fabs(preMax-maxAbsValInList(listU,size))>=e);
fprintf(outputFile,"charactstic vector is:\r\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);
/*mem free*/
free(listV);
free(listU);
/*close the file*/
fclose(outputFile);
return maxLamda;
}
/*相应的辅助函数*/
/* matirxBy2DWith1DCol 一个n*n的矩阵和一个n*1的列向量作乘法*/
void matirxBy2DWith1DCol(double** matrixA,double* matrixListIn,double* matrixListAns,int rowNum,int mNum)
{
/*variable declare*/
int i,k;/*iterator number*/
double sum;
/*assertion*/
assertF(matrixA!=NULL,"in twoMatrixBy matrixA is null\n");
assertF(matrixListIn!=NULL,"in twoMatrixBy matrixB is null\n");
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];
matrixListAns[i]=sum;
}
}
/*求一个一维向量中绝对值的最大值*/
double maxAbsValInList(double* inList,int len)
{
int i;/*iterator num*/
double maxData;
assertF(inList!=NULL,"in maxValInList,inList is NULL\n");
maxData=(double)fabs(inList[0]);
for(i=1;i<len;i++)
if(fabs(inList[i])>maxData)maxData=(double)fabs(inList[i]);
return maxData;
}
/*test program*/
/*maxLamda resolve test program*/
#include "Global.h"
#include "Ulti.h"
#include "Matrix.h"
#include "MyAssert.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *inFileName="inputData.txt";
/*
input data specification
row,col;
//Arr
a11,a12,...;
, , , ,
, , , ,
an1,an2,...;
}
*/
char *outFileName="outputData.txt";
#define DEBUG 1
void main(int argc,char* argv[])
{
FILE *inputFile;/*input file*/
FILE *outputFile;/*output file*/
double startTime,endTime,tweenTime;/*time callopsed info*/
int rowNum,colNum;
double** wArr;
double maxLamda;
int n;/*arr deminision for squre matrix*/
/*default input file open*/
if(argc>1)strcpy(inFileName,argv[1]);
assertF((inputFile=fopen(inFileName,"rb"))!=NULL,"input file error");
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");
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");
#if DEBUG
printf("\n*******start of test program******\n");
printf("now is runnig,please wait...\n");
startTime=(double)clock()/(double)CLOCKS_PER_SEC;
/******************Core program code*************/
/*argu prepare*/
assertF(colNum==rowNum,"in test colNum!=rowNum");
n=rowNum;/*get the size of square matrix*/
maxLamda=powerMethodForLamda(wArr,n,"output2.txt");
printf("the max lamda is:%f.\r\n",maxLamda);
fprintf(outputFile,"the max lamda is:%f.\r\n",maxLamda);
/******************End of Core program**********/
endTime=(double)clock()/(double)CLOCKS_PER_SEC;
tweenTime=endTime-startTime;/*Get the time collapsed*/
/*Time collapsed output*/
printf("the collapsed time in this algorithm implement is:%f\n",tweenTime);
fprintf(outputFile,"the collapsed time in this algorithm implement is:%f\r\n",tweenTime);
printf("\n*******end of test program******\n");
#endif
twoDArrMemFree(&wArr,rowNum);
printf("program end successfully,\n you have to preess any key to clean the buffer area to output,otherwise,you wiil not get the total answer.\n");
getchar();/*Screen Delay Control*/
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -