using System;
namespace Zhou.CSharp.Algorithm
{
/// <summary>
/// 求解线性方程组的类 LEquations
/// 原作 周长发
/// 改编 深度混淆
/// </summary>
public static partial class LEquations
{
/// <summary>
/// 求解线性最小二乘问题的广义逆法
/// </summary>
/// <param name="mtxLECoef">指定的系数矩阵</param>
/// <param name="mtxLEConst">指定的常数矩阵</param>
/// <param name="mtxResult">Matrix对象,返回方程组解矩阵</param>
/// <param name="mtxAP">Matrix对象,返回系数矩阵的广义逆矩阵</param>
/// <param name="mtxU">Matrix对象,返回U矩阵</param>
/// <param name="mtxV">Matrix对象,返回V矩阵</param>
/// <param name="eps">控制精度</param>
/// <return>bool 型,方程组求解是否成功</return>
public static bool GetRootsetGinv(Matrix mtxLECoef, Matrix mtxLEConst, Matrix mtxResult, Matrix mtxAP, Matrix mtxU, Matrix mtxV, double eps)
{
// 方程个数和未知数个数
int m = mtxLECoef.GetNumRows();
int n = mtxLECoef.GetNumColumns();
// 初始化解向量
mtxResult.Init(n, 1);
double[] pDataConst = mtxLEConst.GetData();
double[] x = mtxResult.GetData();
// 临时矩阵
Matrix mtxA = new Matrix(mtxLECoef);
// 求广义逆矩阵
if (!Matrix.InvertUV(mtxA, mtxAP, mtxU, mtxV, eps))
{
return false;
}
double[] pAPData = mtxAP.GetData();
// 求解
for (int i = 0; i <= n - 1; i++)
{
x[i] = 0.0;
for (int j = 0; j <= m - 1; j++)
{
x[i] = x[i] + pAPData[i * m + j] * pDataConst[j];
}
}
return true;
}
}
}