source: git/src/netbits.c @ ecbc6c18

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since ecbc6c18 was ecbc6c18, checked in by Olly Betts <olly@…>, 15 years ago

src/: Update FSF address in licence notices.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@3417 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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