由于里面有很多公式,不便于编辑,所以整理成图片!在后面附有程序所有源代码!
综合上面的分析,现定义一个函数CalculateDet(double[,] A, int DetN)用于计算矩阵A[n,n]的行列式,其中输入参数A表示矩阵,DetN表示矩阵的阶数。该函数的代码如下:
private double CalculateDet(double[,] A, int DetN)
{
double result=1;
int n = DetN;
for (int m = 0; m < n; m++)
{
//判断A[m,m]是否为0,如果为0,则找一个不为的A[p,m],然后将m行和p行调换即可
if (A[m, m] == 0)
{
int p = m + 1;
while (p < n-1 && A[p, m] == 0)
{
p++;
}
if (p == n )//如果p=n,则表示该列中元素全为0,接下来不用计算,直接输出结果为0
{
result = 0;
goto handle01;
}
else
{
double tempExchange;
for (int q = 0; q < n; q++)
{
tempExchange=A[m,q];
A[m, q] = A[p, q];
A[p, q] = tempExchange;
}
}
}
for (int i = m + 1; i < n; i++)
{
double temp = A / A[m, m];
for (int j = m + 1; j < n; j++)
{
A = A - temp * A[m, j];
}
}
}
for (int i = 0; i < n; i++)
{
result *= A;
}
handle01:
return result;
}
设计程序界面如下(各控件名称如下,没有指定名称的控件其名称与显示的名称一样):
程序中所有代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 矩阵计算程序
{
public partial class 行列式计算 : Form
{
public 行列式计算()
{
InitializeComponent();
}
private double CalculateDet(double[,] A, int DetN)
{
double result=1;
int n = DetN;
for (int m = 0; m < n; m++)
{
//判断A[m,m]是否为0,如果为0,则找一个不为的A[p,m],然后将m行和p行调换即可
if (A[m, m] == 0)
{
int p = m + 1;
while (p < n-1 && A[p, m] == 0)
{
p++;
}
if (p == n )//如果p=n,则表示该列中元素全为0,接下来不用计算,直接输出结果为0
{
result = 0;
goto handle01;
}
else
{
double tempExchange;
for (int q = 0; q < n; q++)
{
tempExchange=A[m,q];
A[m, q] = A[p, q];
A[p, q] = tempExchange;
}
}
}
for (int i = m + 1; i < n; i++)
{
double temp = A / A[m, m];
for (int j = m + 1; j < n; j++)
{
A = A - temp * A[m, j];
}
}
}
for (int i = 0; i < n; i++)
{
result *= A;
}
handle01:
return result;
}
private double CalculateDetWithProcess(double[,] A, int DetN)
{
double result = 1;
int n = DetN;
ShowCalProcess(A,0);
for (int m = 0; m < n; m++)
{
//判断A[m,m]是否为0,如果为0,则找一个不为的A[p,m],然后将m行和p行调换即可
if (A[m, m] == 0)
{
int p = m + 1;
while (p < n - 1 && A[p, m] == 0)
{
p++;
}
if (p == n)//如果p=n,则表示该列中元素全为0,接下来不用计算,直接输出结果为0
{
result = 0;
goto handle01;
}
else
{
double tempExchange;
for (int q = 0; q < n; q++)
{
tempExchange = A[m, q];
A[m, q] = A[p, q];
A[p, q] = tempExchange;
}
}
}
for (int i = m + 1; i < n; i++)
{
double temp = A / A[m, m];
for (int j = m + 1; j < n; j++)
{
A = A - temp * A[m, j];
}
}
//使m列中m行一下元素全部设置为0
for (int i = m+1; i < n; i++)
{
A = 0;
}
ShowCalProcess(A,m+1);
}
for (int i = 0; i < n; i++)
{
result *= A;
}
handle01:
return result;
}
private void 建立行列式_Click(object sender, EventArgs e)
{
DetDataGridView.Columns.Clear();
int detN = int.Parse(DetNumbertextBox.Text);
if (detN < 2)
{
MessageBox.Show("行列式行数小于2!请重新输入行数!");
}
else
{
for (int i = 0; i < detN; i++)
{
DetDataGridView.Columns.Add(i.ToString(), "第" + (1 + i).ToString()+"列");
}
DetDataGridView.Rows.Add(detN);
}
}
private void 计算行列式值_Click(object sender, EventArgs e)
{
int detN = int.Parse(DetNumbertextBox.Text);
double[,] A = new double[detN, detN];
for (int i = 0; i < detN; i++)
{
for (int j = 0; j < detN; j++)
{
A= double.Parse(DetDataGridView.Rows.Cells[j].Value.ToString());
}
}
double result;
result = CalculateDet(A, detN);
ProcessRichTextBox.Text += "/n计算行列式结果为:" + result.ToString();
}
private void 显示计算过程_Click(object sender, EventArgs e)
{
int detN = int.Parse(DetNumbertextBox.Text);
double[,] A = new double[detN, detN];
for (int i = 0; i < detN; i++)
{
for (int j = 0; j < detN; j++)
{
A = double.Parse(DetDataGridView.Rows.Cells[j].Value.ToString());
}
}
double result;
result = CalculateDetWithProcess(A, detN);
ProcessRichTextBox.Text += "/n计算行列式结果为:"+result.ToString();
}
private void ShowCalProcess(double[,] A, int time)
{
string s="第"+time.ToString()+"次计算结果:/n";
int detN = int.Parse(DetNumbertextBox.Text);
for (int i = 0; i < detN; i++)
{
for (int j = 0; j < detN-1; j++)
{
s+=A.ToString()+",";
}
s += A + "/n";
}
ProcessRichTextBox.Text += s;
}
}
}
运行程序,计算下面矩阵的行列式:
其计算结果为0,如下图所示: