//--------------------------------------------------------- int CGW_Multi_Regression_Grid::Get_Variables(int x, int y, CSG_Vector &z, CSG_Vector &w, CSG_Matrix &Y) { TSG_Point Point = m_dimModel.Get_Grid_to_World(x, y); int nPoints = m_Search.is_Okay() ? (int)m_Search.Select_Nearest_Points(Point.x, Point.y, m_nPoints_Max, m_Radius, m_Direction) : m_Points.Get_Count(); z.Create(nPoints); w.Create(nPoints); Y.Create(1 + m_nPredictors, nPoints); for(int iPoint=0; iPoint<nPoints; iPoint++) { double ix, iy, iz; CSG_Shape *pPoint = m_Search.is_Okay() && m_Search.Get_Selected_Point(iPoint, ix, iy, iz) ? m_Points.Get_Shape((int)iz) : m_Points.Get_Shape(iPoint); z[iPoint] = pPoint->asDouble(0); w[iPoint] = m_Weighting.Get_Weight(SG_Get_Distance(Point, pPoint->Get_Point(0))); Y[iPoint][0] = 1.0; for(int iPredictor=1; iPredictor<=m_nPredictors; iPredictor++) { Y[iPoint][iPredictor] = pPoint->asDouble(iPredictor); } } return( nPoints ); }
//--------------------------------------------------------- bool CKriging_Simple::Get_Weights(const CSG_Points_Z &Points, CSG_Matrix &W) { int n = Points.Get_Count(); if( n > 0 ) { int n = Points.Get_Count(); W.Create(n, n); for(int i=0; i<n; i++) { W[i][i] = 0.0; // diagonal... for(int j=i+1; j<n; j++) { W[i][j] = W[j][i] = Get_Weight(Points.Get_X(i), Points.Get_Y(i), Points.Get_X(j), Points.Get_Y(j)); } } return( W.Set_Inverse(!m_Search.Do_Use_All(), n) ); } return( false ); }
//--------------------------------------------------------- int CKriging_Universal::Get_Weights(const TSG_Point &p, CSG_Matrix &W, CSG_Points_Z &Points) { //----------------------------------------------------- int n = m_Search.Get_Nearest_Points(Points, p, m_nPoints_Max, m_Radius, m_Direction); if( n >= m_nPoints_Min ) { int i, j, k; int nCoords = m_bCoords ? 2 : 0; int nGrids = m_pGrids->Get_Count(); W.Create(n + 1 + nGrids + nCoords, n + 1 + nGrids + nCoords); //------------------------------------------------- for(i=0; i<n; i++) { W[i][i] = 0.0; // diagonal... W[i][n] = W[n][i] = 1.0; // edge... for(j=i+1; j<n; j++) { W[i][j] = W[j][i] = Get_Weight(Points[i], Points[j]); } for(k=0, j=n+1; k<nGrids; k++, j++) { W[i][j] = W[j][i] = m_pGrids->asGrid(k)->Get_Value(Points[i].x, Points[i].y, m_Interpolation); } for(k=0, j=n+nGrids+1; k<nCoords; k++, j++) { W[i][j] = W[j][i] = k == 0 ? Points[i].x : Points[i].y; } } for(i=n; i<=n+nGrids+nCoords; i++) { for(j=n; j<=n+nGrids+nCoords; j++) { W[i][j] = 0.0; } } if( W.Set_Inverse(true, n + 1 + nGrids + nCoords) ) { return( n ); } } return( 0 ); }
//--------------------------------------------------------- int CGWR_Grid_Downscaling::Get_Variables(int x, int y, CSG_Vector &z, CSG_Vector &w, CSG_Matrix &Y) { int n = 0; z.Create(m_Search.Get_Count()); w.Create(m_Search.Get_Count()); Y.Create(1 + m_nPredictors, m_Search.Get_Count()); //----------------------------------------------------- for(int i=0, ix, iy; i<m_Search.Get_Count(); i++) { double id, iw; if( m_Search.Get_Values(i, ix = x, iy = y, id, iw, true) && m_pDependent->is_InGrid(ix, iy) ) { z[n] = m_pDependent->asDouble(ix, iy); w[n] = iw; Y[n][0] = 1.0; for(int j=0; j<m_nPredictors && iw>0.0; j++) { if( !m_pPredictors[j]->is_NoData(ix, iy) ) { Y[n][j + 1] = m_pPredictors[j]->asDouble(ix, iy); } else { iw = 0.0; } } if( iw > 0.0 ) { n++; } } } //----------------------------------------------------- z.Set_Rows(n); w.Set_Rows(n); Y.Set_Rows(n); return( n ); }
//--------------------------------------------------------- bool CSG_Matrix::Set_Transpose(void) { CSG_Matrix m; if( m.Create(*this) && Create(m_ny, m_nx) ) { for(int y=0; y<m_ny; y++) { for(int x=0; x<m_nx; x++) { m_z[y][x] = m.m_z[x][y]; } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CChange_Detection::Get_Changes(CSG_Table &Initial, CSG_Table &Final, CSG_Table *pChanges, CSG_Matrix &Identity) { int iInitial, iFinal; //----------------------------------------------------- Identity.Create(Final.Get_Count() + 1, Initial.Get_Count() + 1); for(iInitial=0; iInitial<Initial.Get_Count(); iInitial++) { CSG_String s = Initial[iInitial].asString(CLASS_NAM); for(iFinal=0; iFinal<Final.Get_Count(); iFinal++) { Identity[iInitial][iFinal] = s.Cmp(Final[iFinal].asString(CLASS_NAM)) ? 0 : 1; } } Identity[Initial.Get_Count()][Final.Get_Count()] = 1; // unclassified //----------------------------------------------------- pChanges->Destroy(); pChanges->Add_Field(_TL("Name"), SG_DATATYPE_String); for(iFinal=0; iFinal<Final.Get_Count(); iFinal++) { pChanges->Add_Field(Final[iFinal].asString(CLASS_NAM), SG_DATATYPE_Double); } pChanges->Add_Field(_TL("Unclassified"), SG_DATATYPE_Double); //----------------------------------------------------- for(iInitial=0; iInitial<Initial.Get_Count(); iInitial++) { pChanges->Add_Record()->Set_Value(0, Initial[iInitial].asString(CLASS_NAM)); } pChanges->Add_Record()->Set_Value(0, _TL("Unclassified")); return( true ); }
CSG_Matrix CSG_Matrix::Multiply(const CSG_Matrix &Matrix) const { CSG_Matrix m; if( m_nx == Matrix.m_ny && m.Create(Matrix.m_nx, m_ny) ) { for(int y=0; y<m.m_ny; y++) { for(int x=0; x<m.m_nx; x++) { double z = 0.0; for(int n=0; n<m_nx; n++) { z += m_z[y][n] * Matrix.m_z[n][x]; } m.m_z[y][x] = z; } } } return( m ); }
//--------------------------------------------------------- bool CTable_PCA::Get_Matrix(CSG_Matrix &Matrix) { int i, j1, j2; Matrix.Create(m_nFeatures, m_nFeatures); Matrix.Set_Zero(); switch( m_Method ) { //----------------------------------------------------- default: case 0: // Correlation matrix: Center and reduce the column vectors. for(j1=0; j1<m_nFeatures; j1++) { Matrix[j1][j1] = 1.0; } for(i=0; i<m_pTable->Get_Count() && Set_Progress(i, m_pTable->Get_Count()); i++) { if( !is_NoData(i) ) { for(j1=0; j1<m_nFeatures-1; j1++) { for(j2=j1+1; j2<m_nFeatures; j2++) { Matrix[j1][j2] += Get_Value(j1, i) * Get_Value(j2, i); } } } } break; //----------------------------------------------------- case 1: // Variance-covariance matrix: Center the column vectors. case 2: // Sums-of-squares-and-cross-products matrix for(i=0; i<m_pTable->Get_Count() && Set_Progress(i, m_pTable->Get_Count()); i++) { if( !is_NoData(i) ) { for(j1=0; j1<m_nFeatures; j1++) { for(j2=j1; j2<m_nFeatures; j2++) { Matrix[j1][j2] += Get_Value(j1, i) * Get_Value(j2, i); } } } } break; } //----------------------------------------------------- for(j1=0; j1<m_nFeatures; j1++) { for(j2=j1; j2<m_nFeatures; j2++) { Matrix[j2][j1] = Matrix[j1][j2]; } } return( true ); }
//--------------------------------------------------------- // find_normal() - Function to find the set of normal equations // that allow a quadratic trend surface to be // fitted through N points using least squares // V.1.0, Jo Wood, 27th November, 1994. //--------------------------------------------------------- bool CParam_Scale::Get_Normal(CSG_Matrix &Normal) { double x1, y1, x2, y2, x3, y3, x4, y4, xy2, x2y, xy3, x3y, x2y2, xy, N; // coefficients of X-products. x1 = y1 = x2 = y2 = x3 = y3 = x4 = y4 = xy2 = x2y = xy3 = x3y = x2y2 = xy = N = 0.0; // Calculate matrix of sums of squares and cross products for(int y=0; y<m_Weights.Get_NY(); y++) { double dy = Get_Cellsize() * (y - m_Radius); for(int x=0; x<m_Weights.Get_NX(); x++) { double dw = m_Weights[y][x]; double dx = Get_Cellsize() * (x - m_Radius); x4 += dw * dx * dx * dx * dx; x2y2 += dw * dx * dx * dy * dy; x3y += dw * dx * dx * dx * dy; x3 += dw * dx * dx * dx; x2y += dw * dx * dx * dy; x2 += dw * dx * dx; y4 += dw * dy * dy * dy * dy; xy3 += dw * dx * dy * dy * dy; xy2 += dw * dx * dy * dy; y3 += dw * dy * dy * dy; y2 += dw * dy * dy; xy += dw * dx * dy; x1 += dw * dx; y1 += dw * dy; N += dw; } } // Store cross-product matrix elements. Normal.Create(6, 6); Normal[0][0] = x4; Normal[0][1] = Normal[1][0] = x2y2; Normal[0][2] = Normal[2][0] = x3y; Normal[0][3] = Normal[3][0] = x3; Normal[0][4] = Normal[4][0] = x2y; Normal[0][5] = Normal[5][0] = x2; Normal[1][1] = y4; Normal[1][2] = Normal[2][1] = xy3; Normal[1][3] = Normal[3][1] = xy2; Normal[1][4] = Normal[4][1] = y3; Normal[1][5] = Normal[5][1] = y2; Normal[2][2] = x2y2; Normal[2][3] = Normal[3][2] = x2y; Normal[2][4] = Normal[4][2] = xy2; Normal[2][5] = Normal[5][2] = xy; Normal[3][3] = x2; Normal[3][4] = Normal[4][3] = xy; Normal[3][5] = Normal[5][3] = x1; Normal[4][4] = y2; Normal[4][5] = Normal[5][4] = y1; Normal[5][5] = N; return( true ); }
//--------------------------------------------------------- bool CFilter_3x3::On_Execute(void) { bool bAbsolute; CSG_Matrix Filter; CSG_Grid *pInput, *pResult; CSG_Table *pFilter; //----------------------------------------------------- pInput = Parameters("INPUT" )->asGrid(); pResult = Parameters("RESULT" )->asGrid(); bAbsolute = Parameters("ABSOLUTE" )->asBool(); pFilter = Parameters("FILTER" )->asTable() ? Parameters("FILTER" )->asTable() : Parameters("FILTER_3X3")->asTable(); if( pFilter->Get_Count() < 1 || pFilter->Get_Field_Count() < 1 ) { Error_Set(_TL("invalid filter matrix")); return( false ); } //----------------------------------------------------- Filter.Create(pFilter->Get_Field_Count(), pFilter->Get_Count()); { for(int iy=0; iy<Filter.Get_NY(); iy++) { CSG_Table_Record *pRecord = pFilter->Get_Record(iy); for(int ix=0; ix<Filter.Get_NX(); ix++) { Filter[iy][ix] = pRecord->asDouble(ix); } } } int dx = (Filter.Get_NX() - 1) / 2; int dy = (Filter.Get_NY() - 1) / 2; //----------------------------------------------------- if( !pResult || pResult == pInput ) { pResult = SG_Create_Grid(pInput); } else { pResult->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), _TL("Filter"))); pResult->Set_NoData_Value(pInput->Get_NoData_Value()); } //----------------------------------------------------- for(int y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double s = 0.0; double n = 0.0; if( pInput->is_InGrid(x, y) ) { for(int iy=0, jy=y-dy; iy<Filter.Get_NY(); iy++, jy++) { for(int ix=0, jx=x-dx; ix<Filter.Get_NX(); ix++, jx++) { if( pInput->is_InGrid(jx, jy) ) { s += Filter[iy][ix] * pInput->asDouble(jx, jy); n += fabs(Filter[iy][ix]); } } } } if( n > 0.0 ) { pResult->Set_Value(x, y, bAbsolute ? s : s / n); } else { pResult->Set_NoData(x, y); } } } //----------------------------------------------------- if( !Parameters("RESULT")->asGrid() || Parameters("RESULT")->asGrid() == pInput ) { pInput->Assign(pResult); delete(pResult); DataObject_Update(pInput); } return( true ); }
//--------------------------------------------------------- bool CPoint_Trend_Surface::Get_Regression(CSG_Shapes *pPoints, int iAttribute) { //----------------------------------------------------- int i, j, Field; m_Names.Clear(); m_Names += pPoints->Get_Name(); for(i=1; i<=m_xOrder; i++) { m_Names += Get_Power(SG_T("x"), i); } for(i=1; i<=m_yOrder; i++) { m_Names += Get_Power(SG_T("y"), i); for(j=1; j<=m_xOrder && i<m_tOrder && j<m_tOrder; j++) { m_Names += Get_Power(SG_T("x"), j) + Get_Power(SG_T("y"), i); } } //----------------------------------------------------- CSG_Vector Y, xPow, yPow; CSG_Matrix X; Y.Create(pPoints->Get_Count()); X.Create(m_Names.Get_Count(), pPoints->Get_Count()); xPow.Create(m_xOrder + 1); yPow.Create(m_yOrder + 1); xPow[0] = 1.0; yPow[0] = 1.0; //----------------------------------------------------- for(int iShape=0; iShape<pPoints->Get_Count() && Set_Progress(iShape, pPoints->Get_Count()); iShape++) { CSG_Shape *pShape = pPoints->Get_Shape(iShape); if( !pShape->is_NoData(iAttribute) ) { double zShape = pShape->asDouble(iAttribute); TSG_Point Point = pShape->Get_Point(0); Y[iShape] = zShape; X[iShape][0] = 1.0; for(i=1, Field=1; i<=m_xOrder; i++) { X[iShape][Field++] = xPow[i] = xPow[i - 1] * Point.x; } for(i=1; i<=m_yOrder; i++) { X[iShape][Field++] = yPow[i] = yPow[i - 1] * Point.y; for(j=1; j<=m_xOrder && i<m_tOrder && j<m_tOrder; j++) { X[iShape][Field++] = xPow[j] * yPow[i]; } } } } //----------------------------------------------------- CSG_Matrix Xt, XtXinv; Xt = X; Xt .Set_Transpose(); XtXinv = Xt * X; XtXinv .Set_Inverse(); m_Coefficients = XtXinv * Xt * Y; return( true ); }