|
SAGA API v2.0.8
|
00001 /********************************************************** 00002 * Version $Id: mat_tools.h 1227 2011-11-16 10:16:57Z reklov_w $ 00003 *********************************************************/ 00004 00006 // // 00007 // SAGA // 00008 // // 00009 // System for Automated Geoscientific Analyses // 00010 // // 00011 // Application Programming Interface // 00012 // // 00013 // Library: SAGA_API // 00014 // // 00015 //-------------------------------------------------------// 00016 // // 00017 // mat_tools.h // 00018 // // 00019 // Copyright (C) 2005 by Olaf Conrad // 00020 // // 00021 //-------------------------------------------------------// 00022 // // 00023 // This file is part of 'SAGA - System for Automated // 00024 // Geoscientific Analyses'. // 00025 // // 00026 // This library is free software; you can redistribute // 00027 // it and/or modify it under the terms of the GNU Lesser // 00028 // General Public License as published by the Free // 00029 // Software Foundation, version 2.1 of the License. // 00030 // // 00031 // This library is distributed in the hope that it will // 00032 // be useful, but WITHOUT ANY WARRANTY; without even the // 00033 // implied warranty of MERCHANTABILITY or FITNESS FOR A // 00034 // PARTICULAR PURPOSE. See the GNU Lesser General Public // 00035 // License for more details. // 00036 // // 00037 // You should have received a copy of the GNU Lesser // 00038 // General Public License along with this program; if // 00039 // not, write to the Free Software Foundation, Inc., // 00040 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, // 00041 // USA. // 00042 // // 00043 //-------------------------------------------------------// 00044 // // 00045 // contact: Olaf Conrad // 00046 // Institute of Geography // 00047 // University of Goettingen // 00048 // Goldschmidtstr. 5 // 00049 // 37077 Goettingen // 00050 // Germany // 00051 // // 00052 // e-mail: oconrad@saga-gis.org // 00053 // // 00055 00056 //--------------------------------------------------------- 00057 00058 00060 // // 00061 // // 00062 // // 00064 00065 //--------------------------------------------------------- 00066 #ifndef HEADER_INCLUDED__SAGA_API__mat_tools_H 00067 #define HEADER_INCLUDED__SAGA_API__mat_tools_H 00068 00069 00071 // // 00072 // // 00073 // // 00075 00076 //--------------------------------------------------------- 00077 #include "geo_tools.h" 00078 00079 00081 // // 00082 // // 00083 // // 00085 00086 //--------------------------------------------------------- 00087 #ifndef M_PI 00088 #define M_PI 3.141592653589793 00089 #endif 00090 00091 #define M_PI_045 (M_PI / 4.0) 00092 #define M_PI_090 (M_PI / 2.0) 00093 #define M_PI_135 (M_PI * 3.0 / 4.0) 00094 #define M_PI_180 (M_PI) 00095 #define M_PI_225 (M_PI * 5.0 / 4.0) 00096 #define M_PI_270 (M_PI * 3.0 / 2.0) 00097 #define M_PI_315 (M_PI * 7.0 / 4.0) 00098 #define M_PI_360 (M_PI * 2.0) 00099 00100 #define M_RAD_TO_DEG (180.0 / M_PI) 00101 #define M_DEG_TO_RAD (M_PI / 180.0) 00102 00103 //--------------------------------------------------------- 00104 #define M_EULER 2.718281828459045 00105 00106 //--------------------------------------------------------- 00107 #define N_MEGABYTE_BYTES 0x100000 00108 00109 //--------------------------------------------------------- 00110 #define M_ALMOST_ZERO 0.001l 00111 #define M_TINY (1.0e-20) 00112 00113 //--------------------------------------------------------- 00114 #define M_SQR(x) ((x) * (x)) 00115 #define M_SQRT(x) sqrt((double)(x)) 00116 #define M_GET_LENGTH(x, y) sqrt((double)((x)*(x) + (y)*(y))) 00117 00118 #define M_GET_MIN(a, b) (((a) < (b)) ? (a) : (b)) 00119 #define M_GET_MAX(a, b) (((a) > (b)) ? (a) : (b)) 00120 #define M_SET_MINMAX(min, max, x) if( min > x ) { min = x; } else if( max < x ) { max = x; } 00121 00122 #define M_SET_SIGN(x, sign) ((sign) < 0 ? (x < 0 ? x : -x) : (x > 0 ? x : -x)) 00123 00124 00126 // // 00127 // // 00128 // // 00130 00131 //--------------------------------------------------------- 00132 SAGA_API_DLL_EXPORT double SG_Get_Square (double x); 00133 00134 00136 // // 00137 // // 00138 // // 00140 00141 //--------------------------------------------------------- 00142 typedef int (* TSG_PFNC_Compare) (const int iElement_1, const int iElement_2); 00143 00144 //--------------------------------------------------------- 00145 class SAGA_API_DLL_EXPORT CSG_Index 00146 { 00147 public: 00148 CSG_Index(void); 00149 virtual ~CSG_Index(void); 00150 00151 CSG_Index (int nValues, int *Values, bool bAscending = true); 00152 bool Create (int nValues, int *Values, bool bAscending = true); 00153 00154 CSG_Index (int nValues, double *Values, bool bAscending = true); 00155 bool Create (int nValues, double *Values, bool bAscending = true); 00156 00157 CSG_Index (int nValues, TSG_PFNC_Compare fCompare, bool bAscending = true); 00158 bool Create (int nValues, TSG_PFNC_Compare fCompare, bool bAscending = true); 00159 00160 bool Destroy (void); 00161 00162 bool is_Okay (void) const { return( m_nValues > 0 ); } 00163 int Get_Count (void) const { return( m_nValues ); } 00164 int Get_Index (int i, bool bInv = false) const { return( i >= 0 && i < m_nValues ? m_Index[bInv ? m_nValues-1-i : i] : -1 ); } 00165 int operator [] (int i) const { return( i >= 0 && i < m_nValues ? m_Index[i] : -1 ); } 00166 00167 00168 private: 00169 00170 void *m_Values; 00171 00172 int m_nValues, *m_Index, m_iCompare; 00173 00174 TSG_PFNC_Compare m_fCompare; 00175 00176 00177 void _On_Construction (void); 00178 00179 bool _Set_Array (int nValues); 00180 bool _Set_Index (bool bAscending); 00181 00182 int _Compare (const int iElement_1, const int iElement_2); 00183 00184 }; 00185 00186 00188 // // 00189 // // 00190 // // 00192 00193 //--------------------------------------------------------- 00194 class SAGA_API_DLL_EXPORT CSG_Vector 00195 { 00196 public: 00197 CSG_Vector(void); 00198 virtual ~CSG_Vector(void); 00199 00200 CSG_Vector (const CSG_Vector &Vector); 00201 bool Create (const CSG_Vector &Vector); 00202 00203 CSG_Vector (int n, double *Data = NULL); 00204 bool Create (int n, double *Data = NULL); 00205 00206 bool Destroy (void); 00207 00208 int Get_N (void) const { return( m_n ); } 00209 double * Get_Data (void) const { return( m_z ); } 00210 double operator () (int x) const { return( m_z[x] ); } 00211 double & operator [] (int x) { return( m_z[x] ); } 00212 00213 CSG_String asString (void); 00214 00215 bool is_Equal (const CSG_Vector &Vector) const; 00216 00217 bool Assign (double Scalar); 00218 bool Assign (const CSG_Vector &Vector); 00219 bool Add (double Scalar); 00220 bool Add (const CSG_Vector &Vector); 00221 bool Subtract (const CSG_Vector &Vector); 00222 bool Multiply (double Scalar); 00223 bool Multiply (const CSG_Vector &Vector); 00224 double Multiply_Scalar (const CSG_Vector &Vector) const; 00225 bool Multiply (const class CSG_Matrix &Matrix); 00226 00227 bool operator == (const CSG_Vector &Vector) const; 00228 CSG_Vector & operator = (double Scalar); 00229 CSG_Vector & operator = (const CSG_Vector &Vector); 00230 CSG_Vector & operator += (double Scalar); 00231 CSG_Vector & operator += (const CSG_Vector &Vector); 00232 CSG_Vector & operator -= (double Scalar); 00233 CSG_Vector & operator -= (const CSG_Vector &Vector); 00234 CSG_Vector & operator *= (double Scalar); 00235 CSG_Vector & operator *= (const CSG_Vector &Vector); 00236 CSG_Vector & operator *= (const class CSG_Matrix &Matrix); 00237 CSG_Vector operator + (double Scalar) const; 00238 CSG_Vector operator + (const CSG_Vector &Vector) const; 00239 CSG_Vector operator - (double Scalar) const; 00240 CSG_Vector operator - (const CSG_Vector &Vector) const; 00241 CSG_Vector operator * (double Scalar) const; 00242 double operator * (const CSG_Vector &Vector) const; 00243 00244 bool Set_Zero (void); 00245 bool Set_Unity (void); 00246 00247 double Get_Length (void) const; 00248 double Get_Angle (const CSG_Vector &Vector) const; 00249 CSG_Vector Get_Unity (void) const; 00250 00251 00252 private: 00253 00254 int m_n; 00255 00256 double *m_z; 00257 00258 00259 void _On_Construction (void); 00260 00261 }; 00262 00263 //--------------------------------------------------------- 00264 SAGA_API_DLL_EXPORT CSG_Vector operator * (double Scalar, const CSG_Vector &Vector); 00265 00266 00268 // // 00269 // // 00270 // // 00272 00273 //--------------------------------------------------------- 00274 class SAGA_API_DLL_EXPORT CSG_Matrix 00275 { 00276 public: 00277 CSG_Matrix(void); 00278 virtual ~CSG_Matrix(void); 00279 00280 CSG_Matrix (const CSG_Matrix &Matrix); 00281 bool Create (const CSG_Matrix &Matrix); 00282 00283 CSG_Matrix (int nx, int ny, double *Data = NULL); 00284 bool Create (int nx, int ny, double *Data = NULL); 00285 00286 bool Destroy (void); 00287 00288 bool Add_Cols (int nCols); 00289 bool Add_Rows (int nRows); 00290 bool Add_Col ( double *Data = NULL); 00291 bool Add_Col ( const CSG_Vector &Data); 00292 bool Add_Row ( double *Data = NULL); 00293 bool Add_Row ( const CSG_Vector &Data); 00294 bool Ins_Col (int iCol, double *Data = NULL); 00295 bool Ins_Col (int iCol, const CSG_Vector &Data); 00296 bool Ins_Row (int iRow, double *Data = NULL); 00297 bool Ins_Row (int iRow, const CSG_Vector &Data); 00298 bool Set_Col (int iCol, double *Data); 00299 bool Set_Col (int iCol, const CSG_Vector &Data); 00300 bool Set_Row (int iRow, double *Data); 00301 bool Set_Row (int iRow, const CSG_Vector &Data); 00302 bool Del_Col (int iCol); 00303 bool Del_Row (int iRow); 00304 CSG_Vector Get_Col (int iCol) const; 00305 CSG_Vector Get_Row (int iRow) const; 00306 00307 int Get_NX (void) const { return( m_nx ); } 00308 int Get_NCols (void) const { return( m_nx ); } 00309 int Get_NY (void) const { return( m_ny ); } 00310 int Get_NRows (void) const { return( m_ny ); } 00311 00312 double ** Get_Data (void) const { return( m_z ); } 00313 double operator () (int y, int x) const { return( m_z[y][x] ); } 00314 double * operator [] (int y) const { return( m_z[y] ); } 00315 00316 CSG_String asString (void); 00317 00318 bool is_Square (void) const { return( m_nx > 0 && m_nx == m_ny ); } 00319 bool is_Equal (const CSG_Matrix &Matrix) const; 00320 00321 bool Assign (double Scalar); 00322 bool Assign (const CSG_Matrix &Matrix); 00323 bool Add (double Scalar); 00324 bool Add (const CSG_Matrix &Matrix); 00325 bool Subtract (const CSG_Matrix &Matrix); 00326 bool Multiply (double Scalar); 00327 CSG_Vector Multiply (const CSG_Vector &Vector) const; 00328 CSG_Matrix Multiply (const CSG_Matrix &Matrix) const; 00329 00330 bool operator == (const CSG_Matrix &Matrix) const; 00331 CSG_Matrix & operator = (double Scalar); 00332 CSG_Matrix & operator = (const CSG_Matrix &Matrix); 00333 CSG_Matrix & operator += (double Scalar); 00334 CSG_Matrix & operator += (const CSG_Matrix &Matrix); 00335 CSG_Matrix & operator -= (double Scalar); 00336 CSG_Matrix & operator -= (const CSG_Matrix &Matrix); 00337 CSG_Matrix & operator *= (double Scalar); 00338 CSG_Matrix & operator *= (const CSG_Matrix &Matrix); 00339 CSG_Matrix operator + (double Scalar) const; 00340 CSG_Matrix operator + (const CSG_Matrix &Matrix) const; 00341 CSG_Matrix operator - (double Scalar) const; 00342 CSG_Matrix operator - (const CSG_Matrix &Matrix) const; 00343 CSG_Matrix operator * (double Scalar) const; 00344 CSG_Vector operator * (const CSG_Vector &Vector) const; 00345 CSG_Matrix operator * (const CSG_Matrix &Matrix) const; 00346 00347 bool Set_Zero (void); 00348 bool Set_Identity (void); 00349 bool Set_Transpose (void); 00350 bool Set_Inverse (bool bSilent = true, int nSubSquare = 0); 00351 00352 double Get_Determinant (void) const; 00353 CSG_Matrix Get_Transpose (void) const; 00354 CSG_Matrix Get_Inverse (bool bSilent = true, int nSubSquare = 0) const; 00355 00356 00357 private: 00358 00359 int m_nx, m_ny; 00360 00361 double **m_z; 00362 00363 00364 void _On_Construction (void); 00365 00366 }; 00367 00368 //--------------------------------------------------------- 00369 SAGA_API_DLL_EXPORT CSG_Matrix operator * (double Scalar, const CSG_Matrix &Matrix); 00370 00371 //--------------------------------------------------------- 00372 SAGA_API_DLL_EXPORT bool SG_Matrix_Solve (CSG_Matrix &Matrix, CSG_Vector &Vector, bool bSilent = true); 00373 SAGA_API_DLL_EXPORT bool SG_Matrix_Eigen_Reduction (const CSG_Matrix &Matrix, CSG_Matrix &Eigen_Vectors, CSG_Vector &Eigen_Values, bool bSilent = true); 00374 00375 00377 // // 00378 // // 00379 // // 00381 00382 //--------------------------------------------------------- 00383 class SAGA_API_DLL_EXPORT CSG_Grid_Radius 00384 { 00385 public: 00386 CSG_Grid_Radius(int maxRadius = 0); 00387 ~CSG_Grid_Radius(void); 00388 00389 bool Create (int maxRadius); 00390 void Destroy (void); 00391 00392 int Get_Maximum (void) { return( m_maxRadius ); } 00393 00394 int Get_nPoints (void) { return( m_nPoints ); } 00395 int Get_nPoints (int iRadius) { return( iRadius >= 0 && iRadius < m_maxRadius ? m_nPoints_R[iRadius] : 0 ); } 00396 00397 double Get_Point (int iPoint, int &x, int &y) 00398 { 00399 if( iPoint >= 0 && iPoint < m_nPoints ) 00400 { 00401 x = m_Points[iPoint].x; 00402 y = m_Points[iPoint].y; 00403 00404 return( m_Points[iPoint].d ); // Distance... 00405 } 00406 00407 return( -1.0 ); 00408 } 00409 00410 double Get_Point (int iPoint, int xOffset, int yOffset, int &x, int &y) 00411 { 00412 double d; 00413 00414 if( (d = Get_Point(iPoint, x, y)) >= 0.0 ) 00415 { 00416 x += xOffset; 00417 y += yOffset; 00418 } 00419 00420 return( d ); 00421 } 00422 00423 double Get_Point (int iRadius, int iPoint, int &x, int &y) 00424 { 00425 if( iRadius >= 0 && iRadius <= m_maxRadius && iPoint >= 0 && iPoint < m_nPoints_R[iRadius] ) 00426 { 00427 x = m_Points_R[iRadius][iPoint].x; 00428 y = m_Points_R[iRadius][iPoint].y; 00429 00430 return( m_Points_R[iRadius][iPoint].d ); // Distance... 00431 } 00432 00433 return( -1.0 ); 00434 } 00435 00436 double Get_Point (int iRadius, int iPoint, int xOffset, int yOffset, int &x, int &y) 00437 { 00438 double d; 00439 00440 if( (d = Get_Point(iRadius, iPoint, x, y)) >= 0.0 ) 00441 { 00442 x += xOffset; 00443 y += yOffset; 00444 } 00445 00446 return( d ); 00447 } 00448 00449 00450 private: 00451 00452 int m_maxRadius, m_nPoints, *m_nPoints_R; 00453 00454 typedef struct 00455 { 00456 int x, y; 00457 00458 double d; 00459 } 00460 TSG_Grid_Radius; 00461 00462 TSG_Grid_Radius *m_Points, **m_Points_R; 00463 00464 }; 00465 00466 00468 // // 00469 // // 00470 // // 00472 00473 //--------------------------------------------------------- 00474 class SAGA_API_DLL_EXPORT CSG_Simple_Statistics 00475 { 00476 public: 00477 CSG_Simple_Statistics(void); 00478 CSG_Simple_Statistics(bool bHoldValues); 00479 CSG_Simple_Statistics(const CSG_Simple_Statistics &Statistics); 00480 00481 bool Create (bool bHoldValues = false); 00482 bool Create (const CSG_Simple_Statistics &Statistics); 00483 00484 void Invalidate (void); 00485 00486 bool is_Evaluated (void) { return( m_bEvaluated ); } 00487 00488 int Get_Count (void) { return( m_nValues ); } 00489 double Get_Weights (void) { return( m_Weights ); } 00490 00491 double Get_Minimum (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Minimum ); } 00492 double Get_Maximum (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Maximum ); } 00493 double Get_Range (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Range ); } 00494 double Get_Sum (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Sum ); } 00495 double Get_Mean (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Mean ); } 00496 double Get_Variance (void) { if( !m_bEvaluated ) _Evaluate(); return( m_Variance ); } 00497 double Get_StdDev (void) { if( !m_bEvaluated ) _Evaluate(); return( m_StdDev ); } 00498 00499 void Add_Value (double Value, double Weight = 1.0); 00500 00501 double Get_Value (int i) { return( i >= 0 && i < (int)m_Values.Get_Size() ? ((double *)m_Values.Get_Array())[i] : Get_Mean() ); } 00502 00503 CSG_Simple_Statistics & operator = (const CSG_Simple_Statistics &Statistics) { Create(Statistics); return( *this ); } 00504 CSG_Simple_Statistics & operator += (double Value) { Add_Value(Value); return( *this ); } 00505 00506 00507 protected: 00508 00509 bool m_bEvaluated; 00510 00511 int m_nValues; 00512 00513 double m_Weights, m_Sum, m_Sum2, m_Minimum, m_Maximum, m_Range, m_Mean, m_Variance, m_StdDev; 00514 00515 CSG_Array m_Values; 00516 00517 00518 void _Evaluate (void); 00519 00520 }; 00521 00522 00524 // // 00525 // // 00526 // // 00528 00529 //--------------------------------------------------------- 00530 class SAGA_API_DLL_EXPORT CSG_Class_Statistics 00531 { 00532 private: 00533 00534 typedef struct SClass 00535 { 00536 int Count; 00537 00538 double Value; 00539 } 00540 TClass; 00541 00542 00543 public: 00544 CSG_Class_Statistics(void); 00545 ~CSG_Class_Statistics(void); 00546 00547 void Create (void); 00548 void Destroy (void); 00549 00550 void Reset (void) { m_Array.Set_Array(0, (void **)&m_Classes, false); } 00551 00552 int Get_Count (void) { return( (int)m_Array.Get_Size() ); } 00553 00554 int Get_Class_Count (int i) { return( i >= 0 && i < Get_Count() ? m_Classes[i].Count : 0 ); } 00555 double Get_Class_Value (int i) { return( i >= 0 && i < Get_Count() ? m_Classes[i].Value : 0 ); } 00556 00557 bool Get_Class (int i, double &Value, int &Count) 00558 { 00559 if( i >= 0 && i < Get_Count() ) 00560 { 00561 Count = m_Classes[i].Count; 00562 Value = m_Classes[i].Value; 00563 00564 return( true ); 00565 } 00566 00567 return( false ); 00568 } 00569 00570 bool Get_Class (int i, int &Value, int &Count) 00571 { 00572 if( i >= 0 && i < Get_Count() ) 00573 { 00574 Count = m_Classes[i].Count; 00575 Value = (int)m_Classes[i].Value; 00576 00577 return( true ); 00578 } 00579 00580 return( false ); 00581 } 00582 00583 void Add_Value (double Value); 00584 00585 int Get_Majority (void); 00586 bool Get_Majority (double &Value); 00587 bool Get_Majority (double &Value, int &Count); 00588 00589 int Get_Minority (void); 00590 bool Get_Minority (double &Value); 00591 bool Get_Minority (double &Value, int &Count); 00592 00593 00594 private: 00595 00596 CSG_Array m_Array; 00597 00598 TClass *m_Classes; 00599 00600 }; 00601 00602 00604 // // 00605 // // 00606 // // 00608 00609 //--------------------------------------------------------- 00610 enum ESG_Cluster_Analysis_Method 00611 { 00612 SG_CLUSTERANALYSIS_Minimum_Distance = 0, 00613 SG_CLUSTERANALYSIS_Hill_Climbing, 00614 SG_CLUSTERANALYSIS_Combined 00615 }; 00616 00617 //--------------------------------------------------------- 00618 class SAGA_API_DLL_EXPORT CSG_Cluster_Analysis 00619 { 00620 public: 00621 CSG_Cluster_Analysis(void); 00622 ~CSG_Cluster_Analysis(void); 00623 00624 bool Create (int nFeatures); 00625 bool Destroy (void); 00626 00627 bool Add_Element (void); 00628 bool Set_Feature (int iElement, int iFeature, double Value); 00629 00630 int Get_Cluster (int iElement) const { return( iElement >= 0 && iElement < Get_nElements() ? m_Cluster[iElement] : -1 ); } 00631 00632 bool Execute (int Method, int nClusters); 00633 00634 int Get_nElements (void) const { return( (int)m_Features.Get_Size() ); } 00635 int Get_nFeatures (void) const { return( m_nFeatures ); } 00636 int Get_nClusters (void) const { return( m_nClusters ); } 00637 00638 int Get_Iteration (void) const { return( m_Iteration ); } 00639 00640 double Get_SP (void) const { return( m_SP ); } 00641 00642 int Get_nMembers (int iCluster) const { return( m_nMembers[iCluster] ); } 00643 double Get_Variance (int iCluster) const { return( m_Variance[iCluster] ); } 00644 double Get_Centroid (int iCluster, int iFeature) const { return( m_Centroid[iCluster][iFeature] ); } 00645 00646 00647 private: 00648 00649 int *m_Cluster, m_Iteration, m_nFeatures, m_nClusters, *m_nMembers; 00650 00651 double *m_Variance, **m_Centroid, m_SP; 00652 00653 CSG_Array m_Features; 00654 00655 00656 bool Minimum_Distance (bool bInitialize); 00657 bool Hill_Climbing (bool bInitialize); 00658 00659 }; 00660 00661 00663 // // 00664 // // 00665 // // 00667 00668 //--------------------------------------------------------- 00669 class SAGA_API_DLL_EXPORT CSG_Spline 00670 { 00671 public: 00672 CSG_Spline(void); 00673 virtual ~CSG_Spline(void); 00674 00675 void Destroy (void); 00676 00677 bool Create (double *xValues, double *yValues, int nValues, double yA = 1.0e30, double yB = 1.0e30); 00678 bool Create (double yA = 1.0e30, double yB = 1.0e30); 00679 00680 void Add (double x, double y); 00681 00682 double Get_xMin (void) { return( m_nValues > 0 ? m_Values[0 ].x : 0.0 ); } 00683 double Get_xMax (void) { return( m_nValues > 0 ? m_Values[m_nValues - 1].x : 0.0 ); } 00684 00685 bool Get_Value (double x, double &y); 00686 double Get_Value (double x); 00687 00688 00689 protected: 00690 00691 bool m_bCreated; 00692 00693 int m_nValues, m_nBuffer; 00694 00695 TSG_Point_Z *m_Values; 00696 00697 00698 bool _Create (double yA, double yB); 00699 00700 }; 00701 00702 //--------------------------------------------------------- 00703 class SAGA_API_DLL_EXPORT CSG_Thin_Plate_Spline 00704 { 00705 public: 00706 CSG_Thin_Plate_Spline(void); 00707 virtual ~CSG_Thin_Plate_Spline(void); 00708 00709 bool Destroy (void); 00710 00711 bool Set_Point_Count (int Count) { return( m_Points.Set_Count(Count) ); } 00712 int Get_Point_Count (void) { return( m_Points.Get_Count() ); } 00713 00714 CSG_Points_Z & Get_Points (void) { return( m_Points ); } 00715 00716 bool Add_Point (double x, double y, double z) { return( m_Points.Add( x, y, z) ); } 00717 bool Add_Point (const TSG_Point &p, double z) { return( m_Points.Add(p.x, p.y, z) ); } 00718 00719 bool Set_Point (int Index, double x, double y, double z) 00720 { 00721 if( Index >= 0 && Index < m_Points.Get_Count() ) 00722 { 00723 m_Points[Index].x = x; 00724 m_Points[Index].y = y; 00725 m_Points[Index].z = z; 00726 00727 return( true ); 00728 } 00729 00730 return( false ); 00731 } 00732 00733 bool Set_Point (int Index, const TSG_Point &p, double z) { return( Set_Point(Index, p.x, p.y, z) ); } 00734 00735 bool Create (double Regularization = 0.0, bool bSilent = true); 00736 00737 bool is_Okay (void) { return( m_V.Get_N() > 0 ); } 00738 00739 double Get_Value (double x, double y); 00740 00741 00742 private: 00743 00744 CSG_Points_Z m_Points; 00745 00746 CSG_Vector m_V; 00747 00748 00749 double _Get_hDistance (TSG_Point_Z A, TSG_Point_Z B); 00750 double _Get_Base_Funtion (double x); 00751 double _Get_Base_Funtion (TSG_Point_Z A, double x, double y); 00752 00753 }; 00754 00755 00757 // // 00758 // // 00759 // // 00761 00762 //--------------------------------------------------------- 00763 typedef enum ESG_Test_Distribution_Type 00764 { 00765 TESTDIST_TYPE_Left = 0, 00766 TESTDIST_TYPE_Right, 00767 TESTDIST_TYPE_Middle, 00768 TESTDIST_TYPE_TwoTail 00769 } 00770 TSG_Test_Distribution_Type; 00771 00772 //--------------------------------------------------------- 00773 class CSG_Test_Distribution 00774 { 00775 public: 00776 00777 static double Get_F_Tail_from_R2 (double R2, int nPredictors, int nSamples, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 00778 00779 static double Get_F_Tail (double F , int dfn, int dfd, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 00780 static double Get_F_Inverse (double alpha, int dfn, int dfd, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 00781 00782 static double Get_T_Tail (double T , int df, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 00783 static double Get_T_Inverse (double alpha, int df, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 00784 00785 static double Get_Norm_P (double Z); 00786 static double Get_Norm_Z (double P); 00787 00788 00789 private: 00790 00791 static double Get_Gamma (double F, double dfn, double dfd); 00792 static double Get_Log_Gamma (double a); 00793 00794 static double Get_T_P (double T, int df); 00795 static double Get_T_Z (double T, int df); 00796 static double Get_T_Inv (double p, int df); 00797 00798 static double _Change_Tail_Type (double p, TSG_Test_Distribution_Type from, TSG_Test_Distribution_Type to, bool bNegative); 00799 00800 }; 00801 00802 00804 // // 00805 // // 00806 // // 00808 00809 //--------------------------------------------------------- 00810 // Values: (matrix) array with number of variables = number of rows (x), number of samples = number of columns (y) 00811 SAGA_API_DLL_EXPORT CSG_Matrix SG_Get_Correlation_Matrix (const CSG_Matrix &Values, bool bCovariances = false); 00812 00813 00815 // // 00816 // // 00817 // // 00819 00820 //--------------------------------------------------------- 00821 typedef enum ESG_Regression_Correction 00822 { 00823 REGRESSION_CORR_None = 0, 00824 REGRESSION_CORR_Smith, 00825 REGRESSION_CORR_Wherry_1, 00826 REGRESSION_CORR_Wherry_2, 00827 REGRESSION_CORR_Olkin_Pratt, 00828 REGRESSION_CORR_Pratt, 00829 REGRESSION_CORR_Claudy_3 00830 } 00831 TSG_Regression_Correction; 00832 00833 //--------------------------------------------------------- 00834 SAGA_API_DLL_EXPORT double SG_Regression_Get_Adjusted_R2 (double R2, int nSamples, int nPredictors, TSG_Regression_Correction Correction = REGRESSION_CORR_Wherry_1); 00835 00836 00838 // // 00840 00841 //--------------------------------------------------------- 00842 typedef enum ESG_Regression_Type 00843 { 00844 REGRESSION_Linear = 0, // Y = a + b * X 00845 REGRESSION_Rez_X, // Y = a + b / X 00846 REGRESSION_Rez_Y, // Y = a / (b - X) 00847 REGRESSION_Pow, // Y = a * X^b 00848 REGRESSION_Exp, // Y = a * e^(b * X) 00849 REGRESSION_Log // Y = a + b * ln(X) 00850 } 00851 TSG_Regression_Type; 00852 00853 //--------------------------------------------------------- 00854 class SAGA_API_DLL_EXPORT CSG_Regression 00855 { 00856 public: 00857 CSG_Regression(void); 00858 virtual ~CSG_Regression(void); 00859 00860 void Destroy (void); 00861 00862 void Set_Values (int nValues, double *x, double *y); 00863 void Add_Values (double x, double y); 00864 00865 int Get_Count (void) const { return( m_nValues ); } 00866 00867 double Get_xValue (int iValue) const { return( iValue >= 0 && iValue < m_nValues ? m_x[iValue] : 0.0 ); } 00868 double Get_yValue (int iValue) const { return( iValue >= 0 && iValue < m_nValues ? m_y[iValue] : 0.0 ); } 00869 bool Get_Values (int iValue, double &x, double &y) const 00870 { 00871 if( iValue >= 0 && iValue < m_nValues ) 00872 { 00873 x = m_x[iValue]; 00874 y = m_y[iValue]; 00875 00876 return( true ); 00877 } 00878 00879 return( false ); 00880 } 00881 00882 double Get_xMin (void) const { return( m_xMin ); } 00883 double Get_xMax (void) const { return( m_xMax ); } 00884 double Get_xMean (void) const { return( m_xMean ); } 00885 double Get_xVariance (void) const { return( m_xVar ); } 00886 double Get_x (double y) const; // returns INF on error, this can be checked using the _finite() function (libc, include <float.h>)... 00887 00888 double Get_yMin (void) const { return( m_yMin ); } 00889 double Get_yMax (void) const { return( m_yMax ); } 00890 double Get_yMean (void) const { return( m_yMean ); } 00891 double Get_yVariance (void) const { return( m_yVar ); } 00892 double Get_y (double x) const; // returns INF on error, this can be checked using the _finite() function (libc, include <float.h>)... 00893 00894 double Get_Constant (void) const { return( m_RConst ); } 00895 double Get_Coefficient (void) const { return( m_RCoeff ); } 00896 double Get_R (void) const { return( m_R ); } 00897 double Get_R2 (void) const { return( m_R*m_R ); } 00898 00899 const SG_Char * asString (void); 00900 00901 TSG_Regression_Type Get_Type (void) const { return( m_Type ); } 00902 00903 bool Calculate (TSG_Regression_Type Type = REGRESSION_Linear); 00904 bool Calculate (int nValues, double *x, double *y, TSG_Regression_Type Type = REGRESSION_Linear); 00905 00906 00907 protected: 00908 00909 int m_nValues, m_nBuffer; 00910 00911 double m_RConst, m_RCoeff, m_R, 00912 m_xMin, m_xMax, m_xMean, m_xVar, *m_x, 00913 m_yMin, m_yMax, m_yMean, m_yVar, *m_y; 00914 00915 TSG_Regression_Type m_Type; 00916 00917 00918 bool _Get_MinMeanMax (double &xMin, double &xMean, double &xMax, double &yMin, double &yMean, double &yMax); 00919 00920 double _Y_Transform (double x); 00921 double _X_Transform (double y); 00922 00923 bool _Linear (void); 00924 00925 }; 00926 00927 00929 // // 00931 00932 //--------------------------------------------------------- 00933 enum ESG_Multiple_Regression_Info_Vars 00934 { 00935 MLR_VAR_ID = 0, 00936 MLR_VAR_NAME, 00937 MLR_VAR_RCOEFF, 00938 MLR_VAR_R, 00939 MLR_VAR_R2, 00940 MLR_VAR_R2_ADJ, 00941 MLR_VAR_SE, 00942 MLR_VAR_T, 00943 MLR_VAR_SIG, 00944 MLR_VAR_P 00945 }; 00946 00947 //--------------------------------------------------------- 00948 class SAGA_API_DLL_EXPORT CSG_Regression_Multiple 00949 { 00950 public: 00951 CSG_Regression_Multiple(void); 00952 virtual ~CSG_Regression_Multiple(void); 00953 00954 void Destroy (void); 00955 00956 bool Calculate (const CSG_Matrix &Samples , CSG_Strings *pNames = NULL); 00957 bool Calculate_Forward (const CSG_Matrix &Samples, double P_in , CSG_Strings *pNames = NULL); 00958 bool Calculate_Backward (const CSG_Matrix &Samples, double P_out , CSG_Strings *pNames = NULL); 00959 bool Calculate_Stepwise (const CSG_Matrix &Samples, double P_in, double P_out, CSG_Strings *pNames = NULL); 00960 00961 CSG_String Get_Info (void) const; 00962 00963 class CSG_Table * Get_Regression (void) const { return( m_pRegression ); } 00964 class CSG_Table * Get_Model (void) const { return( m_pModel ); } 00965 class CSG_Table * Get_Steps (void) const { return( m_pSteps ); } 00966 00967 double Get_R2 (void) const; 00968 double Get_R2_Adj (void) const; 00969 double Get_StdError (void) const; 00970 double Get_F (void) const; 00971 double Get_P (void) const; 00972 int Get_DegFreedom (void) const; 00973 int Get_nSamples (void) const; 00974 int Get_nPredictors (void) const; 00975 int Get_Predictor (int i) const { return( i >= 0 && i < Get_nPredictors() ? m_Predictor[i] : -1 ); } 00976 00977 double Get_RConst (void) const; 00978 const SG_Char * Get_Name (int iVariable) const; 00979 double Get_ID (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_ID ) ); } 00980 double Get_RCoeff (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_RCOEFF) ); } 00981 double Get_R2_Partial (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_R2 ) ); } 00982 double Get_R2_Partial_Adj (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_R2_ADJ) ); } 00983 double Get_StdError (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_SE ) ); } 00984 double Get_T (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_T ) ); } 00985 double Get_P (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_SIG ) ); } 00986 00987 double Get_Parameter (int iVariable, int Parameter) const; 00988 00989 00990 protected: 00991 00992 int *m_bIncluded, *m_Predictor, m_nPredictors; 00993 00994 CSG_Strings m_Names; 00995 00996 class CSG_Table *m_pRegression, *m_pModel, *m_pSteps; 00997 00998 00999 bool _Initialize (const CSG_Matrix &Samples, CSG_Strings *pNames, bool bInclude); 01000 01001 double _Get_F (int nPredictors, int nSamples, double r2_full, double r2_reduced); 01002 double _Get_P (int nPredictors, int nSamples, double r2_full, double r2_reduced); 01003 01004 bool _Get_Regression (const class CSG_Matrix &Samples); 01005 01006 bool _Set_Step_Info (const CSG_Matrix &X); 01007 bool _Set_Step_Info (const CSG_Matrix &X, double R2_prev, int iVariable, bool bIn); 01008 int _Get_Step_In (CSG_Matrix &X, double P_in , double &R2, const CSG_Matrix &Samples); 01009 int _Get_Step_Out (CSG_Matrix &X, double P_out, double &R2); 01010 01011 bool __Get_Forward (const class CSG_Matrix &Samples, double p_in); 01012 bool __Get_Forward (int nSamples, int nPredictors, double **X, double *Y, int &iMax, double &rMax); 01013 bool __Eliminate (int nSamples, double *X, double *Y); 01014 01015 }; 01016 01017 01019 // // 01020 // Formula Parser (A. Ringeler) // 01021 // // 01023 01024 //--------------------------------------------------------- 01025 typedef double (*TSG_PFNC_Formula_0)(void); 01026 typedef double (*TSG_PFNC_Formula_1)(double); 01027 typedef double (*TSG_PFNC_Formula_2)(double, double); 01028 typedef double (*TSG_PFNC_Formula_3)(double, double, double); 01029 01030 //--------------------------------------------------------- 01031 class SAGA_API_DLL_EXPORT CSG_Formula 01032 { 01033 public: 01034 CSG_Formula(void); 01035 virtual ~CSG_Formula(void); 01036 01037 static CSG_String Get_Help_Operators (void); 01038 static CSG_String Get_Help_Usage (void); 01039 01040 bool Get_Error (int *pPosition = NULL, CSG_String *pMessage = NULL); 01041 bool Get_Error (CSG_String &Message); 01042 01043 int Add_Function (SG_Char *Name, TSG_PFNC_Formula_1 f, int N_of_Pars, int Varying); 01044 int Del_Function (SG_Char *Name); 01045 01046 bool Set_Formula (const SG_Char *Formula); 01047 CSG_String Get_Formula (void) { return( m_sFormula ); } 01048 01049 void Set_Variable (SG_Char Variable, double Value); 01050 01051 double Get_Value (void); 01052 double Get_Value (double x); 01053 double Get_Value (double *Values, int nValues); 01054 double Get_Value (SG_Char *Arguments, ...); 01055 01056 const SG_Char * Get_Used_Variables (void); 01057 01058 01059 //----------------------------------------------------- 01060 typedef struct 01061 { 01062 SG_Char *name; 01063 TSG_PFNC_Formula_1 f; 01064 int n_pars; 01065 int varying; // Does the result of the function vary even when the parameters stay the same? varying = 1 for e.g. random - number generators. 01066 } 01067 TSG_Formula_Item; 01068 01069 01070 private: 01071 01072 //----------------------------------------------------- 01073 typedef struct 01074 { 01075 SG_Char *code; 01076 double *ctable; 01077 } 01078 TMAT_Formula; 01079 01080 01081 //----------------------------------------------------- 01082 bool m_bError, m_Vars_Used[256]; 01083 01084 int m_Error_Position, m_Length; 01085 01086 TMAT_Formula m_Formula; 01087 01088 CSG_String m_sFormula, m_sError; 01089 01090 const SG_Char *i_error; 01091 int i_pctable; // number of items in a table of constants - used only by the translating functions 01092 01093 double m_Parameters[256], 01094 *i_ctable; // current table of constants - used only by the translating functions 01095 01096 01097 void _Set_Error (const SG_Char *Error = NULL); 01098 01099 double _Get_Value (TMAT_Formula Function); 01100 01101 int _is_Operand (SG_Char c); 01102 int _is_Operand_Code (SG_Char c); 01103 int _is_Number (SG_Char c); 01104 01105 int _Get_Function (int i, SG_Char *name, int *n_pars, int *varying); 01106 int _Get_Function (SG_Char *name); 01107 01108 TMAT_Formula _Translate (const SG_Char *source, const SG_Char *args, int *length, int *error); 01109 01110 int max_size(const SG_Char *source); 01111 SG_Char * my_strtok(SG_Char *s); 01112 SG_Char * i_trans(SG_Char *function, SG_Char *begin, SG_Char *end); 01113 SG_Char * comp_time(SG_Char *function, SG_Char *fend, int npars); 01114 01115 }; 01116 01117 01119 // // 01120 // // 01121 // // 01123 01124 //--------------------------------------------------------- 01125 enum ESG_Trend_String 01126 { 01127 SG_TREND_STRING_Formula = 0, 01128 SG_TREND_STRING_Function, 01129 SG_TREND_STRING_Formula_Parameters, 01130 SG_TREND_STRING_Complete 01131 }; 01132 01133 //--------------------------------------------------------- 01134 class SAGA_API_DLL_EXPORT CSG_Trend 01135 { 01136 public: 01137 CSG_Trend(void); 01138 virtual ~CSG_Trend(void); 01139 01140 bool Set_Formula (const SG_Char *Formula = NULL); 01141 CSG_String Get_Formula (int Type = SG_TREND_STRING_Complete); 01142 01143 int Get_Parameter_Count (void) const { return( m_Params.m_Count ); } 01144 double * Get_Parameters (void) const { return( m_Params.m_A ); } 01145 01146 void Clr_Data (void); 01147 void Set_Data (double *xData, double *yData, int nData, bool bAdd = false); 01148 void Set_Data (const CSG_Points &Data, bool bAdd = false); 01149 void Add_Data (double x, double y); 01150 int Get_Data_Count (void) const { return( m_Data.Get_Count() ); } 01151 double Get_Data_X (int Index) { return( m_Data.Get_X(Index) ); } 01152 double Get_Data_Y (int Index) { return( m_Data.Get_Y(Index) ); } 01153 double Get_Data_XMin (void) { return( m_xMin ); } 01154 double Get_Data_XMax (void) { return( m_xMax ); } 01155 double Get_Data_YMin (void) { return( m_yMin ); } 01156 double Get_Data_YMax (void) { return( m_yMax ); } 01157 01158 bool Set_Max_Iterations (int Iterations); 01159 int Get_Max_Iterations (void) { return( m_Iter_Max); } 01160 bool Set_Max_Lambda (double Lambda); 01161 double Get_Max_Lambda (void) { return( m_Lambda_Max); } 01162 01163 bool Get_Trend (double *xData, double *yData, int nData, const SG_Char *Formula = NULL); 01164 bool Get_Trend (const CSG_Points &Data, const SG_Char *Formula = NULL); 01165 bool Get_Trend (void); 01166 01167 bool is_Okay (void) { return( m_bOkay ); } 01168 01169 CSG_String Get_Error (void); 01170 01171 double Get_ChiSquare (void); 01172 double Get_R2 (void); 01173 01174 double Get_Value (double x); 01175 01176 01177 private: 01178 01179 //----------------------------------------------------- 01180 class SAGA_API_DLL_EXPORT CFncParams 01181 { 01182 public: 01183 CFncParams(void); 01184 virtual ~CFncParams(void); 01185 01186 bool Create (const SG_Char *Variables, int nVariables); 01187 bool Destroy (void); 01188 01189 int m_Count; 01190 01191 SG_Char *m_Variables; 01192 01193 double *m_A, *m_Atry, *m_dA, *m_dA2, *m_Beta, **m_Alpha, **m_Covar; 01194 01195 }; 01196 01197 01198 //----------------------------------------------------- 01199 bool m_bOkay; 01200 01201 int m_Iter_Max; 01202 01203 double m_ChiSqr, m_ChiSqr_o, m_Lambda, m_Lambda_Max, m_xMin, m_xMax, m_yMin, m_yMax; 01204 01205 CSG_Points m_Data; 01206 01207 CFncParams m_Params; 01208 01209 CSG_Formula m_Formula; 01210 01211 01212 bool _Fit_Function (void); 01213 bool _Get_Gaussj (void); 01214 bool _Get_mrqcof (double *Parameters, double **Alpha, double *Beta); 01215 01216 void _Get_Function (double x, double *Parameters, double &y, double *dy_da); 01217 01218 }; 01219 01220 01222 // // 01223 // // 01224 // // 01226 01227 //--------------------------------------------------------- 01228 #endif // #ifndef HEADER_INCLUDED__SAGA_API__mat_tools_H