Solving Linear Equations in C Language
Classical problems are solved using the Gaussian appointment algorithm. This requires proper processing of any form of linear equations. It cannot be used only when the number of equations is equal to the number of unknown equations.
First, use the cyclic structure to convert the Augmented Matrix into a stepped matrix. At the end of the loop, a non-zero row number in the tiered matrix is obtained. At the same time, a linked list contains the column labels of each non-zero row principal component, the column names in the linked list are decreased sequentially from left to right. Then, based on the solution of the Linear Equations in linear algebra and the criterion for determining whether the equation has a solution and how many solutions there are. When the linear equations have solutions, you need to use the convert function to convert them into a simplified row-level matrix, and then output the unique or general solution.
The C language code is as follows:
# Include <stdio. h>
# Include <malloc. h>
# Include <math. h>
# Define N 5 // Number of columns in the Augmented Matrix
# Define M 3 // number of rows in the Augmented Matrix
Struct maincol
{
Int col; // contains the struct type of each subject under the main component.
Struct maincol * next;
};
Typedef struct maincol mc1;
Int test (int s, int t, float a [] [N]); // determine whether the child matrix formed by the intersection of the s row of the augmented matrix to the M row and the t column to the N column is zero. If 0 is returned, if the column label of the first non-zero column is not returned
Void add (mc1 * head, int col, mc1 ** tail); // function, used to create a node, which contains the principal component col column label, insert it into the maincol-type linked list in descending order.
Void convert (float a [] [N], int row, mc1 * head); // function, used to convert a tiered matrix into a SIMPLIFIED row-step trapezoid Matrix
Void main ()
{
Float a [M] [N]; // Augmented Matrix
Char str [N + 1];
Int I, j;
Int s, t; // subscript of the element row and column in the upper left corner of the Child Matrix
Int row, col; // row is used to store non-zero rows in the step matrix.
Float swap;
Mc1 * head, * tail, * psnew;
For (I = 0; I <M; I ++) // input and initialize the Augmented Matrix
{
Printf ("Enter % d line \ n of the augmented matrix", I + 1 );
Scanf ("% s", str );
For (j = 0; j <N; j ++)
A [I] [j] = str [j]-48;
}
Head = (mc1 *) malloc (sizeof (mc1 ));
Head-> next = NULL;
Tail = head;
S = t = 1; // The Child matrix is the Augmented Matrix itself. The columns in the upper left corner of the augmented matrix are used to initialize s, t
While (col = test (s, t, ))! = 0) // the submatrix is not zero.
{
If (s = M) // The augmented matrix is converted into a step matrix.
{
Row = s; // records the number of non-zero rows
Add (head, col, & tail); // The primary key column of the last Non-zero row is marked in the maincol type linked list.
Break; // end Loop
}
Else
{
J = s-1;
For (I = s; I <M; I ++)
{
If (fabs (a [j] [col-1]) <fabs (a [I] [col-1]) // select the principal element of a column
J = I;
}
If (S-1! = J)
{
For (I = col-1; I <N; I ++)
{
Swap = a [j] [I];
A [j] [I] = a [s-1] [I]; // select the principal element of a column
A [s-1] [I] = swap;
}
}
If (col = N) // The augmented matrix has been converted into a step matrix.
{
Row = s; // records the number of non-zero rows
Add (head, col, & tail); // The primary key column of the last Non-zero row is marked in the maincol type linked list.
Break; // end Loop
}
For (I = s; I <M; I ++)
A [I] [col-1] =-(a [I] [col-1]/a [s-1] [col-1]);
For (I = col; I <N; I ++) // decode
{
For (j = s; j <M; j ++)
A [j] [I] = a [j] [col-1] * a [s-1] [I] + a [j] [I];
}
Add (head, col, & tail); // put the obtained Principal Component column label col after elimination into the maincol type linked list
S ++;
T = col + 1; // update s, t, so that s, t becomes the column and column icon in the upper left corner of the new Child matrix after the elimination, to prepare for the test function to operate the new Child Matrix
Continue; // start a new cycle
}
}
If (col = 0) // exit from the Loop Control Condition
Row = s-1; // at this time, the Augmented Matrix has become a step matrix. The non-zero row function is S-1.
If (row = 0) // determine the number of solutions to a linear equations based on the discriminant Criterion
{
Printf ("linear equations have infinite Multiple groups of solutions \ n"); // The Augmented Matrix is a zero matrix and an infinite Multiple GROUPS OF SOLUTIONS
Printf ("general solution: \ n ");
For (I = 1; I <N; I ++)
Printf ("x % d = t % d \ n", I, I); // output Solution
}
Else
{
Psnew = head-> next;
If (psnew-> col = N) // The last principal component of the Step matrix is in the last column, with no solution
Printf ("Linear Equations without solutions \ n ");
Else
{
Convert (a, row, head); // use the convert function to convert the step matrix into a simplified step matrix.
If (row = N-1) // The number of non-zero rows equals the number of unknown rows, with a unique solution
{
Printf ("the linear equations have a unique solution: \ n ");
For (I = 1; I <= row; I ++) // unique output Solution
Printf ("x % d = % f \ n", I, a [I-1] [N-1]);
}
Else // The number of non-zero rows is smaller than the number of unknown rows, and there are infinite GROUPS OF SOLUTIONS
{
Int * m;
M = (int *) malloc (N-1-row) * sizeof (int ));
I = N-1-row;
For (j = N-1; j> = 1; j --)
{
If (j! = Psnew-> col)
{
M [-- I] = j; // select a free unknown number from all unknown numbers.
If (I = 0)
Break;
}
Else
{
If (psnew-> next! = NULL)
Psnew = psnew-> next;
}
}
Printf ("linear equations have infinite groups of solutions \ n ");
Printf ("general solution: \ n ");
I = row;
For (psnew = head-> next; psnew! = NULL; psnew = psnew-> next)
{
Printf ("x % d = % f", psnew-> col, a [I-1] [N-1]); // output General Solution
For (j = 0; j <N-1-row; j ++)
{
If (m [j] <psnew-> col)
{
Printf ("-% dx % d", 0, m [j]);
}
Else
{
Printf ("-% fx % d", a [I-1] [m [j]-1], m [j]);
}
}
Printf ("\ n ");
I --;
}
}
}
}
}
Int test (int s, int t, float a [] [N]) // determine whether the child matrix formed by the intersection of the s row of the augmented matrix to the M row and the t column to the N column is zero. If 0 is returned, if the column label of the first non-zero column is not returned
{
Int I, j;
For (j = T-1; j <N; j ++)
{
For (I = s-1; I <M; I ++)
{
If (a [I] [j]! = 0)
Return (j + 1 );
}
}
Return (0 );
}
Void add (mc1 * head, int col, mc1 ** tail) // function, which is used to create a node and stores the principal component col column label, insert it into the maincol-type linked list in descending order.
{
Mc1 * psnew;
Psnew = (mc1 *) malloc (sizeof (mc1 ));
Psnew-> col = col;
If (head-> next = NULL)
{
Psnew-> next = NULL;
Head-> next = psnew;
* Tail = psnew;
}
Else
{
Psnew-> next = head-> next;
Head-> next = psnew;
}
}
Void convert (float a [] [N], int row, mc1 * head) // function, used to convert a tiered matrix into a SIMPLIFIED row-step trapezoid Matrix
{
Mc1 * psnew, * pq;
Int I, j, k, m;
Psnew = head-> next;
For (I = row-1; I> = 0; I --)
{
If (a [I] [psnew-> col-1]! = 1) // each non-zero line is PCA as 1
{
For (j = psnew-> col; j <N; j ++)
A [I] [j] = a [I] [j]/a [I] [psnew-> col-1];
}
Psnew = psnew-> next;
}
Psnew = head-> next; // You can describe to the upper part of the principal component in the column except the first principal component to zero.
For (I = row-1; I> = 1; I --)
{
M = N-psnew-> col-(row-I); // obtain the number of unknown numbers 1, 2 ,--, number of free unknown numbers on the right of the principal component column number in a non-zero I + 1 N-1
For (j = I-1; j> = 0; j --)
{
Pq = head-> next; // pq points to the node that stores the last non-zero row primary key column number.
For (k = N; k> psnew-> col; k --)
{
If (k! = Pq-> col)
{
A [j] [k-1] =-(a [I] [k-1] * a [j] [psnew-> col-1]) + a [j] [k-1]; // perform an elementary row transformation from the right to the left until the column position on the right of the column where the principal element of the I + 1 row is located, and skip the column where the principal element of the I + 2 ---- row is located.
M --;
If (m = 0)
Break;
}
Else
{
If (pq-> next! = Psnew)
Pq = pq-> next;
}
}
}
Psnew = psnew-> next; // progressive to the first line of principal component, preparing for a new round of upward Elimination
}
}