drum_solver.c
drum_solver.c (C)
| 1 | /* |
| 2 | * Exact Discrete Dirichlet Laplacian Eigensolver on Gordon-Webb-Wolpert Drums |
| 3 | * =========================================================================== |
| 4 | * Builds the 5-point discrete Laplacian L_h on an arbitrary union of 7 right- |
| 5 | * isosceles lattice triangles at resolution N (grid spacing h = 1/N). Because |
| 6 | * every boundary leg lies on grid axes and every hypotenuse lies on exact 45-deg |
| 7 | * grid diagonals, the mesh has ZERO staircasing and preserves exact reflection |
| 8 | * symmetry across all triangle edges — so Buser's Fano-plane transplantation is |
| 9 | * an EXACT algebraic intertwiner of the discrete matrix L_h itself! |
| 10 | * |
| 11 | * Solver: exact band Cholesky factorization L_h = R^T R + shift-invert Lanczos |
| 12 | * with full double Gram-Schmidt (DGKS) reorthogonalization + symmetric |
| 13 | * tridiagonal QL (tql2). Finds the lowest K eigenpairs to ~1e-13 accuracy in |
| 14 | * a fraction of a second, and explicitly checks residual ||L u - lam u|| / lam |
| 15 | * plus connected nodal-domain counts via 4-neighbor BFS flood fill. |
| 16 | * |
| 17 | * Usage: |
| 18 | * ./drum_solver <N> <K> <seed> <out_prefix> |
| 19 | * |
| 20 | * Compile: |
| 21 | * gcc -O3 -march=native -Wall -Wextra drum_solver.c -lm -o drum_solver |
| 22 | */ |
| 23 | |
| 24 | #include <math.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | typedef struct { |
| 31 | int x0, y0, x1, y1, x2, y2; /* V0 = right angle, V1 = leg-a end, V2 = leg-b end */ |
| 32 | } Tri; |
| 33 | |
| 34 | /* |
| 35 | * Canonical GL(3, F_2) / Fano-plane Gordon-Webb-Wolpert pair (derived directly |
| 36 | * from the unique zero-slit, zero-pinch 7-triangle right-isosceles pair). |
| 37 | * Vertex order (V0, V1, V2) preserves the exact edge-color correspondence: |
| 38 | * V0 = right angle, edge V0-V1 = color a, edge V0-V2 = color b, V1-V2 = color c (hypotenuse). |
| 39 | */ |
| 40 | static const Tri DRUM_A[7] = { |
| 41 | { 0, 0, 1, 0, 0, 1}, /* p0, parity +1 */ |
| 42 | { 0, 0, -1, 0, 0, -1}, /* p1, parity +1 */ |
| 43 | { 1, -1, 1, -2, 0, -1}, /* p2, parity -1 */ |
| 44 | { 0, 0, 1, 0, 0, -1}, /* p3, parity -1 */ |
| 45 | { 1, -1, 1, 0, 0, -1}, /* p4, parity +1 */ |
| 46 | { 1, -1, 1, -2, 2, -1}, /* p5, parity +1 */ |
| 47 | { 2, -2, 1, -2, 2, -1}, /* p6, parity -1 */ |
| 48 | }; |
| 49 | static const int PARITY_A[7] = { 1, 1, -1, -1, 1, 1, -1 }; |
| 50 | |
| 51 | static const Tri DRUM_B[7] = { |
| 52 | { 0, 0, 1, 0, 0, 1}, /* l0, parity +1 */ |
| 53 | { 0, 0, -1, 0, 0, -1}, /* l1, parity +1 */ |
| 54 | { 1, 1, 1, 2, 0, 1}, /* l2, parity +1 */ |
| 55 | { 0, 0, 1, 0, 0, -1}, /* l3, parity -1 */ |
| 56 | { 1, 1, 1, 0, 0, 1}, /* l4, parity -1 */ |
| 57 | { 1, 1, 1, 2, 2, 1}, /* l5, parity -1 */ |
| 58 | { 0, 2, 1, 2, 0, 1}, /* l6, parity -1 */ |
| 59 | }; |
| 60 | static const int PARITY_B[7] = { 1, 1, 1, -1, -1, -1, -1 }; |
| 61 | |
| 62 | /* Fano incidence matrix I[l][p] = 1 iff dot(l, p) == 0 mod 2 (1-indexed vectors 1..7) */ |
| 63 | static int fano_inc(int l, int p) { |
| 64 | int lv = l + 1, pv = p + 1; |
| 65 | int d = (((lv >> 2) & 1) * ((pv >> 2) & 1) |
| 66 | + ((lv >> 1) & 1) * ((pv >> 1) & 1) |
| 67 | + (lv & 1) * (pv & 1)) & 1; |
| 68 | return d == 0 ? 1 : 0; |
| 69 | } |
| 70 | |
| 71 | /* Check if grid point (gx, gy) (representing (gx/N, gy/N)) lies inside or on closed triangle t. |
| 72 | * Also returns barycentric (s, t_b) in [0, N] along V0->V1 and V0->V2 via out pointers. */ |
| 73 | static int pt_in_tri(const Tri *t, int N, int gx, int gy, int *out_s, int *out_tb) { |
| 74 | int ax = t->x1 - t->x0, ay = t->y1 - t->y0; /* unit vector along leg a */ |
| 75 | int bx = t->x2 - t->x0, by = t->y2 - t->y0; /* unit vector along leg b */ |
| 76 | int dx = gx - t->x0 * N; |
| 77 | int dy = gy - t->y0 * N; |
| 78 | /* Since a, b are orthonormal lattice vectors: s = d.a, tb = d.b */ |
| 79 | int s = dx * ax + dy * ay; |
| 80 | int tb = dx * bx + dy * by; |
| 81 | if (s >= 0 && tb >= 0 && s + tb <= N) { |
| 82 | if (out_s) *out_s = s; |
| 83 | if (out_tb) *out_tb = tb; |
| 84 | return 1; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | typedef struct { |
| 90 | int N; |
| 91 | int min_gx, max_gx, min_gy, max_gy, W, H; |
| 92 | int M; /* number of strictly interior grid nodes */ |
| 93 | int *grid_id; /* size W*H: interior node index [0..M-1], or -1 if outside/boundary */ |
| 94 | int *node_gx; /* size M */ |
| 95 | int *node_gy; /* size M */ |
| 96 | int *node_tri; /* size M: triangle index 0..6 (for interior) or -1 if on interior shared edge */ |
| 97 | int bw; /* half-bandwidth of L_h */ |
| 98 | double *chol; /* band Cholesky factor R (row-major, M * (bw+1)) */ |
| 99 | } DrumGrid; |
| 100 | |
| 101 | static int in_union(const Tri tris[7], int N, int gx, int gy) { |
| 102 | for (int k = 0; k < 7; k++) { |
| 103 | if (pt_in_tri(&tris[k], N, gx, gy, NULL, NULL)) return 1; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* Strictly interior: (gx, gy) is in the union AND all 8 surrounding half-grid points |
| 109 | * (2*gx +/- 1, 2*gy +/- 1) at scale 2*N lie inside the union! |
| 110 | * Wait: on a right-isosceles polygon, a point (gx, gy) is strictly interior to the open |
| 111 | * polygon iff all 8 octant probes (4*gx + dx, 4*gy + dy) for (dx, dy) in {+-1, +-2} ... |
| 112 | * Simplest exact test: a lattice point (gx, gy) is strictly interior iff all 8 small |
| 113 | * probes at radius 1/4 (i.e. scale 4*N: (4*gx + ox, 4*gy + oy) for (ox,oy) in |
| 114 | * {(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)}) lie in the closed union! */ |
| 115 | static int strictly_interior(const Tri tris[7], int N, int gx, int gy) { |
| 116 | static const int ox[8] = { 2, -2, 0, 0, 1, -1, 1, -1 }; |
| 117 | static const int oy[8] = { 0, 0, 2, -2, 1, 1, -1, -1 }; |
| 118 | for (int d = 0; d < 8; d++) { |
| 119 | if (!in_union(tris, 4 * N, 4 * gx + ox[d], 4 * gy + oy[d])) return 0; |
| 120 | } |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | static void build_grid(DrumGrid *G, const Tri tris[7], int N) { |
| 125 | G->N = N; |
| 126 | int minx = 100, maxx = -100, miny = 100, maxy = -100; |
| 127 | for (int k = 0; k < 7; k++) { |
| 128 | int xs[3] = { tris[k].x0, tris[k].x1, tris[k].x2 }; |
| 129 | int ys[3] = { tris[k].y0, tris[k].y1, tris[k].y2 }; |
| 130 | for (int j = 0; j < 3; j++) { |
| 131 | if (xs[j] < minx) minx = xs[j]; |
| 132 | if (xs[j] > maxx) maxx = xs[j]; |
| 133 | if (ys[j] < miny) miny = ys[j]; |
| 134 | if (ys[j] > maxy) maxy = ys[j]; |
| 135 | } |
| 136 | } |
| 137 | G->min_gx = minx * N; G->max_gx = maxx * N; |
| 138 | G->min_gy = miny * N; G->max_gy = maxy * N; |
| 139 | G->W = G->max_gx - G->min_gx + 1; |
| 140 | G->H = G->max_gy - G->min_gy + 1; |
| 141 | G->grid_id = (int *)malloc((size_t)G->W * (size_t)G->H * sizeof(int)); |
| 142 | for (int i = 0; i < G->W * G->H; i++) G->grid_id[i] = -1; |
| 143 | |
| 144 | /* Order along the shorter axis to minimize band Cholesky half-bandwidth! */ |
| 145 | int sweep_y_outer = (G->W <= G->H); |
| 146 | int M = 0; |
| 147 | if (sweep_y_outer) { |
| 148 | for (int gy = G->min_gy; gy <= G->max_gy; gy++) { |
| 149 | for (int gx = G->min_gx; gx <= G->max_gx; gx++) { |
| 150 | if (strictly_interior(tris, N, gx, gy)) { |
| 151 | G->grid_id[(gy - G->min_gy) * G->W + (gx - G->min_gx)] = M++; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } else { |
| 156 | for (int gx = G->min_gx; gx <= G->max_gx; gx++) { |
| 157 | for (int gy = G->min_gy; gy <= G->max_gy; gy++) { |
| 158 | if (strictly_interior(tris, N, gx, gy)) { |
| 159 | G->grid_id[(gy - G->min_gy) * G->W + (gx - G->min_gx)] = M++; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | G->M = M; |
| 165 | G->node_gx = (int *)malloc((size_t)M * sizeof(int)); |
| 166 | G->node_gy = (int *)malloc((size_t)M * sizeof(int)); |
| 167 | G->node_tri = (int *)malloc((size_t)M * sizeof(int)); |
| 168 | for (int gy = G->min_gy; gy <= G->max_gy; gy++) { |
| 169 | for (int gx = G->min_gx; gx <= G->max_gx; gx++) { |
| 170 | int id = G->grid_id[(gy - G->min_gy) * G->W + (gx - G->min_gx)]; |
| 171 | if (id >= 0) { |
| 172 | G->node_gx[id] = gx; |
| 173 | G->node_gy[id] = gy; |
| 174 | int found = -1, cnt = 0; |
| 175 | for (int k = 0; k < 7; k++) { |
| 176 | if (pt_in_tri(&tris[k], N, gx, gy, NULL, NULL)) { |
| 177 | found = k; |
| 178 | cnt++; |
| 179 | } |
| 180 | } |
| 181 | G->node_tri[id] = (cnt == 1) ? found : -1; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* Compute half-bandwidth */ |
| 187 | int bw = 0; |
| 188 | static const int dx4[4] = { 1, -1, 0, 0 }; |
| 189 | static const int dy4[4] = { 0, 0, 1, -1 }; |
| 190 | for (int i = 0; i < M; i++) { |
| 191 | int gx = G->node_gx[i], gy = G->node_gy[i]; |
| 192 | for (int d = 0; d < 4; d++) { |
| 193 | int nx = gx + dx4[d], ny = gy + dy4[d]; |
| 194 | if (nx < G->min_gx || nx > G->max_gx || ny < G->min_gy || ny > G->max_gy) continue; |
| 195 | int j = G->grid_id[(ny - G->min_gy) * G->W + (nx - G->min_gx)]; |
| 196 | if (j > i && j - i > bw) bw = j - i; |
| 197 | } |
| 198 | } |
| 199 | G->bw = bw; |
| 200 | |
| 201 | /* Assemble upper band of unscaled 5-point Laplacian A (diag=4, neighbor=-1) and factor A = R^T R */ |
| 202 | int stride = bw + 1; |
| 203 | double *R = (double *)calloc((size_t)M * (size_t)stride, sizeof(double)); |
| 204 | for (int i = 0; i < M; i++) { |
| 205 | R[(size_t)i * stride + 0] = 4.0; |
| 206 | int gx = G->node_gx[i], gy = G->node_gy[i]; |
| 207 | for (int d = 0; d < 4; d++) { |
| 208 | int nx = gx + dx4[d], ny = gy + dy4[d]; |
| 209 | if (nx < G->min_gx || nx > G->max_gx || ny < G->min_gy || ny > G->max_gy) continue; |
| 210 | int j = G->grid_id[(ny - G->min_gy) * G->W + (nx - G->min_gx)]; |
| 211 | if (j > i) { |
| 212 | R[(size_t)i * stride + (j - i)] = -1.0; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* In-place band Cholesky factorization: A = R^T R (R stored upper banded) */ |
| 218 | for (int i = 0; i < M; i++) { |
| 219 | double *Ri = R + (size_t)i * stride; |
| 220 | double diag = Ri[0]; |
| 221 | if (diag <= 0.0) { |
| 222 | fprintf(stderr, "Cholesky failed at row %d (diag=%.3e)\n", i, diag); |
| 223 | exit(1); |
| 224 | } |
| 225 | double rii = sqrt(diag); |
| 226 | Ri[0] = rii; |
| 227 | double inv_rii = 1.0 / rii; |
| 228 | int kmax = (i + bw < M) ? bw : (M - 1 - i); |
| 229 | for (int k = 1; k <= kmax; k++) { |
| 230 | Ri[k] *= inv_rii; |
| 231 | } |
| 232 | for (int k = 1; k <= kmax; k++) { |
| 233 | double rik = Ri[k]; |
| 234 | if (rik == 0.0) continue; |
| 235 | double *Rik_row = R + (size_t)(i + k) * stride; |
| 236 | int jmax = kmax - k; |
| 237 | for (int j = 0; j <= jmax; j++) { |
| 238 | Rik_row[j] -= rik * Ri[k + j]; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | G->chol = R; |
| 243 | } |
| 244 | |
| 245 | /* Solve A * y = x using precomputed band Cholesky R^T R y = x */ |
| 246 | static void solve_invA(const DrumGrid *G, const double *x, double *y) { |
| 247 | int M = G->M, bw = G->bw, stride = bw + 1; |
| 248 | const double *R = G->chol; |
| 249 | memcpy(y, x, (size_t)M * sizeof(double)); |
| 250 | |
| 251 | /* Forward solve R^T z = x */ |
| 252 | for (int i = 0; i < M; i++) { |
| 253 | const double *Ri = R + (size_t)i * stride; |
| 254 | double zi = y[i] / Ri[0]; |
| 255 | y[i] = zi; |
| 256 | int kmax = (i + bw < M) ? bw : (M - 1 - i); |
| 257 | for (int k = 1; k <= kmax; k++) { |
| 258 | y[i + k] -= Ri[k] * zi; |
| 259 | } |
| 260 | } |
| 261 | /* Back solve R y = z */ |
| 262 | for (int i = M - 1; i >= 0; i--) { |
| 263 | const double *Ri = R + (size_t)i * stride; |
| 264 | double sum = y[i]; |
| 265 | int kmax = (i + bw < M) ? bw : (M - 1 - i); |
| 266 | for (int k = 1; k <= kmax; k++) { |
| 267 | sum -= Ri[k] * y[i + k]; |
| 268 | } |
| 269 | y[i] = sum / Ri[0]; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* Multiply y = (N^2 * A) * x */ |
| 274 | static void apply_L(const DrumGrid *G, const double *x, double *y) { |
| 275 | int M = G->M; |
| 276 | double scale = (double)G->N * (double)G->N; |
| 277 | static const int dx4[4] = { 1, -1, 0, 0 }; |
| 278 | static const int dy4[4] = { 0, 0, 1, -1 }; |
| 279 | for (int i = 0; i < M; i++) { |
| 280 | double acc = 4.0 * x[i]; |
| 281 | int gx = G->node_gx[i], gy = G->node_gy[i]; |
| 282 | for (int d = 0; d < 4; d++) { |
| 283 | int nx = gx + dx4[d], ny = gy + dy4[d]; |
| 284 | if (nx < G->min_gx || nx > G->max_gx || ny < G->min_gy || ny > G->max_gy) continue; |
| 285 | int j = G->grid_id[(ny - G->min_gy) * G->W + (nx - G->min_gx)]; |
| 286 | if (j >= 0) acc -= x[j]; |
| 287 | } |
| 288 | y[i] = scale * acc; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* EISPACK tql2: eigenvalues and eigenvectors of symmetric tridiagonal matrix (d[0..n-1], e[1..n-1]) */ |
| 293 | static void tql2(int n, double *d, double *e, double *z) { |
| 294 | for (int i = 1; i < n; i++) e[i - 1] = e[i]; |
| 295 | e[n - 1] = 0.0; |
| 296 | double f = 0.0, tst1 = 0.0; |
| 297 | const double eps = 2.220446049250313e-16; |
| 298 | for (int l = 0; l < n; l++) { |
| 299 | double h = fabs(d[l]) + fabs(e[l]); |
| 300 | if (tst1 < h) tst1 = h; |
| 301 | int m = l; |
| 302 | while (m < n) { |
| 303 | if (fabs(e[m]) <= eps * tst1) break; |
| 304 | m++; |
| 305 | } |
| 306 | if (m > l) { |
| 307 | int iter = 0; |
| 308 | do { |
| 309 | iter++; |
| 310 | if (iter > 100) { fprintf(stderr, "tql2 did not converge\n"); exit(1); } |
| 311 | double g = d[l]; |
| 312 | double p = (d[l + 1] - g) / (2.0 * e[l]); |
| 313 | double r = hypot(p, 1.0); |
| 314 | if (p < 0.0) r = -r; |
| 315 | d[l] = e[l] / (p + r); |
| 316 | d[l + 1] = e[l] * (p + r); |
| 317 | double dl1 = d[l + 1]; |
| 318 | double h0 = g - d[l]; |
| 319 | for (int i = l + 2; i < n; i++) d[i] -= h0; |
| 320 | f += h0; |
| 321 | |
| 322 | p = d[m]; |
| 323 | double c = 1.0, c2 = 1.0, c3 = 1.0; |
| 324 | double el1 = e[l + 1]; |
| 325 | double s = 0.0, s2 = 0.0; |
| 326 | for (int i = m - 1; i >= l; i--) { |
| 327 | c3 = c2; c2 = c; s2 = s; |
| 328 | g = c * e[i]; |
| 329 | h = c * p; |
| 330 | r = hypot(p, e[i]); |
| 331 | e[i + 1] = s * r; |
| 332 | s = e[i] / r; |
| 333 | c = p / r; |
| 334 | p = c * d[i] - s * g; |
| 335 | d[i + 1] = h + s * (c * g + s * d[i]); |
| 336 | for (int k = 0; k < n; k++) { |
| 337 | double zk1 = z[k * n + (i + 1)]; |
| 338 | double zk0 = z[k * n + i]; |
| 339 | z[k * n + (i + 1)] = s * zk0 + c * zk1; |
| 340 | z[k * n + i] = c * zk0 - s * zk1; |
| 341 | } |
| 342 | } |
| 343 | p = -s * s2 * c3 * el1 * e[l] / dl1; |
| 344 | e[l] = s * p; |
| 345 | d[l] = c * p; |
| 346 | } while (fabs(e[l]) > eps * tst1); |
| 347 | } |
| 348 | d[l] += f; |
| 349 | e[l] = 0.0; |
| 350 | } |
| 351 | /* Sort descending by d[] (largest eigenvalues of A^{-1} = smallest eigenvalues of L_h) */ |
| 352 | for (int i = 0; i < n - 1; i++) { |
| 353 | int k = i; |
| 354 | double p = d[i]; |
| 355 | for (int j = i + 1; j < n; j++) { |
| 356 | if (d[j] > p) { k = j; p = d[j]; } |
| 357 | } |
| 358 | if (k != i) { |
| 359 | d[k] = d[i]; d[i] = p; |
| 360 | for (int j = 0; j < n; j++) { |
| 361 | double tmp = z[j * n + i]; |
| 362 | z[j * n + i] = z[j * n + k]; |
| 363 | z[j * n + k] = tmp; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* Shift-invert Lanczos on A^{-1} with full DGKS reorthogonalization. |
| 370 | * Returns K lowest eigenvalues of L_h in evals[0..K-1] and unit eigenvectors in evecs[k*M + i]. */ |
| 371 | static void solve_eig(const DrumGrid *G, int K, uint64_t seed, double *evals, double *evecs) { |
| 372 | int M = G->M; |
| 373 | int m = K * 2 + 45; |
| 374 | if (m > M) m = M; |
| 375 | |
| 376 | double *Q = (double *)malloc((size_t)m * (size_t)M * sizeof(double)); |
| 377 | double *alpha = (double *)calloc((size_t)m, sizeof(double)); |
| 378 | double *beta = (double *)calloc((size_t)(m + 1), sizeof(double)); |
| 379 | double *w = (double *)malloc((size_t)M * sizeof(double)); |
| 380 | |
| 381 | /* Deterministic splitmix64 random start vector */ |
| 382 | uint64_t st = seed; |
| 383 | double nrm = 0.0; |
| 384 | for (int i = 0; i < M; i++) { |
| 385 | st += 0x9e3779b97f4a7c15ULL; |
| 386 | uint64_t z = st; |
| 387 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; |
| 388 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; |
| 389 | z = z ^ (z >> 31); |
| 390 | double v = ((double)(int64_t)z) / 9.223372036854776e18; |
| 391 | Q[i] = v; |
| 392 | nrm += v * v; |
| 393 | } |
| 394 | nrm = sqrt(nrm); |
| 395 | for (int i = 0; i < M; i++) Q[i] /= nrm; |
| 396 | |
| 397 | int steps = m; |
| 398 | for (int j = 0; j < m; j++) { |
| 399 | const double *qj = Q + (size_t)j * M; |
| 400 | solve_invA(G, qj, w); |
| 401 | if (j > 0) { |
| 402 | const double *qprev = Q + (size_t)(j - 1) * M; |
| 403 | double bj = beta[j]; |
| 404 | for (int i = 0; i < M; i++) w[i] -= bj * qprev[i]; |
| 405 | } |
| 406 | double aj = 0.0; |
| 407 | for (int i = 0; i < M; i++) aj += qj[i] * w[i]; |
| 408 | alpha[j] = aj; |
| 409 | for (int i = 0; i < M; i++) w[i] -= aj * qj[i]; |
| 410 | |
| 411 | /* Full DGKS (two-pass classical Gram-Schmidt) reorthogonalization */ |
| 412 | for (int pass = 0; pass < 2; pass++) { |
| 413 | for (int t = 0; t <= j; t++) { |
| 414 | const double *qt = Q + (size_t)t * M; |
| 415 | double dot = 0.0; |
| 416 | for (int i = 0; i < M; i++) dot += qt[i] * w[i]; |
| 417 | for (int i = 0; i < M; i++) w[i] -= dot * qt[i]; |
| 418 | } |
| 419 | } |
| 420 | double bnext = 0.0; |
| 421 | for (int i = 0; i < M; i++) bnext += w[i] * w[i]; |
| 422 | bnext = sqrt(bnext); |
| 423 | if (bnext < 1e-14 || j == m - 1) { |
| 424 | steps = j + 1; |
| 425 | break; |
| 426 | } |
| 427 | beta[j + 1] = bnext; |
| 428 | double *qnext = Q + (size_t)(j + 1) * M; |
| 429 | double inv_b = 1.0 / bnext; |
| 430 | for (int i = 0; i < M; i++) qnext[i] = w[i] * inv_b; |
| 431 | } |
| 432 | |
| 433 | double *Z = (double *)calloc((size_t)steps * (size_t)steps, sizeof(double)); |
| 434 | for (int i = 0; i < steps; i++) Z[i * steps + i] = 1.0; |
| 435 | double *d = (double *)malloc((size_t)steps * sizeof(double)); |
| 436 | double *e = (double *)malloc((size_t)steps * sizeof(double)); |
| 437 | memcpy(d, alpha, (size_t)steps * sizeof(double)); |
| 438 | memcpy(e, beta, (size_t)steps * sizeof(double)); |
| 439 | |
| 440 | tql2(steps, d, e, Z); |
| 441 | |
| 442 | double scale = (double)G->N * (double)G->N; |
| 443 | for (int k = 0; k < K; k++) { |
| 444 | evals[k] = scale / d[k]; |
| 445 | double *uk = evecs + (size_t)k * M; |
| 446 | memset(uk, 0, (size_t)M * sizeof(double)); |
| 447 | for (int j = 0; j < steps; j++) { |
| 448 | double c = Z[j * steps + k]; |
| 449 | const double *qj = Q + (size_t)j * M; |
| 450 | for (int i = 0; i < M; i++) uk[i] += c * qj[i]; |
| 451 | } |
| 452 | /* Canonicalize sign (largest-magnitude entry positive) and normalize */ |
| 453 | double n2 = 0.0, maxv = 0.0; |
| 454 | int maxi = 0; |
| 455 | for (int i = 0; i < M; i++) { |
| 456 | n2 += uk[i] * uk[i]; |
| 457 | if (fabs(uk[i]) > maxv) { maxv = fabs(uk[i]); maxi = i; } |
| 458 | } |
| 459 | double inv = (uk[maxi] >= 0.0 ? 1.0 : -1.0) / sqrt(n2); |
| 460 | for (int i = 0; i < M; i++) uk[i] *= inv; |
| 461 | } |
| 462 | |
| 463 | free(Q); free(alpha); free(beta); free(w); free(Z); free(d); free(e); |
| 464 | } |
| 465 | |
| 466 | /* Count connected nodal domains (4-connected components of u > tol and u < -tol). */ |
| 467 | static int count_nodal_domains(const DrumGrid *G, const double *u, int *out_pos, int *out_neg) { |
| 468 | int M = G->M; |
| 469 | double maxv = 0.0; |
| 470 | for (int i = 0; i < M; i++) if (fabs(u[i]) > maxv) maxv = fabs(u[i]); |
| 471 | double tol = 1e-9 * maxv; |
| 472 | |
| 473 | uint8_t *vis = (uint8_t *)calloc((size_t)M, 1); |
| 474 | int *queue = (int *)malloc((size_t)M * sizeof(int)); |
| 475 | static const int dx4[4] = { 1, -1, 0, 0 }; |
| 476 | static const int dy4[4] = { 0, 0, 1, -1 }; |
| 477 | int pos = 0, neg = 0; |
| 478 | for (int start = 0; start < M; start++) { |
| 479 | if (vis[start]) continue; |
| 480 | int sgn = (u[start] > tol) ? 1 : (u[start] < -tol ? -1 : 0); |
| 481 | if (sgn == 0) continue; |
| 482 | if (sgn > 0) pos++; else neg++; |
| 483 | int qh = 0, qt = 0; |
| 484 | vis[start] = 1; |
| 485 | queue[qt++] = start; |
| 486 | while (qh < qt) { |
| 487 | int cur = queue[qh++]; |
| 488 | int gx = G->node_gx[cur], gy = G->node_gy[cur]; |
| 489 | for (int d = 0; d < 4; d++) { |
| 490 | int nx = gx + dx4[d], ny = gy + dy4[d]; |
| 491 | if (nx < G->min_gx || nx > G->max_gx || ny < G->min_gy || ny > G->max_gy) continue; |
| 492 | int nb = G->grid_id[(ny - G->min_gy) * G->W + (nx - G->min_gx)]; |
| 493 | if (nb < 0 || vis[nb]) continue; |
| 494 | int ns = (u[nb] > tol) ? 1 : (u[nb] < -tol ? -1 : 0); |
| 495 | if (ns == sgn) { |
| 496 | vis[nb] = 1; |
| 497 | queue[qt++] = nb; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | free(vis); free(queue); |
| 503 | if (out_pos) *out_pos = pos; |
| 504 | if (out_neg) *out_neg = neg; |
| 505 | return pos + neg; |
| 506 | } |
| 507 | |
| 508 | /* Map point (s, tb) inside base triangle -> grid node index on drum G for triangle tri_idx */ |
| 509 | static int tri_point_to_node(const DrumGrid *G, const Tri tris[7], int tri_idx, int s, int tb) { |
| 510 | const Tri *t = &tris[tri_idx]; |
| 511 | int gx = t->x0 * G->N + s * (t->x1 - t->x0) + tb * (t->x2 - t->x0); |
| 512 | int gy = t->y0 * G->N + s * (t->y1 - t->y0) + tb * (t->y2 - t->y0); |
| 513 | if (gx < G->min_gx || gx > G->max_gx || gy < G->min_gy || gy > G->max_gy) return -1; |
| 514 | return G->grid_id[(gy - G->min_gy) * G->W + (gx - G->min_gx)]; |
| 515 | } |
| 516 | |
| 517 | /* Apply exact Fano-plane transplantation v_B = (S_B * I * S_A) u_A on strictly-interior triangle points |
| 518 | * and measure cosine similarity + exact L2 scaling factor against u_B! */ |
| 519 | static void check_transplantation( |
| 520 | const DrumGrid *GA, const DrumGrid *GB, |
| 521 | const double *uA, const double *uB, |
| 522 | double *out_scale, double *out_rel_err, double *out_parity_sum_ratio |
| 523 | ) { |
| 524 | int N = GA->N; |
| 525 | double num_dot = 0.0, vB_n2 = 0.0, uB_n2 = 0.0; |
| 526 | double psum_n2 = 0.0, uA_in_n2 = 0.0; |
| 527 | /* Iterate over strictly interior points of the base triangle: s > 0, tb > 0, s + tb < N */ |
| 528 | for (int s = 1; s < N; s++) { |
| 529 | for (int tb = 1; s + tb < N; tb++) { |
| 530 | double valsA[7]; |
| 531 | double psum = 0.0; |
| 532 | for (int p = 0; p < 7; p++) { |
| 533 | int idA = tri_point_to_node(GA, DRUM_A, p, s, tb); |
| 534 | valsA[p] = (idA >= 0) ? uA[idA] : 0.0; |
| 535 | psum += PARITY_A[p] * valsA[p]; |
| 536 | uA_in_n2 += valsA[p] * valsA[p]; |
| 537 | } |
| 538 | psum_n2 += psum * psum; |
| 539 | for (int l = 0; l < 7; l++) { |
| 540 | double acc = 0.0; |
| 541 | for (int p = 0; p < 7; p++) { |
| 542 | if (fano_inc(l, p)) { |
| 543 | acc += (double)(PARITY_B[l] * PARITY_A[p]) * valsA[p]; |
| 544 | } |
| 545 | } |
| 546 | int idB = tri_point_to_node(GB, DRUM_B, l, s, tb); |
| 547 | double ub = (idB >= 0) ? uB[idB] : 0.0; |
| 548 | vB_n2 += acc * acc; |
| 549 | uB_n2 += ub * ub; |
| 550 | num_dot += acc * ub; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | double scale = sqrt(vB_n2 / uA_in_n2); |
| 555 | double sign = (num_dot >= 0.0) ? 1.0 : -1.0; |
| 556 | double cos_sim = fabs(num_dot) / sqrt(vB_n2 * uB_n2); |
| 557 | /* Relative L2 error between normalized transplanted vector and uB */ |
| 558 | double rel_err = sqrt(fmax(0.0, 2.0 * (1.0 - cos_sim))); |
| 559 | (void)sign; |
| 560 | *out_scale = scale; |
| 561 | *out_rel_err = rel_err; |
| 562 | *out_parity_sum_ratio = sqrt(psum_n2 / uA_in_n2); |
| 563 | } |
| 564 | |
| 565 | int main(int argc, char **argv) { |
| 566 | if (argc < 4) { |
| 567 | fprintf(stderr, "Usage: %s <N> <K> <out_prefix> [dump_modes_csv]\n", argv[0]); |
| 568 | return 1; |
| 569 | } |
| 570 | int N = atoi(argv[1]); |
| 571 | int K = atoi(argv[2]); |
| 572 | const char *prefix = argv[3]; |
| 573 | const char *dump_csv = (argc >= 5) ? argv[4] : NULL; |
| 574 | |
| 575 | DrumGrid GA, GB; |
| 576 | build_grid(&GA, DRUM_A, N); |
| 577 | build_grid(&GB, DRUM_B, N); |
| 578 | fprintf(stderr, "N=%d: M_A=%d (bw=%d), M_B=%d (bw=%d)\n", N, GA.M, GA.bw, GB.M, GB.bw); |
| 579 | |
| 580 | double *evA = (double *)malloc((size_t)K * sizeof(double)); |
| 581 | double *evB = (double *)malloc((size_t)K * sizeof(double)); |
| 582 | double *evA2 = (double *)malloc((size_t)K * sizeof(double)); |
| 583 | double *uA = (double *)malloc((size_t)K * (size_t)GA.M * sizeof(double)); |
| 584 | double *uB = (double *)malloc((size_t)K * (size_t)GB.M * sizeof(double)); |
| 585 | double *uA2 = (double *)malloc((size_t)K * (size_t)GA.M * sizeof(double)); |
| 586 | double *tmpA = (double *)malloc((size_t)GA.M * sizeof(double)); |
| 587 | double *tmpB = (double *)malloc((size_t)GB.M * sizeof(double)); |
| 588 | |
| 589 | solve_eig(&GA, K, 0x123456789abcdef0ULL, evA, uA); |
| 590 | solve_eig(&GB, K, 0xfedcba9876543210ULL, evB, uB); |
| 591 | /* Second independent random seed on A to detect any exact degeneracies! */ |
| 592 | solve_eig(&GA, K, 0xdeadbeefcafebabeULL, evA2, uA2); |
| 593 | |
| 594 | char tsv_path[512]; |
| 595 | snprintf(tsv_path, sizeof(tsv_path), "%s_spectrum.tsv", prefix); |
| 596 | FILE *fp = fopen(tsv_path, "w"); |
| 597 | fprintf(fp, "k\tlam_A\tlam_B\tabs_diff\trel_diff\tres_A\tres_B\tnodal_A\tnodal_B\tsplit\ttrans_scale\ttrans_err\tparity_sum\tsimple_cos\n"); |
| 598 | |
| 599 | double max_rel_diff = 0.0; |
| 600 | int split_count = 0; |
| 601 | for (int k = 0; k < K; k++) { |
| 602 | const double *ua = uA + (size_t)k * GA.M; |
| 603 | const double *ub = uB + (size_t)k * GB.M; |
| 604 | const double *ua2 = uA2 + (size_t)k * GA.M; |
| 605 | apply_L(&GA, ua, tmpA); |
| 606 | apply_L(&GB, ub, tmpB); |
| 607 | double rA = 0.0, rB = 0.0, scos = 0.0; |
| 608 | for (int i = 0; i < GA.M; i++) { |
| 609 | double d = tmpA[i] - evA[k] * ua[i]; |
| 610 | rA += d * d; |
| 611 | scos += ua[i] * ua2[i]; |
| 612 | } |
| 613 | for (int i = 0; i < GB.M; i++) { |
| 614 | double d = tmpB[i] - evB[k] * ub[i]; |
| 615 | rB += d * d; |
| 616 | } |
| 617 | rA = sqrt(rA) / evA[k]; |
| 618 | rB = sqrt(rB) / evB[k]; |
| 619 | |
| 620 | int pA, nA, pB, nB; |
| 621 | int ndA = count_nodal_domains(&GA, ua, &pA, &nA); |
| 622 | int ndB = count_nodal_domains(&GB, ub, &pB, &nB); |
| 623 | if (ndA != ndB) split_count++; |
| 624 | |
| 625 | double tscale = 0.0, terr = 0.0, psum = 0.0; |
| 626 | check_transplantation(&GA, &GB, ua, ub, &tscale, &terr, &psum); |
| 627 | |
| 628 | double adiff = fabs(evA[k] - evB[k]); |
| 629 | double rdiff = adiff / evA[k]; |
| 630 | if (rdiff > max_rel_diff) max_rel_diff = rdiff; |
| 631 | |
| 632 | fprintf(fp, "%d\t%.12f\t%.12f\t%.3e\t%.3e\t%.3e\t%.3e\t%d\t%d\t%d\t%.6f\t%.3e\t%.3e\t%.12f\n", |
| 633 | k + 1, evA[k], evB[k], adiff, rdiff, rA, rB, ndA, ndB, |
| 634 | ndA != ndB ? 1 : 0, tscale, terr, psum, fabs(scos)); |
| 635 | } |
| 636 | fclose(fp); |
| 637 | fprintf(stderr, "Done! max_rel_diff=%.3e, nodal_splits=%d/%d -> %s\n", max_rel_diff, split_count, K, tsv_path); |
| 638 | |
| 639 | /* Optionally dump requested modes (1-based CSV) as raw binary grid arrays for plotting */ |
| 640 | if (dump_csv && dump_csv[0]) { |
| 641 | char buf[512]; |
| 642 | strncpy(buf, dump_csv, sizeof(buf) - 1); |
| 643 | buf[sizeof(buf) - 1] = '\0'; |
| 644 | for (char *tok = strtok(buf, ","); tok; tok = strtok(NULL, ",")) { |
| 645 | int k1 = atoi(tok); |
| 646 | if (k1 < 1 || k1 > K) continue; |
| 647 | int k = k1 - 1; |
| 648 | const DrumGrid *Gs[2] = { &GA, &GB }; |
| 649 | const double *Us[2] = { uA + (size_t)k * GA.M, uB + (size_t)k * GB.M }; |
| 650 | const char *tags[2] = { "A", "B" }; |
| 651 | for (int d = 0; d < 2; d++) { |
| 652 | char mpath[512]; |
| 653 | snprintf(mpath, sizeof(mpath), "%s_mode%d_%s.bin", prefix, k1, tags[d]); |
| 654 | FILE *mf = fopen(mpath, "wb"); |
| 655 | int hdr[6] = { Gs[d]->N, Gs[d]->W, Gs[d]->H, Gs[d]->min_gx, Gs[d]->min_gy, k1 }; |
| 656 | fwrite(hdr, sizeof(int), 6, mf); |
| 657 | double lam = (d == 0) ? evA[k] : evB[k]; |
| 658 | fwrite(&lam, sizeof(double), 1, mf); |
| 659 | double *img = (double *)malloc((size_t)Gs[d]->W * (size_t)Gs[d]->H * sizeof(double)); |
| 660 | for (int i = 0; i < Gs[d]->W * Gs[d]->H; i++) { |
| 661 | int id = Gs[d]->grid_id[i]; |
| 662 | img[i] = (id >= 0) ? Us[d][id] : NAN; |
| 663 | } |
| 664 | fwrite(img, sizeof(double), (size_t)Gs[d]->W * (size_t)Gs[d]->H, mf); |
| 665 | fclose(mf); |
| 666 | free(img); |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | free(evA); free(evB); free(evA2); free(uA); free(uB); free(uA2); free(tmpA); free(tmpB); |
| 672 | free(GA.grid_id); free(GA.node_gx); free(GA.node_gy); free(GA.node_tri); free(GA.chol); |
| 673 | free(GB.grid_id); free(GB.node_gx); free(GB.node_gy); free(GB.node_tri); free(GB.chol); |
| 674 | return 0; |
| 675 | } |
| 676 |