幂法的原理可参考此篇论文:http://d.wanfangdata.com.cn/Periodical/hnnydxxb2001Z1023

本文求解的是 3 阶矩阵最大特征值及其特征向量

下面是其 C++ 实现代码:

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iomanip>
using namespace std; double A[3][3];
double Y[3]={1,1,1};
double X[3]={0,0,0};
int row=0; int col=0;
double max1=0; void open_file()
{
FILE *fp;
fp = fopen("array.txt", "r"); //3*3矩阵由外部读入
if(fp==NULL)
cout<<"File opened failed!"<<endl; for(row=0;row<3;row++)
{
for(col= 0; col < 3; col ++)
fscanf(fp, "%lf,",&A[row][col]);
if(feof(fp)) break;
}
fclose(fp);
} void mult()
{
X[0]=0;X[1]=0;X[2]=0;
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
X[row] +=A[row][col]*Y[col];
}
} void to1()
{
long double tmp=fabs(X[0]);
for(int i=1;i<3;i++)
{
if(fabs(X[i])>tmp)
tmp=fabs(X[i]);
}
for(int i=0;i<3;i++)
{
Y[i]=X[i]/tmp;
}
max1=tmp;
} int main()
{
cout <<setiosflags(ios::fixed);
open_file();
double ago=max1+100.0;
double feature_vector[3];
int k=1;
while(fabs(max1-ago)>0.000001)
{
ago=max1;
for(int j=0;j<3;j++)
{
feature_vector[j]=Y[j];
}
mult();
to1();
cout<<"k= "<<k<<" ";
for(int i=0;i<3;i++)
cout<<X[i]<<" ";
cout<<endl;
k++;
}
cout<<endl<<"totally run "<<k-1<<" times"<<endl;
cout<<endl<<"the matrix eigenvalue is "<<max1<<endl;
cout<<endl<<"the feature vector is "<<"["<<feature_vector[0]<<" , "<<feature_vector[1]<<" , "<<feature_vector[2]<<"]"<<endl; }

  部分参数可修改用于扩展

最新文章

  1. [LeetCode] Can I Win 我能赢吗
  2. int与CString互相转化
  3. The communication of Linux Server and Localtion
  4. maven使用
  5. java 代码的细节优化
  6. C++开源代码覆盖率工具OpenCppCoverage介绍(Windows)
  7. python 任意新闻正文提取
  8. 面向过程部分 Java 和 C++ 的区别
  9. PHP学习笔记-00
  10. Optimal Milking
  11. vijosP1471 教主的游乐场
  12. PS:改装店收的是友情价,包安装十五个毛主席。
  13. MySQL与mabits大小比较、日期比较示例
  14. 【配置】电信华为HG8245 无线路由器配置 有贴图
  15. C++不支持Unicode,即使utf8
  16. poj 3268 最短路dijkstra *
  17. Leetcode刷题记录:计算复数乘法
  18. java对象内存占用
  19. UniGUI的TUniLoginForm窗口自定义背景色
  20. EJB3 jpa 数据库表的映射关系

热门文章

  1. python内置模块(time模块)
  2. sh_14_字符串定义和遍历
  3. dom4j读写XML文档
  4. 局域网与internet
  5. C++入门经典-例6.23-字符串数组赋值与string
  6. 微信小程序支持windows PC版了
  7. linux开启数据库远程连接
  8. mysql timestamp为0值时,python读取后的对象为None
  9. mysql常用操作与日志
  10. 编写javad代码实现使用Scanner从键盘读取一行输入,去掉其中重复字符, 打印出不同的那些字符