# 2.1.18.1 ocmath_2d_kriging_grid

## Description

This function perform random matrix conversion using Kriging algorithm.

## Syntax

int ocmath_2d_kriging_grid( int npts, double* x, double* y, double* z, int noctMin, double radius, double dSmooth, int nXgrid, double* Xgrid, int nYgrid, double* Ygrid, double* Zvalue, int noctMax = -1 );

## Parameters

npts
[Input]the number of given points
x
[Input]the x position and value of each given point
y
[Input]the y position and value of each given point
z
[Input]the z position and value of each given point
noctMin
[Input]the minimum points in each quarter, if the number of points
in any quarter is less than noctMin, the search radius will be enlarged.
[Input]the search radius in the unit of 2 * Mean-distance-of-any-two-given-points.
It means that, the z-value of the unknown points will be evaluated using about
(2 * radius * 2)^2 points.
dSmooth
[Input]the parameter in the power semivariogram.
nXgrid
[Input]the number of grids in the x direction.
Xgrid
[Input]the x grid coordinates
nYgrid
[Input]the number of grids in the y direction.
Ygrid
[Input]the y grid coordinates
Zvalue
[Output]the evaluated matrix in row order.
noctMax
[Input]the maximun points in each quarter, if the number of points in any quarter is larger than noctMax, the search will be stopped.

## Examples

EX1

void ocmath_2d_kriging_grid_ex1()
{
int i, j, m, n, nx, ny;
double xhi, xlo, yhi, ylo;

nx = 20;
ny = 20;
xlo = 0.0;
ylo = 0.0;
xhi = 1.0;
yhi = 1.0;

double x[400], y[400], f[400];

m = 0;
for (j=0; j<ny; ++j)
{
for (i=0; i<nx; ++i)
{
x[m] = (1.0 * (nx-i-1) / (nx-1)) * xlo + (1.0*i / (nx-1)) * xhi;
y[m] = (1.0 * (ny-j-1) / (ny-1)) * ylo +    (1.0* j / (ny-1)) * yhi;
f[m]= x[m]*x[m]+y[m]*y[m]+x[m]*y[m];
++m;
}
}

double px[100], py[100], pf[10000];

n=0;
nx = 100;
ny = 100;
for (j=0; j<ny; ++j)
py[j] = (1.0 * (ny-j-1) / (ny-1)) * ylo +    (1.0* j / (ny-1)) * yhi;

for (i=0; i<nx; ++i)
px[i] = (1.0 * (nx-i-1) / (nx-1)) * xlo + (1.0*i / (nx-1)) * xhi +0.0005;

int nret;
nret = ocmath_2d_kriging_grid(m, x, y, f, 3, 2, 1.8, nx, px, ny, py, pf);
printf("nret = %d\n",nret);
}

EX2

//assume there are x, y, z data in the first three columns of active worksheet
//this example will create a new matrix storing gridding data using krigging algorithm
#include <wks2mat.h>
#include <wksheet.h>
void ocmath_2d_kriging_grid_ex2()
{
Worksheet    wks = Project.ActiveLayer();
if(!wks)
{
out_str("found no active worksheet");
return;
}
Dataset    dsX(wks, 0);
Dataset    dsY(wks, 1);
Dataset dsZ(wks, 2);
int npts = dsX.GetSize();
vector vX = dsX;
vector vY = dsY;
vector vZ = dsZ;

//set parameters
int noctMin = 3;//set the minimum points in each quarter
double dSmooth = 1.8;//set smoothing parameter

//set X and Y of the gridding
double dXMin, dXMax, dYMin, dYMax;
vX.GetMinMax(dXMin, dXMax);
vector vXgrid, vYgrid;
vXgrid.Data(dXMin, dXMax, (dXMax - dXMin)/99);
vY.GetMinMax(dYMin, dYMax);
vYgrid.Data(dYMin, dYMax, (dYMax - dYMin)/199);
int nXgrid = vXgrid.GetSize();
int nYgrid = vYgrid.GetSize();

//perform random matrix conversion using Kriging algorithm
matrix    mZ(nXgrid, nYgrid);
int iRet = ocmath_2d_kriging_grid(npts, vX, vY, vZ, noctMin, radius, dSmooth, nXgrid, vXgrid, nYgrid, vYgrid, mZ);
if(iRet)
{
out_str("error code is " + iRet);
return;
}

//create Matrix storing the result
MatrixLayer    mResultLayer;
mResultLayer.Create();
Matrix    matResult(mResultLayer);
matResult = mZ;
MatrixObject mo = mResultLayer.MatrixObjects(0);
mo.SetXY(dXMin, dYMin, dXMax, dYMax);//set X and Y range of Matrix
}