using System;
namespace Zhou.CSharp.Algorithm
{
/// <summary>
/// 矩阵类
/// 作者:周长发
/// 改进:深度混淆
/// https://blog.csdn.net/beijinghorn
/// </summary>
public partial class Matrix
{
/// <summary>
/// 约化一般实矩阵为赫申伯格矩阵的初等相似变换法
/// </summary>
/// <param name="src">源矩阵</param>
public static void MakeHberg(Matrix src)
{
int i = 0, j, k, u, v;
double d, t;
for (k = 1; k <= src.Columns - 2; k++)
{
d = 0.0;
for (j = k; j <= src.Columns - 1; j++)
{
u = j * src.Columns + k - 1;
t = src[u];
if (Math.Abs(t) > Math.Abs(d))
{
d = t;
i = j;
}
}
if (Math.Abs(d) > float.Epsilon) //d != 0.0)
{
if (i != k)
{
for (j = k - 1; j <= src.Columns - 1; j++)
{
u = i * src.Columns + j;
v = k * src.Columns + j;
t = src[u];
src[u] = src[v];
src[v] = t;
}
for (j = 0; j <= src.Columns - 1; j++)
{
u = j * src.Columns + i;
v = j * src.Columns + k;
t = src[u];
src[u] = src[v];
src[v] = t;
}
}
for (i = k + 1; i <= src.Columns - 1; i++)
{
u = i * src.Columns + k - 1;
t = src[u] / d;
src[u] = 0.0;
for (j = k; j <= src.Columns - 1; j++)
{
v = i * src.Columns + j;
src[v] = src[v] - t * src[k * src.Columns + j];
}
for (j = 0; j <= src.Columns - 1; j++)
{
v = j * src.Columns + k;
src[v] = src[v] + t * src[j * src.Columns + i];
}
}
}
}
}
}
}