1 | /* matrix.c |
---|
2 | * Matrix building and solving routines |
---|
3 | * Copyright (C) 1993-2003,2010,2013,2024 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
18 | */ |
---|
19 | |
---|
20 | /*#define SOR 1*/ |
---|
21 | |
---|
22 | #if 0 |
---|
23 | # define DEBUG_INVALID 1 |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <config.h> |
---|
27 | |
---|
28 | #include "debug.h" |
---|
29 | #include "cavern.h" |
---|
30 | #include "filename.h" |
---|
31 | #include "message.h" |
---|
32 | #include "netbits.h" |
---|
33 | #include "matrix.h" |
---|
34 | #include "out.h" |
---|
35 | |
---|
36 | #undef PRINT_MATRICES |
---|
37 | #define PRINT_MATRICES 0 |
---|
38 | |
---|
39 | #undef DEBUG_MATRIX_BUILD |
---|
40 | #define DEBUG_MATRIX_BUILD 0 |
---|
41 | |
---|
42 | #undef DEBUG_MATRIX |
---|
43 | #define DEBUG_MATRIX 0 |
---|
44 | |
---|
45 | #if PRINT_MATRICES |
---|
46 | static void print_matrix(real *M, real *B, long n); |
---|
47 | #endif |
---|
48 | |
---|
49 | static void choleski(real *M, real *B, long n); |
---|
50 | |
---|
51 | #ifdef SOR |
---|
52 | static void sor(real *M, real *B, long n); |
---|
53 | #endif |
---|
54 | |
---|
55 | /* for M(row, col) col must be <= row, so Y <= X */ |
---|
56 | # define M(X, Y) ((real *)M)[((((OSSIZE_T)(X)) * ((X) + 1)) >> 1) + (Y)] |
---|
57 | /* +(Y>X?0*printf("row<col (line %d)\n",__LINE__):0) */ |
---|
58 | /*#define M_(X, Y) ((real *)M)[((((OSSIZE_T)(Y)) * ((Y) + 1)) >> 1) + (X)]*/ |
---|
59 | |
---|
60 | #define COLOUR_FIXED -1 |
---|
61 | #define COLOUR_TODO -2 |
---|
62 | |
---|
63 | static void set_row(node *stn, int row_number) { |
---|
64 | // We store the matrix row/column index in stn->colour for quick and easy |
---|
65 | // lookup when copying out the solved station coordinates. |
---|
66 | stn->colour = row_number; |
---|
67 | for (int d = 0; d < 3; d++) { |
---|
68 | linkfor *leg = stn->leg[d]; |
---|
69 | if (!leg) break; |
---|
70 | node *to = leg->l.to; |
---|
71 | if (to->colour == COLOUR_TODO && stn->name->pos == to->name->pos) { |
---|
72 | set_row(to, row_number); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | #ifdef NO_COVARIANCES |
---|
78 | # define FACTOR 1 |
---|
79 | #else |
---|
80 | # define FACTOR 3 |
---|
81 | #endif |
---|
82 | |
---|
83 | extern void |
---|
84 | solve_matrix(node *list) |
---|
85 | { |
---|
86 | node *stn; |
---|
87 | bool unfixed_stations = false; |
---|
88 | FOR_EACH_STN(stn, list) { |
---|
89 | if (!fixed(stn)) { |
---|
90 | unfixed_stations = true; |
---|
91 | stn->colour = COLOUR_TODO; |
---|
92 | } else { |
---|
93 | stn->colour = COLOUR_FIXED; |
---|
94 | } |
---|
95 | } |
---|
96 | if (!unfixed_stations) { |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | // Assign a matrix row/column index to each group of stations with the same |
---|
101 | // pos. |
---|
102 | long n = 0; |
---|
103 | FOR_EACH_STN(stn, list) { |
---|
104 | if (stn->colour == COLOUR_TODO) { |
---|
105 | set_row(stn, n++); |
---|
106 | } |
---|
107 | } |
---|
108 | SVX_ASSERT(n > 0); |
---|
109 | |
---|
110 | // Array to map from row/column index to pos. We fill this in as we build |
---|
111 | // the matrix, and use it to know where to copy the solved station |
---|
112 | // coordinates to. |
---|
113 | pos **stn_tab = osmalloc((OSSIZE_T)(n * ossizeof(pos*))); |
---|
114 | |
---|
115 | /* (OSSIZE_T) cast may be needed if n >= 181 */ |
---|
116 | real *M = osmalloc((OSSIZE_T)((((OSSIZE_T)n * FACTOR * (n * FACTOR + 1)) >> 1)) * ossizeof(real)); |
---|
117 | real *B = osmalloc((OSSIZE_T)(n * FACTOR * ossizeof(real))); |
---|
118 | |
---|
119 | if (!fQuiet) { |
---|
120 | if (n == 1) |
---|
121 | out_current_action(msg(/*Solving one equation*/78)); |
---|
122 | else |
---|
123 | out_current_action1(msg(/*Solving %d simultaneous equations*/75), n); |
---|
124 | } |
---|
125 | |
---|
126 | #ifdef NO_COVARIANCES |
---|
127 | int dim = 2; |
---|
128 | #else |
---|
129 | int dim = 0; /* Collapse loop to a single iteration. */ |
---|
130 | #endif |
---|
131 | for ( ; dim >= 0; dim--) { |
---|
132 | /* Initialise M and B to zero - zeroing "linearly" will minimise |
---|
133 | * paging when the matrix is large */ |
---|
134 | { |
---|
135 | int end = n * FACTOR; |
---|
136 | for (int row = 0; row < end; row++) B[row] = (real)0.0; |
---|
137 | end = ((OSSIZE_T)n * FACTOR * (n * FACTOR + 1)) >> 1; |
---|
138 | for (int row = 0; row < end; row++) M[row] = (real)0.0; |
---|
139 | } |
---|
140 | |
---|
141 | /* Construct matrix by going through the stn list. |
---|
142 | * |
---|
143 | * All legs between two fixed stations can be ignored here. |
---|
144 | * |
---|
145 | * Other legs we want to add exactly once to M. To achieve this we |
---|
146 | * want to: |
---|
147 | * |
---|
148 | * - add forward legs between two unfixed stations, |
---|
149 | * |
---|
150 | * - add legs from unfixed stations to fixed stations (we do them from |
---|
151 | * the unfixed end so we don't need to detect when we're at a fixed |
---|
152 | * point cut line and determine which side we're currently dealing |
---|
153 | * with). |
---|
154 | * |
---|
155 | * To implement this, we only look at legs from unfixed stations and add |
---|
156 | * a leg if to a fixed station, or to an unfixed station and it's a |
---|
157 | * forward leg. |
---|
158 | */ |
---|
159 | FOR_EACH_STN(stn, list) { |
---|
160 | if (dim == 0) { |
---|
161 | if (stn->colour != COLOUR_FIXED) { |
---|
162 | stn_tab[stn->colour] = stn->name->pos; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | #ifdef NO_COVARIANCES |
---|
167 | real e; |
---|
168 | #else |
---|
169 | svar e; |
---|
170 | delta a; |
---|
171 | #endif |
---|
172 | #if DEBUG_MATRIX_BUILD |
---|
173 | print_prefix(stn->name); |
---|
174 | printf(" used: %d colour %ld\n", |
---|
175 | (!!stn->leg[2]) << 2 | (!!stn -> leg[1]) << 1 | (!!stn->leg[0]), |
---|
176 | stn->colour); |
---|
177 | |
---|
178 | for (int dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
179 | printf("Leg %d, vx=%f, reverse=%d, to ", dirn, |
---|
180 | stn->leg[dirn]->v[0], stn->leg[dirn]->l.reverse); |
---|
181 | print_prefix(stn->leg[dirn]->l.to->name); |
---|
182 | putnl(); |
---|
183 | } |
---|
184 | putnl(); |
---|
185 | #endif /* DEBUG_MATRIX_BUILD */ |
---|
186 | |
---|
187 | int f = stn->colour; |
---|
188 | if (f != COLOUR_FIXED) { |
---|
189 | for (int dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
190 | linkfor *leg = stn->leg[dirn]; |
---|
191 | node *to = leg->l.to; |
---|
192 | int t = to->colour; |
---|
193 | if (t == COLOUR_FIXED) { |
---|
194 | bool fRev = !data_here(leg); |
---|
195 | if (fRev) leg = reverse_leg(leg); |
---|
196 | /* Ignore equated nodes */ |
---|
197 | #ifdef NO_COVARIANCES |
---|
198 | e = leg->v[dim]; |
---|
199 | if (e != (real)0.0) { |
---|
200 | e = ((real)1.0) / e; |
---|
201 | M(f,f) += e; |
---|
202 | B[f] += e * POS(to, dim); |
---|
203 | if (fRev) { |
---|
204 | B[f] += leg->d[dim]; |
---|
205 | } else { |
---|
206 | B[f] -= leg->d[dim]; |
---|
207 | } |
---|
208 | } |
---|
209 | #else |
---|
210 | if (invert_svar(&e, &leg->v)) { |
---|
211 | if (fRev) { |
---|
212 | adddd(&a, &POSD(to), &leg->d); |
---|
213 | } else { |
---|
214 | subdd(&a, &POSD(to), &leg->d); |
---|
215 | } |
---|
216 | delta b; |
---|
217 | mulsd(&b, &e, &a); |
---|
218 | for (int i = 0; i < 3; i++) { |
---|
219 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
220 | B[f * FACTOR + i] += b[i]; |
---|
221 | } |
---|
222 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
223 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
224 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
225 | } |
---|
226 | #endif |
---|
227 | } else if (data_here(leg)) { |
---|
228 | /* forward leg, unfixed -> unfixed */ |
---|
229 | #if DEBUG_MATRIX |
---|
230 | # ifdef NO_COVARIANCES |
---|
231 | printf("Leg %d to %d, var %f, delta %f\n", f, t, e, |
---|
232 | leg->d[dim]); |
---|
233 | # else |
---|
234 | printf("Leg %d to %d, var (%f, %f, %f; %f, %f, %f), " |
---|
235 | "delta %f\n", f, t, e[0], e[1], e[2], e[3], e[4], e[5], |
---|
236 | leg->d[dim]); |
---|
237 | # endif |
---|
238 | #endif |
---|
239 | /* Ignore equated nodes & lollipops */ |
---|
240 | #ifdef NO_COVARIANCES |
---|
241 | e = leg->v[dim]; |
---|
242 | if (t != f && e != (real)0.0) { |
---|
243 | e = ((real)1.0) / e; |
---|
244 | M(f,f) += e; |
---|
245 | M(t,t) += e; |
---|
246 | if (f < t) M(t,f) -= e; else M(f,t) -= e; |
---|
247 | real a = e * leg->d[dim]; |
---|
248 | B[f] -= a; |
---|
249 | B[t] += a; |
---|
250 | } |
---|
251 | #else |
---|
252 | if (t != f && invert_svar(&e, &leg->v)) { |
---|
253 | mulsd(&a, &e, &leg->d); |
---|
254 | for (int i = 0; i < 3; i++) { |
---|
255 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
256 | M(t * FACTOR + i, t * FACTOR + i) += e[i]; |
---|
257 | if (f < t) |
---|
258 | M(t * FACTOR + i, f * FACTOR + i) -= e[i]; |
---|
259 | else |
---|
260 | M(f * FACTOR + i, t * FACTOR + i) -= e[i]; |
---|
261 | B[f * FACTOR + i] -= a[i]; |
---|
262 | B[t * FACTOR + i] += a[i]; |
---|
263 | } |
---|
264 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
265 | M(t * FACTOR + 1, t * FACTOR) += e[3]; |
---|
266 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
267 | M(t * FACTOR + 2, t * FACTOR) += e[4]; |
---|
268 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
269 | M(t * FACTOR + 2, t * FACTOR + 1) += e[5]; |
---|
270 | if (f < t) { |
---|
271 | M(t * FACTOR + 1, f * FACTOR) -= e[3]; |
---|
272 | M(t * FACTOR, f * FACTOR + 1) -= e[3]; |
---|
273 | M(t * FACTOR + 2, f * FACTOR) -= e[4]; |
---|
274 | M(t * FACTOR, f * FACTOR + 2) -= e[4]; |
---|
275 | M(t * FACTOR + 2, f * FACTOR + 1) -= e[5]; |
---|
276 | M(t * FACTOR + 1, f * FACTOR + 2) -= e[5]; |
---|
277 | } else { |
---|
278 | M(f * FACTOR + 1, t * FACTOR) -= e[3]; |
---|
279 | M(f * FACTOR, t * FACTOR + 1) -= e[3]; |
---|
280 | M(f * FACTOR + 2, t * FACTOR) -= e[4]; |
---|
281 | M(f * FACTOR, t * FACTOR + 2) -= e[4]; |
---|
282 | M(f * FACTOR + 2, t * FACTOR + 1) -= e[5]; |
---|
283 | M(f * FACTOR + 1, t * FACTOR + 2) -= e[5]; |
---|
284 | } |
---|
285 | } |
---|
286 | #endif |
---|
287 | } |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | #if PRINT_MATRICES |
---|
293 | print_matrix(M, B, n * FACTOR); /* 'ave a look! */ |
---|
294 | #endif |
---|
295 | |
---|
296 | #ifdef SOR |
---|
297 | /* defined in network.c, may be altered by -z<letters> on command line */ |
---|
298 | if (optimize & BITA('i')) |
---|
299 | sor(M, B, n * FACTOR); |
---|
300 | else |
---|
301 | #endif |
---|
302 | choleski(M, B, n * FACTOR); |
---|
303 | |
---|
304 | { |
---|
305 | for (int m = (int)(n - 1); m >= 0; m--) { |
---|
306 | #ifdef NO_COVARIANCES |
---|
307 | stn_tab[m]->p[dim] = B[m]; |
---|
308 | if (dim == 0) { |
---|
309 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
310 | "setting station coordinates didn't mark pos as fixed"); |
---|
311 | } |
---|
312 | #else |
---|
313 | for (int i = 0; i < 3; i++) { |
---|
314 | stn_tab[m]->p[i] = B[m * FACTOR + i]; |
---|
315 | } |
---|
316 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
317 | "setting station coordinates didn't mark pos as fixed"); |
---|
318 | #endif |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | osfree(B); |
---|
323 | osfree(M); |
---|
324 | osfree(stn_tab); |
---|
325 | |
---|
326 | #if DEBUG_MATRIX |
---|
327 | FOR_EACH_STN(stn, list) { |
---|
328 | printf("(%8.2f, %8.2f, %8.2f ) ", POS(stn, 0), POS(stn, 1), POS(stn, 2)); |
---|
329 | print_prefix(stn->name); |
---|
330 | putnl(); |
---|
331 | } |
---|
332 | #endif |
---|
333 | } |
---|
334 | |
---|
335 | /* Solve MX=B for X by Choleski factorisation - modified Choleski actually |
---|
336 | * since we factor into LDL' while Choleski is just LL' |
---|
337 | */ |
---|
338 | /* Note M must be symmetric positive definite */ |
---|
339 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
340 | static void |
---|
341 | choleski(real *M, real *B, long n) |
---|
342 | { |
---|
343 | for (int j = 1; j < n; j++) { |
---|
344 | real V; |
---|
345 | for (int i = 0; i < j; i++) { |
---|
346 | V = (real)0.0; |
---|
347 | for (int k = 0; k < i; k++) V += M(i,k) * M(j,k) * M(k,k); |
---|
348 | M(j,i) = (M(j,i) - V) / M(i,i); |
---|
349 | } |
---|
350 | V = (real)0.0; |
---|
351 | for (int k = 0; k < j; k++) V += M(j,k) * M(j,k) * M(k,k); |
---|
352 | M(j,j) -= V; /* may be best to add M() last for numerical reasons too */ |
---|
353 | } |
---|
354 | |
---|
355 | /* Multiply x by L inverse */ |
---|
356 | for (int i = 0; i < n - 1; i++) { |
---|
357 | for (int j = i + 1; j < n; j++) { |
---|
358 | B[j] -= M(j,i) * B[i]; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | /* Multiply x by D inverse */ |
---|
363 | for (int i = 0; i < n; i++) { |
---|
364 | B[i] /= M(i,i); |
---|
365 | } |
---|
366 | |
---|
367 | /* Multiply x by (L transpose) inverse */ |
---|
368 | for (int i = (int)(n - 1); i > 0; i--) { |
---|
369 | for (int j = i - 1; j >= 0; j--) { |
---|
370 | B[j] -= M(i,j) * B[i]; |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | /* printf("\n%ld/%ld\n\n",flops,flopsTot); */ |
---|
375 | } |
---|
376 | |
---|
377 | #ifdef SOR |
---|
378 | /* factor to use for SOR (must have 1 <= SOR_factor < 2) */ |
---|
379 | #define SOR_factor 1.93 /* 1.95 */ |
---|
380 | |
---|
381 | /* Solve MX=B for X by SOR of Gauss-Siedel */ |
---|
382 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
383 | static void |
---|
384 | sor(real *M, real *B, long n) |
---|
385 | { |
---|
386 | long it = 0; |
---|
387 | |
---|
388 | real *X = osmalloc(n * ossizeof(real)); |
---|
389 | |
---|
390 | const real threshold = 0.00001; |
---|
391 | |
---|
392 | printf("reciprocating diagonal\n"); /* TRANSLATE */ |
---|
393 | |
---|
394 | /* munge diagonal so we can multiply rather than divide */ |
---|
395 | for (int row = n - 1; row >= 0; row--) { |
---|
396 | M(row,row) = 1 / M(row,row); |
---|
397 | X[row] = 0; |
---|
398 | } |
---|
399 | |
---|
400 | printf("starting iteration\n"); /* TRANSLATE */ |
---|
401 | |
---|
402 | real t; |
---|
403 | do { |
---|
404 | /*printf("*");*/ |
---|
405 | it++; |
---|
406 | t = 0.0; |
---|
407 | for (int row = 0; row < n; row++) { |
---|
408 | real x = B[row]; |
---|
409 | int col; |
---|
410 | for (col = 0; col < row; col++) x -= M(row,col) * X[col]; |
---|
411 | for (col++; col < n; col++) x -= M(col,row) * X[col]; |
---|
412 | x *= M(row,row); |
---|
413 | real sor_delta = (x - X[row]) * SOR_factor; |
---|
414 | X[row] += sor_delta; |
---|
415 | real t2 = fabs(sor_delta); |
---|
416 | if (t2 > t) t = t2; |
---|
417 | } |
---|
418 | printf("% 6ld: %8.6f\n", it, t); |
---|
419 | } while (t >= threshold && it < 100000); |
---|
420 | |
---|
421 | if (t >= threshold) { |
---|
422 | fprintf(stderr, "*not* converged after %ld iterations\n", it); |
---|
423 | BUG("iteration stinks"); |
---|
424 | } |
---|
425 | |
---|
426 | printf("%ld iterations\n", it); /* TRANSLATE */ |
---|
427 | |
---|
428 | #if 0 |
---|
429 | putnl(); |
---|
430 | for (int row = n - 1; row >= 0; row--) { |
---|
431 | t = 0.0; |
---|
432 | for (int col = 0; col < row; col++) t += M(row, col) * X[col]; |
---|
433 | t += X[row] / M(row, row); |
---|
434 | for (col = row + 1; col < n; col++) |
---|
435 | t += M(col, row) * X[col]; |
---|
436 | printf("[ %f %f ]\n", t, B[row]); |
---|
437 | } |
---|
438 | #endif |
---|
439 | |
---|
440 | for (int row = n - 1; row >= 0; row--) B[row] = X[row]; |
---|
441 | |
---|
442 | osfree(X); |
---|
443 | printf("\ndone\n"); /* TRANSLATE */ |
---|
444 | } |
---|
445 | #endif |
---|
446 | |
---|
447 | #if PRINT_MATRICES |
---|
448 | static void |
---|
449 | print_matrix(real *M, real *B, long n) |
---|
450 | { |
---|
451 | printf("Matrix, M and vector, B:\n"); |
---|
452 | for (long row = 0; row < n; row++) { |
---|
453 | long col; |
---|
454 | for (col = 0; col <= row; col++) printf("%6.2f\t", M(row, col)); |
---|
455 | for (; col <= n; col++) printf(" \t"); |
---|
456 | printf("\t%6.2f\n", B[row]); |
---|
457 | } |
---|
458 | putnl(); |
---|
459 | return; |
---|
460 | } |
---|
461 | #endif |
---|