diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 00:12:46 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-25 00:12:46 +0100 |
| commit | e1e7a4b8e86087da79b86ba2ea394785346396db (patch) | |
| tree | 80d5b1259bc09831ac5b83a671ed77333707b83c /2023 | |
| parent | a3319c804b0ef3a6d9e91909e5b510feeb8baab9 (diff) | |
| download | aoc-e1e7a4b8e86087da79b86ba2ea394785346396db.tar.gz aoc-e1e7a4b8e86087da79b86ba2ea394785346396db.zip | |
Improved numerical stability by choosing better pivot
Diffstat (limited to '2023')
| -rw-r--r-- | 2023/24/24b.c | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/2023/24/24b.c b/2023/24/24b.c index 3011108..4881099 100644 --- a/2023/24/24b.c +++ b/2023/24/24b.c | |||
| @@ -4,19 +4,21 @@ machine (ahah - doesn't work on my machine (: ) | |||
| 4 | However, with slightly different values for eps I got two answers whose | 4 | However, with slightly different values for eps I got two answers whose |
| 5 | difference was 4 and one was too high, the other too low. A quick binary | 5 | difference was 4 and one was too high, the other too low. A quick binary |
| 6 | search gave the correct result. | 6 | search gave the correct result. |
| 7 | I'll make a more stable version if I feel like it. | 7 | I'll make a more stable version if I feel like it. EDIT: made slighly more |
| 8 | numerically stable, now the result is actually correct (if rounded to the | ||
| 9 | nearest integer). | ||
| 8 | */ | 10 | */ |
| 9 | 11 | ||
| 10 | #include <stdbool.h> | 12 | #include <stdbool.h> |
| 11 | #include <stdio.h> | 13 | #include <stdio.h> |
| 12 | #include <stdlib.h> | 14 | #include <stdlib.h> |
| 13 | 15 | ||
| 14 | #define D 10 | 16 | #define D 10 |
| 15 | #define N 300 | 17 | #define N 300 |
| 16 | 18 | ||
| 17 | #define ABS(x) ((x)>0?(x):-(x)) | 19 | #define ABS(x) ((x)>0?(x):-(x)) |
| 18 | #define eps 1e1 | 20 | #define eps 1e-10 |
| 19 | #define EQ(x,y) (ABS((x)-(y))<eps) | 21 | #define EQ(x,y) (ABS((x)-(y))<eps*(ABS(x)+ABS(y))) |
| 20 | 22 | ||
| 21 | typedef struct { double x, y, z; } point_t; | 23 | typedef struct { double x, y, z; } point_t; |
| 22 | typedef struct { point_t p, v; } line_t; | 24 | typedef struct { point_t p, v; } line_t; |
| @@ -57,12 +59,18 @@ bool solvesystem(double A[D][D], double C[D], int d, double *X) { | |||
| 57 | /* Row reduction */ | 59 | /* Row reduction */ |
| 58 | for (int i = 0; i < d; i++) { | 60 | for (int i = 0; i < d; i++) { |
| 59 | /* Make first nonzero */ | 61 | /* Make first nonzero */ |
| 60 | int nz; | 62 | int imax; |
| 61 | for (nz = i; nz < d && EQ(A[i][nz], 0.0); nz++) ; | 63 | double maxi = 0.0; |
| 62 | if (nz == d) return false; | 64 | for (int j = i; j < d; j++) { |
| 63 | swap(&C[i], &C[nz]); | 65 | if (A[j][i] > maxi) { |
| 66 | maxi = A[j][i]; | ||
| 67 | imax = j; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | if (EQ(maxi, 0.0)) return false; | ||
| 71 | swap(&C[i], &C[imax]); | ||
| 64 | for (int j = 0; j < d; j++) | 72 | for (int j = 0; j < d; j++) |
| 65 | swap(&A[i][j], &A[nz][j]); | 73 | swap(&A[i][j], &A[imax][j]); |
| 66 | 74 | ||
| 67 | /* Reduce rows */ | 75 | /* Reduce rows */ |
| 68 | for (int ii = i+1; ii < d; ii++) { | 76 | for (int ii = i+1; ii < d; ii++) { |
| @@ -106,9 +114,11 @@ void testsolution(line_t sol) { | |||
| 106 | if (!EQ(sol.v.z, l[i].v.z)) | 114 | if (!EQ(sol.v.z, l[i].v.z)) |
| 107 | t = (sol.p.z-l[i].p.z)/(l[i].v.z-sol.v.z); | 115 | t = (sol.p.z-l[i].p.z)/(l[i].v.z-sol.v.z); |
| 108 | point_t p1 = pos(sol, t), p2 = pos(l[i], t); | 116 | point_t p1 = pos(sol, t), p2 = pos(l[i], t); |
| 117 | /* | ||
| 109 | printf("Line %d intersected at t = %.2lf " | 118 | printf("Line %d intersected at t = %.2lf " |
| 110 | "in (%.2lf, %.2lf, %.2lf) and (%.2lf, %.2lf, %.2lf)\n", | 119 | "in (%.2lf, %.2lf, %.2lf) and (%.2lf, %.2lf, %.2lf)\n", |
| 111 | i, t, p1.x, p1.y, p1.z, p2.x, p2.y, p2.z); | 120 | i, t, p1.x, p1.y, p1.z, p2.x, p2.y, p2.z); |
| 121 | */ | ||
| 112 | if (t < eps || | 122 | if (t < eps || |
| 113 | !EQ(p1.x, p2.x) || !EQ(p1.y, p2.y) || !EQ(p1.z, p2.z)) { | 123 | !EQ(p1.x, p2.x) || !EQ(p1.y, p2.y) || !EQ(p1.z, p2.z)) { |
| 114 | printf("Error!\n"); | 124 | printf("Error!\n"); |
