aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-25 00:12:46 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-25 00:12:46 +0100
commite1e7a4b8e86087da79b86ba2ea394785346396db (patch)
tree80d5b1259bc09831ac5b83a671ed77333707b83c
parenta3319c804b0ef3a6d9e91909e5b510feeb8baab9 (diff)
downloadaoc-e1e7a4b8e86087da79b86ba2ea394785346396db.tar.gz
aoc-e1e7a4b8e86087da79b86ba2ea394785346396db.zip
Improved numerical stability by choosing better pivot
-rw-r--r--2023/24/24b.c30
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 (: )
4However, with slightly different values for eps I got two answers whose 4However, with slightly different values for eps I got two answers whose
5difference was 4 and one was too high, the other too low. A quick binary 5difference was 4 and one was too high, the other too low. A quick binary
6search gave the correct result. 6search gave the correct result.
7I'll make a more stable version if I feel like it. 7I'll make a more stable version if I feel like it. EDIT: made slighly more
8numerically stable, now the result is actually correct (if rounded to the
9nearest 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
21typedef struct { double x, y, z; } point_t; 23typedef struct { double x, y, z; } point_t;
22typedef struct { point_t p, v; } line_t; 24typedef 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");

Generated with cgit - Back to sebastiano.tronto.net