33,008
社区成员
发帖
与我相关
我的任务
分享
public class solution
{
private double v;
private double [] weight;
public solution(double v, double [] weight)
{
this.v = v;
this.weight = weight;
}
public double V
{
get
{
return this.v;
}
set
{
this.v = value;
}
}
public double [] Weight
{
get
{
return this.weight;
}
set
{
this.weight = value;
}
}
}
/// <summary>
/// 幂法求矩阵最大特征根和特征向量实现
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
public static solution GetSolution(double [][] matrix)
{
double [] DL = new double[matrix.Length];
double [] tmp = new double[matrix.Length];
double [] tmp1 = new double[matrix.Length];
for (int i = 0; i < matrix.Length; i++)
tmp[i] = 1;
do
{
DL = tmp;
double max = Max(tmp);
for (int i = 0; i < tmp.Length; i++)
tmp1[i] = tmp[i] / max;
tmp = mMultiply(matrix, tmp1);
}while(Math.Abs(Max(tmp) - Max(DL)) > 0.000001);
DL = tmp;
double maxVL = Max(DL);
double sum = 0.0;
foreach (double d in DL)
sum += d;
for (int i = 0; i < DL.Length; i++)
DL[i] /= sum;
return new solution(maxVL, DL);
}