C# Program for Calculating the Value of Determinants of Order N and the System of First Order Equations of N Elements

C# Program for Calculating the Value of Determinants of Order N and the System of First Order Equations of N Elements

It took quite a long time to complete the C # program for calculating the values of determinants of order N and the first order equations of order N. Because he did not consult other information on the Internet, so he can only use the most naive ideas and basic algorithms to program. Before I give you the code, let me give you a few crude comments...

1. Mathematical Thought: With the formula provided by Gauss in linear algebra, we can easily get a unified method of calculating the solution of N-order equation: xn=Dn/D. D is the determinant value of the coefficient matrix, and Dn is the new determinant value obtained by replacing each column value of the coefficient matrix with the result of each equation. So our key problem is (1) how to calculate the value of a determinant of order N (2) how to get a new determinant of order N. The following two key issues are discussed.

Question 1: How to calculate the value of determinant of order N. I didn't choose some methods on the internet, such as "adding edges" and so on. The basic calculating formula of N-order determinant is selected. That is to say, all elements of any row or column are multiplied by their remainder formula to reduce the order. Finally, the product of the principal diagonal line and the product of the subtractive pair diagonal line are used to calculate the second order. Simple thinking has the characteristics of "easy to understand, difficult to operate or low performance". The essence of this method is recursion.

Problem 2: Problem 2 is better than Problem 1. Traversing each column, replacing column group with equation value group, creating new determinant and calculating it in function of Problem 1.

The following code is attached:

static void Main()
{
bool tap = true;
while (tap)
{
//Output title and input order
Console.SetCursorPosition(48, 3); Console.WriteLine("solution N Primary equations of variables");
Console.Write("Please enter N The Order of the System of Element Equations (Number of Unknown Numbers):");
int n = Convert.ToInt32(Console.ReadLine());

//Input the coefficients and results of each line equation in turn
double[,] Xishu = new double[n, n];
double[] zhi = new double[n];
double[] EachLineResult = new double[n];
Console.WriteLine("Please enter the number of coefficients and results per line in turn:");
Console.WriteLine();
for (int i = 0; i < n; i++)
{
Console.WriteLine("Please enter _____________{0}Coefficient and result values of rows", i + 1);
for (int j = 0; j < n; j++)
{
Xishu[i, j] = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("Please enter _____________{0}Result values of rows", i + 1);
}

//Calculate the value of determinant and replace the value of coefficient determinant with the result value
double result = Hanglieshi(n, Xishu);
//Test Sentence 1: Console.WriteLine("The results of calculating the determinant are as follows:{0}", result);
if (result == 0) Console.WriteLine("This equation has no solution!");//The determinant value is 0 and the equation has no solution.
else
{
for (int i = 0; i < n; i++)
{
double[,] TempXishu = new double[n, n];
for (int ii = 0; ii < n; ii++)
{
for (int jj = 0; jj < n; jj++)
{
TempXishu[ii, jj] = Xishu[ii, jj];
}
}
EachLineResult[i] = Rexishu(i, TempXishu, zhi, n);
//Test Sentence 2: Console.WriteLine("First{0}The value of the result determinant is:{1}",i+1,EachLineResult[i]);
}

//Output the value of each result
Console.WriteLine();
Console.WriteLine("{0}The solution set of the system of first order equations of variables is as follows:", n);
for (int i = 0; i < n; i++)
{
Console.WriteLine("X{0}:{1}", i + 1, EachLineResult[i] / result);
}
}

Console.WriteLine();
Console.WriteLine("Do you want to continue the calculation? Answer: Yes or No");
string choice = Console.ReadLine();
while (choice != "yes"&& choice  != "No") choice = Console.ReadLine();
if (choice == "yes") {  Console.Clear();  }
else tap = false;
}

//Calculating determinant function: Calculating by recursive and determinant mathematical formulas. The time complexity is O(n Cubic)，Low performance.
double Hanglieshi (int N,double [,] xishu)
{
double Mo = 0;
if (N == 0) return 0;
else if (N == 1) return xishu[0, 0];
else if (N == 2) return xishu[0, 0] * xishu[1, 1] - xishu[0,1] * xishu[1,0];
else
{

for (int i = 0; i < N; i++)
{
double[,] NewXishu = new double[N - 1, N - 1];
for(int j = 0; j < N - 1; j++)
{
int mark = 0;
for (int k = 0; k <N-1; k++)
{

if (k == i) { NewXishu[j, k] = xishu[j + 1, mark + 1]; mark++; }
else NewXishu[j, k] = xishu[j + 1, mark];
//Console.WriteLine("k The value is:{0}\tmark The value is:{1}\t The value of the array is:{2}",k,mark,NewXishu[j,k]);
mark++;
}
}
//Console.WriteLine("This is the first{0}Subcycle",i+1);
if(i%2==0)
Mo += xishu[0,i]*Hanglieshi(N - 1, NewXishu);
else
Mo -= xishu[0, i] * Hanglieshi(N - 1, NewXishu);
}
return Mo;
}
}
/*Create a new array to replace the column value with the result value of the equation. The time complexity is O (n). The main problem is the space complexity.
At the same time, we need to copy the original array, so we need O (n cubic). Note: Normal function parameters are passed by value, function internal parameters are not.
Change the value of the external argument of the function. But arrays are special and will be changed. */
double Rexishu(int lieshu,double [,]xishu,double[]Zhi,int Size)
{
Console.WriteLine();

for (int i = 0; i <Size; i++)
{
xishu[i, lieshu] = Zhi[i];
}
double resulti=Hanglieshi(Size,xishu);
return resulti;
}

}

Tags: C#

Posted on Wed, 09 Oct 2019 05:51:46 -0700 by abdbuet