C language for Solving Linear Equations, Solving Linear Equations

Source: Internet
Author: User

C language for Solving Linear Equations, Solving Linear Equations

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:

1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <math.h>
  4 #define N 5 // Enlarge the number of matrix columns
  5 #define M 3 // Expand matrix rows
  6 struct maincol
  7 {
  8 int col; // The structure type that stores the index of each component
  9 struct maincol * next;
 10};
 11 typedef struct maincol mc1;
 12
 13 int test (int s, int t, float a [] [N]); // Determine whether the sub-matrix formed by the intersection of rows s to M of the augmented matrix and columns t to N is a zero matrix, and returns 0 if it is If not the column index of the first non-zero column
 14 void add (mc1 * head, int col, mc1 ** tail); // Function, used to create a new node, which contains the col column label of the principal, and then insert it into the maincol type linked list in descending order
 15 void convert (float a [] [N], int row, mc1 * head); // Function for converting a stepwise matrix into a simplified row stepwise matrix
 16
 17 void main ()
 18 {
 19 float a [M] [N]; // augment matrix
 20 char str [N + 1];
 21 int i, j;
 22 int s, t; // Sub-matrix row and column subscripts
 23 int row, col; // row is used to store the number of non-zero rows and rows of the ladder matrix
 24 float swap;
 25 mc1 * head, * tail, * psnew;
 26
 27 for (i = 0; i <M; i ++) // input and initialize augmentation matrix
 28 {
 29 printf ("Please enter line% d of augmentation matrix \ n", i + 1);
 30 scanf ("% s", str);
 31 for (j = 0; j <N; j ++)
 32 a [i] [j] = str [j] -48;
 33}
 34
 35 head = (mc1 *) malloc (sizeof (mc1));
 36 head-> next = NULL;
 37 tail = head;
 38 s = t = 1; // The sub-matrix is the augmented matrix itself, and s, t is initialized with the row and column labels of the elements in the upper-left corner of the augmented matrix.
 39
 40 while ((col = test (s, t, a))! = 0) // The submatrix is not a zero matrix
 41 {
 42 if (s == M) // The augmented matrix has been reduced to a stepped matrix
 43 {
 44 row = s; // Record the number of non-zero rows
 45 add (head, col, & tail); // The last non-zero row primary column label is put into the maincol type linked list
 46 break; // End the loop
 47}
 48 else
 49 {
 50 j = s-1;
 51 for (i = s; i <M; i ++)
 52 {
 53 if (fabs (a [j] [col-1]) <fabs (a [i] [col-1])) // Column selection
 54 j = i;
 55}
 56
 57 if (s-1! = J)
 58 {
 59 for (i = col-1; i <N; i ++)
 60 {
 61 swap = a [j] [i];
 62 a [j] [i] = a [s-1] [i]; // Column selection
 63 a [s-1] [i] = swap;
 64}
 65}
 66
 67 if (col == N) // The augmentation matrix has been transformed into a step matrix
 68 {
 69 row = s; // Record the number of non-zero rows
 70 add (head, col, & tail); // The last non-zero row primary column label is placed in the maincol type linked list
 71 break; // End the loop
 72}
 73
 74 for (i = s; i <M; i ++)
 75 a [i] [col-1] =-(a [i] [col-1] / a [s-1] [col-1]);
 76
 77 for (i = col; i <N; i ++) // elimination
 78 {
 79 for (j = s; j <M; j ++)
 80 a [j] [i] = a [j] [col-1] * a [s-1] [i] + a [j] [i];
 81}
 82
 83 add (head, col, & tail); // Put the principal column col obtained after elimination into the maincol type linked list
 84 s ++;
 85 t = col + 1; // Update s, t so that s, t becomes the row and column label of the upper left corner of the new submatrix obtained after elimination, in preparation for the test function to operate the new submatrix
 86 continue; // Start a new cycle
 87}
 88}
 89 if (col == 0) // Exit the loop from the loop control condition
 90 row = s-1; // At this time, the augmentation matrix has become a step matrix, and the non-zero row function is s-1
 91
 92 if (row == 0) // Using the criteria for the solution of linear equations to determine whether there is a solution and how many solutions there are
 93 {
 94 printf ("The linear equations have infinitely many solutions \ n"); // The augmented matrix is a zero matrix, and there are infinitely many solutions
 95 printf ("General solution: \ n");
 96 for (i = 1; i <N; i ++)
 97 printf ("x% d = t% d \ n", i, i); // print the solution
 98}
 99 else
100 {
101 psnew = head-> next;
102 if (psnew-> col == N) // The last principal element of the ladder matrix is in the last column, there is no solution
103 printf ("No solution to linear equations \ n");
104 else
105 {
106 convert (a, row, head); // Convert the stepwise matrix into a simplified row stepwise matrix using the convert function
107 if (row == N-1) // The number of non-zero rows is equal to the number of unknowns, and there is a unique solution
108 {
109 printf ("The system of linear equations has a unique solution: \ n");
110 for (i = 1; i <= row; i ++) // output unique solution
111 printf ("x% d =% f \ n", i, a [i-1] [N-1]);
112}
113 else // The number of non-zero rows is less than the number of unknowns, and there are infinitely many solutions
114 {
115 int * m;
116 m = (int *) malloc ((N-1-row) * sizeof (int));
117
118 i = N-1-row;
119 for (j = N-1; j> = 1; j--)
120 {
121 if (j! = Psnew-> col)
122 {
123 m [-i] = j; // Filter free unknown labels from all unknown labels
124 if (i == 0)
125 break;
126}
127 else
128 {
129 if (psnew-> next! = NULL)
130 psnew = psnew-> next;
131}
132}
133 printf ("Linear equations have infinitely many solutions \ n");
134 printf ("General solution: \ n");
135 i = row;
136 for (psnew = head-> next; psnew! = NULL; psnew = psnew-> next)
137 {
138 printf ("x% d =% f", psnew-> col, a [i-1] [N-1]); // output general solution
139 for (j = 0; j <N-1-row; j ++)
140 {
141 if (m [j] <psnew-> col)
142 {
143 printf ("-% dx% d", 0, m [j]);
144}
145 else
146 {
147 printf ("-% fx% d", a [i-1] [m [j] -1], m [j]);
148}
149
}
150 printf ("\ n");
151 i--;
152}
153}
154}
155}
156}
157
158 int test (int s, int t, float a [] [N]) // Determine whether the submatrix formed by the intersection of rows s to M of the augmented matrix and columns t to N is the zero matrix. If it returns 0, If not the column index of the first non-zero column
159 {
160 int i, j;
161
162 for (j = t-1; j <N; j ++)
163 {
164 for (i = s-1; i <M; i ++)
165 {
166 if (a [i] [j]! = 0)
167 return (j + 1);
168}
169}
170 return (0);
171}
172
173 void add (mc1 * head, int col, mc1 ** tail) // function, used to create a new node, which contains the column index of the column col, and then insert it into the linked list of maincol in descending order
174 {
175 mc1 * psnew;
176
177 psnew = (mc1 *) malloc (sizeof (mc1));
178 psnew-> col = col;
179
180 if (head-> next == NULL)
181 {
182 psnew-> next = NULL;
183 head-> next = psnew;
184 * tail = psnew;
185}
186 else
187 {
188 psnew-> next = head-> next;
189 head-> next = psnew;
190}
191}
192
193 void convert (float a [] [N], int row, mc1 * head) // Function for converting a stepwise matrix into a simplified row stepwise matrix
194 {
195 mc1 * psnew, * pq;
196 int i, j, k, m;
197
198 psnew = head-> next;
199 for (i = row-1; i> = 0; i--)
200 {
201 if (a [i] [psnew-> col-1]! = 1) // Every non-zero row pivot is 1
202 {
203 for (j = psnew-> col; j <N; j ++)
204 a [i] [j] = a [i] [j] / a [i] [psnew-> col-1];
205}
206
207 psnew = psnew-> next;
208}
209
210 psnew = head-> next; // Upward elimination will change the part above the principal in the column except the first principal to zero
211 for (i = row-1; i> = 1; i--)
212 {
213 m = N-psnew-> col- (row-i); // Get unknown unknown label 1,2,-, N-1 is the free unknown label located to the right of i + 1 non-zero row principal column label Number
214 for (j = i-1; j> = 0; j--)
215 {
216 pq = head-> next; // pq points to the node holding the last non-zero row pivot column label
217 for (k = N; k> psnew-> col; k--)
218 {
219 if (k! = Pq-> col)
220 {
221 a [j] [k-1] =-(a [i] [k-1] * a [j] [psnew-> col-1]) + a [j] [k-1]; // From Right-to-left elementary row transformation is performed until the column position to the right of the column where i + 1 row principal is located, and i + 2 is skipped during the row.
222 m--;
223 if (m == 0)
224 break;
225}
226 else
227 {
228 if (pq-> next! = Psnew)
229 pq = pq-> next;
230}
231}
232}
233 psnew = psnew-> next; // Proceed to the previous line of principals to prepare for a new round of upward elimination
234}
235}  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.