2
2
#include <stdlib.h>
3
3
#include <math.h>
4
4
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
+
6
8
for (size_t j = 0 ; j < cols ; ++ j ) {
7
9
double tmp = a [i * cols + j ];
8
10
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) {
11
13
}
12
14
13
15
void 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 ;
15
17
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 ;
18
20
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 ])) {
21
23
pivot = i ;
22
24
}
23
25
}
24
26
25
- if (a [pivot * cols + k ] == 0 ) {
27
+ if (a [pivot * cols + col ] == 0 ) {
26
28
printf ("The matrix is singular.\n" );
27
- exit ( 0 ) ;
29
+ continue ;
28
30
}
29
31
30
- if (k != pivot ) {
31
- swap_rows (a , k , pivot , cols );
32
+ if (col != pivot ) {
33
+ swap_rows (a , col , pivot , cols );
32
34
}
33
35
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 ];
36
38
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 ;
39
41
}
40
42
41
- a [i * cols + k ] = 0 ;
43
+ a [i * cols + col ] = 0 ;
42
44
}
45
+
46
+ row ++ ;
43
47
}
44
48
}
45
49
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 ) {
48
52
49
53
for (int i = rows - 1 ; i >= 0 ; -- i ) {
50
54
double sum = 0.0 ;
@@ -57,13 +61,46 @@ void back_substitution(const double * const a, double * const x,
57
61
}
58
62
}
59
63
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
+
60
84
int main () {
61
85
double a [3 ][4 ] = {{3.0 , 2.0 , -4.0 , 3.0 },
62
86
{2.0 , 3.0 , 3.0 , 15.0 },
63
87
{5.0 , -3.0 , 1.0 , 14.0 }};
64
88
65
89
gaussian_elimination ((double * )a , 3 , 4 );
66
90
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
+
67
104
for (size_t i = 0 ; i < 3 ; ++ i ) {
68
105
printf ("[" );
69
106
for (size_t j = 0 ; j < 4 ; ++ j ) {
@@ -72,7 +109,7 @@ int main() {
72
109
printf ("]\n" );
73
110
}
74
111
75
- printf ("\n" );
112
+ printf ("\nSolutions are:\ n" );
76
113
77
114
double x [3 ] = {0 , 0 , 0 };
78
115
back_substitution ((double * )a , x , 3 , 4 );
0 commit comments