[421b7d2] | 1 | /* matrix.c |
---|
[d1b1380] | 2 | * Matrix building and solving routines |
---|
[a4adf09] | 3 | * Copyright (C) 1993-2003,2010,2013 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 */ |
---|
[9965b2b] | 56 | # define M(X, Y) ((real *)M)[((((OSSIZE_T)(X)) * ((X) + 1)) >> 1) + (Y)] |
---|
[421b7d2] | 57 | /* +(Y>X?0*printf("row<col (line %d)\n",__LINE__):0) */ |
---|
[9965b2b] | 58 | /*#define M_(X, Y) ((real *)M)[((((OSSIZE_T)(Y)) * ((Y) + 1)) >> 1) + (X)]*/ |
---|
[d1b1380] | 59 | |
---|
[a420b49] | 60 | static int find_stn_in_tab(node *stn); |
---|
| 61 | static int add_stn_to_tab(node *stn); |
---|
[eb18f4d] | 62 | static void build_matrix(node *list); |
---|
[d1b1380] | 63 | |
---|
| 64 | static long n_stn_tab; |
---|
| 65 | |
---|
[c19f129] | 66 | static pos **stn_tab; |
---|
[d1b1380] | 67 | |
---|
[032ed06] | 68 | extern void |
---|
[d9b5db53] | 69 | solve_matrix(node *list) |
---|
[032ed06] | 70 | { |
---|
| 71 | node *stn; |
---|
[702f518] | 72 | long n = 0; |
---|
[d9b5db53] | 73 | FOR_EACH_STN(stn, list) { |
---|
[032ed06] | 74 | if (!fixed(stn)) n++; |
---|
| 75 | } |
---|
| 76 | if (n == 0) return; |
---|
| 77 | |
---|
| 78 | /* we just need n to be a reasonable estimate >= the number |
---|
| 79 | * of stations left after reduction. If memory is |
---|
| 80 | * plentiful, we can be crass. |
---|
| 81 | */ |
---|
[66de220] | 82 | stn_tab = osmalloc((OSSIZE_T)(n * ossizeof(pos*))); |
---|
[4f613e0] | 83 | n_stn_tab = 0; |
---|
[cb3d1e2] | 84 | |
---|
[d9b5db53] | 85 | FOR_EACH_STN(stn, list) { |
---|
[032ed06] | 86 | if (!fixed(stn)) add_stn_to_tab(stn); |
---|
| 87 | } |
---|
| 88 | |
---|
[66de220] | 89 | if (n_stn_tab < n) { |
---|
| 90 | /* release unused entries in stn_tab */ |
---|
| 91 | stn_tab = osrealloc(stn_tab, n_stn_tab * ossizeof(pos*)); |
---|
| 92 | } |
---|
[cb3d1e2] | 93 | |
---|
[eb18f4d] | 94 | build_matrix(list); |
---|
[2c9c3ff] | 95 | #if DEBUG_MATRIX |
---|
[2164fa4] | 96 | FOR_EACH_STN(stn, list) { |
---|
[2aa930f] | 97 | printf("(%8.2f, %8.2f, %8.2f ) ", POS(stn, 0), POS(stn, 1), POS(stn, 2)); |
---|
[2164fa4] | 98 | print_prefix(stn->name); |
---|
[2aa930f] | 99 | putnl(); |
---|
[2164fa4] | 100 | } |
---|
[2c9c3ff] | 101 | #endif |
---|
[4f613e0] | 102 | |
---|
| 103 | osfree(stn_tab); |
---|
[032ed06] | 104 | } |
---|
[d1b1380] | 105 | |
---|
[3fde384f] | 106 | #ifdef NO_COVARIANCES |
---|
[702f518] | 107 | # define FACTOR 1 |
---|
[3fde384f] | 108 | #else |
---|
[702f518] | 109 | # define FACTOR 3 |
---|
[3fde384f] | 110 | #endif |
---|
| 111 | |
---|
[a420b49] | 112 | static void |
---|
[eb18f4d] | 113 | build_matrix(node *list) |
---|
[a420b49] | 114 | { |
---|
[9965b2b] | 115 | real *M; |
---|
[dbd68203] | 116 | real *B; |
---|
[702f518] | 117 | int dim; |
---|
[dbd68203] | 118 | |
---|
[eb18f4d] | 119 | if (n_stn_tab == 0) { |
---|
[5b68ae1] | 120 | if (!fQuiet) |
---|
| 121 | puts(msg(/*Network solved by reduction - no simultaneous equations to solve.*/74)); |
---|
[dbd68203] | 122 | return; |
---|
| 123 | } |
---|
[eb18f4d] | 124 | /* (OSSIZE_T) cast may be needed if n_stn_tab>=181 */ |
---|
| 125 | M = osmalloc((OSSIZE_T)((((OSSIZE_T)n_stn_tab * FACTOR * (n_stn_tab * FACTOR + 1)) >> 1)) * ossizeof(real)); |
---|
| 126 | B = osmalloc((OSSIZE_T)(n_stn_tab * FACTOR * ossizeof(real))); |
---|
[dbd68203] | 127 | |
---|
[647407d] | 128 | if (!fQuiet) { |
---|
[a4adf09] | 129 | if (n_stn_tab == 1) |
---|
| 130 | out_current_action(msg(/*Solving one equation*/78)); |
---|
| 131 | else |
---|
| 132 | out_current_action1(msg(/*Solving %d simultaneous equations*/75), n_stn_tab); |
---|
[dbd68203] | 133 | } |
---|
| 134 | |
---|
[3fde384f] | 135 | #ifdef NO_COVARIANCES |
---|
| 136 | dim = 2; |
---|
| 137 | #else |
---|
| 138 | dim = 0; /* fudge next loop for now */ |
---|
| 139 | #endif |
---|
[a420b49] | 140 | for ( ; dim >= 0; dim--) { |
---|
[2164fa4] | 141 | node *stn; |
---|
| 142 | int row; |
---|
| 143 | |
---|
[907fe10] | 144 | /* Initialise M and B to zero - zeroing "linearly" will minimise |
---|
[421b7d2] | 145 | * paging when the matrix is large */ |
---|
[66de220] | 146 | { |
---|
| 147 | int end = n_stn_tab * FACTOR; |
---|
| 148 | for (row = 0; row < end; row++) B[row] = (real)0.0; |
---|
| 149 | end = ((OSSIZE_T)n_stn_tab * FACTOR * (n_stn_tab * FACTOR + 1)) >> 1; |
---|
| 150 | for (row = 0; row < end; row++) M[row] = (real)0.0; |
---|
| 151 | } |
---|
[dbd68203] | 152 | |
---|
[3c7ab9a] | 153 | /* Construct matrix by going through the stn list. |
---|
[421b7d2] | 154 | * |
---|
[907fe10] | 155 | * All legs between two fixed stations can be ignored here. |
---|
[421b7d2] | 156 | * |
---|
[3c7ab9a] | 157 | * Other legs we want to add exactly once to M. To achieve this we |
---|
| 158 | * wan to: |
---|
| 159 | * |
---|
| 160 | * - add forward legs between two unfixed stations, |
---|
| 161 | * |
---|
| 162 | * - add legs from unfixed stations to fixed stations (we do them from |
---|
| 163 | * the unfixed end so we don't need to detect when we're at a fixed |
---|
| 164 | * point cut line and determine which side we're currently dealing |
---|
| 165 | * with). |
---|
| 166 | * |
---|
| 167 | * To implement this, we only look at legs from unfixed stations and add |
---|
| 168 | * a leg if to a fixed station, or to an unfixed station and it's a |
---|
| 169 | * forward leg. |
---|
| 170 | */ |
---|
[d9b5db53] | 171 | FOR_EACH_STN(stn, list) { |
---|
[2164fa4] | 172 | #ifdef NO_COVARIANCES |
---|
| 173 | real e; |
---|
| 174 | #else |
---|
[dac18d8] | 175 | svar e; |
---|
[eb18f4d] | 176 | delta a; |
---|
[2164fa4] | 177 | #endif |
---|
[b5d3988] | 178 | int f, t; |
---|
[dbd68203] | 179 | int dirn; |
---|
[b5d3988] | 180 | #if DEBUG_MATRIX_BUILD |
---|
[dbd68203] | 181 | print_prefix(stn->name); |
---|
[b5d3988] | 182 | printf(" used: %d colour %ld\n", |
---|
[a420b49] | 183 | (!!stn->leg[2]) << 2 | (!!stn -> leg[1]) << 1 | (!!stn->leg[0]), |
---|
[b5d3988] | 184 | stn->colour); |
---|
[3fde384f] | 185 | |
---|
[907fe10] | 186 | for (dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
[b5d3988] | 187 | #ifdef NO_COVARIANCES |
---|
[907fe10] | 188 | printf("Leg %d, vx=%f, reverse=%d, to ", dirn, |
---|
| 189 | stn->leg[dirn]->v[0], stn->leg[dirn]->l.reverse); |
---|
[b5d3988] | 190 | #else |
---|
[907fe10] | 191 | printf("Leg %d, vx=%f, reverse=%d, to ", dirn, |
---|
| 192 | stn->leg[dirn]->v[0][0], stn->leg[dirn]->l.reverse); |
---|
[b5d3988] | 193 | #endif |
---|
[907fe10] | 194 | print_prefix(stn->leg[dirn]->l.to->name); |
---|
| 195 | putnl(); |
---|
| 196 | } |
---|
[dbd68203] | 197 | putnl(); |
---|
[d1b1380] | 198 | #endif /* DEBUG_MATRIX_BUILD */ |
---|
[b5d3988] | 199 | |
---|
[907fe10] | 200 | if (!fixed(stn)) { |
---|
[564f471] | 201 | f = find_stn_in_tab(stn); |
---|
[907fe10] | 202 | for (dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
| 203 | linkfor *leg = stn->leg[dirn]; |
---|
| 204 | node *to = leg->l.to; |
---|
| 205 | if (fixed(to)) { |
---|
| 206 | bool fRev = !data_here(leg); |
---|
| 207 | if (fRev) leg = reverse_leg(leg); |
---|
| 208 | /* Ignore equated nodes */ |
---|
[3fde384f] | 209 | #ifdef NO_COVARIANCES |
---|
[907fe10] | 210 | e = leg->v[dim]; |
---|
| 211 | if (e != (real)0.0) { |
---|
| 212 | e = ((real)1.0) / e; |
---|
| 213 | M(f,f) += e; |
---|
[f52dcc7] | 214 | B[f] += e * POS(to, dim); |
---|
[907fe10] | 215 | if (fRev) { |
---|
[f52dcc7] | 216 | B[f] += leg->d[dim]; |
---|
[907fe10] | 217 | } else { |
---|
[f52dcc7] | 218 | B[f] -= leg->d[dim]; |
---|
[564f471] | 219 | } |
---|
[907fe10] | 220 | } |
---|
[3fde384f] | 221 | #else |
---|
[907fe10] | 222 | if (invert_svar(&e, &leg->v)) { |
---|
| 223 | delta b; |
---|
| 224 | int i; |
---|
| 225 | if (fRev) { |
---|
| 226 | adddd(&a, &POSD(to), &leg->d); |
---|
| 227 | } else { |
---|
| 228 | subdd(&a, &POSD(to), &leg->d); |
---|
| 229 | } |
---|
| 230 | mulsd(&b, &e, &a); |
---|
| 231 | for (i = 0; i < 3; i++) { |
---|
| 232 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
| 233 | B[f * FACTOR + i] += b[i]; |
---|
[564f471] | 234 | } |
---|
[907fe10] | 235 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
| 236 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
| 237 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
| 238 | } |
---|
[3fde384f] | 239 | #endif |
---|
[907fe10] | 240 | } else if (data_here(leg)) { |
---|
| 241 | /* forward leg, unfixed -> unfixed */ |
---|
| 242 | t = find_stn_in_tab(to); |
---|
[d1b1380] | 243 | #if DEBUG_MATRIX |
---|
[907fe10] | 244 | printf("Leg %d to %d, var %f, delta %f\n", f, t, e, |
---|
| 245 | leg->d[dim]); |
---|
[d1b1380] | 246 | #endif |
---|
[907fe10] | 247 | /* Ignore equated nodes & lollipops */ |
---|
[3fde384f] | 248 | #ifdef NO_COVARIANCES |
---|
[907fe10] | 249 | e = leg->v[dim]; |
---|
| 250 | if (t != f && e != (real)0.0) { |
---|
| 251 | real a; |
---|
| 252 | e = ((real)1.0) / e; |
---|
| 253 | M(f,f) += e; |
---|
| 254 | M(t,t) += e; |
---|
| 255 | if (f < t) M(t,f) -= e; else M(f,t) -= e; |
---|
| 256 | a = e * leg->d[dim]; |
---|
| 257 | B[f] -= a; |
---|
| 258 | B[t] += a; |
---|
| 259 | } |
---|
[3fde384f] | 260 | #else |
---|
[907fe10] | 261 | if (t != f && invert_svar(&e, &leg->v)) { |
---|
| 262 | int i; |
---|
| 263 | mulsd(&a, &e, &leg->d); |
---|
| 264 | for (i = 0; i < 3; i++) { |
---|
| 265 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
| 266 | M(t * FACTOR + i, t * FACTOR + i) += e[i]; |
---|
| 267 | if (f < t) |
---|
| 268 | M(t * FACTOR + i, f * FACTOR + i) -= e[i]; |
---|
| 269 | else |
---|
| 270 | M(f * FACTOR + i, t * FACTOR + i) -= e[i]; |
---|
| 271 | B[f * FACTOR + i] -= a[i]; |
---|
| 272 | B[t * FACTOR + i] += a[i]; |
---|
| 273 | } |
---|
| 274 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
| 275 | M(t * FACTOR + 1, t * FACTOR) += e[3]; |
---|
| 276 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
| 277 | M(t * FACTOR + 2, t * FACTOR) += e[4]; |
---|
| 278 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
| 279 | M(t * FACTOR + 2, t * FACTOR + 1) += e[5]; |
---|
| 280 | if (f < t) { |
---|
| 281 | M(t * FACTOR + 1, f * FACTOR) -= e[3]; |
---|
| 282 | M(t * FACTOR, f * FACTOR + 1) -= e[3]; |
---|
| 283 | M(t * FACTOR + 2, f * FACTOR) -= e[4]; |
---|
| 284 | M(t * FACTOR, f * FACTOR + 2) -= e[4]; |
---|
| 285 | M(t * FACTOR + 2, f * FACTOR + 1) -= e[5]; |
---|
| 286 | M(t * FACTOR + 1, f * FACTOR + 2) -= e[5]; |
---|
| 287 | } else { |
---|
| 288 | M(f * FACTOR + 1, t * FACTOR) -= e[3]; |
---|
| 289 | M(f * FACTOR, t * FACTOR + 1) -= e[3]; |
---|
| 290 | M(f * FACTOR + 2, t * FACTOR) -= e[4]; |
---|
| 291 | M(f * FACTOR, t * FACTOR + 2) -= e[4]; |
---|
| 292 | M(f * FACTOR + 2, t * FACTOR + 1) -= e[5]; |
---|
| 293 | M(f * FACTOR + 1, t * FACTOR + 2) -= e[5]; |
---|
[dbd68203] | 294 | } |
---|
| 295 | } |
---|
[907fe10] | 296 | #endif |
---|
[564f471] | 297 | } |
---|
[907fe10] | 298 | } |
---|
[dbd68203] | 299 | } |
---|
[d1b1380] | 300 | } |
---|
| 301 | |
---|
| 302 | #if PRINT_MATRICES |
---|
[eb18f4d] | 303 | print_matrix(M, B, n_stn_tab * FACTOR); /* 'ave a look! */ |
---|
[d1b1380] | 304 | #endif |
---|
| 305 | |
---|
| 306 | #ifdef SOR |
---|
[032ed06] | 307 | /* defined in network.c, may be altered by -z<letters> on command line */ |
---|
[a420b49] | 308 | if (optimize & BITA('i')) |
---|
[eb18f4d] | 309 | sor(M, B, n_stn_tab * FACTOR); |
---|
[dbd68203] | 310 | else |
---|
[d1b1380] | 311 | #endif |
---|
[eb18f4d] | 312 | choleski(M, B, n_stn_tab * FACTOR); |
---|
[d1b1380] | 313 | |
---|
[dbd68203] | 314 | { |
---|
[a420b49] | 315 | int m; |
---|
[eb18f4d] | 316 | for (m = (int)(n_stn_tab - 1); m >= 0; m--) { |
---|
[3fde384f] | 317 | #ifdef NO_COVARIANCES |
---|
[c19f129] | 318 | stn_tab[m]->p[dim] = B[m]; |
---|
[032ed06] | 319 | if (dim == 0) { |
---|
[4c07c51] | 320 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
[032ed06] | 321 | "setting station coordinates didn't mark pos as fixed"); |
---|
| 322 | } |
---|
[3fde384f] | 323 | #else |
---|
[702f518] | 324 | int i; |
---|
| 325 | for (i = 0; i < 3; i++) { |
---|
[c19f129] | 326 | stn_tab[m]->p[i] = B[m * FACTOR + i]; |
---|
[702f518] | 327 | } |
---|
[4c07c51] | 328 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
[032ed06] | 329 | "setting station coordinates didn't mark pos as fixed"); |
---|
[3fde384f] | 330 | #endif |
---|
[a420b49] | 331 | } |
---|
[d1b1380] | 332 | #if EXPLICIT_FIXED_FLAG |
---|
[eb18f4d] | 333 | for (m = n_stn_tab - 1; m >= 0; m--) fixpos(stn_tab[m]); |
---|
[d1b1380] | 334 | #endif |
---|
[dbd68203] | 335 | } |
---|
| 336 | } |
---|
| 337 | osfree(B); |
---|
| 338 | osfree(M); |
---|
[d1b1380] | 339 | } |
---|
| 340 | |
---|
[a420b49] | 341 | static int |
---|
| 342 | find_stn_in_tab(node *stn) |
---|
| 343 | { |
---|
[dbd68203] | 344 | int i = 0; |
---|
[eb18f4d] | 345 | pos *p = stn->name->pos; |
---|
| 346 | while (stn_tab[i] != p) |
---|
[dbd68203] | 347 | if (++i == n_stn_tab) { |
---|
[d1b1380] | 348 | #if DEBUG_INVALID |
---|
[a420b49] | 349 | fputs("Station ", stderr); |
---|
[eb18f4d] | 350 | fprint_prefix(stderr, stn->name); |
---|
| 351 | fputs(" not in table\n\n", stderr); |
---|
[d1b1380] | 352 | #endif |
---|
| 353 | #if 0 |
---|
[dbd68203] | 354 | print_prefix(stn->name); |
---|
[b5d3988] | 355 | printf(" used: %d colour %d\n", |
---|
[dbd68203] | 356 | (!!stn->leg[2])<<2 | (!!stn->leg[1])<<1 | (!!stn->leg[0]), |
---|
[eb18f4d] | 357 | stn->colour); |
---|
[d1b1380] | 358 | #endif |
---|
[a420b49] | 359 | fatalerror(/*Bug in program detected! Please report this to the authors*/11); |
---|
[dbd68203] | 360 | } |
---|
| 361 | return i; |
---|
[d1b1380] | 362 | } |
---|
| 363 | |
---|
[a420b49] | 364 | static int |
---|
| 365 | add_stn_to_tab(node *stn) |
---|
| 366 | { |
---|
[dbd68203] | 367 | int i; |
---|
[eb18f4d] | 368 | pos *p = stn->name->pos; |
---|
[a420b49] | 369 | for (i = 0; i < n_stn_tab; i++) { |
---|
[eb18f4d] | 370 | if (stn_tab[i] == p) return i; |
---|
[dbd68203] | 371 | } |
---|
[eb18f4d] | 372 | stn_tab[n_stn_tab++] = p; |
---|
[dbd68203] | 373 | return i; |
---|
[d1b1380] | 374 | } |
---|
| 375 | |
---|
[702f518] | 376 | /* Solve MX=B for X by Choleski factorisation - modified Choleski actually |
---|
| 377 | * since we factor into LDL' while Choleski is just LL' |
---|
| 378 | */ |
---|
[d1b1380] | 379 | /* Note M must be symmetric positive definite */ |
---|
| 380 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
[a420b49] | 381 | static void |
---|
[9965b2b] | 382 | choleski(real *M, real *B, long n) |
---|
[a420b49] | 383 | { |
---|
[dbd68203] | 384 | int i, j, k; |
---|
[d1b1380] | 385 | |
---|
[a420b49] | 386 | for (j = 1; j < n; j++) { |
---|
[3fde384f] | 387 | real V; |
---|
[a420b49] | 388 | for (i = 0; i < j; i++) { |
---|
[421b7d2] | 389 | V = (real)0.0; |
---|
[a420b49] | 390 | for (k = 0; k < i; k++) V += M(i,k) * M(j,k) * M(k,k); |
---|
| 391 | M(j,i) = (M(j,i) - V) / M(i,i); |
---|
[dbd68203] | 392 | } |
---|
| 393 | V = (real)0.0; |
---|
[a420b49] | 394 | for (k = 0; k < j; k++) V += M(j,k) * M(j,k) * M(k,k); |
---|
[3fde384f] | 395 | M(j,j) -= V; /* may be best to add M() last for numerical reasons too */ |
---|
[dbd68203] | 396 | } |
---|
[d1b1380] | 397 | |
---|
[dbd68203] | 398 | /* Multiply x by L inverse */ |
---|
[a420b49] | 399 | for (i = 0; i < n - 1; i++) { |
---|
| 400 | for (j = i + 1; j < n; j++) { |
---|
[dbd68203] | 401 | B[j] -= M(j,i) * B[i]; |
---|
[3fde384f] | 402 | } |
---|
[dbd68203] | 403 | } |
---|
[d1b1380] | 404 | |
---|
[dbd68203] | 405 | /* Multiply x by D inverse */ |
---|
[a420b49] | 406 | for (i = 0; i < n; i++) { |
---|
[dbd68203] | 407 | B[i] /= M(i,i); |
---|
[3fde384f] | 408 | } |
---|
| 409 | |
---|
| 410 | /* Multiply x by (L transpose) inverse */ |
---|
[9f5d1675] | 411 | for (i = (int)(n - 1); i > 0; i--) { |
---|
[a420b49] | 412 | for (j = i - 1; j >= 0; j--) { |
---|
[421b7d2] | 413 | B[j] -= M(i,j) * B[i]; |
---|
[3fde384f] | 414 | } |
---|
[dbd68203] | 415 | } |
---|
[d1b1380] | 416 | |
---|
[dbd68203] | 417 | /* printf("\n%ld/%ld\n\n",flops,flopsTot); */ |
---|
[d1b1380] | 418 | } |
---|
| 419 | |
---|
| 420 | #ifdef SOR |
---|
| 421 | /* factor to use for SOR (must have 1 <= SOR_factor < 2) */ |
---|
[702f518] | 422 | #define SOR_factor 1.93 /* 1.95 */ |
---|
[d1b1380] | 423 | |
---|
| 424 | /* Solve MX=B for X by SOR of Gauss-Siedel */ |
---|
| 425 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
[a420b49] | 426 | static void |
---|
[9965b2b] | 427 | sor(real *M, real *B, long n) |
---|
[a420b49] | 428 | { |
---|
[dbd68203] | 429 | real t, x, delta, threshold, t2; |
---|
| 430 | int row, col; |
---|
| 431 | real *X; |
---|
| 432 | long it = 0; |
---|
[d1b1380] | 433 | |
---|
[a420b49] | 434 | X = osmalloc(n * ossizeof(real)); |
---|
[d1b1380] | 435 | |
---|
[dbd68203] | 436 | threshold = 0.00001; |
---|
[d1b1380] | 437 | |
---|
[647407d] | 438 | printf("reciprocating diagonal\n"); /* TRANSLATE */ |
---|
[d1b1380] | 439 | |
---|
[3fde384f] | 440 | /* munge diagonal so we can multiply rather than divide */ |
---|
[a420b49] | 441 | for (row = n - 1; row >= 0; row--) { |
---|
[dbd68203] | 442 | M(row,row) = 1 / M(row,row); |
---|
[702f518] | 443 | X[row] = 0; |
---|
[dbd68203] | 444 | } |
---|
[d1b1380] | 445 | |
---|
[647407d] | 446 | printf("starting iteration\n"); /* TRANSLATE */ |
---|
[d1b1380] | 447 | |
---|
[dbd68203] | 448 | do { |
---|
| 449 | /*printf("*");*/ |
---|
| 450 | it++; |
---|
| 451 | t = 0.0; |
---|
[a420b49] | 452 | for (row = 0; row < n; row++) { |
---|
[dbd68203] | 453 | x = B[row]; |
---|
[a420b49] | 454 | for (col = 0; col < row; col++) x -= M(row,col) * X[col]; |
---|
| 455 | for (col++; col < n; col++) x -= M(col,row) * X[col]; |
---|
[dbd68203] | 456 | x *= M(row,row); |
---|
| 457 | delta = (x - X[row]) * SOR_factor; |
---|
| 458 | X[row] += delta; |
---|
| 459 | t2 = fabs(delta); |
---|
| 460 | if (t2 > t) t = t2; |
---|
| 461 | } |
---|
[702f518] | 462 | printf("% 6d: %8.6f\n", it, t); |
---|
[dbd68203] | 463 | } while (t >= threshold && it < 100000); |
---|
[d1b1380] | 464 | |
---|
[dbd68203] | 465 | if (t >= threshold) { |
---|
| 466 | fprintf(stderr, "*not* converged after %ld iterations\n", it); |
---|
| 467 | BUG("iteration stinks"); |
---|
| 468 | } |
---|
[d1b1380] | 469 | |
---|
[647407d] | 470 | printf("%ld iterations\n", it); /* TRANSLATE */ |
---|
[d1b1380] | 471 | |
---|
| 472 | #if 0 |
---|
[dbd68203] | 473 | putnl(); |
---|
[a420b49] | 474 | for (row = n - 1; row >= 0; row--) { |
---|
[dbd68203] | 475 | t = 0.0; |
---|
[a420b49] | 476 | for (col = 0; col < row; col++) t += M(row, col) * X[col]; |
---|
| 477 | t += X[row] / M(row, row); |
---|
| 478 | for (col = row + 1; col < n; col++) |
---|
| 479 | t += M(col, row) * X[col]; |
---|
[b5d3988] | 480 | printf("[ %f %f ]\n", t, B[row]); |
---|
[dbd68203] | 481 | } |
---|
[d1b1380] | 482 | #endif |
---|
| 483 | |
---|
[a420b49] | 484 | for (row = n - 1; row >= 0; row--) B[row] = X[row]; |
---|
[d1b1380] | 485 | |
---|
[dbd68203] | 486 | osfree(X); |
---|
[647407d] | 487 | printf("\ndone\n"); /* TRANSLATE */ |
---|
[dbd68203] | 488 | } |
---|
[d1b1380] | 489 | #endif |
---|
| 490 | |
---|
| 491 | #if PRINT_MATRICES |
---|
[a420b49] | 492 | static void |
---|
[9965b2b] | 493 | print_matrix(real *M, real *B, long n) |
---|
[a420b49] | 494 | { |
---|
[702f518] | 495 | long row, col; |
---|
[dbd68203] | 496 | printf("Matrix, M and vector, B:\n"); |
---|
[a420b49] | 497 | for (row = 0; row < n; row++) { |
---|
| 498 | for (col = 0; col <= row; col++) printf("%6.2f\t", M(row, col)); |
---|
| 499 | for (; col <= n; col++) printf(" \t"); |
---|
[dbd68203] | 500 | printf("\t%6.2f\n", B[row]); |
---|
| 501 | } |
---|
| 502 | putnl(); |
---|
| 503 | return; |
---|
[d1b1380] | 504 | } |
---|
| 505 | #endif |
---|