[ff6cfe1] | 1 | /* netbits.c |
---|
[d1b1380] | 2 | * Miscellaneous primitive network routines for Survex |
---|
[b5a3219] | 3 | * Copyright (C) 1992-2003 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 |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
[d1b1380] | 23 | |
---|
[f98f8a5] | 24 | #if 0 |
---|
[b84a343] | 25 | # define DEBUG_INVALID 1 |
---|
[f98f8a5] | 26 | #endif |
---|
| 27 | |
---|
[7a05e07] | 28 | #include "debug.h" |
---|
[a420b49] | 29 | #include "cavern.h" |
---|
[7a05e07] | 30 | #include "filename.h" |
---|
| 31 | #include "message.h" |
---|
[d1b1380] | 32 | #include "netbits.h" |
---|
[a420b49] | 33 | #include "datain.h" /* for compile_error */ |
---|
[a838042] | 34 | #include "validate.h" /* for compile_error */ |
---|
[d1b1380] | 35 | |
---|
[856b6a0] | 36 | #ifdef CHASM3DX |
---|
| 37 | #include <stddef.h> /* for offsetof */ |
---|
| 38 | #endif |
---|
| 39 | |
---|
[dd368da] | 40 | #define THRESHOLD (REAL_EPSILON * 1000) /* 100 was too small */ |
---|
[daf88e1] | 41 | |
---|
[564f471] | 42 | node *stn_iter = NULL; /* for FOR_EACH_STN */ |
---|
| 43 | |
---|
[f98f8a5] | 44 | #ifdef NO_COVARIANCES |
---|
[407084d] | 45 | static void check_var(/*const*/ var *v) { |
---|
[647407d] | 46 | int bad = 0; |
---|
| 47 | int i; |
---|
| 48 | |
---|
| 49 | for (i = 0; i < 3; i++) { |
---|
| 50 | char buf[32]; |
---|
| 51 | sprintf(buf, "%6.3f", v[i]); |
---|
[dac18d8] | 52 | if (strstr(buf, "NaN") || strstr(buf, "nan")) |
---|
| 53 | printf("*** NaN!!!\n"), bad = 1; |
---|
[647407d] | 54 | } |
---|
| 55 | if (bad) print_var(v); |
---|
| 56 | return; |
---|
[f98f8a5] | 57 | } |
---|
| 58 | #else |
---|
[a862ee8] | 59 | #define V(A,B) ((*v)[A][B]) |
---|
[407084d] | 60 | static void check_var(/*const*/ var *v) { |
---|
[a862ee8] | 61 | int bad = 0; |
---|
| 62 | int ok = 0; |
---|
| 63 | int i, j; |
---|
[407084d] | 64 | #if DEBUG_INVALID |
---|
[a862ee8] | 65 | real det = 0.0; |
---|
[407084d] | 66 | #endif |
---|
[a862ee8] | 67 | |
---|
| 68 | for (i = 0; i < 3; i++) { |
---|
| 69 | for (j = 0; j < 3; j++) { |
---|
| 70 | char buf[32]; |
---|
[a5da11c] | 71 | sprintf(buf, "%6.3f", V(i, j)); |
---|
[dac18d8] | 72 | if (strstr(buf, "NaN") || strstr(buf, "nan")) |
---|
| 73 | printf("*** NaN!!!\n"), bad = 1, ok = 1; |
---|
[a5da11c] | 74 | if (V(i, j) != 0.0) ok = 1; |
---|
[a862ee8] | 75 | } |
---|
| 76 | } |
---|
| 77 | if (!ok) return; /* ignore all-zero matrices */ |
---|
| 78 | |
---|
[40fb2e4] | 79 | #if DEBUG_INVALID |
---|
[a862ee8] | 80 | for (i = 0; i < 3; i++) { |
---|
[a5da11c] | 81 | det += V(i, 0) * (V((i + 1) % 3, 1) * V((i + 2) % 3, 2) - |
---|
| 82 | V((i + 1) % 3, 2) * V((i + 2) % 3, 1)); |
---|
[a862ee8] | 83 | } |
---|
| 84 | |
---|
[daf88e1] | 85 | if (fabs(det) < THRESHOLD) |
---|
[a862ee8] | 86 | printf("*** Singular!!!\n"), bad = 1; |
---|
[40fb2e4] | 87 | #endif |
---|
[a862ee8] | 88 | |
---|
| 89 | #if 0 |
---|
[840e496] | 90 | /* don't check this - it isn't always the case! */ |
---|
[daf88e1] | 91 | if (fabs(V(0,1) - V(1,0)) > THRESHOLD || |
---|
| 92 | fabs(V(0,2) - V(2,0)) > THRESHOLD || |
---|
| 93 | fabs(V(1,2) - V(2,1)) > THRESHOLD) |
---|
[a862ee8] | 94 | printf("*** Not symmetric!!!\n"), bad = 1; |
---|
| 95 | if (V(0,0) <= 0.0 || V(1,1) <= 0.0 || V(2,2) <= 0.0) |
---|
| 96 | printf("*** Not positive definite (diag <= 0)!!!\n"), bad = 1; |
---|
| 97 | if (sqrd(V(0,1)) >= V(0,0)*V(1,1) || sqrd(V(0,2)) >= V(0,0)*V(2,2) || |
---|
| 98 | sqrd(V(1,0)) >= V(0,0)*V(1,1) || sqrd(V(2,0)) >= V(0,0)*V(2,2) || |
---|
| 99 | sqrd(V(1,2)) >= V(2,2)*V(1,1) || sqrd(V(2,1)) >= V(2,2)*V(1,1)) |
---|
| 100 | printf("*** Not positive definite (off diag^2 >= diag product)!!!\n"), bad = 1; |
---|
| 101 | #endif |
---|
| 102 | if (bad) print_var(*v); |
---|
| 103 | } |
---|
[59f2dbb] | 104 | |
---|
| 105 | #define SN(V,A,B) ((*(V))[(A)==(B)?(A):2+(A)+(B)]) |
---|
| 106 | #define S(A,B) SN(v,A,B) |
---|
| 107 | |
---|
| 108 | static void check_svar(/*const*/ svar *v) { |
---|
| 109 | int bad = 0; |
---|
| 110 | int ok = 0; |
---|
[dac18d8] | 111 | int i; |
---|
[59f2dbb] | 112 | #if DEBUG_INVALID |
---|
| 113 | real det = 0.0; |
---|
| 114 | #endif |
---|
| 115 | |
---|
[dac18d8] | 116 | for (i = 0; i < 6; i++) { |
---|
| 117 | char buf[32]; |
---|
| 118 | sprintf(buf, "%6.3f", (*v)[i]); |
---|
| 119 | if (strstr(buf, "NaN") || strstr(buf, "nan")) |
---|
| 120 | printf("*** NaN!!!\n"), bad = 1, ok = 1; |
---|
| 121 | if ((*v)[i] != 0.0) ok = 1; |
---|
[59f2dbb] | 122 | } |
---|
| 123 | if (!ok) return; /* ignore all-zero matrices */ |
---|
| 124 | |
---|
| 125 | #if DEBUG_INVALID |
---|
| 126 | for (i = 0; i < 3; i++) { |
---|
| 127 | det += S(i, 0) * (S((i + 1) % 3, 1) * S((i + 2) % 3, 2) - |
---|
| 128 | S((i + 1) % 3, 2) * S((i + 2) % 3, 1)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (fabs(det) < THRESHOLD) |
---|
| 132 | printf("*** Singular!!!\n"), bad = 1; |
---|
| 133 | #endif |
---|
| 134 | |
---|
[840e496] | 135 | #if 0 |
---|
| 136 | /* don't check this - it isn't always the case! */ |
---|
[dac18d8] | 137 | if ((*v)[0] <= 0.0 || (*v)[1] <= 0.0 || (*v)[2] <= 0.0) |
---|
[59f2dbb] | 138 | printf("*** Not positive definite (diag <= 0)!!!\n"), bad = 1; |
---|
[dac18d8] | 139 | if (sqrd((*v)[3]) >= (*v)[0]*(*v)[1] || |
---|
| 140 | sqrd((*v)[4]) >= (*v)[0]*(*v)[2] || |
---|
| 141 | sqrd((*v)[5]) >= (*v)[1]*(*v)[2]) |
---|
[59f2dbb] | 142 | printf("*** Not positive definite (off diag^2 >= diag product)!!!\n"), bad = 1; |
---|
[840e496] | 143 | #endif |
---|
[59f2dbb] | 144 | if (bad) print_svar(*v); |
---|
| 145 | } |
---|
[f98f8a5] | 146 | #endif |
---|
[a862ee8] | 147 | |
---|
[eb18f4d] | 148 | static void check_d(/*const*/ delta *d) { |
---|
[5d26a94] | 149 | int bad = 0; |
---|
| 150 | int i; |
---|
| 151 | |
---|
| 152 | for (i = 0; i < 3; i++) { |
---|
| 153 | char buf[32]; |
---|
| 154 | sprintf(buf, "%6.3f", (*d)[i]); |
---|
[dac18d8] | 155 | if (strstr(buf, "NaN") || strstr(buf, "nan")) |
---|
| 156 | printf("*** NaN!!!\n"), bad = 1; |
---|
[5d26a94] | 157 | } |
---|
| 158 | |
---|
| 159 | if (bad) printf("(%4.2f,%4.2f,%4.2f)\n", (*d)[0], (*d)[1], (*d)[2]); |
---|
| 160 | } |
---|
| 161 | |
---|
[564f471] | 162 | /* insert at head of double-linked list */ |
---|
| 163 | void |
---|
| 164 | add_stn_to_list(node **list, node *stn) { |
---|
[4c07c51] | 165 | SVX_ASSERT(list); |
---|
| 166 | SVX_ASSERT(stn); |
---|
| 167 | SVX_ASSERT(stn_iter != stn); /* if it does, we're still on a list... */ |
---|
[85fe934] | 168 | #if 0 |
---|
| 169 | printf("add_stn_to_list(%p, [%p] ", list, stn); |
---|
| 170 | if (stn->name) print_prefix(stn->name); |
---|
| 171 | printf(")\n"); |
---|
| 172 | #endif |
---|
[564f471] | 173 | stn->next = *list; |
---|
| 174 | stn->prev = NULL; |
---|
| 175 | if (*list) (*list)->prev = stn; |
---|
| 176 | *list = stn; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /* remove from double-linked list */ |
---|
| 180 | void |
---|
| 181 | remove_stn_from_list(node **list, node *stn) { |
---|
[4c07c51] | 182 | SVX_ASSERT(list); |
---|
| 183 | SVX_ASSERT(stn); |
---|
[85fe934] | 184 | #if 0 |
---|
| 185 | printf("remove_stn_from_list(%p, [%p] ", list, stn); |
---|
| 186 | if (stn->name) print_prefix(stn->name); |
---|
| 187 | printf(")\n"); |
---|
| 188 | #endif |
---|
[b84a343] | 189 | #if DEBUG_INVALID |
---|
[85fe934] | 190 | { |
---|
| 191 | /* check station is actually in this list */ |
---|
| 192 | node *stn_to_remove_is_in_list = *list; |
---|
| 193 | validate(); |
---|
| 194 | while (stn_to_remove_is_in_list != stn) { |
---|
[4c07c51] | 195 | SVX_ASSERT(stn_to_remove_is_in_list); |
---|
[85fe934] | 196 | stn_to_remove_is_in_list = stn_to_remove_is_in_list->next; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | #endif |
---|
[564f471] | 200 | /* adjust the iterator if it points to the element we're deleting */ |
---|
| 201 | if (stn_iter == stn) stn_iter = stn_iter->next; |
---|
| 202 | /* need a special case if we're removing the list head */ |
---|
| 203 | if (stn->prev == NULL) { |
---|
| 204 | *list = stn->next; |
---|
| 205 | if (*list) (*list)->prev = NULL; |
---|
| 206 | } else { |
---|
| 207 | stn->prev->next = stn->next; |
---|
[28ddfe1] | 208 | if (stn->next) stn->next->prev = stn->prev; |
---|
[564f471] | 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[d1b1380] | 212 | /* Create (uses osmalloc) a forward leg containing the data in leg, or |
---|
| 213 | * the reversed data in the reverse of leg, if leg doesn't hold data |
---|
| 214 | */ |
---|
[2140502] | 215 | linkfor * |
---|
[a420b49] | 216 | copy_link(linkfor *leg) |
---|
| 217 | { |
---|
[d1b1380] | 218 | linkfor *legOut; |
---|
| 219 | int d; |
---|
[a420b49] | 220 | legOut = osnew(linkfor); |
---|
[d1b1380] | 221 | if (data_here(leg)) { |
---|
[a420b49] | 222 | for (d = 2; d >= 0; d--) legOut->d[d] = leg->d[d]; |
---|
[d1b1380] | 223 | } else { |
---|
[a420b49] | 224 | leg = reverse_leg(leg); |
---|
[4c07c51] | 225 | SVX_ASSERT(data_here(leg)); |
---|
[a420b49] | 226 | for (d = 2; d >= 0; d--) legOut->d[d] = -leg->d[d]; |
---|
[d1b1380] | 227 | } |
---|
| 228 | #if 1 |
---|
[7a05e07] | 229 | # ifndef NO_COVARIANCES |
---|
[59f2dbb] | 230 | check_svar(&(leg->v)); |
---|
[cb3d1e2] | 231 | { |
---|
[59f2dbb] | 232 | int i; |
---|
| 233 | for (i = 0; i < 6; i++) legOut->v[i] = leg->v[i]; |
---|
[d1b1380] | 234 | } |
---|
[7a05e07] | 235 | # else |
---|
[a420b49] | 236 | for (d = 2; d >= 0; d--) legOut->v[d] = leg->v[d]; |
---|
[7a05e07] | 237 | # endif |
---|
[d1b1380] | 238 | #else |
---|
[59f2dbb] | 239 | memcpy(legOut->v, leg->v, sizeof(svar)); |
---|
[d1b1380] | 240 | #endif |
---|
[b5a3219] | 241 | legOut->meta = pcs->meta; |
---|
| 242 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
[d1b1380] | 243 | return legOut; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /* Adds to the forward leg `leg', the data in leg2, or the reversed data |
---|
| 247 | * in the reverse of leg2, if leg2 doesn't hold data |
---|
| 248 | */ |
---|
[2140502] | 249 | linkfor * |
---|
[a5da11c] | 250 | addto_link(linkfor *leg, const linkfor *leg2) |
---|
[a420b49] | 251 | { |
---|
[d1b1380] | 252 | if (data_here(leg2)) { |
---|
[407084d] | 253 | adddd(&leg->d, &leg->d, &((linkfor *)leg2)->d); |
---|
[d1b1380] | 254 | } else { |
---|
[a420b49] | 255 | leg2 = reverse_leg(leg2); |
---|
[4c07c51] | 256 | SVX_ASSERT(data_here(leg2)); |
---|
[407084d] | 257 | subdd(&leg->d, &leg->d, &((linkfor *)leg2)->d); |
---|
[d1b1380] | 258 | } |
---|
[59f2dbb] | 259 | addss(&leg->v, &leg->v, &((linkfor *)leg2)->v); |
---|
[d1b1380] | 260 | return leg; |
---|
| 261 | } |
---|
| 262 | |
---|
[693388e] | 263 | static void |
---|
| 264 | addleg_(node *fr, node *to, |
---|
| 265 | real dx, real dy, real dz, |
---|
| 266 | real vx, real vy, real vz, |
---|
| 267 | #ifndef NO_COVARIANCES |
---|
| 268 | real cyz, real czx, real cxy, |
---|
| 269 | #endif |
---|
| 270 | int leg_flags) |
---|
| 271 | { |
---|
| 272 | int i, j; |
---|
| 273 | linkfor *leg, *leg2; |
---|
| 274 | int shape; |
---|
| 275 | /* we have been asked to add a leg with the same node at both ends |
---|
| 276 | * - this should be trapped by the caller */ |
---|
[4c07c51] | 277 | SVX_ASSERT(fr->name != to->name); |
---|
[693388e] | 278 | |
---|
| 279 | leg = osnew(linkfor); |
---|
| 280 | leg2 = (linkfor*)osnew(linkrev); |
---|
| 281 | |
---|
| 282 | i = freeleg(&fr); |
---|
| 283 | j = freeleg(&to); |
---|
| 284 | |
---|
| 285 | leg->l.to = to; |
---|
| 286 | leg2->l.to = fr; |
---|
| 287 | leg->d[0] = dx; |
---|
| 288 | leg->d[1] = dy; |
---|
| 289 | leg->d[2] = dz; |
---|
| 290 | #ifndef NO_COVARIANCES |
---|
| 291 | leg->v[0] = vx; |
---|
| 292 | leg->v[1] = vy; |
---|
| 293 | leg->v[2] = vz; |
---|
| 294 | leg->v[3] = cxy; |
---|
| 295 | leg->v[4] = czx; |
---|
| 296 | leg->v[5] = cyz; |
---|
| 297 | check_svar(&(leg->v)); |
---|
| 298 | #else |
---|
| 299 | leg->v[0] = vx; |
---|
| 300 | leg->v[1] = vy; |
---|
| 301 | leg->v[2] = vz; |
---|
| 302 | #endif |
---|
| 303 | leg2->l.reverse = i; |
---|
| 304 | leg->l.reverse = j | FLAG_DATAHERE | leg_flags; |
---|
| 305 | |
---|
| 306 | leg->l.flags = pcs->flags; |
---|
[b5a3219] | 307 | leg->meta = pcs->meta; |
---|
| 308 | if (pcs->meta) ++pcs->meta->ref_count; |
---|
[693388e] | 309 | |
---|
| 310 | fr->leg[i] = leg; |
---|
| 311 | to->leg[j] = leg2; |
---|
| 312 | |
---|
| 313 | shape = fr->name->shape + 1; |
---|
| 314 | if (shape < 1) shape = 1 - shape; |
---|
| 315 | fr->name->shape = shape; |
---|
| 316 | |
---|
| 317 | shape = to->name->shape + 1; |
---|
| 318 | if (shape < 1) shape = 1 - shape; |
---|
| 319 | to->name->shape = shape; |
---|
| 320 | } |
---|
| 321 | |
---|
[be374fc] | 322 | /* Add a leg between names *fr_name and *to_name |
---|
| 323 | * If either is a three node, then it is split into two |
---|
| 324 | * and the data structure adjusted as necessary. |
---|
| 325 | */ |
---|
[2140502] | 326 | void |
---|
| 327 | addlegbyname(prefix *fr_name, prefix *to_name, bool fToFirst, |
---|
| 328 | real dx, real dy, real dz, |
---|
| 329 | real vx, real vy, real vz |
---|
| 330 | #ifndef NO_COVARIANCES |
---|
| 331 | , real cyz, real czx, real cxy |
---|
| 332 | #endif |
---|
| 333 | ) |
---|
| 334 | { |
---|
| 335 | node *to, *fr; |
---|
| 336 | if (to_name == fr_name) { |
---|
| 337 | compile_error(/*Survey leg with same station (`%s') at both ends - typing error?*/50, |
---|
| 338 | sprint_prefix(to_name)); |
---|
| 339 | return; |
---|
[421b7d2] | 340 | } |
---|
[2140502] | 341 | if (fToFirst) { |
---|
| 342 | to = StnFromPfx(to_name); |
---|
| 343 | fr = StnFromPfx(fr_name); |
---|
| 344 | } else { |
---|
| 345 | fr = StnFromPfx(fr_name); |
---|
| 346 | to = StnFromPfx(to_name); |
---|
| 347 | } |
---|
[693388e] | 348 | cLegs++; /* increment count (first as compiler may do tail recursion) */ |
---|
| 349 | addleg_(fr, to, dx, dy, dz, vx, vy, vz, |
---|
[2140502] | 350 | #ifndef NO_COVARIANCES |
---|
[693388e] | 351 | cyz, czx, cxy, |
---|
[2140502] | 352 | #endif |
---|
[693388e] | 353 | 0); |
---|
[2140502] | 354 | } |
---|
| 355 | |
---|
[be374fc] | 356 | /* helper function for replace_pfx */ |
---|
| 357 | static void |
---|
| 358 | replace_pfx_(node *stn, node *from, pos *pos_replace, pos *pos_with) |
---|
| 359 | { |
---|
| 360 | int d; |
---|
| 361 | stn->name->pos = pos_with; |
---|
| 362 | for (d = 0; d < 3; d++) { |
---|
| 363 | linkfor *leg = stn->leg[d]; |
---|
| 364 | node *to; |
---|
| 365 | if (!leg) break; |
---|
| 366 | to = leg->l.to; |
---|
| 367 | if (to == from) continue; |
---|
| 368 | |
---|
| 369 | if (fZeros(data_here(leg) ? &leg->v : &reverse_leg(leg)->v)) |
---|
| 370 | replace_pfx_(to, stn, pos_replace, pos_with); |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | /* We used to iterate over the whole station list (inefficient) - now we |
---|
| 375 | * just look at any neighbouring nodes to see if they are equated */ |
---|
| 376 | static void |
---|
| 377 | replace_pfx(const prefix *pfx_replace, const prefix *pfx_with) |
---|
| 378 | { |
---|
| 379 | pos *pos_replace; |
---|
[4c07c51] | 380 | SVX_ASSERT(pfx_replace); |
---|
| 381 | SVX_ASSERT(pfx_with); |
---|
[be374fc] | 382 | pos_replace = pfx_replace->pos; |
---|
[4c07c51] | 383 | SVX_ASSERT(pos_replace != pfx_with->pos); |
---|
[be374fc] | 384 | |
---|
| 385 | replace_pfx_(pfx_replace->stn, NULL, pos_replace, pfx_with->pos); |
---|
| 386 | |
---|
| 387 | #if DEBUG_INVALID |
---|
| 388 | { |
---|
| 389 | node *stn; |
---|
| 390 | FOR_EACH_STN(stn, stnlist) { |
---|
[4c07c51] | 391 | SVX_ASSERT(stn->name->pos != pos_replace); |
---|
[be374fc] | 392 | } |
---|
| 393 | } |
---|
| 394 | #endif |
---|
| 395 | |
---|
| 396 | /* free the (now-unused) old pos */ |
---|
| 397 | osfree(pos_replace); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | /* Add an equating leg between existing stations *fr and *to (whose names are |
---|
| 401 | * name1 and name2). |
---|
[d1b1380] | 402 | */ |
---|
[a420b49] | 403 | void |
---|
[be374fc] | 404 | process_equate(prefix *name1, prefix *name2) |
---|
[a420b49] | 405 | { |
---|
[925fa451] | 406 | node *stn1, *stn2; |
---|
[be374fc] | 407 | if (name1 == name2) { |
---|
| 408 | /* catch something like *equate "fred fred" */ |
---|
| 409 | compile_warning(/*Station `%s' equated to itself*/13, |
---|
| 410 | sprint_prefix(name1)); |
---|
| 411 | return; |
---|
| 412 | } |
---|
[925fa451] | 413 | stn1 = StnFromPfx(name1); |
---|
| 414 | stn2 = StnFromPfx(name2); |
---|
[be374fc] | 415 | /* equate nodes if not already equated */ |
---|
[925fa451] | 416 | if (name1->pos != name2->pos) { |
---|
[be374fc] | 417 | if (pfx_fixed(name1)) { |
---|
| 418 | if (pfx_fixed(name2)) { |
---|
| 419 | /* both are fixed, but let them off iff their coordinates match */ |
---|
| 420 | char *s = osstrdup(sprint_prefix(name1)); |
---|
| 421 | int d; |
---|
| 422 | for (d = 2; d >= 0; d--) { |
---|
| 423 | if (name1->pos->p[d] != name2->pos->p[d]) { |
---|
| 424 | compile_error(/*Tried to equate two non-equal fixed stations: `%s' and `%s'*/52, |
---|
| 425 | s, sprint_prefix(name2)); |
---|
| 426 | osfree(s); |
---|
| 427 | return; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | compile_warning(/*Equating two equal fixed points: `%s' and `%s'*/53, |
---|
| 431 | s, sprint_prefix(name2)); |
---|
| 432 | osfree(s); |
---|
| 433 | } |
---|
[925fa451] | 434 | |
---|
[be374fc] | 435 | /* name1 is fixed, so replace all refs to name2's pos with name1's */ |
---|
| 436 | replace_pfx(name2, name1); |
---|
| 437 | } else { |
---|
| 438 | /* name1 isn't fixed, so replace all refs to its pos with name2's */ |
---|
| 439 | replace_pfx(name1, name2); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | /* count equates as legs for now... */ |
---|
| 443 | cLegs++; |
---|
| 444 | addleg_(stn1, stn2, |
---|
| 445 | (real)0.0, (real)0.0, (real)0.0, |
---|
| 446 | (real)0.0, (real)0.0, (real)0.0, |
---|
[7a05e07] | 447 | #ifndef NO_COVARIANCES |
---|
[be374fc] | 448 | (real)0.0, (real)0.0, (real)0.0, |
---|
[7a05e07] | 449 | #endif |
---|
[be374fc] | 450 | FLAG_FAKE); |
---|
| 451 | } |
---|
[d1b1380] | 452 | } |
---|
| 453 | |
---|
| 454 | /* Add a 'fake' leg (not counted) between existing stations *fr and *to |
---|
[2140502] | 455 | * (which *must* be different) |
---|
[d1b1380] | 456 | * If either node is a three node, then it is split into two |
---|
| 457 | * and the data structure adjusted as necessary |
---|
| 458 | */ |
---|
[a420b49] | 459 | void |
---|
| 460 | addfakeleg(node *fr, node *to, |
---|
| 461 | real dx, real dy, real dz, |
---|
| 462 | real vx, real vy, real vz |
---|
[7a05e07] | 463 | #ifndef NO_COVARIANCES |
---|
[a420b49] | 464 | , real cyz, real czx, real cxy |
---|
[7a05e07] | 465 | #endif |
---|
[a420b49] | 466 | ) |
---|
| 467 | { |
---|
[693388e] | 468 | addleg_(fr, to, dx, dy, dz, vx, vy, vz, |
---|
[8cf2e04] | 469 | #ifndef NO_COVARIANCES |
---|
[693388e] | 470 | cyz, czx, cxy, |
---|
[8cf2e04] | 471 | #endif |
---|
[693388e] | 472 | FLAG_FAKE); |
---|
[d1b1380] | 473 | } |
---|
| 474 | |
---|
[a420b49] | 475 | char |
---|
| 476 | freeleg(node **stnptr) |
---|
| 477 | { |
---|
[d1b1380] | 478 | node *stn, *oldstn; |
---|
| 479 | linkfor *leg, *leg2; |
---|
[94f6ef3] | 480 | #ifndef NO_COVARIANCES |
---|
[59f2dbb] | 481 | int i; |
---|
[94f6ef3] | 482 | #endif |
---|
[7a05e07] | 483 | |
---|
[a420b49] | 484 | stn = *stnptr; |
---|
[7a05e07] | 485 | |
---|
[a420b49] | 486 | if (stn->leg[0] == NULL) return 0; /* leg[0] unused */ |
---|
| 487 | if (stn->leg[1] == NULL) return 1; /* leg[1] unused */ |
---|
| 488 | if (stn->leg[2] == NULL) return 2; /* leg[2] unused */ |
---|
[d1b1380] | 489 | |
---|
| 490 | /* All legs used, so split node in two */ |
---|
[a420b49] | 491 | oldstn = stn; |
---|
| 492 | stn = osnew(node); |
---|
| 493 | leg = osnew(linkfor); |
---|
| 494 | leg2 = (linkfor*)osnew(linkrev); |
---|
[d1b1380] | 495 | |
---|
[a420b49] | 496 | *stnptr = stn; |
---|
[d1b1380] | 497 | |
---|
[564f471] | 498 | add_stn_to_list(&stnlist, stn); |
---|
[a420b49] | 499 | stn->name = oldstn->name; |
---|
[d1b1380] | 500 | |
---|
[a420b49] | 501 | leg->l.to = stn; |
---|
| 502 | leg->d[0] = leg->d[1] = leg->d[2] = (real)0.0; |
---|
[7a05e07] | 503 | |
---|
[8cf2e04] | 504 | #ifndef NO_COVARIANCES |
---|
[59f2dbb] | 505 | for (i = 0; i < 6; i++) leg->v[i] = (real)0.0; |
---|
[8cf2e04] | 506 | #else |
---|
[a420b49] | 507 | leg->v[0] = leg->v[1] = leg->v[2] = (real)0.0; |
---|
[8cf2e04] | 508 | #endif |
---|
[693388e] | 509 | leg->l.reverse = 1 | FLAG_DATAHERE | FLAG_FAKE; |
---|
[5c3c61a] | 510 | leg->l.flags = pcs->flags; |
---|
[d1b1380] | 511 | |
---|
[a420b49] | 512 | leg2->l.to = oldstn; |
---|
| 513 | leg2->l.reverse = 0; |
---|
[d1b1380] | 514 | |
---|
[eb04711] | 515 | /* NB this preserves pos->stn->leg[0] to point to the "real" fixed point |
---|
| 516 | * for stations fixed with error estimates |
---|
| 517 | */ |
---|
[a420b49] | 518 | stn->leg[0] = oldstn->leg[0]; |
---|
[d1b1380] | 519 | /* correct reverse leg */ |
---|
[a420b49] | 520 | reverse_leg(stn->leg[0])->l.to = stn; |
---|
| 521 | stn->leg[1] = leg2; |
---|
[d1b1380] | 522 | |
---|
[a420b49] | 523 | oldstn->leg[0] = leg; |
---|
[d1b1380] | 524 | |
---|
[a420b49] | 525 | stn->leg[2] = NULL; /* needed as stn->leg[dirn]==NULL indicates unused */ |
---|
[d1b1380] | 526 | |
---|
| 527 | return(2); /* leg[2] unused */ |
---|
| 528 | } |
---|
| 529 | |
---|
[a420b49] | 530 | node * |
---|
| 531 | StnFromPfx(prefix *name) |
---|
| 532 | { |
---|
[d1b1380] | 533 | node *stn; |
---|
[a420b49] | 534 | if (name->stn != NULL) return (name->stn); |
---|
| 535 | stn = osnew(node); |
---|
| 536 | stn->name = name; |
---|
| 537 | if (name->pos == NULL) { |
---|
[be97baf] | 538 | #ifdef CHASM3DX |
---|
| 539 | if (fUseNewFormat) { |
---|
| 540 | name->pos = osnew(pos); |
---|
| 541 | name->pos->id = 0; |
---|
| 542 | } else { |
---|
| 543 | /* only allocate the part of the structure we need... */ |
---|
| 544 | name->pos = (pos *)osmalloc(offsetof(pos, id)); |
---|
| 545 | } |
---|
| 546 | #else |
---|
[a420b49] | 547 | name->pos = osnew(pos); |
---|
[647407d] | 548 | #endif |
---|
[d1b1380] | 549 | unfix(stn); |
---|
| 550 | } |
---|
[a420b49] | 551 | stn->leg[0] = stn->leg[1] = stn->leg[2] = NULL; |
---|
[564f471] | 552 | add_stn_to_list(&stnlist, stn); |
---|
[a420b49] | 553 | name->stn = stn; |
---|
[d1b1380] | 554 | cStns++; |
---|
| 555 | return stn; |
---|
| 556 | } |
---|
| 557 | |
---|
[a420b49] | 558 | extern void |
---|
[a862ee8] | 559 | fprint_prefix(FILE *fh, const prefix *ptr) |
---|
[a420b49] | 560 | { |
---|
[4c07c51] | 561 | SVX_ASSERT(ptr); |
---|
[a420b49] | 562 | if (ptr->up != NULL) { |
---|
| 563 | fprint_prefix(fh, ptr->up); |
---|
| 564 | if (ptr->up->up != NULL) fputc('.', fh); |
---|
[4c07c51] | 565 | SVX_ASSERT(ptr->ident); |
---|
[a420b49] | 566 | fputs(ptr->ident, fh); |
---|
[d1b1380] | 567 | } |
---|
| 568 | } |
---|
| 569 | |
---|
[ee2e333] | 570 | static char *buffer = NULL; |
---|
[95d4862] | 571 | static OSSIZE_T buffer_len = 256; |
---|
[ee2e333] | 572 | |
---|
[95d4862] | 573 | static OSSIZE_T |
---|
[ee2e333] | 574 | sprint_prefix_(const prefix *ptr) |
---|
[a420b49] | 575 | { |
---|
[95d4862] | 576 | OSSIZE_T len = 1; |
---|
[8cf2e04] | 577 | if (ptr->up != NULL) { |
---|
[4c07c51] | 578 | SVX_ASSERT(ptr->ident); |
---|
[95d4862] | 579 | len = sprint_prefix_(ptr->up) + strlen(ptr->ident); |
---|
[ee2e333] | 580 | if (ptr->up->up != NULL) len++; |
---|
| 581 | if (len > buffer_len) { |
---|
| 582 | buffer = osrealloc(buffer, len); |
---|
| 583 | buffer_len = len; |
---|
[a420b49] | 584 | } |
---|
[ee2e333] | 585 | if (ptr->up->up != NULL) strcat(buffer, "."); |
---|
| 586 | strcat(buffer, ptr->ident); |
---|
[d1b1380] | 587 | } |
---|
[ee2e333] | 588 | return len; |
---|
[d1b1380] | 589 | } |
---|
| 590 | |
---|
[a420b49] | 591 | extern char * |
---|
[a862ee8] | 592 | sprint_prefix(const prefix *ptr) |
---|
[a420b49] | 593 | { |
---|
[4c07c51] | 594 | SVX_ASSERT(ptr); |
---|
[ee2e333] | 595 | if (!buffer) buffer = osmalloc(buffer_len); |
---|
| 596 | *buffer = '\0'; |
---|
| 597 | sprint_prefix_(ptr); |
---|
[a420b49] | 598 | return buffer; |
---|
[d1b1380] | 599 | } |
---|
| 600 | |
---|
| 601 | /* r = ab ; r,a,b are variance matrices */ |
---|
[a420b49] | 602 | void |
---|
[407084d] | 603 | mulvv(var *r, /*const*/ var *a, /*const*/ var *b) |
---|
[a420b49] | 604 | { |
---|
[d1b1380] | 605 | #ifdef NO_COVARIANCES |
---|
| 606 | /* variance-only version */ |
---|
| 607 | (*r)[0] = (*a)[0] * (*b)[0]; |
---|
| 608 | (*r)[1] = (*a)[1] * (*b)[1]; |
---|
| 609 | (*r)[2] = (*a)[2] * (*b)[2]; |
---|
| 610 | #else |
---|
| 611 | int i, j, k; |
---|
| 612 | real tot; |
---|
| 613 | |
---|
[4c07c51] | 614 | SVX_ASSERT((/*const*/ var *)r != a); |
---|
| 615 | SVX_ASSERT((/*const*/ var *)r != b); |
---|
[a862ee8] | 616 | |
---|
| 617 | check_var(a); |
---|
| 618 | check_var(b); |
---|
| 619 | |
---|
[a420b49] | 620 | for (i = 0; i < 3; i++) { |
---|
| 621 | for (j = 0; j < 3; j++) { |
---|
[d1b1380] | 622 | tot = 0; |
---|
[a420b49] | 623 | for (k = 0; k < 3; k++) { |
---|
[d1b1380] | 624 | tot += (*a)[i][k] * (*b)[k][j]; |
---|
| 625 | } |
---|
| 626 | (*r)[i][j] = tot; |
---|
| 627 | } |
---|
| 628 | } |
---|
[a862ee8] | 629 | check_var(r); |
---|
[d1b1380] | 630 | #endif |
---|
| 631 | } |
---|
| 632 | |
---|
[59f2dbb] | 633 | /* r = ab ; r,a,b are variance matrices */ |
---|
| 634 | void |
---|
| 635 | mulss(var *r, /*const*/ svar *a, /*const*/ svar *b) |
---|
| 636 | { |
---|
| 637 | #ifdef NO_COVARIANCES |
---|
| 638 | /* variance-only version */ |
---|
| 639 | (*r)[0] = (*a)[0] * (*b)[0]; |
---|
| 640 | (*r)[1] = (*a)[1] * (*b)[1]; |
---|
| 641 | (*r)[2] = (*a)[2] * (*b)[2]; |
---|
| 642 | #else |
---|
| 643 | int i, j, k; |
---|
| 644 | real tot; |
---|
| 645 | |
---|
| 646 | #if 0 |
---|
[4c07c51] | 647 | SVX_ASSERT((/*const*/ var *)r != a); |
---|
| 648 | SVX_ASSERT((/*const*/ var *)r != b); |
---|
[59f2dbb] | 649 | #endif |
---|
| 650 | |
---|
| 651 | check_svar(a); |
---|
| 652 | check_svar(b); |
---|
| 653 | |
---|
| 654 | for (i = 0; i < 3; i++) { |
---|
| 655 | for (j = 0; j < 3; j++) { |
---|
| 656 | tot = 0; |
---|
| 657 | for (k = 0; k < 3; k++) { |
---|
| 658 | tot += SN(a,i,k) * SN(b,k,j); |
---|
| 659 | } |
---|
| 660 | (*r)[i][j] = tot; |
---|
| 661 | } |
---|
| 662 | } |
---|
| 663 | check_var(r); |
---|
| 664 | #endif |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | /* r = ab ; r,a,b are variance matrices */ |
---|
| 668 | void |
---|
| 669 | smulvs(svar *r, /*const*/ var *a, /*const*/ svar *b) |
---|
| 670 | { |
---|
| 671 | #ifdef NO_COVARIANCES |
---|
| 672 | /* variance-only version */ |
---|
| 673 | (*r)[0] = (*a)[0] * (*b)[0]; |
---|
| 674 | (*r)[1] = (*a)[1] * (*b)[1]; |
---|
| 675 | (*r)[2] = (*a)[2] * (*b)[2]; |
---|
| 676 | #else |
---|
| 677 | int i, j, k; |
---|
| 678 | real tot; |
---|
| 679 | |
---|
| 680 | #if 0 |
---|
[4c07c51] | 681 | SVX_ASSERT((/*const*/ var *)r != a); |
---|
[59f2dbb] | 682 | #endif |
---|
[4c07c51] | 683 | SVX_ASSERT((/*const*/ svar *)r != b); |
---|
[59f2dbb] | 684 | |
---|
| 685 | check_var(a); |
---|
| 686 | check_svar(b); |
---|
| 687 | |
---|
| 688 | (*r)[3]=(*r)[4]=(*r)[5]=-999; |
---|
| 689 | for (i = 0; i < 3; i++) { |
---|
| 690 | for (j = 0; j < 3; j++) { |
---|
| 691 | tot = 0; |
---|
| 692 | for (k = 0; k < 3; k++) { |
---|
| 693 | tot += (*a)[i][k] * SN(b,k,j); |
---|
| 694 | } |
---|
| 695 | if (i <= j) |
---|
| 696 | SN(r,i,j) = tot; |
---|
| 697 | else if (fabs(SN(r,j,i) - tot) > THRESHOLD) { |
---|
| 698 | printf("not sym - %d,%d = %f, %d,%d was %f\n", |
---|
| 699 | i,j,tot,j,i,SN(r,j,i)); |
---|
| 700 | BUG("smulvs didn't produce a sym mx\n"); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | check_svar(r); |
---|
| 705 | #endif |
---|
| 706 | } |
---|
| 707 | |
---|
[d1b1380] | 708 | /* r = ab ; r,b delta vectors; a variance matrix */ |
---|
[a420b49] | 709 | void |
---|
[eb18f4d] | 710 | mulvd(delta *r, /*const*/ var *a, /*const*/ delta *b) |
---|
[a420b49] | 711 | { |
---|
[d1b1380] | 712 | #ifdef NO_COVARIANCES |
---|
| 713 | /* variance-only version */ |
---|
| 714 | (*r)[0] = (*a)[0] * (*b)[0]; |
---|
| 715 | (*r)[1] = (*a)[1] * (*b)[1]; |
---|
| 716 | (*r)[2] = (*a)[2] * (*b)[2]; |
---|
| 717 | #else |
---|
| 718 | int i, k; |
---|
| 719 | real tot; |
---|
| 720 | |
---|
[4c07c51] | 721 | SVX_ASSERT((/*const*/ delta*)r != b); |
---|
[5d26a94] | 722 | check_var(a); |
---|
[cb3d1e2] | 723 | check_d(b); |
---|
[a862ee8] | 724 | |
---|
[a420b49] | 725 | for (i = 0; i < 3; i++) { |
---|
[d1b1380] | 726 | tot = 0; |
---|
[a420b49] | 727 | for (k = 0; k < 3; k++) tot += (*a)[i][k] * (*b)[k]; |
---|
[d1b1380] | 728 | (*r)[i] = tot; |
---|
| 729 | } |
---|
[5d26a94] | 730 | check_d(r); |
---|
[d1b1380] | 731 | #endif |
---|
| 732 | } |
---|
| 733 | |
---|
[59f2dbb] | 734 | /* r = vb ; r,b delta vectors; a variance matrix */ |
---|
| 735 | void |
---|
| 736 | mulsd(delta *r, /*const*/ svar *v, /*const*/ delta *b) |
---|
| 737 | { |
---|
| 738 | #ifdef NO_COVARIANCES |
---|
| 739 | /* variance-only version */ |
---|
| 740 | (*r)[0] = (*v)[0] * (*b)[0]; |
---|
| 741 | (*r)[1] = (*v)[1] * (*b)[1]; |
---|
| 742 | (*r)[2] = (*v)[2] * (*b)[2]; |
---|
| 743 | #else |
---|
| 744 | int i, j; |
---|
| 745 | real tot; |
---|
| 746 | |
---|
[4c07c51] | 747 | SVX_ASSERT((/*const*/ delta*)r != b); |
---|
[59f2dbb] | 748 | check_svar(v); |
---|
| 749 | check_d(b); |
---|
| 750 | |
---|
| 751 | for (i = 0; i < 3; i++) { |
---|
| 752 | tot = 0; |
---|
| 753 | for (j = 0; j < 3; j++) tot += S(i,j) * (*b)[j]; |
---|
| 754 | (*r)[i] = tot; |
---|
| 755 | } |
---|
| 756 | check_d(r); |
---|
| 757 | #endif |
---|
| 758 | } |
---|
| 759 | |
---|
[a862ee8] | 760 | /* r = ca ; r,a delta vectors; c real scaling factor */ |
---|
| 761 | void |
---|
[eb18f4d] | 762 | muldc(delta *r, /*const*/ delta *a, real c) { |
---|
[cb3d1e2] | 763 | check_d(a); |
---|
[a862ee8] | 764 | (*r)[0] = (*a)[0] * c; |
---|
| 765 | (*r)[1] = (*a)[1] * c; |
---|
| 766 | (*r)[2] = (*a)[2] * c; |
---|
[5d26a94] | 767 | check_d(r); |
---|
[a862ee8] | 768 | } |
---|
| 769 | |
---|
| 770 | /* r = ca ; r,a variance matrices; c real scaling factor */ |
---|
| 771 | void |
---|
[407084d] | 772 | mulvc(var *r, /*const*/ var *a, real c) |
---|
[a862ee8] | 773 | { |
---|
| 774 | #ifdef NO_COVARIANCES |
---|
| 775 | /* variance-only version */ |
---|
| 776 | (*r)[0] = (*a)[0] * c; |
---|
| 777 | (*r)[1] = (*a)[1] * c; |
---|
| 778 | (*r)[2] = (*a)[2] * c; |
---|
| 779 | #else |
---|
| 780 | int i, j; |
---|
| 781 | |
---|
[5d26a94] | 782 | check_var(a); |
---|
[a862ee8] | 783 | for (i = 0; i < 3; i++) { |
---|
| 784 | for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] * c; |
---|
| 785 | } |
---|
[5d26a94] | 786 | check_var(r); |
---|
[a862ee8] | 787 | #endif |
---|
| 788 | } |
---|
| 789 | |
---|
[59f2dbb] | 790 | /* r = ca ; r,a variance matrices; c real scaling factor */ |
---|
| 791 | void |
---|
| 792 | mulsc(svar *r, /*const*/ svar *a, real c) |
---|
| 793 | { |
---|
| 794 | #ifdef NO_COVARIANCES |
---|
| 795 | /* variance-only version */ |
---|
| 796 | (*r)[0] = (*a)[0] * c; |
---|
| 797 | (*r)[1] = (*a)[1] * c; |
---|
| 798 | (*r)[2] = (*a)[2] * c; |
---|
| 799 | #else |
---|
| 800 | int i; |
---|
| 801 | |
---|
| 802 | check_svar(a); |
---|
| 803 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] * c; |
---|
| 804 | check_svar(r); |
---|
| 805 | #endif |
---|
| 806 | } |
---|
| 807 | |
---|
[d1b1380] | 808 | /* r = a + b ; r,a,b delta vectors */ |
---|
[a420b49] | 809 | void |
---|
[eb18f4d] | 810 | adddd(delta *r, /*const*/ delta *a, /*const*/ delta *b) |
---|
[a420b49] | 811 | { |
---|
[5d26a94] | 812 | check_d(a); |
---|
| 813 | check_d(b); |
---|
[d1b1380] | 814 | (*r)[0] = (*a)[0] + (*b)[0]; |
---|
| 815 | (*r)[1] = (*a)[1] + (*b)[1]; |
---|
| 816 | (*r)[2] = (*a)[2] + (*b)[2]; |
---|
[5d26a94] | 817 | check_d(r); |
---|
[d1b1380] | 818 | } |
---|
| 819 | |
---|
| 820 | /* r = a - b ; r,a,b delta vectors */ |
---|
[a420b49] | 821 | void |
---|
[eb18f4d] | 822 | subdd(delta *r, /*const*/ delta *a, /*const*/ delta *b) { |
---|
[5d26a94] | 823 | check_d(a); |
---|
| 824 | check_d(b); |
---|
[d1b1380] | 825 | (*r)[0] = (*a)[0] - (*b)[0]; |
---|
| 826 | (*r)[1] = (*a)[1] - (*b)[1]; |
---|
| 827 | (*r)[2] = (*a)[2] - (*b)[2]; |
---|
[5d26a94] | 828 | check_d(r); |
---|
[d1b1380] | 829 | } |
---|
| 830 | |
---|
| 831 | /* r = a + b ; r,a,b variance matrices */ |
---|
[a420b49] | 832 | void |
---|
[407084d] | 833 | addvv(var *r, /*const*/ var *a, /*const*/ var *b) |
---|
[a420b49] | 834 | { |
---|
[d1b1380] | 835 | #ifdef NO_COVARIANCES |
---|
| 836 | /* variance-only version */ |
---|
| 837 | (*r)[0] = (*a)[0] + (*b)[0]; |
---|
| 838 | (*r)[1] = (*a)[1] + (*b)[1]; |
---|
| 839 | (*r)[2] = (*a)[2] + (*b)[2]; |
---|
| 840 | #else |
---|
[a420b49] | 841 | int i, j; |
---|
[7a05e07] | 842 | |
---|
[5d26a94] | 843 | check_var(a); |
---|
| 844 | check_var(b); |
---|
[a420b49] | 845 | for (i = 0; i < 3; i++) { |
---|
| 846 | for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] + (*b)[i][j]; |
---|
| 847 | } |
---|
[5d26a94] | 848 | check_var(r); |
---|
[d1b1380] | 849 | #endif |
---|
| 850 | } |
---|
| 851 | |
---|
[59f2dbb] | 852 | /* r = a + b ; r,a,b variance matrices */ |
---|
| 853 | void |
---|
| 854 | addss(svar *r, /*const*/ svar *a, /*const*/ svar *b) |
---|
| 855 | { |
---|
| 856 | #ifdef NO_COVARIANCES |
---|
| 857 | /* variance-only version */ |
---|
| 858 | (*r)[0] = (*a)[0] + (*b)[0]; |
---|
| 859 | (*r)[1] = (*a)[1] + (*b)[1]; |
---|
| 860 | (*r)[2] = (*a)[2] + (*b)[2]; |
---|
| 861 | #else |
---|
| 862 | int i; |
---|
| 863 | |
---|
| 864 | check_svar(a); |
---|
| 865 | check_svar(b); |
---|
| 866 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] + (*b)[i]; |
---|
| 867 | check_svar(r); |
---|
| 868 | #endif |
---|
| 869 | } |
---|
| 870 | |
---|
[7a05e07] | 871 | /* r = a - b ; r,a,b variance matrices */ |
---|
[a420b49] | 872 | void |
---|
[407084d] | 873 | subvv(var *r, /*const*/ var *a, /*const*/ var *b) |
---|
[a420b49] | 874 | { |
---|
[7a05e07] | 875 | #ifdef NO_COVARIANCES |
---|
| 876 | /* variance-only version */ |
---|
| 877 | (*r)[0] = (*a)[0] - (*b)[0]; |
---|
| 878 | (*r)[1] = (*a)[1] - (*b)[1]; |
---|
| 879 | (*r)[2] = (*a)[2] - (*b)[2]; |
---|
| 880 | #else |
---|
[a420b49] | 881 | int i, j; |
---|
[7a05e07] | 882 | |
---|
[5d26a94] | 883 | check_var(a); |
---|
| 884 | check_var(b); |
---|
[a420b49] | 885 | for (i = 0; i < 3; i++) { |
---|
| 886 | for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] - (*b)[i][j]; |
---|
| 887 | } |
---|
[5d26a94] | 888 | check_var(r); |
---|
[7a05e07] | 889 | #endif |
---|
| 890 | } |
---|
| 891 | |
---|
[59f2dbb] | 892 | /* r = a - b ; r,a,b variance matrices */ |
---|
| 893 | void |
---|
| 894 | subss(svar *r, /*const*/ svar *a, /*const*/ svar *b) |
---|
| 895 | { |
---|
| 896 | #ifdef NO_COVARIANCES |
---|
| 897 | /* variance-only version */ |
---|
| 898 | (*r)[0] = (*a)[0] - (*b)[0]; |
---|
| 899 | (*r)[1] = (*a)[1] - (*b)[1]; |
---|
| 900 | (*r)[2] = (*a)[2] - (*b)[2]; |
---|
| 901 | #else |
---|
| 902 | int i; |
---|
| 903 | |
---|
| 904 | check_svar(a); |
---|
| 905 | check_svar(b); |
---|
| 906 | for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] - (*b)[i]; |
---|
| 907 | check_svar(r); |
---|
| 908 | #endif |
---|
| 909 | } |
---|
| 910 | |
---|
[7a05e07] | 911 | /* inv = v^-1 ; inv,v variance matrices */ |
---|
[f98f8a5] | 912 | #ifdef NO_COVARIANCES |
---|
| 913 | extern int |
---|
[407084d] | 914 | invert_var(var *inv, /*const*/ var *v) |
---|
[f98f8a5] | 915 | { |
---|
| 916 | int i; |
---|
| 917 | for (i = 0; i < 3; i++) { |
---|
[42d23c5] | 918 | if ((*v)[i] == 0.0) return 0; /* matrix is singular */ |
---|
[f98f8a5] | 919 | (*inv)[i] = 1.0 / (*v)[i]; |
---|
| 920 | } |
---|
| 921 | return 1; |
---|
| 922 | } |
---|
| 923 | #else |
---|
[a420b49] | 924 | extern int |
---|
[407084d] | 925 | invert_var(var *inv, /*const*/ var *v) |
---|
[a420b49] | 926 | { |
---|
| 927 | int i, j; |
---|
[d1b1380] | 928 | real det = 0; |
---|
| 929 | |
---|
[4c07c51] | 930 | SVX_ASSERT((/*const*/ var *)inv != v); |
---|
[a862ee8] | 931 | |
---|
| 932 | check_var(v); |
---|
[a420b49] | 933 | for (i = 0; i < 3; i++) { |
---|
[a5da11c] | 934 | det += V(i, 0) * (V((i + 1) % 3, 1) * V((i + 2) % 3, 2) - |
---|
| 935 | V((i + 1) % 3, 2) * V((i + 2) % 3, 1)); |
---|
[d1b1380] | 936 | } |
---|
[7a05e07] | 937 | |
---|
[42d23c5] | 938 | if (det == 0.0) { |
---|
[a862ee8] | 939 | /* printf("det=%.20f\n", det); */ |
---|
[647407d] | 940 | return 0; /* matrix is singular */ |
---|
[7a05e07] | 941 | } |
---|
| 942 | |
---|
[a420b49] | 943 | det = 1 / det; |
---|
[d1b1380] | 944 | |
---|
[7a05e07] | 945 | #define B(I,J) ((*v)[(J)%3][(I)%3]) |
---|
[a420b49] | 946 | for (i = 0; i < 3; i++) { |
---|
| 947 | for (j = 0; j < 3; j++) { |
---|
[421b7d2] | 948 | (*inv)[i][j] = det * (B(i+1,j+1)*B(i+2,j+2) - B(i+2,j+1)*B(i+1,j+2)); |
---|
[d1b1380] | 949 | } |
---|
| 950 | } |
---|
| 951 | #undef B |
---|
[94f6ef3] | 952 | |
---|
[2c887b5] | 953 | #if 0 |
---|
| 954 | /* This test fires very occasionally, and there's not much point in |
---|
| 955 | * it anyhow - the matrix inversion algorithm is simple enough that |
---|
| 956 | * we can be confident it's correctly implemented, so we might as |
---|
| 957 | * well save the cycles and not perform this check. |
---|
| 958 | */ |
---|
[a862ee8] | 959 | { /* check that original * inverse = identity matrix */ |
---|
[7a05e07] | 960 | var p; |
---|
| 961 | real d = 0; |
---|
[a862ee8] | 962 | mulvv(&p, v, inv); |
---|
[a420b49] | 963 | for (i = 0; i < 3; i++) { |
---|
[dac18d8] | 964 | for (j = 0; j < 3; j++) d += fabs(p[i][j] - (real)(i==j)); |
---|
[7a05e07] | 965 | } |
---|
[daf88e1] | 966 | if (d > THRESHOLD) { |
---|
[7a05e07] | 967 | printf("original * inverse=\n"); |
---|
[71cd713] | 968 | print_var(*v); |
---|
[7a05e07] | 969 | printf("*\n"); |
---|
[71cd713] | 970 | print_var(*inv); |
---|
[7a05e07] | 971 | printf("=\n"); |
---|
[71cd713] | 972 | print_var(p); |
---|
[7a05e07] | 973 | BUG("matrix didn't invert"); |
---|
| 974 | } |
---|
[a862ee8] | 975 | check_var(inv); |
---|
[7a05e07] | 976 | } |
---|
[2c887b5] | 977 | #endif |
---|
[7a05e07] | 978 | |
---|
| 979 | return 1; |
---|
[d1b1380] | 980 | } |
---|
| 981 | #endif |
---|
| 982 | |
---|
[59f2dbb] | 983 | /* inv = v^-1 ; inv,v variance matrices */ |
---|
| 984 | #ifndef NO_COVARIANCES |
---|
| 985 | extern int |
---|
[dac18d8] | 986 | invert_svar(svar *inv, /*const*/ svar *v) |
---|
[59f2dbb] | 987 | { |
---|
[dac18d8] | 988 | real det, a, b, c, d, e, f, bcff, efcd, dfbe; |
---|
[59f2dbb] | 989 | |
---|
| 990 | #if 0 |
---|
[4c07c51] | 991 | SVX_ASSERT((/*const*/ var *)inv != v); |
---|
[59f2dbb] | 992 | #endif |
---|
| 993 | |
---|
| 994 | check_svar(v); |
---|
[dac18d8] | 995 | /* a d e |
---|
| 996 | * d b f |
---|
| 997 | * e f c |
---|
| 998 | */ |
---|
| 999 | a = (*v)[0], b = (*v)[1], c = (*v)[2]; |
---|
| 1000 | d = (*v)[3], e = (*v)[4], f = (*v)[5]; |
---|
| 1001 | bcff = b * c - f * f; |
---|
| 1002 | efcd = e * f - c * d; |
---|
| 1003 | dfbe = d * f - b * e; |
---|
| 1004 | det = a * bcff + d * efcd + e * dfbe; |
---|
[59f2dbb] | 1005 | |
---|
[42d23c5] | 1006 | if (det == 0.0) { |
---|
[59f2dbb] | 1007 | /* printf("det=%.20f\n", det); */ |
---|
| 1008 | return 0; /* matrix is singular */ |
---|
| 1009 | } |
---|
| 1010 | |
---|
| 1011 | det = 1 / det; |
---|
| 1012 | |
---|
[dac18d8] | 1013 | (*inv)[0] = det * bcff; |
---|
| 1014 | (*inv)[1] = det * (c * a - e * e); |
---|
| 1015 | (*inv)[2] = det * (a * b - d * d); |
---|
| 1016 | (*inv)[3] = det * efcd; |
---|
| 1017 | (*inv)[4] = det * dfbe; |
---|
| 1018 | (*inv)[5] = det * (e * d - a * f); |
---|
[59f2dbb] | 1019 | |
---|
[38193a5] | 1020 | #if 0 |
---|
| 1021 | /* This test fires very occasionally, and there's not much point in |
---|
| 1022 | * it anyhow - the matrix inversion algorithm is simple enough that |
---|
| 1023 | * we can be confident it's correctly implemented, so we might as |
---|
| 1024 | * well save the cycles and not perform this check. |
---|
| 1025 | */ |
---|
[59f2dbb] | 1026 | { /* check that original * inverse = identity matrix */ |
---|
[b4fe9fb] | 1027 | int i; |
---|
[59f2dbb] | 1028 | var p; |
---|
[dac18d8] | 1029 | real D = 0; |
---|
| 1030 | mulss(&p, v, inv); |
---|
[59f2dbb] | 1031 | for (i = 0; i < 3; i++) { |
---|
[dac18d8] | 1032 | int j; |
---|
| 1033 | for (j = 0; j < 3; j++) D += fabs(p[i][j] - (real)(i==j)); |
---|
[59f2dbb] | 1034 | } |
---|
[dac18d8] | 1035 | if (D > THRESHOLD) { |
---|
[59f2dbb] | 1036 | printf("original * inverse=\n"); |
---|
[dac18d8] | 1037 | print_svar(*v); |
---|
[59f2dbb] | 1038 | printf("*\n"); |
---|
[dac18d8] | 1039 | print_svar(*inv); |
---|
[59f2dbb] | 1040 | printf("=\n"); |
---|
| 1041 | print_var(p); |
---|
| 1042 | BUG("matrix didn't invert"); |
---|
| 1043 | } |
---|
[dac18d8] | 1044 | check_svar(inv); |
---|
[59f2dbb] | 1045 | } |
---|
[38193a5] | 1046 | #endif |
---|
[59f2dbb] | 1047 | return 1; |
---|
| 1048 | } |
---|
| 1049 | #endif |
---|
| 1050 | |
---|
[d1b1380] | 1051 | /* r = (b^-1)a ; r,a delta vectors; b variance matrix */ |
---|
[a420b49] | 1052 | void |
---|
[eb18f4d] | 1053 | divdv(delta *r, /*const*/ delta *a, /*const*/ var *b) |
---|
[a420b49] | 1054 | { |
---|
[d1b1380] | 1055 | #ifdef NO_COVARIANCES |
---|
| 1056 | /* variance-only version */ |
---|
| 1057 | (*r)[0] = (*a)[0] / (*b)[0]; |
---|
| 1058 | (*r)[1] = (*a)[1] / (*b)[1]; |
---|
| 1059 | (*r)[2] = (*a)[2] / (*b)[2]; |
---|
| 1060 | #else |
---|
| 1061 | var b_inv; |
---|
[a420b49] | 1062 | if (!invert_var(&b_inv, b)) { |
---|
[71cd713] | 1063 | print_var(*b); |
---|
[7a05e07] | 1064 | BUG("covariance matrix is singular"); |
---|
| 1065 | } |
---|
[a420b49] | 1066 | mulvd(r, &b_inv, a); |
---|
[d1b1380] | 1067 | #endif |
---|
| 1068 | } |
---|
| 1069 | |
---|
[59f2dbb] | 1070 | /* r = (b^-1)a ; r,a delta vectors; b variance matrix */ |
---|
| 1071 | #ifndef NO_COVARIANCES |
---|
| 1072 | void |
---|
| 1073 | divds(delta *r, /*const*/ delta *a, /*const*/ svar *b) |
---|
| 1074 | { |
---|
| 1075 | svar b_inv; |
---|
[dac18d8] | 1076 | if (!invert_svar(&b_inv, b)) { |
---|
[59f2dbb] | 1077 | print_svar(*b); |
---|
| 1078 | BUG("covariance matrix is singular"); |
---|
| 1079 | } |
---|
| 1080 | mulsd(r, &b_inv, a); |
---|
| 1081 | } |
---|
| 1082 | #endif |
---|
| 1083 | |
---|
[d1b1380] | 1084 | /* f = a(b^-1) ; r,a,b variance matrices */ |
---|
[a420b49] | 1085 | void |
---|
[407084d] | 1086 | divvv(var *r, /*const*/ var *a, /*const*/ var *b) |
---|
[a420b49] | 1087 | { |
---|
[d1b1380] | 1088 | #ifdef NO_COVARIANCES |
---|
| 1089 | /* variance-only version */ |
---|
| 1090 | (*r)[0] = (*a)[0] / (*b)[0]; |
---|
| 1091 | (*r)[1] = (*a)[1] / (*b)[1]; |
---|
| 1092 | (*r)[2] = (*a)[2] / (*b)[2]; |
---|
| 1093 | #else |
---|
| 1094 | var b_inv; |
---|
[a862ee8] | 1095 | check_var(a); |
---|
| 1096 | check_var(b); |
---|
[a420b49] | 1097 | if (!invert_var(&b_inv, b)) { |
---|
[71cd713] | 1098 | print_var(*b); |
---|
[7a05e07] | 1099 | BUG("covariance matrix is singular"); |
---|
| 1100 | } |
---|
[a420b49] | 1101 | mulvv(r, a, &b_inv); |
---|
[a862ee8] | 1102 | check_var(r); |
---|
[d1b1380] | 1103 | #endif |
---|
| 1104 | } |
---|
| 1105 | |
---|
[a420b49] | 1106 | bool |
---|
[dac18d8] | 1107 | fZeros(/*const*/ svar *v) { |
---|
[d1b1380] | 1108 | #ifdef NO_COVARIANCES |
---|
| 1109 | /* variance-only version */ |
---|
[a420b49] | 1110 | return ((*v)[0] == 0.0 && (*v)[1] == 0.0 && (*v)[2] == 0.0); |
---|
[d1b1380] | 1111 | #else |
---|
[59f2dbb] | 1112 | int i; |
---|
| 1113 | |
---|
| 1114 | check_svar(v); |
---|
| 1115 | for (i = 0; i < 6; i++) if ((*v)[i] != 0.0) return fFalse; |
---|
| 1116 | |
---|
| 1117 | return fTrue; |
---|
| 1118 | #endif |
---|
[dac18d8] | 1119 | } |
---|