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