22#include <stdlib.h>
33#include <math.h>
44
5- void swap_rows (double * const a , size_t i , size_t pivot , size_t cols ) {
5+ void swap_rows (double * a , const size_t i , const size_t pivot ,
6+ const size_t cols ) {
7+
68 for (size_t j = 0 ; j < cols ; ++ j ) {
79 double tmp = a [i * cols + j ];
810 a [i * cols + j ] = a [pivot * cols + j ];
@@ -11,40 +13,42 @@ void swap_rows(double * const a, size_t i, size_t pivot, size_t cols) {
1113}
1214
1315void gaussian_elimination (double * a , const size_t rows , const size_t cols ) {
14- size_t min_dim = ( rows < cols )? rows : cols ;
16+ size_t row = 0 ;
1517
16- for (size_t k = 0 ; k < min_dim ; ++ k ) {
17- size_t pivot = k ;
18+ for (size_t col = 0 ; col < cols - 1 ; ++ col ) {
19+ size_t pivot = row ;
1820
19- for (size_t i = k + 1 ; i < rows ; ++ i ) {
20- if (fabs (a [i * cols + k ]) > fabs (a [pivot * cols + k ])) {
21+ for (size_t i = row + 1 ; i < rows ; ++ i ) {
22+ if (fabs (a [i * cols + col ]) > fabs (a [pivot * cols + col ])) {
2123 pivot = i ;
2224 }
2325 }
2426
25- if (a [pivot * cols + k ] == 0 ) {
27+ if (a [pivot * cols + col ] == 0 ) {
2628 printf ("The matrix is singular.\n" );
27- exit ( 0 ) ;
29+ continue ;
2830 }
2931
30- if (k != pivot ) {
31- swap_rows (a , k , pivot , cols );
32+ if (col != pivot ) {
33+ swap_rows (a , col , pivot , cols );
3234 }
3335
34- for (size_t i = k + 1 ; i < rows ; ++ i ) {
35- double scale = a [i * cols + k ] / a [k * cols + k ];
36+ for (size_t i = row + 1 ; i < rows ; ++ i ) {
37+ double scale = a [i * cols + col ] / a [row * cols + col ];
3638
37- for (size_t j = k + 1 ; j < cols ; ++ j ) {
38- a [i * cols + j ] -= a [k * cols + j ] * scale ;
39+ for (size_t j = col + 1 ; j < cols ; ++ j ) {
40+ a [i * cols + j ] -= a [row * cols + j ] * scale ;
3941 }
4042
41- a [i * cols + k ] = 0 ;
43+ a [i * cols + col ] = 0 ;
4244 }
45+
46+ row ++ ;
4347 }
4448}
4549
46- void back_substitution (const double * const a , double * const x ,
47- const size_t rows , const size_t cols ) {
50+ void back_substitution (const double * a , double * x , const size_t rows ,
51+ const size_t cols ) {
4852
4953 for (int i = rows - 1 ; i >= 0 ; -- i ) {
5054 double sum = 0.0 ;
@@ -57,13 +61,46 @@ void back_substitution(const double * const a, double * const x,
5761 }
5862}
5963
64+ void gauss_jordan (double * a , const size_t rows , const size_t cols ) {
65+ int row = 0 ;
66+
67+ for (int col = 0 ; col < cols - 1 ; ++ col ) {
68+ if (a [row * cols + col ] != 0 ) {
69+ for (int i = cols - 1 ; i > col - 1 ; -- i ) {
70+ a [row * cols + i ] /= a [row * cols + col ];
71+ }
72+
73+ for (int i = 0 ; i < row ; ++ i ) {
74+ for (int j = cols - 1 ; j > col - 1 ; -- j ) {
75+ a [i * cols + j ] -= a [i * cols + col ] * a [row * cols + j ];
76+ }
77+ }
78+
79+ row ++ ;
80+ }
81+ }
82+ }
83+
6084int main () {
6185 double a [3 ][4 ] = {{3.0 , 2.0 , -4.0 , 3.0 },
6286 {2.0 , 3.0 , 3.0 , 15.0 },
6387 {5.0 , -3.0 , 1.0 , 14.0 }};
6488
6589 gaussian_elimination ((double * )a , 3 , 4 );
6690
91+ printf ("Gaussian elimination:\n" );
92+ for (size_t i = 0 ; i < 3 ; ++ i ) {
93+ printf ("[" );
94+ for (size_t j = 0 ; j < 4 ; ++ j ) {
95+ printf ("%f " , a [i ][j ]);
96+ }
97+ printf ("]\n" );
98+ }
99+
100+ printf ("\nGauss-Jordan:\n" );
101+
102+ gauss_jordan ((double * )a , 3 , 4 );
103+
67104 for (size_t i = 0 ; i < 3 ; ++ i ) {
68105 printf ("[" );
69106 for (size_t j = 0 ; j < 4 ; ++ j ) {
@@ -72,7 +109,7 @@ int main() {
72109 printf ("]\n" );
73110 }
74111
75- printf ("\n" );
112+ printf ("\nSolutions are:\ n" );
76113
77114 double x [3 ] = {0 , 0 , 0 };
78115 back_substitution ((double * )a , x , 3 , 4 );
0 commit comments