source: git/src/netbits.c @ 0c323ec

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 0c323ec was 736f7df, checked in by Olly Betts <olly@…>, 10 years ago

lib/extract-msgs.pl,lib/survex.pot,src/: Insert "TRANSLATORS"
comments into source code.

  • Property mode set to 100644
File size: 21.3 KB
Line 
1/* netbits.c
2 * Miscellaneous primitive network routines for Survex
3 * Copyright (C) 1992-2003,2006,2011,2013,2014 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#if 0
25# define DEBUG_INVALID 1
26#endif
27
28#include "debug.h"
29#include "cavern.h"
30#include "filename.h"
31#include "message.h"
32#include "netbits.h"
33#include "datain.h" /* for compile_error */
34#include "validate.h" /* for compile_error */
35
36#define THRESHOLD (REAL_EPSILON * 1000) /* 100 was too small */
37
38node *stn_iter = NULL; /* for FOR_EACH_STN */
39
40static char freeleg(node **stnptr);
41
42#ifdef NO_COVARIANCES
43static void check_var(/*const*/ var *v) {
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]);
50      if (strstr(buf, "NaN") || strstr(buf, "nan"))
51         printf("*** NaN!!!\n"), bad = 1;
52   }
53   if (bad) print_var(v);
54   return;
55}
56#else
57#define V(A,B) ((*v)[A][B])
58static void check_var(/*const*/ var *v) {
59   int bad = 0;
60   int ok = 0;
61   int i, j;
62#if DEBUG_INVALID
63   real det = 0.0;
64#endif
65
66   for (i = 0; i < 3; i++) {
67      for (j = 0; j < 3; j++) {
68         char buf[32];
69         sprintf(buf, "%6.3f", V(i, j));
70         if (strstr(buf, "NaN") || strstr(buf, "nan"))
71            printf("*** NaN!!!\n"), bad = 1, ok = 1;
72         if (V(i, j) != 0.0) ok = 1;
73      }
74   }
75   if (!ok) return; /* ignore all-zero matrices */
76
77#if DEBUG_INVALID
78   for (i = 0; i < 3; i++) {
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));
81   }
82
83   if (fabs(det) < THRESHOLD)
84      printf("*** Singular!!!\n"), bad = 1;
85#endif
86
87#if 0
88   /* don't check this - it isn't always the case! */
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)
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}
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;
109   int i;
110#if DEBUG_INVALID
111   real det = 0.0;
112#endif
113
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;
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
133#if 0
134   /* don't check this - it isn't always the case! */
135   if ((*v)[0] <= 0.0 || (*v)[1] <= 0.0 || (*v)[2] <= 0.0)
136      printf("*** Not positive definite (diag <= 0)!!!\n"), bad = 1;
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])
140      printf("*** Not positive definite (off diag^2 >= diag product)!!!\n"), bad = 1;
141#endif
142   if (bad) print_svar(*v);
143}
144#endif
145
146static void check_d(/*const*/ delta *d) {
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]);
153      if (strstr(buf, "NaN") || strstr(buf, "nan"))
154         printf("*** NaN!!!\n"), bad = 1;
155   }
156
157   if (bad) printf("(%4.2f,%4.2f,%4.2f)\n", (*d)[0], (*d)[1], (*d)[2]);
158}
159
160/* insert at head of double-linked list */
161void
162add_stn_to_list(node **list, node *stn) {
163   SVX_ASSERT(list);
164   SVX_ASSERT(stn);
165   SVX_ASSERT(stn_iter != stn); /* if it does, we're still on a list... */
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
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) {
180   SVX_ASSERT(list);
181   SVX_ASSERT(stn);
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
187#if DEBUG_INVALID
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) {
193           SVX_ASSERT(stn_to_remove_is_in_list);
194           stn_to_remove_is_in_list = stn_to_remove_is_in_list->next;
195        }
196     }
197#endif
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;
206      if (stn->next) stn->next->prev = stn->prev;
207   }
208}
209
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 */
213linkfor *
214copy_link(linkfor *leg)
215{
216   linkfor *legOut;
217   int d;
218   legOut = osnew(linkfor);
219   if (data_here(leg)) {
220      for (d = 2; d >= 0; d--) legOut->d[d] = leg->d[d];
221   } else {
222      leg = reverse_leg(leg);
223      SVX_ASSERT(data_here(leg));
224      for (d = 2; d >= 0; d--) legOut->d[d] = -leg->d[d];
225   }
226#if 1
227# ifndef NO_COVARIANCES
228   check_svar(&(leg->v));
229     {
230        int i;
231        for (i = 0; i < 6; i++) legOut->v[i] = leg->v[i];
232     }
233# else
234   for (d = 2; d >= 0; d--) legOut->v[d] = leg->v[d];
235# endif
236#else
237   memcpy(legOut->v, leg->v, sizeof(svar));
238#endif
239   legOut->meta = pcs->meta;
240   if (pcs->meta) ++pcs->meta->ref_count;
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 */
247linkfor *
248addto_link(linkfor *leg, const linkfor *leg2)
249{
250   if (data_here(leg2)) {
251      adddd(&leg->d, &leg->d, &((linkfor *)leg2)->d);
252   } else {
253      leg2 = reverse_leg(leg2);
254      SVX_ASSERT(data_here(leg2));
255      subdd(&leg->d, &leg->d, &((linkfor *)leg2)->d);
256   }
257   addss(&leg->v, &leg->v, &((linkfor *)leg2)->v);
258   return leg;
259}
260
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   /* we have been asked to add a leg with the same node at both ends
273    * - this should be trapped by the caller */
274   SVX_ASSERT(fr->name != to->name);
275
276   leg = osnew(linkfor);
277   leg2 = (linkfor*)osnew(linkrev);
278
279   i = freeleg(&fr);
280   j = freeleg(&to);
281
282   leg->l.to = to;
283   leg2->l.to = fr;
284   leg->d[0] = dx;
285   leg->d[1] = dy;
286   leg->d[2] = dz;
287#ifndef NO_COVARIANCES
288   leg->v[0] = vx;
289   leg->v[1] = vy;
290   leg->v[2] = vz;
291   leg->v[3] = cxy;
292   leg->v[4] = czx;
293   leg->v[5] = cyz;
294   check_svar(&(leg->v));
295#else
296   leg->v[0] = vx;
297   leg->v[1] = vy;
298   leg->v[2] = vz;
299#endif
300   leg2->l.reverse = i;
301   leg->l.reverse = j | FLAG_DATAHERE | leg_flags;
302
303   leg->l.flags = pcs->flags | (pcs->style << FLAGS_STYLE_BIT0);
304   leg->meta = pcs->meta;
305   if (pcs->meta) ++pcs->meta->ref_count;
306
307   fr->leg[i] = leg;
308   to->leg[j] = leg2;
309
310   ++fr->name->shape;
311   ++to->name->shape;
312}
313
314/* Add a leg between names *fr_name and *to_name
315 * If either is a three node, then it is split into two
316 * and the data structure adjusted as necessary.
317 */
318void
319addlegbyname(prefix *fr_name, prefix *to_name, bool fToFirst,
320             real dx, real dy, real dz,
321             real vx, real vy, real vz
322#ifndef NO_COVARIANCES
323             , real cyz, real czx, real cxy
324#endif
325             )
326{
327   node *to, *fr;
328   if (to_name == fr_name) {
329      /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
330       * "survey stations".
331       *
332       * %s is replaced by the name of the station. */
333      compile_error(/*Survey leg with same station (“%s”) at both ends - typing error?*/50,
334                    sprint_prefix(to_name));
335      return;
336   }
337   if (fToFirst) {
338      to = StnFromPfx(to_name);
339      fr = StnFromPfx(fr_name);
340   } else {
341      fr = StnFromPfx(fr_name);
342      to = StnFromPfx(to_name);
343   }
344   cLegs++; /* increment count (first as compiler may do tail recursion) */
345   addleg_(fr, to, dx, dy, dz, vx, vy, vz,
346#ifndef NO_COVARIANCES
347           cyz, czx, cxy,
348#endif
349           0);
350}
351
352/* helper function for replace_pfx */
353static void
354replace_pfx_(node *stn, node *from, pos *pos_replace, pos *pos_with)
355{
356   int d;
357   stn->name->pos = pos_with;
358   for (d = 0; d < 3; d++) {
359      linkfor *leg = stn->leg[d];
360      node *to;
361      if (!leg) break;
362      to = leg->l.to;
363      if (to == from) continue;
364
365      if (fZeros(data_here(leg) ? &leg->v : &reverse_leg(leg)->v))
366         replace_pfx_(to, stn, pos_replace, pos_with);
367   }
368}
369
370/* We used to iterate over the whole station list (inefficient) - now we
371 * just look at any neighbouring nodes to see if they are equated */
372static void
373replace_pfx(const prefix *pfx_replace, const prefix *pfx_with)
374{
375   pos *pos_replace;
376   SVX_ASSERT(pfx_replace);
377   SVX_ASSERT(pfx_with);
378   pos_replace = pfx_replace->pos;
379   SVX_ASSERT(pos_replace != pfx_with->pos);
380
381   replace_pfx_(pfx_replace->stn, NULL, pos_replace, pfx_with->pos);
382
383#if DEBUG_INVALID
384   {
385      node *stn;
386      FOR_EACH_STN(stn, stnlist) {
387         SVX_ASSERT(stn->name->pos != pos_replace);
388      }
389   }
390#endif
391
392   /* free the (now-unused) old pos */
393   osfree(pos_replace);
394}
395
396/* Add an equating leg between existing stations *fr and *to (whose names are
397 * name1 and name2).
398 */
399void
400process_equate(prefix *name1, prefix *name2)
401{
402   node *stn1, *stn2;
403   if (name1 == name2) {
404      /* catch something like *equate "fred fred" */
405      /* TRANSLATORS: Here "station" is a survey station, not a train station.
406       */
407      compile_warning(/*Station “%s” equated to itself*/13,
408                      sprint_prefix(name1));
409      return;
410   }
411   stn1 = StnFromPfx(name1);
412   stn2 = StnFromPfx(name2);
413   /* equate nodes if not already equated */
414   if (name1->pos != name2->pos) {
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            /* TRANSLATORS: "equal" as in:
429             *
430             * *fix a 1 2 3
431             * *fix b 1 2 3
432             * *equate a b */
433            compile_warning(/*Equating two equal fixed points: “%s” and “%s”*/53,
434                            s, sprint_prefix(name2));
435            osfree(s);
436         }
437
438         /* name1 is fixed, so replace all refs to name2's pos with name1's */
439         replace_pfx(name2, name1);
440      } else {
441         /* name1 isn't fixed, so replace all refs to its pos with name2's */
442         replace_pfx(name1, name2);
443      }
444
445      /* count equates as legs for now... */
446      cLegs++;
447      addleg_(stn1, stn2,
448              (real)0.0, (real)0.0, (real)0.0,
449              (real)0.0, (real)0.0, (real)0.0,
450#ifndef NO_COVARIANCES
451              (real)0.0, (real)0.0, (real)0.0,
452#endif
453              FLAG_FAKE);
454   }
455}
456
457/* Add a 'fake' leg (not counted) between existing stations *fr and *to
458 * (which *must* be different)
459 * If either node is a three node, then it is split into two
460 * and the data structure adjusted as necessary
461 */
462void
463addfakeleg(node *fr, node *to,
464           real dx, real dy, real dz,
465           real vx, real vy, real vz
466#ifndef NO_COVARIANCES
467           , real cyz, real czx, real cxy
468#endif
469           )
470{
471   addleg_(fr, to, dx, dy, dz, vx, vy, vz,
472#ifndef NO_COVARIANCES
473           cyz, czx, cxy,
474#endif
475           FLAG_FAKE);
476}
477
478static char
479freeleg(node **stnptr)
480{
481   node *stn, *oldstn;
482   linkfor *leg, *leg2;
483#ifndef NO_COVARIANCES
484   int i;
485#endif
486
487   stn = *stnptr;
488
489   if (stn->leg[0] == NULL) return 0; /* leg[0] unused */
490   if (stn->leg[1] == NULL) return 1; /* leg[1] unused */
491   if (stn->leg[2] == NULL) return 2; /* leg[2] unused */
492
493   /* All legs used, so split node in two */
494   oldstn = stn;
495   stn = osnew(node);
496   leg = osnew(linkfor);
497   leg2 = (linkfor*)osnew(linkrev);
498
499   *stnptr = stn;
500
501   add_stn_to_list(&stnlist, stn);
502   stn->name = oldstn->name;
503
504   leg->l.to = stn;
505   leg->d[0] = leg->d[1] = leg->d[2] = (real)0.0;
506
507#ifndef NO_COVARIANCES
508   for (i = 0; i < 6; i++) leg->v[i] = (real)0.0;
509#else
510   leg->v[0] = leg->v[1] = leg->v[2] = (real)0.0;
511#endif
512   leg->l.reverse = 1 | FLAG_DATAHERE | FLAG_FAKE;
513   leg->l.flags = pcs->flags | (pcs->style << FLAGS_STYLE_BIT0);
514
515   leg2->l.to = oldstn;
516   leg2->l.reverse = 0;
517
518   /* NB this preserves pos->stn->leg[0] to point to the "real" fixed point
519    * for stations fixed with error estimates
520    */
521   stn->leg[0] = oldstn->leg[0];
522   /* correct reverse leg */
523   reverse_leg(stn->leg[0])->l.to = stn;
524   stn->leg[1] = leg2;
525
526   oldstn->leg[0] = leg;
527
528   stn->leg[2] = NULL; /* needed as stn->leg[dirn]==NULL indicates unused */
529
530   return(2); /* leg[2] unused */
531}
532
533node *
534StnFromPfx(prefix *name)
535{
536   node *stn;
537   if (name->stn != NULL) return (name->stn);
538   stn = osnew(node);
539   stn->name = name;
540   if (name->pos == NULL) {
541      name->pos = osnew(pos);
542      unfix(stn);
543   }
544   stn->leg[0] = stn->leg[1] = stn->leg[2] = NULL;
545   add_stn_to_list(&stnlist, stn);
546   name->stn = stn;
547   cStns++;
548   return stn;
549}
550
551extern void
552fprint_prefix(FILE *fh, const prefix *ptr)
553{
554   SVX_ASSERT(ptr);
555   if (TSTBIT(ptr->sflags, SFLAGS_ANON)) {
556      /* We release the stations, so ptr->stn is NULL late on, so we can't
557       * use that to print "anonymous station surveyed from somesurvey.12"
558       * here.  FIXME */
559      fputs("anonymous station", fh);
560      /* FIXME: if ident is set, show it? */
561      return;
562   }
563   if (ptr->up != NULL) {
564      fprint_prefix(fh, ptr->up);
565      if (ptr->up->up != NULL) fputc('.', fh);
566      SVX_ASSERT(ptr->ident);
567      fputs(ptr->ident, fh);
568   }
569}
570
571static char *buffer = NULL;
572static OSSIZE_T buffer_len = 256;
573
574static OSSIZE_T
575sprint_prefix_(const prefix *ptr)
576{
577   OSSIZE_T len = 1;
578   if (ptr->up != NULL) {
579      SVX_ASSERT(ptr->ident);
580      len = sprint_prefix_(ptr->up) + strlen(ptr->ident);
581      if (ptr->up->up != NULL) len++;
582      if (len > buffer_len) {
583         buffer = osrealloc(buffer, len);
584         buffer_len = len;
585      }
586      if (ptr->up->up != NULL) strcat(buffer, ".");
587      strcat(buffer, ptr->ident);
588   }
589   return len;
590}
591
592extern char *
593sprint_prefix(const prefix *ptr)
594{
595   SVX_ASSERT(ptr);
596   if (!buffer) buffer = osmalloc(buffer_len);
597   if (TSTBIT(ptr->sflags, SFLAGS_ANON)) {
598      /* We release the stations, so ptr->stn is NULL late on, so we can't
599       * use that to print "anonymous station surveyed from somesurvey.12"
600       * here.  FIXME */
601      sprintf(buffer, "anonymous station");
602      /* FIXME: if ident is set, show it? */
603      return buffer;
604   }
605   *buffer = '\0';
606   sprint_prefix_(ptr);
607   return buffer;
608}
609
610/* r = ab ; r,a,b are variance matrices */
611void
612mulss(var *r, /*const*/ svar *a, /*const*/ svar *b)
613{
614#ifdef NO_COVARIANCES
615   /* variance-only version */
616   (*r)[0] = (*a)[0] * (*b)[0];
617   (*r)[1] = (*a)[1] * (*b)[1];
618   (*r)[2] = (*a)[2] * (*b)[2];
619#else
620   int i, j, k;
621   real tot;
622
623#if 0
624   SVX_ASSERT((/*const*/ var *)r != a);
625   SVX_ASSERT((/*const*/ var *)r != b);
626#endif
627
628   check_svar(a);
629   check_svar(b);
630
631   for (i = 0; i < 3; i++) {
632      for (j = 0; j < 3; j++) {
633         tot = 0;
634         for (k = 0; k < 3; k++) {
635            tot += SN(a,i,k) * SN(b,k,j);
636         }
637         (*r)[i][j] = tot;
638      }
639   }
640   check_var(r);
641#endif
642}
643
644#ifndef NO_COVARIANCES
645/* r = ab ; r,a,b are variance matrices */
646void
647smulvs(svar *r, /*const*/ var *a, /*const*/ svar *b)
648{
649   int i, j, k;
650   real tot;
651
652#if 0
653   SVX_ASSERT((/*const*/ var *)r != a);
654#endif
655   SVX_ASSERT((/*const*/ svar *)r != b);
656
657   check_var(a);
658   check_svar(b);
659
660   (*r)[3]=(*r)[4]=(*r)[5]=-999;
661   for (i = 0; i < 3; i++) {
662      for (j = 0; j < 3; j++) {
663         tot = 0;
664         for (k = 0; k < 3; k++) {
665            tot += (*a)[i][k] * SN(b,k,j);
666         }
667         if (i <= j)
668            SN(r,i,j) = tot;
669         else if (fabs(SN(r,j,i) - tot) > THRESHOLD) {
670            printf("not sym - %d,%d = %f, %d,%d was %f\n",
671                   i,j,tot,j,i,SN(r,j,i));
672            BUG("smulvs didn't produce a sym mx\n");
673         }
674      }
675   }
676   check_svar(r);
677}
678#endif
679
680/* r = vb ; r,b delta vectors; a variance matrix */
681void
682mulsd(delta *r, /*const*/ svar *v, /*const*/ delta *b)
683{
684#ifdef NO_COVARIANCES
685   /* variance-only version */
686   (*r)[0] = (*v)[0] * (*b)[0];
687   (*r)[1] = (*v)[1] * (*b)[1];
688   (*r)[2] = (*v)[2] * (*b)[2];
689#else
690   int i, j;
691   real tot;
692
693   SVX_ASSERT((/*const*/ delta*)r != b);
694   check_svar(v);
695   check_d(b);
696
697   for (i = 0; i < 3; i++) {
698      tot = 0;
699      for (j = 0; j < 3; j++) tot += S(i,j) * (*b)[j];
700      (*r)[i] = tot;
701   }
702   check_d(r);
703#endif
704}
705
706/* r = ca ; r,a variance matrices; c real scaling factor  */
707void
708mulsc(svar *r, /*const*/ svar *a, real c)
709{
710#ifdef NO_COVARIANCES
711   /* variance-only version */
712   (*r)[0] = (*a)[0] * c;
713   (*r)[1] = (*a)[1] * c;
714   (*r)[2] = (*a)[2] * c;
715#else
716   int i;
717
718   check_svar(a);
719   for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] * c;
720   check_svar(r);
721#endif
722}
723
724/* r = a + b ; r,a,b delta vectors */
725void
726adddd(delta *r, /*const*/ delta *a, /*const*/ delta *b)
727{
728   check_d(a);
729   check_d(b);
730   (*r)[0] = (*a)[0] + (*b)[0];
731   (*r)[1] = (*a)[1] + (*b)[1];
732   (*r)[2] = (*a)[2] + (*b)[2];
733   check_d(r);
734}
735
736/* r = a - b ; r,a,b delta vectors */
737void
738subdd(delta *r, /*const*/ delta *a, /*const*/ delta *b) {
739   check_d(a);
740   check_d(b);
741   (*r)[0] = (*a)[0] - (*b)[0];
742   (*r)[1] = (*a)[1] - (*b)[1];
743   (*r)[2] = (*a)[2] - (*b)[2];
744   check_d(r);
745}
746
747/* r = a + b ; r,a,b variance matrices */
748void
749addss(svar *r, /*const*/ svar *a, /*const*/ svar *b)
750{
751#ifdef NO_COVARIANCES
752   /* variance-only version */
753   (*r)[0] = (*a)[0] + (*b)[0];
754   (*r)[1] = (*a)[1] + (*b)[1];
755   (*r)[2] = (*a)[2] + (*b)[2];
756#else
757   int i;
758
759   check_svar(a);
760   check_svar(b);
761   for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] + (*b)[i];
762   check_svar(r);
763#endif
764}
765
766/* r = a - b ; r,a,b variance matrices */
767void
768subss(svar *r, /*const*/ svar *a, /*const*/ svar *b)
769{
770#ifdef NO_COVARIANCES
771   /* variance-only version */
772   (*r)[0] = (*a)[0] - (*b)[0];
773   (*r)[1] = (*a)[1] - (*b)[1];
774   (*r)[2] = (*a)[2] - (*b)[2];
775#else
776   int i;
777
778   check_svar(a);
779   check_svar(b);
780   for (i = 0; i < 6; i++) (*r)[i] = (*a)[i] - (*b)[i];
781   check_svar(r);
782#endif
783}
784
785/* inv = v^-1 ; inv,v variance matrices */
786extern int
787invert_svar(svar *inv, /*const*/ svar *v)
788{
789#ifdef NO_COVARIANCES
790   int i;
791   for (i = 0; i < 3; i++) {
792      if ((*v)[i] == 0.0) return 0; /* matrix is singular */
793      (*inv)[i] = 1.0 / (*v)[i];
794   }
795#else
796   real det, a, b, c, d, e, f, bcff, efcd, dfbe;
797
798#if 0
799   SVX_ASSERT((/*const*/ var *)inv != v);
800#endif
801
802   check_svar(v);
803   /* a d e
804    * d b f
805    * e f c
806    */
807   a = (*v)[0], b = (*v)[1], c = (*v)[2];
808   d = (*v)[3], e = (*v)[4], f = (*v)[5];
809   bcff = b * c - f * f;
810   efcd = e * f - c * d;
811   dfbe = d * f - b * e;
812   det = a * bcff + d * efcd + e * dfbe;
813
814   if (det == 0.0) {
815      /* printf("det=%.20f\n", det); */
816      return 0; /* matrix is singular */
817   }
818
819   det = 1 / det;
820
821   (*inv)[0] = det * bcff;
822   (*inv)[1] = det * (c * a - e * e);
823   (*inv)[2] = det * (a * b - d * d);
824   (*inv)[3] = det * efcd;
825   (*inv)[4] = det * dfbe;
826   (*inv)[5] = det * (e * d - a * f);
827
828#if 0
829   /* This test fires very occasionally, and there's not much point in
830    * it anyhow - the matrix inversion algorithm is simple enough that
831    * we can be confident it's correctly implemented, so we might as
832    * well save the cycles and not perform this check.
833    */
834     { /* check that original * inverse = identity matrix */
835        int i;
836        var p;
837        real D = 0;
838        mulss(&p, v, inv);
839        for (i = 0; i < 3; i++) {
840           int j;
841           for (j = 0; j < 3; j++) D += fabs(p[i][j] - (real)(i==j));
842        }
843        if (D > THRESHOLD) {
844           printf("original * inverse=\n");
845           print_svar(*v);
846           printf("*\n");
847           print_svar(*inv);
848           printf("=\n");
849           print_var(p);
850           BUG("matrix didn't invert");
851        }
852        check_svar(inv);
853     }
854#endif
855#endif
856   return 1;
857}
858
859/* r = (b^-1)a ; r,a delta vectors; b variance matrix */
860#ifndef NO_COVARIANCES
861void
862divds(delta *r, /*const*/ delta *a, /*const*/ svar *b)
863{
864#ifdef NO_COVARIANCES
865   /* variance-only version */
866   (*r)[0] = (*a)[0] / (*b)[0];
867   (*r)[1] = (*a)[1] / (*b)[1];
868   (*r)[2] = (*a)[2] / (*b)[2];
869#else
870   svar b_inv;
871   if (!invert_svar(&b_inv, b)) {
872      print_svar(*b);
873      BUG("covariance matrix is singular");
874   }
875   mulsd(r, &b_inv, a);
876#endif
877}
878#endif
879
880bool
881fZeros(/*const*/ svar *v) {
882#ifdef NO_COVARIANCES
883   /* variance-only version */
884   return ((*v)[0] == 0.0 && (*v)[1] == 0.0 && (*v)[2] == 0.0);
885#else
886   int i;
887
888   check_svar(v);
889   for (i = 0; i < 6; i++) if ((*v)[i] != 0.0) return fFalse;
890
891   return fTrue;
892#endif
893}
Note: See TracBrowser for help on using the repository browser.