本文发布矩阵(Matrix)的一些初级算法。
一、矩阵的行列式(Determinant)
矩阵行列式是指矩阵的全部元素构成的行列式,设A=(a)是数域P上的一个n阶矩阵,则所有A=(a)中的元素组成的行列式称为矩阵A的行列式,记为|A|或det(A)。若A,B是数域P上的两个n阶矩阵,k是P中的任一个数,则|AB|=|A||B|,|kA|=kⁿ|A|,|A*|=|A|,其中A*是A的伴随矩阵;若A是可逆矩阵,则|A|=|A|。
/// <summary>
/// 计算 A[p,q] 位于 [,]temp 的块辅因子
/// </summary>
/// <param name="matrix"></param>
/// <param name="temp"></param>
/// <param name="p"></param>
/// <param name="q"></param>
/// <param name="n"></param>
private static void BlockCofactor(double[,] matrix, ref double[,] temp, int p, int q, int n)
{
int i = 0;
int j = 0;
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n; col++)
{
if (row != p && col != q)
{
temp[i, j++] = matrix[row, col];
if (j == (n - 1))
{
j = 0;
i++;
}
}
}
}
}
/// <summary>
/// 求矩阵行列式(递归算法)
/// </summary>
/// <param name="N"></param>
/// <param name="matrix"></param>
/// <param name="n"></param>
/// <returns></returns>
public static double Determinant(int N, double[,] matrix, int n)
{
if (n == 1)
{
return matrix[0, 0];
}
double D = 0.0;
double[,] temp = new double[N, N];
int sign = 1;
for (int f = 0; f < n; f++)
{
BlockCofactor(matrix, ref temp, 0, f, n);
D += sign * matrix[0, f] * Determinant(N, temp, n - 1);
sign = -sign;
}
return D;
}
/// <summary>
/// 计算 A[p,q] 位于 [,]temp 的块辅因子
/// </summary>
/// <param name="matrix"></param>
/// <param name="temp"></param>
/// <param name="p"></param>
/// <param name="q"></param>
/// <param name="n"></param>
private static void BlockCofactor(double[,] matrix, ref double[,] temp, int p, int q, int n)
{int i = 0;int j = 0;for (int row = 0; row < n; row++){for (int col = 0; col < n; col++){if (row != p && col != q){temp[i, j++] = matrix[row, col];if (j == (n - 1)){j = 0;i++;}}}}
}/// <summary>
/// 求矩阵行列式(递归算法)
/// </summary>
/// <param name="N"></param>
/// <param name="matrix"></param>
/// <param name="n"></param>
/// <returns></returns>
public static double Determinant(int N, double[,] matrix, int n)
{if (n == 1){return matrix[0, 0];}double D = 0.0;double[,] temp = new double[N, N];int sign = 1;for (int f = 0; f < n; f++){BlockCofactor(matrix, ref temp, 0, f, n);D += sign * matrix[0, f] * Determinant(N, temp, n - 1);sign = -sign;}return D;
}
二、矩阵的伴随矩阵(Adjoint Matrix)
一个方形矩阵的伴随矩阵是一个类似于逆矩阵的概念。如果二维矩阵可逆,那么它的逆矩阵和它的伴随矩阵之间只差一个系数,对多维矩阵也存在这个规律。然而,伴随矩阵对不可逆的矩阵也有定义,并且不需要用到除法。
/// <summary>
/// 伴随矩阵
/// </summary>
/// <param name="A"></param>
/// <param name="adj"></param>
public static void Adjoint(double[,] matrix, out double[,] adjoint)
{
int N = matrix.GetLength(0);
adjoint = new double[N, N];
if (N == 1)
{
adjoint[0, 0] = 1.0;
return;
}
int sign = 1;
double[,] temp = new double[N, N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
BlockCofactor(matrix, ref temp, i, j, N);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adjoint[j, i] = (sign) * (Determinant(N, temp, N - 1));
}
}
}
/// <summary>
/// 伴随矩阵
/// </summary>
/// <param name="A"></param>
/// <param name="adj"></param>
public static void Adjoint(double[,] matrix, out double[,] adjoint)
{int N = matrix.GetLength(0);adjoint = new double[N, N];if (N == 1){adjoint[0, 0] = 1.0;return;}int sign = 1;double[,] temp = new double[N, N];for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){BlockCofactor(matrix, ref temp, i, j, N);sign = ((i + j) % 2 == 0) ? 1 : -1;adjoint[j, i] = (sign) * (Determinant(N, temp, N - 1));}}
}
三、矩阵的逆矩阵(Inverse Matrix)
设A是一个n阶矩阵,若存在另一个n阶矩阵B,使得: AB=BA=E ,则称方阵A可逆,并称方阵B是A的逆矩阵。矩阵求逆,即求矩阵的逆矩阵。矩阵是线性代数的主要内容,很多实际问题用矩阵的思想去解既简单又快捷。逆矩阵又是矩阵理论的很重要的内容,逆矩阵的求法自然也就成为线性代数研究的主要内容之一。
/// <summary>
/// 矩阵求逆
/// </summary>
/// <param name="A"></param>
/// <param name="inverse"></param>
/// <returns></returns>
public static bool Inverse(double[,] matrix, out double[,] inverse)
{
int N = matrix.GetLength(0);
inverse = new double[N, N];
double det = Determinant(N, matrix, N);
if (det == 0)
{
return false;
}
Adjoint(matrix, out double[,] adj);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
inverse[i, j] = adj[i, j] / (double)det;
}
}
return true;
}
/// <summary>
/// 矩阵求逆
/// </summary>
/// <param name="A"></param>
/// <param name="inverse"></param>
/// <returns></returns>
public static bool Inverse(double[,] matrix, out double[,] inverse)
{int N = matrix.GetLength(0);inverse = new double[N, N];double det = Determinant(N, matrix, N);if (det == 0){return false;}Adjoint(matrix, out double[,] adj);for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){inverse[i, j] = adj[i, j] / (double)det;}}return true;
}
演算代码:
private void button1_Click(object sender, EventArgs e)
{
double[,] A = {
{5, -2, 2, 7},
{1, 0, 0, 3},
{-3, 1, 5, 0},
{3, -1, -9, 4}
};
double d = Algorithm_Gallery.Determinant(4, A, 4);
StringBuilder sb = new StringBuilder();
sb.Append(Welcome());
sb.AppendLine("1、<b>原始矩阵</b>(Source Matrix):<br>");
sb.Append(Algorithm_Gallery.ToHtml(A));
sb.AppendLine("行列式(Determinant)=" + d + "<br>");
Algorithm_Gallery.Adjoint(A, out double[,] adj);
sb.AppendLine("<br>2、<b>伴随矩阵</b>(Adjoint Matrix):<br>");
sb.Append(Algorithm_Gallery.ToHtml(adj));
Algorithm_Gallery.Inverse(A, out double[,] inv);
sb.AppendLine("<br>3、<b>逆矩阵</b>(Inverse Matrix):<br>");
sb.Append(Algorithm_Gallery.ToHtml(inv));
sb.Append(Bye());
webBrowser1.DocumentText = sb.ToString();
}
private void button1_Click(object sender, EventArgs e)
{double[,] A = { {5, -2, 2, 7},{1, 0, 0, 3},{-3, 1, 5, 0},{3, -1, -9, 4}};double d = Algorithm_Gallery.Determinant(4, A, 4);StringBuilder sb = new StringBuilder();sb.Append(Welcome());sb.AppendLine("1、<b>原始矩阵</b>(Source Matrix):<br>");sb.Append(Algorithm_Gallery.ToHtml(A));sb.AppendLine("行列式(Determinant)=" + d + "<br>");Algorithm_Gallery.Adjoint(A, out double[,] adj);sb.AppendLine("<br>2、<b>伴随矩阵</b>(Adjoint Matrix):<br>");sb.Append(Algorithm_Gallery.ToHtml(adj));Algorithm_Gallery.Inverse(A, out double[,] inv);sb.AppendLine("<br>3、<b>逆矩阵</b>(Inverse Matrix):<br>");sb.Append(Algorithm_Gallery.ToHtml(inv));sb.Append(Bye());webBrowser1.DocumentText = sb.ToString();
}
打印矩阵的代码:
public static string ToHtml(double[,] y)
{
int m = y.GetLength(0);
int n = y.GetLength(1);
StringBuilder sb = new StringBuilder();
sb.AppendLine("<style>");
sb.AppendLine("td { padding:5px;text-align:right; }");
sb.AppendLine("</style>");
sb.AppendLine("<table width='100%' border=1 bordercolor='#999999' style='border-collapse:collapse;'>");
for (int i = 0; i < m; i++)
{
sb.AppendLine("<tr>");
for (int j = 0; j < n; j++)
{
sb.AppendLine("<td>" + String.Format("{0:F8}", y[i, j]) + "</td>");
}
sb.AppendLine("</tr>");
}
sb.AppendLine("</table>");
return sb.ToString();
}
————————————————————————————————
POWER BY TRUFFER.CN 50018.COM 315SOFT.COM
public static string ToHtml(double[,] y)
{int m = y.GetLength(0);int n = y.GetLength(1);StringBuilder sb = new StringBuilder();sb.AppendLine("<style>");sb.AppendLine("td { padding:5px;text-align:right; }");sb.AppendLine("</style>");sb.AppendLine("<table width='100%' border=1 bordercolor='#999999' style='border-collapse:collapse;'>");for (int i = 0; i < m; i++){sb.AppendLine("<tr>");for (int j = 0; j < n; j++){sb.AppendLine("<td>" + String.Format("{0:F8}", y[i, j]) + "</td>");}sb.AppendLine("</tr>");}sb.AppendLine("</table>");return sb.ToString();
}