int sgn(double x)
{
if(x>0)
{
return 1;
}else if(x==0)
{
return 0;
}else
{
return -1;
}
}
vector<vector<double> > Hessenberg(vector<vector<double> > A)
{
for(int r=0;r<(int)A.size()-2;++r)
{
vector<double> ar(A.size(),0);
for(int i=0;i<(int)A.size();++i)
{
ar=A[r];
}
double c=0;
for(int i=r+1;i<(int)A.size();++i)
{
c+=pow(ar,2);
}
c=sqrt(c);
c=(-c*sgn(ar[r+1]));
double p=sqrt(2*c*(c-ar[r+1]));
vector<double> u(A.size(),0);
for(int i=r+1;i<(int)A.size();++i)
{
if(i==r+1)
{
u=(ar-c)/p;
}
else
{
u=ar/p;
}
}
vector<vector<double> > I;
for(int i=0;i<(int)A.size();++i)
{
vector<double> z(A.size(),0);
z=1;
I.push_back(z);
}
vector<vector<double> > H=I-2*(u*u);
bool s;
A=H*A*Inverse(H,s);
}
return A;
}
vector<vector<double> > QR(vector<vector<double> > A,vector<vector<double> > &Q)
{
vector<vector<double> > I;
for(int i=0;i<(int)A.size();++i)
{
vector<double> z(A.size(),0);
z=1;
I.push_back(z);
}
for(int i=0;i<(int)A.size()-1;++i)
{
double theta=atan(A[i+1]/A);
vector<vector<double> > P;
for(int r=0;r<(int)A.size();++r)
{
vector<double> z(A.size(),0);
z[r]=1;
P.push_back(z);
}
P[i+1]=sin(theta);
P=cos(theta);
P[i+1][i+1]=cos(theta);
P[i+1]=(-sin(theta));
I=P*I;
vector<double> aa(A);
vector<double> aa1(A[i+1]);
for(int j=i;j<(int)A.size();++j)
{
A[j]=aa[j]*cos(theta)+aa1[j]*sin(theta);
A[i+1][j]=(-aa[j]*sin(theta)+aa1[j]*cos(theta));
}
}
bool s;
Q=Inverse(I,s);
return A;
}
void Print(vector<vector<double> > a,string s)
{
ofstream out(s.c_str());
for(int i=0;i<(int)a.size();++i)
{
for(int j=0;j<(int)a.size();++j)
{
out<<a[j]<<"\t";
}
out<<endl;
}
}
double Delta(vector<vector<double> > a)
{
double ss=0;
for(int i=0;i<(int)a.size();++i)
{
for(int j=0;j<(int)a.size();++j)
{
if(i!=j)
{
if(fabs(a[j])>ss)
{
ss=fabs(a[j]);
}
}
}
}
return ss;
}
double Delta1(vector<vector<double> > A)
{
double d=0;
for(int i=0;i<(int)A.size()-1;++i)
{
if(fabs(A[i+1])>d)
{
d=fabs(A[i+1]);
}
if(fabs(A[i+1])>d)
{
d=fabs(A[i+1]);
}
}
return d;
}
vector<complex<double> > Namta(vector<vector<double> > A,double delta)
{
double delta1=0;
while((Delta(A)>=delta)&&(fabs(Delta1(A)-delta1)>=delta))
{
delta1=Delta1(A);
A=Hessenberg(A);
vector<vector<double> > Q;
vector<vector<double> > R;
R=QR(A,Q);
A=R*Q;
}
string s("An.txt");
Print(A,s);
vector<complex<double> > namta;
if(Delta(A)<delta)
{
for(int i=0;i<(int)A.size();++i)
{
complex<double> dd(A,0);
namta.push_back(dd);
}
}
else
{
int r=0;
while(r<A.size()-1)
{
if(fabs(A[r+1][r])>=delta)
{
double b=-(A[r][r]+A[r+1][r+1]);
double c=(A[r][r]*A[r+1][r+1]-A[r][r+1]*A[r+1][r]);
if(pow(b,2)-4*c<0)
{
complex<double> d1(-b/2,sqrt(4*c-pow(b,2))/2);
complex<double> d2(-b/2,-sqrt(4*c-pow(b,2))/2);
namta.push_back(d1);
namta.push_back(d2);
}
else
{
complex<double> d1(-b/2+sqrt(pow(b,2)-4*c)/2,0);
complex<double> d2(-b/2-sqrt(pow(b,2)-4*c)/2,0);
namta.push_back(d1);
namta.push_back(d2);
}
r+=2;
}
else
{
complex<double> d(A[r][r],0);
namta.push_back(d);
++r;
}
}
if(r==A.size()-1)
{
complex<double> d(A[r][r],0);
namta.push_back(d);
}
}
return namta;
}
void Print(vector<complex<double> > a)
{
for(int i=0;i<(int)a.size();++i)
{
cout<<a<<endl;
}
}
void Print(vector<double> a)
{
for(int i=0;i<(int)a.size();++i)
{
cout<<a<<"\t";
}
cout<<endl;
}
double Delta(vector<double> a,vector<double> b)
{
double x=0;
for(int i=0;i<(int)a.size();++i)
{
if(fabs(a-b)>x)
{
x=fabs(a-b);
}
}
return x;
}
double Max(vector<double> a)
{
double x=0;
int n=0;
for(int i=0;i<(int)a.size();++i)
{
if(fabs(a)>x)
{
x=fabs(a);
n=i;
}
}
return a[n];
}
vector<double> operator / (vector<double> a,double b)
{
for(int i=0;i<(int)a.size();++i)
{
a/=b;
}
return a;
}
vector<double> ComputeVector(vector<vector<double> > A,complex<double> namta,double delta)
{
vector<vector<double> > I;
for(int i=0;i<(int)A.size();++i)
{
vector<double> z(A.size(),0);
z=1;
I.push_back(z);
}
A=A-namta.real()*I;
string s("A-1.txt");
bool d;
A=Inverse(A,d);
Print(A,s);
vector<double> z(A.size(),1);
vector<double> z1;
do
{
z1=z;
vector<double> y=A*z;
double m=Max(y);
z=y/m;
}while(Delta(z,z1)>=delta);
return z;
}
int main()
{
ifstream ina("A.txt");
cout<<"输入矩阵的维数:"<<endl;
int n;
cin>>n;
vector<vector<double> > A(n);
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
double x;
if(ina>>x)
{
A.push_back(x);
}
}
}
cout<<"输入误差:"<<endl;
double delta;
cin>>delta;
string s("namta.txt");
vector<complex<double> > namta=Namta(A,delta);
cout<<"特征值:"<<endl;
Print(namta);
cout<<namta[1]<<"所对应的特征向量:"<<endl;
Print(ComputeVector(A,namta[1],0.00001));
system("AUSE");
} |