C language to calculate linear equations

Posted by mrwhale on Thu, 10 Mar 2022 09:20:42 +0100

Today, let's solve linear equations with c.

There are two ways to solve linear equations manually.

1. Solve by elimination

2. Solve through matrix determinant, etc

Now we want to use c language to solve linear equations with determinant. Before solving, we must first learn the knowledge of determinant. If you don't know determinant, please go to Baidu determinant to learn it first and remember to come back~

It can be found that the digital arrangement of determinant is very similar to that of two-dimensional array, so we use two-dimensional array to save the contents of determinant. So we only need to store the input data into a two-dimensional array.

Because the size of the array must be determined when defining, and we may have to calculate a system of binary primary equations. We have four data, or we may have three data. We have nine data, so we need to define an a[2][2] for binary and a[3][3] for ternary. This is not a definite number, so we need to use a special way (dynamic memory allocation). This produces a dynamic array.

The code is as follows:

//Save from the input data into the array
double* getArrFromDet(int n)
{
	double num;
	double* p = (double*)calloc(1, sizeof(double) * n * n);
	if (!p)return -1;
	int i = 0;
	while (n * n != i)
	{
		scanf("%lf", &num);
		*(p + i) = num;
		i++;
	}
	return p;
}

Now our remaining problem is to calculate the value of the determinant in the array.

We've learned that there's a way to calculate determinants, called the Laplace theorem. A calculation method of Laplace's determinant. The theorem asserts that: in the n-order determinant D=|aij|, arbitrarily take K rows (columns), 1 ≤ K ≤ n-1, and the sum of the products of all k-order subformulas composed of the elements of the K rows (columns) and their algebraic cofactors is equal to the value of determinant D. This exhibition is called the Laplace exhibition.

So we use this theorem. We use the elements in the first row multiplied by the algebraic cofactor for each element. Algebraic cofactor is another determinant. Continue to use this method until there is only one left, and then return the value of this. Does this look like recursion? Let's use recursion to solve this problem

Oh ~ by the way, I don't seem to tell you how to find the cofactor of algebra. Now let's talk about his method.

Just remove all the rows and columns of the selected element to form a determinant that is a cofactor, and then the algebraic cofactor is a sign with a ± sign. For example, the algebraic cofactor of the element in row 1 and column 1 is 1 × The element in row 1 and column 2 is - 1 × Is there any law that can be found in the cofactor, that is (- 1) Λ (row + column) × Just determinant. Okay, I see

Let's look at the code.

Recursive end condition, leaving only one element in the determinant.

//Row and column calculation of array
double calDet(double *arr,int n)
{
	int i = 0;
    double sum = 0;
	if (n == 1)
	{
		return *arr;
	}
	else
	{
		while (i < n)
		{
			double* arrTemp = (double*)calloc(1, sizeof(double) * (n - 1) * (n - 1));
			double* temp = arrTemp;
			//Remove the row and column being recursive
			for (int k =1; k < n; k++)
			{
				for (int j = 0; j < n; j++)
				{
					if (j != i)
					{
						*temp = *((arr + k * n) + j);
						temp++;
					}
				}
			}
			sum += pow(-1,i)*(*(arr + i) * calDet(arrTemp, n - 1));//Recursive call
			i++;

		}
	}
	return sum;
}

Now that we can find determinants, there is only one problem left: calculate the values of multiple determinants and save them, and then use Kramer's law to calculate the solution of the equations. The specific codes are as follows:

//Solving linear equations
void solveEqu(int n)
{
	double value[127] = { 0 };
	//Get the value of d
	printf("d=");
	value[0] = calDet(getArrFromDet(n), n);
	int i = 1;
	while (i != n + 1)
	{
		printf("d%d=", i);
		value[i] = calDet(getArrFromDet(n), n);
		i++;
	}
	//Solution of output equation
	i = 1;
	while (i != n + 1)
	{
		if (value[0] == 0 && value[i] != 0)
		{
			printf("The equation has no solution");
			break;
		}
		else
		{

			printf("x%d = %lf\n", i, value[i] / value[0]);
			i++;
		}
	}
}

So we can solve a system of linear equations.

Next, let's look at this simple main function

int main()
{
	int n;
	printf("Enter the number of elements of the system of equations");
	scanf("%d", &n);
    solveEqu(n);
	while (1);
	return 0;
}

It's so simple. Is it useless

Topics: C linear algebra