//----------------------------------------------------------------------- void Kriging1D::krigVector(double * data, double * trend, int nd, float dz) { // // This class takes the incomplete vector 'data' and fills it using kriging. // int * index = new int[nd]; double range = 500.0; double power = 1.5; int md; locateValidData(data, index, nd, md); if (md < nd) { subtractTrend(data, trend, index, md); double ** K; // Kriging matrix double ** C; // Kriging matrix cholesky decomposed double * k; // Kriging vector allocateSpaceForMatrixEq(K, C, k, md); fillKrigingMatrix(K, index, md, range, power, static_cast<double>(dz)); cholesky(K, C, md); for (int krigK = 0 ; krigK < nd ; krigK++) { if (data[krigK] == RMISSING) { fillKrigingVector(k, index, md, range, power, static_cast<double>(dz), krigK); lib_matrAxeqbR(md, C, k); // solve kriging equation data[krigK] = 0.0f; for (int i = 0 ; i < md ; i++) { data[krigK] += static_cast<float>(k[i]) * data[index[i]]; } } } deAllocateSpaceForMatrixEq(K, C, k, md); addTrend(data, trend, nd); bool debug = false; if (debug) { for (int i = 0 ; i < nd ; i++) { LogKit::LogFormatted(LogKit::Low," i krigedData[i] : %3d %.5f\n",i,data[i]); } } } delete [] index; }
void Kriging2D::krigSurface(Grid2D & trend, const KrigingData2D & krigingData, const CovGrid2D & cov, bool getResiduals) { // // This routine by default returns z(x) = m(x) + k(x)K^{-1}(d - m). If only // residuals are wanted a copy of the input trend // int md = krigingData.getNumberOfData(); const std::vector<int> & indexi = krigingData.getIndexI(); const std::vector<int> & indexj = krigingData.getIndexJ(); std::vector<float> data = krigingData.getData(); // Take an editable copy int nx = static_cast<int>(trend.GetNI()); int ny = static_cast<int>(trend.GetNJ()); if (md > 0 && md < nx*ny) { NRLib::SymmetricMatrix K(md); NRLib::Vector residual(md); NRLib::Vector k(md); NRLib::Vector x(md); subtractTrend(residual, data, trend, indexi, indexj); fillKrigingMatrix(K, cov, indexi, indexj); NRLib::CholeskySolve(K, residual, x); for (int i = 0 ; i < nx ; i++) { for (int j = 0 ; j < ny ; j++) { fillKrigingVector(k, cov, indexi, indexj, i, j); if (getResiduals) { // Only get the residuals trend(i,j) = k * x; } else { trend(i,j) += k * x; } } } } }