/* Authors Martin Schlather, martin.schlather@math.uni-goettingen.de Kriging methods Copyright (C) 2001 -- 2011 Martin Schlather, This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* PRINTING LEVELS =============== error messages: 1 forcation : 2 minor tracing information : 3--5 large debugging information: >10 */ /* calculate the transformation of the points only once and store the result in a register (indexed by ncov) of key, not of s->. Advantages: points have to calculated only once for several tries; if zonal anisotropy then specific methods may be called; */ #include #include #include #include #include #include "RF.h" #include #include #include #include #define KRIGE_TOLERANCE -1e-10 void CalculateVariogram(double *x, int lx, cov_model *cov, double *vario); void matmult(double *A, double *B, double * C, int l, int m, int n); void matmulttransposed(double *A, double *B, double *C, int m, int l, int n); void InvChol(double *C, int dim); // to replace quote(...solve) in kriging.R void simpleKriging(double *tgiven, double *x, double *invcov, int *notna, int *Nx, int *Ngiven, int *Dim, int *Rep, double *Mean, double *Krig) { // kriging variance is not calculated cov_model *cov = STORED_MODEL[MODEL_INTERN]; int d, i, j, r, krigi, v, err = NOERROR, dim = *Dim, ngiven = *Ngiven, rep = *Rep, nx = *Nx, vdim = cov->vdim, vdimng = vdim * ngiven, len_tgiven = dim * ngiven, divachtzig = (nx<79) ? 1 : (nx / 79), divachtzigM1 = divachtzig - 1; double xx[MAXSIMUDIM], dummy, *M = NULL, *pM, *dist = NULL; // Ngiven : length given = length M vector to be calculated // Rep : repetition invcov // Nx : number of points in x, length(x) = nx * dim if ((M = (double*) malloc(sizeof(double) * vdimng * vdim))==NULL) { err = ERRORMEMORYALLOCATION; goto ErrorHandling; } if ((dist = (double*) malloc(sizeof(double) * len_tgiven))==NULL) { err = ERRORMEMORYALLOCATION; goto ErrorHandling; } for (krigi=i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d KRIGE_TOLERANCE); sigma2[i] = 0.0; } for (d=r=0; r 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (lambda!=NULL) free(lambda); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i0 && (krigi % divachtzig ==divachtzigM1)) PRINTF("."); for (d=0, j=i; d KRIGE_TOLERANCE); sigma2[i] = 0.0; } for (d=r=0; r 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (lambda!=NULL) free(lambda); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d 0) PRINTF("\n"); ErrorHandling: if (covf0!=NULL) free(covf0); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i 0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); //Determining x for(d=0, j=i; d KRIGE_TOLERANCE); sigma2[i] = 0.0; } for (d=r=0; r 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (lambda!=NULL) free(lambda); if (dist!=NULL) free(dist); if (fvector!=NULL) free(fvector); if (X1 !=NULL) free(X1); if (lambdak !=NULL) free(lambdak); if (Qmatrix!=NULL) free(Qmatrix); if (Rvector!=NULL) free(Rvector); if (mu!=NULL) free(mu); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for(i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d 0) PRINTF("\n"); ErrorHandling: if (covf0!=NULL) free(covf0); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i 0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); //Determining x for(d=0, j=i; d KRIGE_TOLERANCE); sigma2[i] = 0.0; } for (d=r=0; r 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (lambda!=NULL) free(lambda); if (dist!=NULL) free(dist); if (Fmatrix!=NULL) free(Fmatrix); if (fvector!=NULL) free(fvector); if (X1 !=NULL) free(X1); if (lambdak !=NULL) free(lambdak); if (Qmatrix!=NULL) free(Qmatrix); if (Rvector!=NULL) free(Rvector); if (mu!=NULL) free(mu); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for(i=0; i0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); for (d=0, j=i; d 0) PRINTF("\n"); ErrorHandling: if (covf0!=NULL) free(covf0); if (dist!=NULL) free(dist); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for (i=0; i 0 && (krigi % divachtzig == divachtzigM1)) PRINTF("."); //Determining x for(d=0, j=i; d KRIGE_TOLERANCE); sigma2[i] = 0.0; } for (d=r=0; r 0) PRINTF("\n"); ErrorHandling: if (M!=NULL) free(M); if (lambda!=NULL) free(lambda); if (dist!=NULL) free(dist); if (Fmatrix!=NULL) free(Fmatrix); if (fvector!=NULL) free(fvector); if (X1 !=NULL) free(X1); if (lambdak !=NULL) free(lambdak); if (Qmatrix!=NULL) free(Qmatrix); if (Rvector!=NULL) free(Rvector); if (mu!=NULL) free(mu); if (err != NOERROR) { int nKrig; nKrig = nx * rep; for(i=0; i0; i+=dimP1, ii--) { // filling lower half endfor = i + ii; for (ve = i + 1, ho = i + dim; ve < endfor; ve++, ho += dim) { C[ve] = C[ho]; } } }