Skip to content

Commit 0ea3ebb

Browse files
committed
Merging master to Gathros-new-gauss-c
1 parent 5dd7fce commit 0ea3ebb

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

contents/gaussian_elimination/code/c/gaussian_elimination.c

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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

1315
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;
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+
6084
int 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);

contents/gaussian_elimination/gaussian_elimination.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ In code, this looks like:
355355
{% sample lang="jl" %}
356356
[import:1-45, lang:"julia"](code/julia/gaussian_elimination.jl)
357357
{% sample lang="c" %}
358-
[import:13-44, lang:"c_cpp"](code/c/gaussian_elimination.c)
358+
[import:15-48, lang:"c_cpp"](code/c/gaussian_elimination.c)
359359
{% sample lang="rs" %}
360360
[import:41-78, lang:"rust"](code/rust/gaussian_elimination.rs)
361361
{% sample lang="hs" %}
@@ -387,8 +387,7 @@ Here it is in code:
387387
{% sample lang="jl" %}
388388
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
389389
{% sample lang="c" %}
390-
This code does not exist yet in C, so here's Julia code (sorry for the inconvenience)
391-
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
390+
[import:64-82, lang:"c_cpp"](code/c/gaussian_elimination.c)
392391
{% sample lang="rs" %}
393392
This code does not exist yet in rust, so here's Julia code (sorry for the inconvenience)
394393
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
@@ -419,7 +418,7 @@ In code, this involves keeping a rolling sum of all the values we substitute in
419418
{% sample lang="jl" %}
420419
[import:47-67, lang:"julia"](code/julia/gaussian_elimination.jl)
421420
{% sample lang="c" %}
422-
[import:46-58, lang:"c_cpp"](code/c/gaussian_elimination.c)
421+
[import:50-62, lang:"c_cpp"](code/c/gaussian_elimination.c)
423422
{% sample lang="rs" %}
424423
[import:79-94, lang:"rust"](code/rust/gaussian_elimination.rs)
425424
{% sample lang="hs" %}

0 commit comments

Comments
 (0)