2
2
* Miscellaneous primitive network routines for Survex
3
* Copyright (C) 1992-2002 Olly Betts
3
* Copyright (C) 1992-2003,2006 Olly Betts
5
5
* This program is free software; you can redistribute it and/or modify
6
6
* it under the terms of the GNU General Public License as published by
15
15
* You should have received a copy of the GNU General Public License
16
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
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
20
#ifdef HAVE_CONFIG_H
33
33
#include "datain.h" /* for compile_error */
34
34
#include "validate.h" /* for compile_error */
37
#include <stddef.h> /* for offsetof */
40
36
#define THRESHOLD (REAL_EPSILON * 1000) /* 100 was too small */
42
38
node *stn_iter = NULL; /* for FOR_EACH_STN */
40
static char freeleg(node **stnptr);
44
42
#ifdef NO_COVARIANCES
45
43
static void check_var(/*const*/ var *v) {
597
589
/* r = ab ; r,a,b are variance matrices */
599
mulvv(var *r, /*const*/ var *a, /*const*/ var *b)
601
#ifdef NO_COVARIANCES
602
/* variance-only version */
603
(*r)[0] = (*a)[0] * (*b)[0];
604
(*r)[1] = (*a)[1] * (*b)[1];
605
(*r)[2] = (*a)[2] * (*b)[2];
610
SVX_ASSERT((/*const*/ var *)r != a);
611
SVX_ASSERT((/*const*/ var *)r != b);
616
for (i = 0; i < 3; i++) {
617
for (j = 0; j < 3; j++) {
619
for (k = 0; k < 3; k++) {
620
tot += (*a)[i][k] * (*b)[k][j];
629
/* r = ab ; r,a,b are variance matrices */
631
591
mulss(var *r, /*const*/ svar *a, /*const*/ svar *b)
633
593
#ifdef NO_COVARIANCES
623
#ifndef NO_COVARIANCES
663
624
/* r = ab ; r,a,b are variance matrices */
665
626
smulvs(svar *r, /*const*/ var *a, /*const*/ svar *b)
667
#ifdef NO_COVARIANCES
668
/* variance-only version */
669
(*r)[0] = (*a)[0] * (*b)[0];
670
(*r)[1] = (*a)[1] * (*b)[1];
671
(*r)[2] = (*a)[2] * (*b)[2];
704
/* r = ab ; r,b delta vectors; a variance matrix */
706
mulvd(delta *r, /*const*/ var *a, /*const*/ delta *b)
708
#ifdef NO_COVARIANCES
709
/* variance-only version */
710
(*r)[0] = (*a)[0] * (*b)[0];
711
(*r)[1] = (*a)[1] * (*b)[1];
712
(*r)[2] = (*a)[2] * (*b)[2];
717
SVX_ASSERT((/*const*/ delta*)r != b);
721
for (i = 0; i < 3; i++) {
723
for (k = 0; k < 3; k++) tot += (*a)[i][k] * (*b)[k];
730
659
/* r = vb ; r,b delta vectors; a variance matrix */
756
/* r = ca ; r,a delta vectors; c real scaling factor */
758
muldc(delta *r, /*const*/ delta *a, real c) {
760
(*r)[0] = (*a)[0] * c;
761
(*r)[1] = (*a)[1] * c;
762
(*r)[2] = (*a)[2] * c;
766
/* r = ca ; r,a variance matrices; c real scaling factor */
768
mulvc(var *r, /*const*/ var *a, real c)
770
#ifdef NO_COVARIANCES
771
/* variance-only version */
772
(*r)[0] = (*a)[0] * c;
773
(*r)[1] = (*a)[1] * c;
774
(*r)[2] = (*a)[2] * c;
779
for (i = 0; i < 3; i++) {
780
for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] * c;
786
685
/* r = ca ; r,a variance matrices; c real scaling factor */
788
687
mulsc(svar *r, /*const*/ svar *a, real c)
827
726
/* r = a + b ; r,a,b variance matrices */
829
addvv(var *r, /*const*/ var *a, /*const*/ var *b)
831
#ifdef NO_COVARIANCES
832
/* variance-only version */
833
(*r)[0] = (*a)[0] + (*b)[0];
834
(*r)[1] = (*a)[1] + (*b)[1];
835
(*r)[2] = (*a)[2] + (*b)[2];
841
for (i = 0; i < 3; i++) {
842
for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] + (*b)[i][j];
848
/* r = a + b ; r,a,b variance matrices */
850
728
addss(svar *r, /*const*/ svar *a, /*const*/ svar *b)
852
730
#ifdef NO_COVARIANCES
867
745
/* r = a - b ; r,a,b variance matrices */
869
subvv(var *r, /*const*/ var *a, /*const*/ var *b)
871
#ifdef NO_COVARIANCES
872
/* variance-only version */
873
(*r)[0] = (*a)[0] - (*b)[0];
874
(*r)[1] = (*a)[1] - (*b)[1];
875
(*r)[2] = (*a)[2] - (*b)[2];
881
for (i = 0; i < 3; i++) {
882
for (j = 0; j < 3; j++) (*r)[i][j] = (*a)[i][j] - (*b)[i][j];
888
/* r = a - b ; r,a,b variance matrices */
890
747
subss(svar *r, /*const*/ svar *a, /*const*/ svar *b)
892
749
#ifdef NO_COVARIANCES
907
764
/* inv = v^-1 ; inv,v variance matrices */
908
#ifdef NO_COVARIANCES
910
invert_var(var *inv, /*const*/ var *v)
766
invert_svar(svar *inv, /*const*/ svar *v)
768
#ifdef NO_COVARIANCES
913
770
for (i = 0; i < 3; i++) {
914
771
if ((*v)[i] == 0.0) return 0; /* matrix is singular */
915
772
(*inv)[i] = 1.0 / (*v)[i];
921
invert_var(var *inv, /*const*/ var *v)
926
SVX_ASSERT((/*const*/ var *)inv != v);
929
for (i = 0; i < 3; i++) {
930
det += V(i, 0) * (V((i + 1) % 3, 1) * V((i + 2) % 3, 2) -
931
V((i + 1) % 3, 2) * V((i + 2) % 3, 1));
935
/* printf("det=%.20f\n", det); */
936
return 0; /* matrix is singular */
941
#define B(I,J) ((*v)[(J)%3][(I)%3])
942
for (i = 0; i < 3; i++) {
943
for (j = 0; j < 3; j++) {
944
(*inv)[i][j] = det * (B(i+1,j+1)*B(i+2,j+2) - B(i+2,j+1)*B(i+1,j+2));
950
/* This test fires very occasionally, and there's not much point in
951
* it anyhow - the matrix inversion algorithm is simple enough that
952
* we can be confident it's correctly implemented, so we might as
953
* well save the cycles and not perform this check.
955
{ /* check that original * inverse = identity matrix */
959
for (i = 0; i < 3; i++) {
960
for (j = 0; j < 3; j++) d += fabs(p[i][j] - (real)(i==j));
963
printf("original * inverse=\n");
969
BUG("matrix didn't invert");
979
/* inv = v^-1 ; inv,v variance matrices */
980
#ifndef NO_COVARIANCES
982
invert_svar(svar *inv, /*const*/ svar *v)
984
775
real det, a, b, c, d, e, f, bcff, efcd, dfbe;
1040
831
check_svar(inv);
1047
/* r = (b^-1)a ; r,a delta vectors; b variance matrix */
1049
divdv(delta *r, /*const*/ delta *a, /*const*/ var *b)
1051
#ifdef NO_COVARIANCES
1052
/* variance-only version */
1053
(*r)[0] = (*a)[0] / (*b)[0];
1054
(*r)[1] = (*a)[1] / (*b)[1];
1055
(*r)[2] = (*a)[2] / (*b)[2];
1058
if (!invert_var(&b_inv, b)) {
1060
BUG("covariance matrix is singular");
1062
mulvd(r, &b_inv, a);
1066
838
/* r = (b^-1)a ; r,a delta vectors; b variance matrix */
1067
839
#ifndef NO_COVARIANCES
1069
841
divds(delta *r, /*const*/ delta *a, /*const*/ svar *b)
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];
1072
850
if (!invert_svar(&b_inv, b)) {
1074
852
BUG("covariance matrix is singular");
1076
854
mulsd(r, &b_inv, a);
1080
/* f = a(b^-1) ; r,a,b variance matrices */
1082
divvv(var *r, /*const*/ var *a, /*const*/ var *b)
1084
#ifdef NO_COVARIANCES
1085
/* variance-only version */
1086
(*r)[0] = (*a)[0] / (*b)[0];
1087
(*r)[1] = (*a)[1] / (*b)[1];
1088
(*r)[2] = (*a)[2] / (*b)[2];
1093
if (!invert_var(&b_inv, b)) {
1095
BUG("covariance matrix is singular");
1097
mulvv(r, a, &b_inv);
1103
860
fZeros(/*const*/ svar *v) {