1 2 /////////////////////////////////////////////////////////// 3 // // 4 // SAGA // 5 // // 6 // System for Automated Geoscientific Analyses // 7 // // 8 // Application Programming Interface // 9 // // 10 // Library: SAGA_API // 11 // // 12 //-------------------------------------------------------// 13 // // 14 // mat_tools.h // 15 // // 16 // Copyright (C) 2005 by Olaf Conrad // 17 // // 18 //-------------------------------------------------------// 19 // // 20 // This file is part of 'SAGA - System for Automated // 21 // Geoscientific Analyses'. // 22 // // 23 // This library is free software; you can redistribute // 24 // it and/or modify it under the terms of the GNU Lesser // 25 // General Public License as published by the Free // 26 // Software Foundation, either version 2.1 of the // 27 // License, or (at your option) any later version. // 28 // // 29 // This library is distributed in the hope that it will // 30 // be useful, but WITHOUT ANY WARRANTY; without even the // 31 // implied warranty of MERCHANTABILITY or FITNESS FOR A // 32 // PARTICULAR PURPOSE. See the GNU Lesser General Public // 33 // License for more details. // 34 // // 35 // You should have received a copy of the GNU Lesser // 36 // General Public License along with this program; if // 37 // not, see <http://www.gnu.org/licenses/>. // 38 // // 39 //-------------------------------------------------------// 40 // // 41 // contact: Olaf Conrad // 42 // Institute of Geography // 43 // University of Goettingen // 44 // Goldschmidtstr. 5 // 45 // 37077 Goettingen // 46 // Germany // 47 // // 48 // e-mail: oconrad@saga-gis.org // 49 // // 50 /////////////////////////////////////////////////////////// 51 52 //--------------------------------------------------------- 53 #ifndef HEADER_INCLUDED__SAGA_API__mat_tools_H 54 #define HEADER_INCLUDED__SAGA_API__mat_tools_H 55 56 57 /////////////////////////////////////////////////////////// 58 // // 59 // // 60 // // 61 /////////////////////////////////////////////////////////// 62 63 //--------------------------------------------------------- 64 /** \file mat_tools.h 65 * A set of basic mathematical, numerical, statistical tools. 66 * @see CSG_Vector 67 * @see CSG_Matrix 68 * @see CSG_Formula 69 * @see CSG_Random 70 * @see CSG_Index 71 * @see CSG_Histogram 72 * @see CSG_Simple_Statistics 73 * @see CSG_Unique_Value_Statistics 74 * @see CSG_Regression 75 * @see CSG_Trend 76 * @see CSG_Spline 77 */ 78 79 80 /////////////////////////////////////////////////////////// 81 // // 82 // // 83 // // 84 /////////////////////////////////////////////////////////// 85 86 //--------------------------------------------------------- 87 #include "geo_tools.h" 88 89 90 /////////////////////////////////////////////////////////// 91 // // 92 // // 93 // // 94 /////////////////////////////////////////////////////////// 95 96 //--------------------------------------------------------- 97 #ifndef M_PI 98 #define M_PI 3.141592653589793 99 #endif 100 101 #define M_PI_045 (M_PI / 4.) 102 #define M_PI_090 (M_PI / 2.) 103 #define M_PI_135 (M_PI * 3. / 4.) 104 #define M_PI_180 (M_PI) 105 #define M_PI_225 (M_PI * 5. / 4.) 106 #define M_PI_270 (M_PI * 3. / 2.) 107 #define M_PI_315 (M_PI * 7. / 4.) 108 #define M_PI_360 (M_PI * 2.) 109 110 #define M_RAD_TO_DEG (180. / M_PI) 111 #define M_DEG_TO_RAD (M_PI / 180.) 112 113 //--------------------------------------------------------- 114 #define M_EULER 2.718281828459045 115 116 //--------------------------------------------------------- 117 #define N_MEGABYTE_BYTES 0x100000 118 119 //--------------------------------------------------------- 120 #define M_ALMOST_ZERO 0.001l 121 #define M_TINY (1.0e-20) 122 123 //--------------------------------------------------------- 124 #define M_SQR(x) ((x) * (x)) 125 #define M_SQRT(x) sqrt((double)(x)) 126 #define M_GET_LENGTH(x, y) sqrt((double)((x)*(x) + (y)*(y))) 127 128 #define M_GET_MIN(a, b) (((a) < (b)) ? (a) : (b)) 129 #define M_GET_MAX(a, b) (((a) > (b)) ? (a) : (b)) 130 #define M_SET_MINMAX(min, max, x) if( min > x ) { min = x; } else if( max < x ) { max = x; } 131 132 #define M_SET_SIGN(x, sign) ((sign) < 0 ? (x < 0 ? x : -x) : (x > 0 ? x : -x)) 133 134 //--------------------------------------------------------- 135 #define SG_ROUND_TO_BYTE(x) ((BYTE )(x < 0. ? x - 0.5 : x + 0.5)) 136 #define SG_ROUND_TO_CHAR(x) ((char )(x < 0. ? x - 0.5 : x + 0.5)) 137 #define SG_ROUND_TO_WORD(x) ((WORD )(x < 0. ? x - 0.5 : x + 0.5)) 138 #define SG_ROUND_TO_SHORT(x) ((short)(x < 0. ? x - 0.5 : x + 0.5)) 139 #define SG_ROUND_TO_DWORD(x) ((DWORD)(x < 0. ? x - 0.5 : x + 0.5)) 140 #define SG_ROUND_TO_INT(x) ((int )(x < 0. ? x - 0.5 : x + 0.5)) 141 #define SG_ROUND_TO_LONG(x) ((long )(x < 0. ? x - 0.5 : x + 0.5)) 142 #define SG_ROUND_TO_ULONG(x) ((uLong)(x < 0. ? x - 0.5 : x + 0.5)) 143 #define SG_ROUND_TO_SLONG(x) ((sLong)(x < 0. ? x - 0.5 : x + 0.5)) 144 145 146 /////////////////////////////////////////////////////////// 147 // // 148 // // 149 // // 150 /////////////////////////////////////////////////////////// 151 152 //--------------------------------------------------------- 153 SAGA_API_DLL_EXPORT double SG_Get_Square (double Value); 154 155 SAGA_API_DLL_EXPORT double SG_Get_Rounded (double Value, int Decimals = 0); 156 SAGA_API_DLL_EXPORT double SG_Get_Rounded_To_SignificantFigures (double Value, int Decimals); 157 158 //--------------------------------------------------------- 159 SAGA_API_DLL_EXPORT int SG_Get_Digit_Count (int Number); 160 161 //--------------------------------------------------------- 162 SAGA_API_DLL_EXPORT CSG_String SG_Get_Double_asString (double Number, int Width = -1, int Precision = -1, bool bScientific = false); 163 164 //--------------------------------------------------------- 165 SAGA_API_DLL_EXPORT int SG_Compare_Int (const void *a, const void *b); 166 SAGA_API_DLL_EXPORT int SG_Compare_Double (const void *a, const void *b); 167 SAGA_API_DLL_EXPORT int SG_Compare_Char_Ptr (const void *a, const void *b); 168 169 //--------------------------------------------------------- 170 SAGA_API_DLL_EXPORT double SG_Degree_To_Decimal ( double Deg, double Min, double Sec); 171 SAGA_API_DLL_EXPORT void SG_Decimal_To_Degree (double Value, double &Deg, double &Min, double &Sec); 172 173 174 /////////////////////////////////////////////////////////// 175 // // 176 // // 177 // // 178 /////////////////////////////////////////////////////////// 179 180 //--------------------------------------------------------- 181 typedef int (* TSG_PFNC_Compare) (const int a, const int b); 182 183 //--------------------------------------------------------- 184 class SAGA_API_DLL_EXPORT CSG_Index 185 { 186 public: 187 class CSG_Index_Compare 188 { 189 public: CSG_Index_Compare(void)190 CSG_Index_Compare(void) {} ~CSG_Index_Compare(void)191 virtual ~CSG_Index_Compare(void) {} 192 193 virtual int Compare (const int a, const int b) = 0; 194 }; 195 196 197 public: 198 CSG_Index(void); 199 virtual ~CSG_Index(void); 200 201 CSG_Index (int nValues, CSG_Index_Compare &Compare); 202 bool Create (int nValues, CSG_Index_Compare &Compare); 203 204 CSG_Index (int nValues, CSG_Index_Compare *pCompare); 205 bool Create (int nValues, CSG_Index_Compare *pCompare); 206 207 CSG_Index (int nValues, int *Values, bool bAscending = true); 208 bool Create (int nValues, int *Values, bool bAscending = true); 209 210 CSG_Index (int nValues, double *Values, bool bAscending = true); 211 bool Create (int nValues, double *Values, bool bAscending = true); 212 213 CSG_Index (int nValues, TSG_PFNC_Compare fCompare); 214 bool Create (int nValues, TSG_PFNC_Compare fCompare); 215 216 bool Destroy (void); 217 218 void Show_Progress (bool bProgress = true); 219 220 bool Add_Entry (int Position = -1); 221 bool Del_Entry (int Position = -1); 222 is_Okay(void)223 bool is_Okay (void) const { return( m_nValues > 0 ); } Get_Count(void)224 int Get_Count (void) const { return( m_nValues ); } 225 226 int Get_Index (int Position, bool Ascending = true) const 227 { 228 return( Position < 0 || Position >= m_nValues ? -1 : m_Index[Ascending ? Position : m_nValues - 1 - Position] ); 229 } 230 231 int operator [] (int Position) const 232 { 233 return( Position < 0 || Position >= m_nValues ? -1 : m_Index[Position] ); 234 } 235 236 237 private: 238 239 bool m_bProgress; 240 241 int m_nValues, *m_Index; 242 243 244 void _On_Construction (void); 245 246 bool _Set_Array (int nValues); 247 bool _Set_Index (CSG_Index_Compare *pCompare); 248 249 }; 250 251 252 /////////////////////////////////////////////////////////// 253 // // 254 // // 255 // // 256 /////////////////////////////////////////////////////////// 257 258 //--------------------------------------------------------- 259 class SAGA_API_DLL_EXPORT CSG_PriorityQueue 260 { 261 public: 262 263 //----------------------------------------------------- 264 class CSG_PriorityQueueItem 265 { 266 public: CSG_PriorityQueueItem(void)267 CSG_PriorityQueueItem(void) {} 268 269 virtual int Compare (CSG_PriorityQueueItem *pItem) = 0; 270 271 }; 272 273 //----------------------------------------------------- 274 CSG_PriorityQueue(size_t maxSize = 256); 275 276 virtual ~CSG_PriorityQueue(void); 277 278 void Create (size_t maxSize = 256); 279 void Destroy (void); 280 281 //----------------------------------------------------- is_Empty(void)282 bool is_Empty (void) const { return( m_nItems == 0 ); } Get_Size(void)283 size_t Get_Size (void) const { return( m_nItems ); } Get_Item(size_t i)284 CSG_PriorityQueueItem * Get_Item (size_t i) const { return( m_Items[i] ); } 285 286 void Add (CSG_PriorityQueueItem *pItem); 287 Peek(void)288 CSG_PriorityQueueItem * Peek (void) const { return( Minimum() ); } 289 CSG_PriorityQueueItem * Poll (void); 290 291 //----------------------------------------------------- Minimum(void)292 CSG_PriorityQueueItem * Minimum (void) const 293 { 294 if( m_nItems ) 295 { 296 if( m_pLeaf[0] ) 297 { 298 return( m_pLeaf[0]->Minimum() ); 299 } 300 301 return( m_Items[0] ); 302 } 303 304 return( NULL ); 305 } 306 Maximum(void)307 CSG_PriorityQueueItem * Maximum (void) const 308 { 309 if( m_nItems ) 310 { 311 if( m_pLeaf[1] ) 312 { 313 return( m_pLeaf[1]->Maximum() ); 314 } 315 316 return( m_Items[m_nItems - 1] ); 317 } 318 319 return( NULL ); 320 } 321 322 323 private: 324 325 size_t m_nItems, m_maxSize; 326 327 CSG_PriorityQueue *m_pLeaf[2]; 328 329 CSG_PriorityQueueItem **m_Items; 330 331 332 size_t _Insert_Position (CSG_PriorityQueueItem *pItem); 333 334 }; 335 336 337 /////////////////////////////////////////////////////////// 338 // // 339 // // 340 // // 341 /////////////////////////////////////////////////////////// 342 343 //--------------------------------------------------------- 344 class SAGA_API_DLL_EXPORT CSG_Vector 345 { 346 public: 347 CSG_Vector(void); 348 virtual ~CSG_Vector(void); 349 350 CSG_Vector (const CSG_Vector &Vector); 351 bool Create (const CSG_Vector &Vector); 352 353 CSG_Vector (int n, double *Data = NULL); 354 bool Create (int n, double *Data = NULL); 355 356 CSG_Vector (size_t n, double *Data = NULL); 357 bool Create (size_t n, double *Data = NULL); 358 359 bool Destroy (void); 360 361 bool Set_Rows (int nRows); 362 bool Set_Rows (size_t nRows); 363 bool Add_Rows (int nRows); 364 bool Add_Rows (size_t nRows); 365 bool Del_Rows (int nRows); 366 bool Del_Rows (size_t nRows); 367 bool Add_Row (double Value = 0.); 368 bool Del_Row (int Row = -1); 369 bool Del_Row (size_t Row); 370 Get_N(void)371 int Get_N (void) const { return( (int)Get_Size() ); } Get_Size(void)372 size_t Get_Size (void) const { return( m_Array.Get_Size() ); } Get_Data(void)373 double * Get_Data (void) const { return( (double *)m_Array.Get_Array() ); } Get_Data(int x)374 double Get_Data (int x) const { return( Get_Data()[x] ); } operator()375 double operator () (int x) const { return( Get_Data()[x] ); } 376 double & operator [] (int x) { return( Get_Data()[x] ); } 377 double & operator [] (size_t x) { return( Get_Data()[x] ); } 378 operator const double * (void) const { return( Get_Data() ); } 379 380 CSG_String to_String (int Width = -1, int Precision = -1, bool bScientific = false, const SG_Char *Separator = NULL) const; 381 bool from_String (const CSG_String &String); 382 383 bool is_Equal (const CSG_Vector &Vector) const; 384 385 bool Assign (double Scalar); 386 bool Assign (const CSG_Vector &Vector); 387 bool Add (double Scalar); 388 bool Add (const CSG_Vector &Vector); 389 bool Subtract (const CSG_Vector &Vector); 390 bool Multiply (double Scalar); 391 bool Multiply (const CSG_Vector &Vector); 392 double Multiply_Scalar (const CSG_Vector &Vector) const; 393 bool Multiply (const class CSG_Matrix &Matrix); 394 395 bool operator == (const CSG_Vector &Vector) const; 396 CSG_Vector & operator = (double Scalar); 397 CSG_Vector & operator = (const CSG_Vector &Vector); 398 CSG_Vector & operator += (double Scalar); 399 CSG_Vector & operator += (const CSG_Vector &Vector); 400 CSG_Vector & operator -= (double Scalar); 401 CSG_Vector & operator -= (const CSG_Vector &Vector); 402 CSG_Vector & operator *= (double Scalar); 403 CSG_Vector & operator *= (const CSG_Vector &Vector); 404 CSG_Vector & operator *= (const class CSG_Matrix &Matrix); 405 CSG_Vector operator + (double Scalar) const; 406 CSG_Vector operator + (const CSG_Vector &Vector) const; 407 CSG_Vector operator - (double Scalar) const; 408 CSG_Vector operator - (const CSG_Vector &Vector) const; 409 CSG_Vector operator * (double Scalar) const; 410 double operator * (const CSG_Vector &Vector) const; 411 412 bool Set_Zero (void); 413 bool Set_Unity (void); 414 415 bool Flip_Values (void); 416 417 bool Sort (bool bAscending = true); 418 419 double Get_Length (void) const; 420 double Get_Angle (const CSG_Vector &Vector) const; 421 CSG_Vector Get_Unity (void) const; 422 423 typedef double const * const_iterator; 424 begin(void)425 const_iterator begin (void) const { return( Get_Data() ); } end(void)426 const_iterator end (void) const { return( Get_Data() + Get_Size() ); } cbegin(void)427 const_iterator cbegin (void) const { return( Get_Data() ); } cend(void)428 const_iterator cend (void) const { return( Get_Data() + Get_Size() ); } 429 430 431 private: 432 433 CSG_Array m_Array; 434 435 }; 436 437 //--------------------------------------------------------- 438 SAGA_API_DLL_EXPORT CSG_Vector operator * (double Scalar, const CSG_Vector &Vector); 439 440 //--------------------------------------------------------- 441 SAGA_API_DLL_EXPORT bool SG_VectorR2_Rotate (double &x, double &y, double Angle); 442 SAGA_API_DLL_EXPORT bool SG_VectorR2_Rotate (double Vector[2], double Angle); 443 SAGA_API_DLL_EXPORT bool SG_VectorR2_Rotate (CSG_Vector &Vector , double Angle); 444 445 SAGA_API_DLL_EXPORT bool SG_VectorR3_Rotate (double Vector[3], size_t Axis, double Angle); 446 SAGA_API_DLL_EXPORT bool SG_VectorR3_Rotate (CSG_Vector &Vector , size_t Axis, double Angle); 447 448 449 /////////////////////////////////////////////////////////// 450 // // 451 // // 452 // // 453 /////////////////////////////////////////////////////////// 454 455 //--------------------------------------------------------- 456 class SAGA_API_DLL_EXPORT CSG_Matrix 457 { 458 public: 459 CSG_Matrix(void); 460 virtual ~CSG_Matrix(void); 461 462 CSG_Matrix (const CSG_Matrix &Matrix); 463 bool Create (const CSG_Matrix &Matrix); 464 465 CSG_Matrix (int nCols, int nRows, double *Data = NULL); 466 bool Create (int nCols, int nRows, double *Data = NULL); 467 468 CSG_Matrix (int nCols, int nRows, double **Data); 469 bool Create (int nCols, int nRows, double **Data); 470 471 bool Destroy (void); 472 473 bool Set_Size (int nRows, int nCols); 474 bool Set_Cols (int nCols); 475 bool Set_Rows (int nRows); 476 bool Add_Cols (int nCols); 477 bool Add_Rows (int nRows); 478 bool Del_Cols (int nCols); 479 bool Del_Rows (int nRows); 480 bool Add_Col ( double *Data = NULL); 481 bool Add_Col ( const CSG_Vector &Data); 482 bool Add_Row ( double *Data = NULL); 483 bool Add_Row ( const CSG_Vector &Data); 484 bool Ins_Col (int Col, double *Data = NULL); 485 bool Ins_Col (int Col, const CSG_Vector &Data); 486 bool Ins_Row (int Row, double *Data = NULL); 487 bool Ins_Row (int Row, const CSG_Vector &Data); 488 bool Set_Col (int Col, double *Data); 489 bool Set_Col (int Col, const CSG_Vector &Data); 490 bool Set_Col ( const CSG_Vector &Data); 491 bool Set_Row (int Row, double *Data); 492 bool Set_Row (int Row, const CSG_Vector &Data); 493 bool Set_Row ( const CSG_Vector &Data); 494 bool Del_Col (int Col); 495 bool Del_Row (int Row); 496 CSG_Vector Get_Col (int Col) const; 497 CSG_Vector Get_Row (int Row) const; 498 Get_NX(void)499 int Get_NX (void) const { return( m_nx ); } Get_NY(void)500 int Get_NY (void) const { return( m_ny ); } Get_NCols(void)501 int Get_NCols (void) const { return( m_nx ); } Get_NRows(void)502 int Get_NRows (void) const { return( m_ny ); } 503 504 operator const double ** (void) const { return( (const double **)m_z ); } Get_Data(void)505 double ** Get_Data (void) const { return( m_z ); } 506 operator()507 double operator () (int Row, int Col) const { return( m_z[Row][Col] ); } operator()508 double operator () (size_t Row, size_t Col) const { return( m_z[Row][Col] ); } 509 double * operator [] (int Row) const { return( m_z[Row] ); } 510 double * operator [] (size_t Row) const { return( m_z[Row] ); } 511 512 CSG_String to_String (int Width = -1, int Precision = -1, bool bScientific = false, const SG_Char *Separator = NULL) const; 513 bool from_String (const CSG_String &String); 514 is_Square(void)515 bool is_Square (void) const { return( m_nx > 0 && m_nx == m_ny ); } 516 bool is_Equal (const CSG_Matrix &Matrix) const; 517 518 bool Assign (double Scalar); 519 bool Assign (const CSG_Matrix &Matrix); 520 bool Add (double Scalar); 521 bool Add (const CSG_Matrix &Matrix); 522 bool Subtract (const CSG_Matrix &Matrix); 523 bool Multiply (double Scalar); 524 CSG_Vector Multiply (const CSG_Vector &Vector) const; 525 CSG_Matrix Multiply (const CSG_Matrix &Matrix) const; 526 527 bool operator == (const CSG_Matrix &Matrix) const; 528 CSG_Matrix & operator = (double Scalar); 529 CSG_Matrix & operator = (const CSG_Matrix &Matrix); 530 CSG_Matrix & operator += (double Scalar); 531 CSG_Matrix & operator += (const CSG_Matrix &Matrix); 532 CSG_Matrix & operator -= (double Scalar); 533 CSG_Matrix & operator -= (const CSG_Matrix &Matrix); 534 CSG_Matrix & operator *= (double Scalar); 535 CSG_Matrix & operator *= (const CSG_Matrix &Matrix); 536 CSG_Matrix operator + (double Scalar) const; 537 CSG_Matrix operator + (const CSG_Matrix &Matrix) const; 538 CSG_Matrix operator - (double Scalar) const; 539 CSG_Matrix operator - (const CSG_Matrix &Matrix) const; 540 CSG_Matrix operator * (double Scalar) const; 541 CSG_Vector operator * (const CSG_Vector &Vector) const; 542 CSG_Matrix operator * (const CSG_Matrix &Matrix) const; 543 544 bool Set_Zero (void); 545 bool Set_Identity (void); 546 bool Set_Transpose (void); 547 bool Set_Inverse (bool bSilent = true, int nSubSquare = 0); 548 549 double Get_Determinant (void) const; 550 CSG_Matrix Get_Transpose (void) const; 551 CSG_Matrix Get_Inverse (bool bSilent = true, int nSubSquare = 0) const; 552 553 554 private: 555 556 int m_nx, m_ny; 557 558 double **m_z; 559 560 561 void _On_Construction (void); 562 563 }; 564 565 //--------------------------------------------------------- 566 SAGA_API_DLL_EXPORT CSG_Matrix operator * (double Scalar, const CSG_Matrix &Matrix); 567 568 //--------------------------------------------------------- 569 SAGA_API_DLL_EXPORT bool SG_Matrix_LU_Decomposition (int n, int *Permutation, double **Matrix , bool bSilent = true, int *nRowChanges = NULL); 570 SAGA_API_DLL_EXPORT bool SG_Matrix_LU_Solve (int n, const int *Permutation, const double **Matrix, double *Vector, bool bSilent = true); 571 572 SAGA_API_DLL_EXPORT bool SG_Matrix_Solve (CSG_Matrix &Matrix, CSG_Vector &Vector, bool bSilent = true); 573 SAGA_API_DLL_EXPORT bool SG_Matrix_Eigen_Reduction (const CSG_Matrix &Matrix, CSG_Matrix &Eigen_Vectors, CSG_Vector &Eigen_Values, bool bSilent = true); 574 575 576 /////////////////////////////////////////////////////////// 577 // // 578 // // 579 // // 580 /////////////////////////////////////////////////////////// 581 582 //--------------------------------------------------------- 583 class SAGA_API_DLL_EXPORT CSG_Grid_Radius 584 { 585 public: 586 CSG_Grid_Radius(int maxRadius = 0); 587 ~CSG_Grid_Radius(void); 588 589 bool Create (int maxRadius); 590 void Destroy (void); 591 Get_Maximum(void)592 int Get_Maximum (void) { return( m_maxRadius ); } 593 Get_nPoints(void)594 int Get_nPoints (void) { return( m_nPoints ); } Get_nPoints(int iRadius)595 int Get_nPoints (int iRadius) { return( iRadius >= 0 && iRadius < m_maxRadius ? m_nPoints_R[iRadius] : 0 ); } 596 Get_Point(int iPoint,int & x,int & y)597 double Get_Point (int iPoint, int &x, int &y) 598 { 599 if( iPoint >= 0 && iPoint < m_nPoints ) 600 { 601 x = m_Points[iPoint].x; 602 y = m_Points[iPoint].y; 603 604 return( m_Points[iPoint].d ); // Distance... 605 } 606 607 return( -1. ); 608 } 609 Get_Point(int iPoint,int xOffset,int yOffset,int & x,int & y)610 double Get_Point (int iPoint, int xOffset, int yOffset, int &x, int &y) 611 { 612 double d; 613 614 if( (d = Get_Point(iPoint, x, y)) >= 0. ) 615 { 616 x += xOffset; 617 y += yOffset; 618 } 619 620 return( d ); 621 } 622 Get_Point(int iRadius,int iPoint,int & x,int & y)623 double Get_Point (int iRadius, int iPoint, int &x, int &y) 624 { 625 if( iRadius >= 0 && iRadius <= m_maxRadius && iPoint >= 0 && iPoint < m_nPoints_R[iRadius] ) 626 { 627 x = m_Points_R[iRadius][iPoint].x; 628 y = m_Points_R[iRadius][iPoint].y; 629 630 return( m_Points_R[iRadius][iPoint].d ); // Distance... 631 } 632 633 return( -1. ); 634 } 635 Get_Point(int iRadius,int iPoint,int xOffset,int yOffset,int & x,int & y)636 double Get_Point (int iRadius, int iPoint, int xOffset, int yOffset, int &x, int &y) 637 { 638 double d; 639 640 if( (d = Get_Point(iRadius, iPoint, x, y)) >= 0. ) 641 { 642 x += xOffset; 643 y += yOffset; 644 } 645 646 return( d ); 647 } 648 649 650 private: 651 652 int m_maxRadius, m_nPoints, *m_nPoints_R; 653 654 typedef struct SSG_Grid_Radius 655 { 656 int x, y; 657 658 double d; 659 } 660 TSG_Grid_Radius; 661 662 TSG_Grid_Radius *m_Points, **m_Points_R; 663 664 }; 665 666 667 /////////////////////////////////////////////////////////// 668 // // 669 // // 670 // // 671 /////////////////////////////////////////////////////////// 672 673 //--------------------------------------------------------- 674 class SAGA_API_DLL_EXPORT CSG_Random 675 { 676 public: 677 CSG_Random(void); 678 679 static void Initialize (void); 680 static void Initialize (unsigned int Value); 681 682 static double Get_Uniform (void); 683 static double Get_Uniform (double min, double max); 684 685 static double Get_Gaussian (double mean, double stddev); 686 687 }; 688 689 690 /////////////////////////////////////////////////////////// 691 // // 692 // // 693 // // 694 /////////////////////////////////////////////////////////// 695 696 //--------------------------------------------------------- 697 class SAGA_API_DLL_EXPORT CSG_Simple_Statistics 698 { 699 public: 700 CSG_Simple_Statistics(void); 701 CSG_Simple_Statistics(bool bHoldValues); 702 CSG_Simple_Statistics(const CSG_Simple_Statistics &Statistics); 703 CSG_Simple_Statistics(double Mean, double StdDev, sLong Count = 1000); 704 CSG_Simple_Statistics(const CSG_Vector &Values, bool bHoldValues = false); 705 706 bool Create (bool bHoldValues = false); 707 bool Create (const CSG_Simple_Statistics &Statistics); 708 bool Create (double Mean, double StdDev, sLong Count = 1000); 709 bool Create (const CSG_Vector &Values, bool bHoldValues = false); 710 711 void Invalidate (void); 712 bool Evaluate (void); 713 is_Evaluated(void)714 int is_Evaluated (void) const { return( m_bEvaluated ); } 715 716 bool Set_Count (sLong Count); 717 Get_Count(void)718 sLong Get_Count (void) const { return( m_nValues ); } Get_Weights(void)719 double Get_Weights (void) const { return( m_Weights ); } 720 Get_Minimum(void)721 double Get_Minimum (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Minimum ); } Get_Maximum(void)722 double Get_Maximum (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Maximum ); } Get_Range(void)723 double Get_Range (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Range ); } Get_Sum(void)724 double Get_Sum (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Sum ); } Get_Sum_Of_Squares(void)725 double Get_Sum_Of_Squares (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Sum2 ); } Get_Mean(void)726 double Get_Mean (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Mean ); } Get_Variance(void)727 double Get_Variance (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_Variance ); } Get_StdDev(void)728 double Get_StdDev (void) { if( m_bEvaluated < 1 ) _Evaluate(1); return( m_StdDev ); } 729 Get_Kurtosis(void)730 double Get_Kurtosis (void) { if( m_bEvaluated < 2 ) _Evaluate(2); return( m_Kurtosis ); } Get_Skewness(void)731 double Get_Skewness (void) { if( m_bEvaluated < 2 ) _Evaluate(2); return( m_Skewness ); } 732 double Get_SkewnessPearson (void); 733 734 double Get_Quantile (double Quantile); 735 double Get_Percentile (double Percentile); 736 double Get_Median (void); 737 double Get_Gini (void); 738 739 sLong Get_IndexOfMinimum (void); 740 sLong Get_IndexOfMaximum (void); 741 742 sLong Get_nValues_Above (double Threshold, bool bEquals = false); 743 sLong Get_nValues_Below (double Threshold, bool bEquals = false); 744 745 void Add (const CSG_Simple_Statistics &Statistics); 746 747 void Add_Value (double Value, double Weight = 1.); 748 Get_Values(void)749 double * Get_Values (void) const { return( (double *)m_Values.Get_Array() ); } Get_Value(sLong i)750 double Get_Value (sLong i) const { return( i >= 0 && i < (sLong)m_Values.Get_Size() ? Get_Values()[i] : m_Mean ); } 751 double operator [] (sLong i) const { return( i >= 0 && i < (sLong)m_Values.Get_Size() ? Get_Values()[i] : m_Mean ); } 752 753 754 CSG_Simple_Statistics & operator = (const CSG_Simple_Statistics &Statistics) { Create(Statistics); return( *this ); } 755 CSG_Simple_Statistics & operator += (const CSG_Simple_Statistics &Statistics) { Add(Statistics); return( *this ); } 756 CSG_Simple_Statistics & operator += (double Value) { Add_Value(Value); return( *this ); } 757 758 759 protected: 760 761 bool m_bSorted; 762 763 int m_bEvaluated; 764 765 sLong m_nValues; 766 767 double m_Weights, m_Sum, m_Sum2, m_Minimum, m_Maximum, m_Range, m_Mean, m_Variance, m_StdDev, m_Kurtosis, m_Skewness, m_Gini; 768 769 CSG_Array m_Values; 770 771 772 void _Evaluate (int Level = 1); 773 774 }; 775 776 777 /////////////////////////////////////////////////////////// 778 // // 779 // // 780 // // 781 /////////////////////////////////////////////////////////// 782 783 //--------------------------------------------------------- 784 class SAGA_API_DLL_EXPORT CSG_Unique_Value_Statistics 785 { 786 public: CSG_Unique_Value_Statistics(void)787 CSG_Unique_Value_Statistics (void) {} ~CSG_Unique_Value_Statistics(void)788 virtual ~CSG_Unique_Value_Statistics (void) {} 789 790 virtual void Create (bool bWeights = false) { m_bWeights = bWeights; } 791 Get_Count(void)792 int Get_Count (void) const { return( (int)m_Count.Get_Size() ); } Get_Count(int i)793 int Get_Count (int i) const { return( m_Count[i] ); } Get_Weight(int i)794 double Get_Weight (int i) const { return( m_bWeights ? m_Weight[i] : m_Count[i] ); } 795 796 virtual int Get_Majority (bool bWeighted = false) const; 797 virtual int Get_Minority (bool bWeighted = false) const; 798 799 800 protected: 801 802 bool m_bWeights; 803 804 CSG_Array_Int m_Count; 805 806 CSG_Vector m_Weight; 807 808 }; 809 810 //--------------------------------------------------------- 811 class SAGA_API_DLL_EXPORT CSG_Unique_Number_Statistics : public CSG_Unique_Value_Statistics 812 { 813 public: 814 CSG_Unique_Number_Statistics (bool bWeights = false) { Create(bWeights); } ~CSG_Unique_Number_Statistics(void)815 virtual ~CSG_Unique_Number_Statistics (void) {} 816 817 virtual void Create (bool bWeights = false); 818 819 void operator += (double Value) { Add_Value(Value); } 820 void Add_Value (double Value, double Weight = 1.); Get_Value(int Index)821 double Get_Value (int Index) const { return( m_Value[Index] ); } 822 int Get_Class_Index (double Value) const; Get_Class(int Index,double & Value,int & Count)823 bool Get_Class (int Index, double &Value, int &Count) const 824 { 825 if( Index < 0 || Index >= Get_Count() ) return( false ); 826 827 Count = m_Count[Index]; 828 Value = m_Value[Index]; 829 830 return( true ); 831 } 832 Get_Majority(double & Value)833 bool Get_Majority (double &Value ) const { int Count; return( Get_Class(CSG_Unique_Value_Statistics::Get_Majority(), Value, Count) ); } Get_Majority(double & Value,int & Count)834 bool Get_Majority (double &Value, int &Count) const { return( Get_Class(CSG_Unique_Value_Statistics::Get_Majority(), Value, Count) && Count > 0 ); } Get_Minority(double & Value)835 bool Get_Minority (double &Value ) const { int Count; return( Get_Class(CSG_Unique_Value_Statistics::Get_Minority(), Value, Count) ); } Get_Minority(double & Value,int & Count)836 bool Get_Minority (double &Value, int &Count) const { return( Get_Class(CSG_Unique_Value_Statistics::Get_Minority(), Value, Count) && Count > 0 ); } 837 838 839 private: 840 841 CSG_Vector m_Value; 842 843 }; 844 845 //--------------------------------------------------------- 846 class SAGA_API_DLL_EXPORT CSG_Unique_String_Statistics : public CSG_Unique_Value_Statistics 847 { 848 public: 849 CSG_Unique_String_Statistics (bool bWeights = false) { Create(bWeights); } ~CSG_Unique_String_Statistics(void)850 virtual ~CSG_Unique_String_Statistics (void) {} 851 852 virtual void Create (bool bWeights = false); 853 854 void operator += (const CSG_String &Value) { Add_Value(Value); } 855 void Add_Value (const CSG_String &Value, double Weight = 1.); Get_Value(int Index)856 const SG_Char * Get_Value (int Index) const { return( m_Value[Index] ); } 857 int Get_Class_Index (const CSG_String &Value) const; Get_Class(int Index,CSG_String & Value,int & Count)858 bool Get_Class (int Index, CSG_String &Value, int &Count) const 859 { 860 if( Index < 0 || Index >= Get_Count() ) return( false ); 861 862 Count = m_Count[Index]; 863 Value = m_Value[Index]; 864 865 return( true ); 866 } 867 Get_Majority(CSG_String & Value)868 bool Get_Majority (CSG_String &Value ) const { int Count; return( Get_Class(CSG_Unique_Value_Statistics::Get_Majority(), Value, Count) ); } Get_Majority(CSG_String & Value,int & Count)869 bool Get_Majority (CSG_String &Value, int &Count) const { return( Get_Class(CSG_Unique_Value_Statistics::Get_Majority(), Value, Count) && Count > 0 ); } Get_Minority(CSG_String & Value)870 bool Get_Minority (CSG_String &Value ) const { int Count; return( Get_Class(CSG_Unique_Value_Statistics::Get_Minority(), Value, Count) ); } Get_Minority(CSG_String & Value,int & Count)871 bool Get_Minority (CSG_String &Value, int &Count) const { return( Get_Class(CSG_Unique_Value_Statistics::Get_Minority(), Value, Count) && Count > 0 ); } 872 873 874 private: 875 876 CSG_Strings m_Value; 877 878 }; 879 880 881 /////////////////////////////////////////////////////////// 882 // // 883 // // 884 // // 885 /////////////////////////////////////////////////////////// 886 887 //--------------------------------------------------------- 888 class SAGA_API_DLL_EXPORT CSG_Category_Statistics 889 { 890 public: 891 CSG_Category_Statistics (TSG_Data_Type Type = SG_DATATYPE_String); 892 virtual ~CSG_Category_Statistics (void); 893 894 void Create (TSG_Data_Type Type = SG_DATATYPE_String); 895 void Destroy (void); 896 897 TSG_Data_Type Get_Category_Type (void) const; 898 899 int Add_Value (int Value); 900 int Add_Value (double Value); 901 int Add_Value (const CSG_String &Value); 902 903 void operator += (int Value) { Add_Value(Value); } 904 void operator += (double Value) { Add_Value(Value); } 905 void operator += (const CSG_String &Value) { Add_Value(Value); } 906 907 bool Sort (void); 908 909 int Get_Count (void ) const; 910 int Get_Count (int iCategory) const; 911 912 int asInt (int iCategory) const; 913 double asDouble (int iCategory) const; 914 CSG_String asString (int iCategory) const; 915 916 int Get_Category (int Value) const; 917 int Get_Category (double Value) const; 918 int Get_Category (const CSG_String &Value) const; 919 Get_Category(int iCategory,int & Value,int & Count)920 bool Get_Category (int iCategory, int &Value, int &Count) const 921 { 922 Count = Get_Count(iCategory); 923 Value = asInt (iCategory); 924 925 return( iCategory >= 0 && iCategory < Get_Count() ); 926 } 927 Get_Category(int iCategory,double & Value,int & Count)928 bool Get_Category (int iCategory, double &Value, int &Count) const 929 { 930 Count = Get_Count(iCategory); 931 Value = asDouble (iCategory); 932 933 return( iCategory >= 0 && iCategory < Get_Count() ); 934 } 935 Get_Category(int iCategory,CSG_String & Value,int & Count)936 bool Get_Category (int iCategory, CSG_String &Value, int &Count) const 937 { 938 Count = Get_Count(iCategory); 939 Value = asString (iCategory); 940 941 return( iCategory >= 0 && iCategory < Get_Count() ); 942 } 943 944 int Get_Majority (void); Get_Majority(int & Value)945 bool Get_Majority (int &Value ) { int Count; return( Get_Category(Get_Majority(), Value, Count) ); } Get_Majority(double & Value)946 bool Get_Majority (double &Value ) { int Count; return( Get_Category(Get_Majority(), Value, Count) ); } Get_Majority(CSG_String & Value)947 bool Get_Majority (CSG_String &Value ) { int Count; return( Get_Category(Get_Majority(), Value, Count) ); } Get_Majority(int & Value,int & Count)948 bool Get_Majority (int &Value, int &Count) { return( Get_Category(Get_Majority(), Value, Count) && Count > 0 ); } Get_Majority(double & Value,int & Count)949 bool Get_Majority (double &Value, int &Count) { return( Get_Category(Get_Majority(), Value, Count) && Count > 0 ); } Get_Majority(CSG_String & Value,int & Count)950 bool Get_Majority (CSG_String &Value, int &Count) { return( Get_Category(Get_Majority(), Value, Count) && Count > 0 ); } 951 952 int Get_Minority (void); Get_Minority(int & Value)953 bool Get_Minority (int &Value ) { int Count; return( Get_Category(Get_Minority(), Value, Count) ); } Get_Minority(double & Value)954 bool Get_Minority (double &Value ) { int Count; return( Get_Category(Get_Minority(), Value, Count) ); } Get_Minority(CSG_String & Value)955 bool Get_Minority (CSG_String &Value ) { int Count; return( Get_Category(Get_Minority(), Value, Count) ); } Get_Minority(int & Value,int & Count)956 bool Get_Minority (int &Value, int &Count) { return( Get_Category(Get_Minority(), Value, Count) && Count > 0 ); } Get_Minority(double & Value,int & Count)957 bool Get_Minority (double &Value, int &Count) { return( Get_Category(Get_Minority(), Value, Count) && Count > 0 ); } Get_Minority(CSG_String & Value,int & Count)958 bool Get_Minority (CSG_String &Value, int &Count) { return( Get_Category(Get_Minority(), Value, Count) && Count > 0 ); } 959 960 961 private: 962 963 class CSG_Table *m_pTable; 964 965 }; 966 967 968 /////////////////////////////////////////////////////////// 969 // // 970 // // 971 // // 972 /////////////////////////////////////////////////////////// 973 974 //--------------------------------------------------------- 975 class SAGA_API_DLL_EXPORT CSG_Histogram 976 { 977 public: 978 CSG_Histogram(void); 979 virtual ~CSG_Histogram(void); 980 981 bool Destroy (void); 982 983 CSG_Histogram (const CSG_Histogram &Histogram); 984 bool Create (const CSG_Histogram &Histogram); 985 986 CSG_Histogram (size_t nClasses, double Minimum, double Maximum); 987 bool Create (size_t nClasses, double Minimum, double Maximum); 988 989 CSG_Histogram (size_t nClasses, double Minimum, double Maximum, const CSG_Vector &Values , size_t maxSamples = 0); 990 bool Create (size_t nClasses, double Minimum, double Maximum, const CSG_Vector &Values , size_t maxSamples = 0); 991 992 CSG_Histogram (size_t nClasses, double Minimum, double Maximum, class CSG_Table *pTable, int Field, size_t maxSamples = 0); 993 bool Create (size_t nClasses, double Minimum, double Maximum, class CSG_Table *pTable, int Field, size_t maxSamples = 0); 994 995 CSG_Histogram (size_t nClasses, double Minimum, double Maximum, class CSG_Grid *pGrid , size_t maxSamples = 0); 996 bool Create (size_t nClasses, double Minimum, double Maximum, class CSG_Grid *pGrid , size_t maxSamples = 0); 997 998 CSG_Histogram (size_t nClasses, double Minimum, double Maximum, class CSG_Grids *pGrids , size_t maxSamples = 0); 999 bool Create (size_t nClasses, double Minimum, double Maximum, class CSG_Grids *pGrids , size_t maxSamples = 0); 1000 1001 //----------------------------------------------------- 1002 void Add_Value (double Value); 1003 1004 bool Scale_Element_Count (double Scale); 1005 1006 bool Update (void); 1007 Get_Class_Count(void)1008 size_t Get_Class_Count (void) const { return( m_nClasses ); } 1009 Get_Element_Count(void)1010 size_t Get_Element_Count (void) const { return( m_nClasses > 0 ? m_Cumulative[m_nClasses - 1] : 0 ); } Get_Element_Maximum(void)1011 size_t Get_Element_Maximum (void) const { return( m_nMaximum ); } 1012 Get_Elements(int i)1013 size_t Get_Elements (int i) const { return( Get_Elements((size_t)i) ); } Get_Elements(size_t i)1014 size_t Get_Elements (size_t i) const { return( i < m_nClasses ? m_Elements[i] : 0 ); } 1015 Get_Cumulative(int i)1016 size_t Get_Cumulative (int i) const { return( Get_Cumulative((size_t)i) ); } Get_Cumulative(size_t i)1017 size_t Get_Cumulative (size_t i) const { return( i < m_nClasses ? m_Cumulative[i] : 0 ); } 1018 Get_Value(double i)1019 double Get_Value (double i) const { return( m_nClasses < 1 ? m_Minimum : m_Minimum + i * m_ClassWidth ); } 1020 Get_Break(int i)1021 double Get_Break (int i) const { return( Get_Value((double)(i)) ); } Get_Break(size_t i)1022 double Get_Break (size_t i) const { return( Get_Value((double)(i)) ); } 1023 Get_Center(int i)1024 double Get_Center (int i) const { return( Get_Value((double)(i + 0.5)) ); } Get_Center(size_t i)1025 double Get_Center (size_t i) const { return( Get_Value((double)(i + 0.5)) ); } 1026 1027 //----------------------------------------------------- 1028 CSG_Histogram & operator = (const CSG_Histogram &Histogram); 1029 1030 void operator += (double Value) { Add_Value(Value); } 1031 1032 size_t operator [] (int i) const { return( Get_Elements(i) ); } 1033 size_t operator [] (size_t i) const { return( Get_Elements(i) ); } 1034 1035 double Get_Quantile (double Quantile) const; 1036 double Get_Quantile_Value (double Value) const; 1037 1038 double Get_Percentile (double Percentile) const; 1039 double Get_Percentile_Value(double Value) const; 1040 Get_Statistics(void)1041 const CSG_Simple_Statistics & Get_Statistics (void) const { return( m_Statistics ); } 1042 1043 1044 private: 1045 1046 size_t m_nClasses, m_nMaximum, *m_Elements, *m_Cumulative; 1047 1048 double m_Minimum, m_Maximum, m_ClassWidth; 1049 1050 CSG_Simple_Statistics m_Statistics; 1051 1052 1053 void _On_Construction (void); 1054 1055 bool _Create (size_t nClasses, double Minimum, double Maximum); 1056 1057 bool _Update (sLong nElements); 1058 1059 }; 1060 1061 1062 /////////////////////////////////////////////////////////// 1063 // // 1064 // // 1065 // // 1066 /////////////////////////////////////////////////////////// 1067 1068 //--------------------------------------------------------- 1069 class SAGA_API_DLL_EXPORT CSG_Natural_Breaks 1070 { 1071 public: 1072 CSG_Natural_Breaks(void); 1073 virtual ~CSG_Natural_Breaks(void); 1074 1075 CSG_Natural_Breaks (class CSG_Table *pTable, int Field, int nClasses, int Histogram = 0); 1076 bool Create (class CSG_Table *pTable, int Field, int nClasses, int Histogram = 0); 1077 1078 CSG_Natural_Breaks (class CSG_Grid *pGrid , int nClasses, int Histogram = 0); 1079 bool Create (class CSG_Grid *pGrid , int nClasses, int Histogram = 0); 1080 1081 CSG_Natural_Breaks (class CSG_Grids *pGrids , int nClasses, int Histogram = 0); 1082 bool Create (class CSG_Grids *pGrids , int nClasses, int Histogram = 0); 1083 1084 CSG_Natural_Breaks (const CSG_Vector &Values , int nClasses, int Histogram = 0); 1085 bool Create (const CSG_Vector &Values , int nClasses, int Histogram = 0); 1086 Get_Count(void)1087 int Get_Count (void) const { return( m_Breaks.Get_N() ); } Get_Break(int i)1088 double Get_Break (int i) const { return( m_Breaks[i] ); } 1089 1090 double operator [] (int i) const { return( m_Breaks[i] ); } 1091 1092 1093 private: 1094 1095 CSG_Histogram m_Histogram; 1096 1097 CSG_Vector m_Breaks, m_Values; 1098 1099 1100 bool _Histogram (int nClasses); 1101 1102 double _Get_Value (int i); 1103 1104 bool _Calculate (int nClasses); 1105 1106 }; 1107 1108 1109 /////////////////////////////////////////////////////////// 1110 // // 1111 // // 1112 // // 1113 /////////////////////////////////////////////////////////// 1114 1115 //--------------------------------------------------------- 1116 enum ESG_Cluster_Analysis_Method 1117 { 1118 SG_CLUSTERANALYSIS_Minimum_Distance = 0, 1119 SG_CLUSTERANALYSIS_Hill_Climbing, 1120 SG_CLUSTERANALYSIS_Combined 1121 }; 1122 1123 //--------------------------------------------------------- 1124 class SAGA_API_DLL_EXPORT CSG_Cluster_Analysis 1125 { 1126 public: 1127 CSG_Cluster_Analysis(void); 1128 virtual ~CSG_Cluster_Analysis(void); 1129 1130 bool Create (int nFeatures); 1131 bool Destroy (void); 1132 1133 bool Add_Element (void); 1134 bool Set_Feature (int iElement, int iFeature, double Value); 1135 Get_Cluster(int iElement)1136 int Get_Cluster (int iElement) const { return( iElement >= 0 && iElement < Get_nElements() ? m_Clusters[iElement] : -1 ); } 1137 1138 bool Execute (int Method, int nClusters, int nMaxIterations = 0, int Initialization = 0); 1139 Get_nElements(void)1140 int Get_nElements (void) const { return( (int)m_Features.Get_Size() ); } Get_nFeatures(void)1141 int Get_nFeatures (void) const { return( m_nFeatures ); } Get_nClusters(void)1142 int Get_nClusters (void) const { return( (int)m_nMembers.Get_Size() ); } 1143 Get_Iteration(void)1144 int Get_Iteration (void) const { return( m_Iteration ); } 1145 Get_SP(void)1146 double Get_SP (void) const { return( m_SP ); } 1147 Get_nMembers(int iCluster)1148 int Get_nMembers (int iCluster) const { return( m_nMembers[iCluster] ); } Get_Variance(int iCluster)1149 double Get_Variance (int iCluster) const { return( m_Variance[iCluster] ); } Get_Centroid(int iCluster,int iFeature)1150 double Get_Centroid (int iCluster, int iFeature) const { return( m_Centroid[iCluster][iFeature] ); } 1151 1152 1153 private: 1154 1155 int m_Iteration, m_nFeatures; 1156 1157 double m_SP; 1158 1159 CSG_Array_Int m_Clusters, m_nMembers; 1160 1161 CSG_Array m_Features; 1162 1163 CSG_Vector m_Variance; 1164 1165 CSG_Matrix m_Centroid; 1166 1167 1168 bool _Minimum_Distance (bool bInitialize, int nMaxIterations); 1169 1170 bool _Hill_Climbing (bool bInitialize, int nMaxIterations); 1171 1172 }; 1173 1174 1175 /////////////////////////////////////////////////////////// 1176 // // 1177 // // 1178 // // 1179 /////////////////////////////////////////////////////////// 1180 1181 //--------------------------------------------------------- 1182 enum ESG_Classify_Supervised 1183 { 1184 SG_CLASSIFY_SUPERVISED_BinaryEncoding = 0, 1185 SG_CLASSIFY_SUPERVISED_ParallelEpiped, 1186 SG_CLASSIFY_SUPERVISED_MinimumDistance, 1187 SG_CLASSIFY_SUPERVISED_Mahalonobis, 1188 SG_CLASSIFY_SUPERVISED_MaximumLikelihood, 1189 SG_CLASSIFY_SUPERVISED_SAM, 1190 SG_CLASSIFY_SUPERVISED_WTA, 1191 SG_CLASSIFY_SUPERVISED_SID, 1192 SG_CLASSIFY_SUPERVISED_SVM 1193 }; 1194 1195 //--------------------------------------------------------- 1196 class SAGA_API_DLL_EXPORT CSG_Classifier_Supervised 1197 { 1198 public: 1199 CSG_Classifier_Supervised(void); 1200 virtual ~CSG_Classifier_Supervised(void); 1201 1202 void Create (int nFeatures); 1203 void Destroy (void); 1204 1205 bool Load (const CSG_String &File); 1206 bool Save (const CSG_String &File, const SG_Char *Feature_Info = NULL); 1207 1208 bool Train_Clr_Samples (void); 1209 bool Train_Add_Sample (const CSG_String &Class_ID, const CSG_Vector &Features); 1210 bool Train (bool bClr_Samples = false); 1211 1212 bool Add_Class (const CSG_String &Class_ID, const CSG_Vector &Mean, const CSG_Vector &Min, const CSG_Vector &Max, const CSG_Matrix &Cov); 1213 1214 CSG_String Print (void); 1215 1216 //----------------------------------------------------- Get_Feature_Count(void)1217 int Get_Feature_Count (void) { return( m_nFeatures ); } 1218 Get_Class_Count(void)1219 int Get_Class_Count (void) { return( m_nClasses ); } 1220 Get_Class_ID(int iClass)1221 const CSG_String & Get_Class_ID (int iClass) { return( m_pClasses[iClass]->m_ID ); } Get_Class_Mean(int iClass,int iFeature)1222 double Get_Class_Mean (int iClass, int iFeature) { return( m_pClasses[iClass]->m_Mean[iFeature] ); } Get_Class_Minimum(int iClass,int iFeature)1223 double Get_Class_Minimum (int iClass, int iFeature) { return( m_pClasses[iClass]->m_Min [iFeature] ); } Get_Class_Maximum(int iClass,int iFeature)1224 double Get_Class_Maximum (int iClass, int iFeature) { return( m_pClasses[iClass]->m_Max [iFeature] ); } 1225 1226 int Get_Class (const CSG_String &Class_ID); 1227 bool Get_Class (const CSG_Vector &Features, int &Class, double &Quality, int Method); 1228 1229 //----------------------------------------------------- 1230 void Set_Threshold_Distance (double Value); 1231 double Get_Threshold_Distance (void); 1232 1233 void Set_Threshold_Angle (double Value); 1234 double Get_Threshold_Angle (void); 1235 1236 void Set_Threshold_Probability (double Value); 1237 double Get_Threshold_Probability (void); 1238 1239 void Set_Probability_Relative (bool Value); 1240 bool Get_Probability_Relative (void); 1241 1242 void Set_WTA (int Method, bool bOn); 1243 bool Get_WTA (int Method); 1244 1245 static CSG_String Get_Name_of_Method (int Method); 1246 static CSG_String Get_Name_of_Quality (int Method); 1247 1248 1249 private: 1250 1251 //----------------------------------------------------- 1252 class CClass 1253 { 1254 public: CClass(const CSG_String & ID)1255 CClass(const CSG_String &ID) : m_ID(ID) {} 1256 1257 CSG_String m_ID; 1258 1259 double m_Cov_Det, m_Mean_Spectral; 1260 1261 CSG_Vector m_Mean, m_Min, m_Max; 1262 1263 CSG_Matrix m_Cov, m_Cov_Inv, m_Samples; 1264 1265 1266 bool Train (void); 1267 1268 }; 1269 1270 //----------------------------------------------------- 1271 bool m_Probability_Relative, m_bWTA[SG_CLASSIFY_SUPERVISED_WTA]; 1272 1273 int m_nFeatures, m_nClasses; 1274 1275 double m_Threshold_Distance, m_Threshold_Probability, m_Threshold_Angle; 1276 1277 CSG_String m_Info; 1278 1279 CClass **m_pClasses; 1280 1281 1282 void _Get_Binary_Encoding (const CSG_Vector &Features, int &Class, double &Quality); 1283 void _Get_Parallel_Epiped (const CSG_Vector &Features, int &Class, double &Quality); 1284 void _Get_Minimum_Distance (const CSG_Vector &Features, int &Class, double &Quality); 1285 void _Get_Mahalanobis_Distance (const CSG_Vector &Features, int &Class, double &Quality); 1286 void _Get_Maximum_Likelihood (const CSG_Vector &Features, int &Class, double &Quality); 1287 void _Get_Spectral_Angle_Mapping (const CSG_Vector &Features, int &Class, double &Quality); 1288 void _Get_Spectral_Divergence (const CSG_Vector &Features, int &Class, double &Quality); 1289 void _Get_Winner_Takes_All (const CSG_Vector &Features, int &Class, double &Quality); 1290 1291 }; 1292 1293 1294 /////////////////////////////////////////////////////////// 1295 // // 1296 // // 1297 // // 1298 /////////////////////////////////////////////////////////// 1299 1300 //--------------------------------------------------------- 1301 #ifdef WITH_MRMR 1302 1303 //--------------------------------------------------------- 1304 enum ESG_mRMR_Method 1305 { 1306 SG_mRMR_Method_MID = 0, // Mutual Information Difference (MID) 1307 SG_mRMR_Method_MIQ // Mutual Information Quotient (MIQ) 1308 }; 1309 1310 //--------------------------------------------------------- 1311 class SAGA_API_DLL_EXPORT CSG_mRMR 1312 { 1313 public: ////// public members and functions: ////////////// 1314 1315 CSG_mRMR(void); 1316 virtual ~CSG_mRMR(void); 1317 1318 void Destroy (void); 1319 1320 static CSG_String Get_Description (void); 1321 1322 static bool Parameters_Add (class CSG_Parameters *pParameters, class CSG_Parameter *pNode = NULL); 1323 static int Parameters_Enable (class CSG_Parameters *pParameters, class CSG_Parameter *pParameter); 1324 1325 void Set_Verbose (bool bOn = true) { m_bVerbose = bOn; } 1326 1327 bool Set_Data (CSG_Table &Data, int ClassField, class CSG_Parameters *pParameters); 1328 bool Set_Data (CSG_Matrix &Data, int ClassField, class CSG_Parameters *pParameters); 1329 bool Set_Data (CSG_Table &Data, int ClassField = 0, double Threshold = -1.); 1330 bool Set_Data (CSG_Matrix &Data, int ClassField = 0, double Threshold = -1.); 1331 1332 bool Get_Selection (class CSG_Parameters *pParameters); 1333 bool Get_Selection (int nFeatures, int Method); 1334 1335 int Get_Count (void) const; 1336 int Get_Index (int i) const; 1337 CSG_String Get_Name (int i) const; 1338 double Get_Score (int i) const; 1339 1340 1341 private: ///// private members and functions: ///////////// 1342 1343 bool m_bDiscretized, m_bVerbose; 1344 1345 long m_nSamples, m_nVars; 1346 1347 double **m_Samples; 1348 1349 CSG_Strings m_VarNames; 1350 1351 class CSG_Table *m_pSelection; 1352 1353 1354 bool Get_Memory (int nVars, int nSamples); 1355 1356 bool Discretize (double Threshold); 1357 1358 double Get_MutualInfo (long v1, long v2); 1359 double Get_MutualInfo (double *pab, long pabhei, long pabwid); 1360 1361 template <class T> double * Get_JointProb (T *img1, T *img2, long len, long maxstatenum, int &nstate1, int &nstate2); 1362 template <class T> void Copy_Vector (T *srcdata, long len, int *desdata, int &nstate); 1363 1364 1365 typedef struct SPool 1366 { 1367 char Mask; 1368 long Index; 1369 double mival; 1370 } 1371 TPool; 1372 1373 static int Pool_Compare (const void *a, const void *b); 1374 1375 }; 1376 1377 //--------------------------------------------------------- 1378 #endif // WITH_MRMR 1379 1380 1381 /////////////////////////////////////////////////////////// 1382 // // 1383 // // 1384 // // 1385 /////////////////////////////////////////////////////////// 1386 1387 //--------------------------------------------------------- 1388 class SAGA_API_DLL_EXPORT CSG_Spline 1389 { 1390 public: 1391 CSG_Spline(void); 1392 virtual ~CSG_Spline(void); 1393 1394 void Destroy (void); 1395 1396 bool Create (double *xValues, double *yValues, int nValues, double yA = 1.0e30, double yB = 1.0e30); 1397 bool Create (double yA = 1.0e30, double yB = 1.0e30); 1398 1399 void Add (double x, double y); 1400 Get_Count(void)1401 int Get_Count (void) const { return( m_x.Get_N() ); } Get_xMin(void)1402 double Get_xMin (void) const { return( m_x.Get_N() > 0 ? m_x(0 ) : 0. ); } Get_xMax(void)1403 double Get_xMax (void) const { return( m_x.Get_N() > 0 ? m_x(m_x.Get_N() - 1) : 0. ); } Get_x(int i)1404 double Get_x (int i) const { return( i >= 0 && i < m_x.Get_N() ? m_x(i) : 0. ); } Get_y(int i)1405 double Get_y (int i) const { return( i >= 0 && i < m_y.Get_N() ? m_y(i) : 0. ); } 1406 1407 bool Get_Value (double x, double &y); 1408 double Get_Value (double x); 1409 1410 1411 protected: 1412 1413 bool m_bCreated; 1414 1415 CSG_Vector m_x, m_y, m_z; 1416 1417 1418 bool _Create (double yA, double yB); 1419 1420 }; 1421 1422 //--------------------------------------------------------- 1423 class SAGA_API_DLL_EXPORT CSG_Thin_Plate_Spline 1424 { 1425 public: 1426 CSG_Thin_Plate_Spline(void); 1427 virtual ~CSG_Thin_Plate_Spline(void); 1428 1429 bool Destroy (void); 1430 Set_Point_Count(int Count)1431 bool Set_Point_Count (int Count) { return( m_Points.Set_Count(Count) ); } Get_Point_Count(void)1432 int Get_Point_Count (void) { return( m_Points.Get_Count() ); } 1433 Get_Points(void)1434 CSG_Points_Z & Get_Points (void) { return( m_Points ); } 1435 Add_Point(double x,double y,double z)1436 bool Add_Point (double x, double y, double z) { return( m_Points.Add( x, y, z) ); } Add_Point(const TSG_Point & p,double z)1437 bool Add_Point (const TSG_Point &p, double z) { return( m_Points.Add(p.x, p.y, z) ); } 1438 Set_Point(int Index,double x,double y,double z)1439 bool Set_Point (int Index, double x, double y, double z) 1440 { 1441 if( Index >= 0 && Index < m_Points.Get_Count() ) 1442 { 1443 m_Points[Index].x = x; 1444 m_Points[Index].y = y; 1445 m_Points[Index].z = z; 1446 1447 return( true ); 1448 } 1449 1450 return( false ); 1451 } 1452 Set_Point(int Index,const TSG_Point & p,double z)1453 bool Set_Point (int Index, const TSG_Point &p, double z) { return( Set_Point(Index, p.x, p.y, z) ); } 1454 1455 bool Create (double Regularization = 0., bool bSilent = true); 1456 is_Okay(void)1457 bool is_Okay (void) { return( m_V.Get_N() > 0 ); } 1458 1459 double Get_Value (double x, double y); 1460 1461 1462 private: 1463 1464 CSG_Points_Z m_Points; 1465 1466 CSG_Vector m_V; 1467 1468 1469 double _Get_hDistance (TSG_Point_Z A, TSG_Point_Z B); 1470 double _Get_Base_Funtion (double x); 1471 double _Get_Base_Funtion (TSG_Point_Z A, double x, double y); 1472 1473 }; 1474 1475 1476 /////////////////////////////////////////////////////////// 1477 // // 1478 // // 1479 // // 1480 /////////////////////////////////////////////////////////// 1481 1482 //--------------------------------------------------------- 1483 typedef enum ESG_Test_Distribution_Type 1484 { 1485 TESTDIST_TYPE_Left = 0, 1486 TESTDIST_TYPE_Right, 1487 TESTDIST_TYPE_Middle, 1488 TESTDIST_TYPE_TwoTail 1489 } 1490 TSG_Test_Distribution_Type; 1491 1492 //--------------------------------------------------------- 1493 class SAGA_API_DLL_EXPORT CSG_Test_Distribution 1494 { 1495 public: 1496 1497 static double Get_F_Tail_from_R2 (double R2, int nPredictors, int nSamples, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 1498 1499 static double Get_F_Tail (double F , int dfn, int dfd, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 1500 static double Get_F_Inverse (double alpha, int dfn, int dfd, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 1501 1502 static double Get_T_Tail (double T , int df, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 1503 static double Get_T_Inverse (double alpha, int df, TSG_Test_Distribution_Type Type = TESTDIST_TYPE_Right); 1504 1505 static double Get_Norm_P (double Z); 1506 static double Get_Norm_Z (double P); 1507 1508 1509 private: 1510 1511 static double Get_Gamma (double F, double dfn, double dfd); 1512 static double Get_Log_Gamma (double a); 1513 1514 static double Get_T_P (double T, int df); 1515 static double Get_T_Z (double T, int df); 1516 static double Get_T_Inv (double p, int df); 1517 1518 static double _Change_Tail_Type (double p, TSG_Test_Distribution_Type from, TSG_Test_Distribution_Type to, bool bNegative); 1519 1520 }; 1521 1522 1523 /////////////////////////////////////////////////////////// 1524 // // 1525 // // 1526 // // 1527 /////////////////////////////////////////////////////////// 1528 1529 //--------------------------------------------------------- 1530 // Values: (matrix) array with number of variables = number of rows (x), number of samples = number of columns (y) 1531 SAGA_API_DLL_EXPORT CSG_Matrix SG_Get_Correlation_Matrix (const CSG_Matrix &Values, bool bCovariances = false); 1532 1533 1534 /////////////////////////////////////////////////////////// 1535 // // 1536 // // 1537 // // 1538 /////////////////////////////////////////////////////////// 1539 1540 //--------------------------------------------------------- 1541 typedef enum ESG_Regression_Correction 1542 { 1543 REGRESSION_CORR_None = 0, 1544 REGRESSION_CORR_Smith, 1545 REGRESSION_CORR_Wherry_1, 1546 REGRESSION_CORR_Wherry_2, 1547 REGRESSION_CORR_Olkin_Pratt, 1548 REGRESSION_CORR_Pratt, 1549 REGRESSION_CORR_Claudy_3 1550 } 1551 TSG_Regression_Correction; 1552 1553 //--------------------------------------------------------- 1554 SAGA_API_DLL_EXPORT double SG_Regression_Get_Adjusted_R2 (double R2, int nSamples, int nPredictors, TSG_Regression_Correction Correction = REGRESSION_CORR_Wherry_1); 1555 1556 1557 /////////////////////////////////////////////////////////// 1558 // // 1559 /////////////////////////////////////////////////////////// 1560 1561 //--------------------------------------------------------- 1562 typedef enum ESG_Regression_Type 1563 { 1564 REGRESSION_Linear = 0, // Y = a + b * X 1565 REGRESSION_Rez_X, // Y = a + b / X 1566 REGRESSION_Rez_Y, // Y = a / (b - X) 1567 REGRESSION_Pow, // Y = a * X^b 1568 REGRESSION_Exp, // Y = a * e^(b * X) 1569 REGRESSION_Log // Y = a + b * ln(X) 1570 } 1571 TSG_Regression_Type; 1572 1573 //--------------------------------------------------------- 1574 class SAGA_API_DLL_EXPORT CSG_Regression 1575 { 1576 public: 1577 CSG_Regression(void); 1578 virtual ~CSG_Regression(void); 1579 1580 void Destroy (void); 1581 1582 void Set_Values (int nValues, double *x, double *y); 1583 void Add_Values (double x, double y); 1584 Get_Count(void)1585 int Get_Count (void) const { return( m_nValues ); } 1586 Get_xValue(int iValue)1587 double Get_xValue (int iValue) const { return( iValue >= 0 && iValue < m_nValues ? m_x[iValue] : 0. ); } Get_yValue(int iValue)1588 double Get_yValue (int iValue) const { return( iValue >= 0 && iValue < m_nValues ? m_y[iValue] : 0. ); } Get_Values(int iValue,double & x,double & y)1589 bool Get_Values (int iValue, double &x, double &y) const 1590 { 1591 if( iValue >= 0 && iValue < m_nValues ) 1592 { 1593 x = m_x[iValue]; 1594 y = m_y[iValue]; 1595 1596 return( true ); 1597 } 1598 1599 return( false ); 1600 } 1601 Get_xMin(void)1602 double Get_xMin (void) const { return( m_xMin ); } Get_xMax(void)1603 double Get_xMax (void) const { return( m_xMax ); } Get_xMean(void)1604 double Get_xMean (void) const { return( m_xMean ); } Get_xVariance(void)1605 double Get_xVariance (void) const { return( m_xVar ); } 1606 double Get_x (double y) const; // returns INF on error, this can be checked using the _finite() function (libc, include <float.h>)... 1607 Get_yMin(void)1608 double Get_yMin (void) const { return( m_yMin ); } Get_yMax(void)1609 double Get_yMax (void) const { return( m_yMax ); } Get_yMean(void)1610 double Get_yMean (void) const { return( m_yMean ); } Get_yVariance(void)1611 double Get_yVariance (void) const { return( m_yVar ); } 1612 double Get_y (double x) const; // returns INF on error, this can be checked using the _finite() function (libc, include <float.h>)... 1613 Get_Constant(void)1614 double Get_Constant (void) const { return( m_RConst ); } Get_Coefficient(void)1615 double Get_Coefficient (void) const { return( m_RCoeff ); } Get_R(void)1616 double Get_R (void) const { return( m_R ); } Get_R2(void)1617 double Get_R2 (void) const { return( m_R*m_R ); } 1618 1619 const SG_Char * asString (void); 1620 Get_Type(void)1621 TSG_Regression_Type Get_Type (void) const { return( m_Type ); } 1622 1623 bool Calculate (TSG_Regression_Type Type = REGRESSION_Linear); 1624 bool Calculate (int nValues, double *x, double *y, TSG_Regression_Type Type = REGRESSION_Linear); 1625 1626 1627 protected: 1628 1629 int m_nValues, m_nBuffer; 1630 1631 double m_RConst, m_RCoeff, m_R, 1632 m_xMin, m_xMax, m_xMean, m_xVar, *m_x, 1633 m_yMin, m_yMax, m_yMean, m_yVar, *m_y; 1634 1635 TSG_Regression_Type m_Type; 1636 1637 1638 bool _Get_MinMeanMax (double &xMin, double &xMean, double &xMax, double &yMin, double &yMean, double &yMax); 1639 1640 double _Y_Transform (double x); 1641 double _X_Transform (double y); 1642 1643 bool _Linear (void); 1644 1645 }; 1646 1647 1648 /////////////////////////////////////////////////////////// 1649 // // 1650 /////////////////////////////////////////////////////////// 1651 1652 //--------------------------------------------------------- 1653 enum ESG_Multiple_Regression_Info_Vars 1654 { 1655 MLR_VAR_ID = 0, 1656 MLR_VAR_NAME, 1657 MLR_VAR_RCOEFF, 1658 MLR_VAR_R, 1659 MLR_VAR_R2, 1660 MLR_VAR_R2_ADJ, 1661 MLR_VAR_SE, 1662 MLR_VAR_T, 1663 MLR_VAR_SIG, 1664 MLR_VAR_P 1665 }; 1666 1667 //--------------------------------------------------------- 1668 class SAGA_API_DLL_EXPORT CSG_Regression_Multiple 1669 { 1670 public: 1671 CSG_Regression_Multiple(bool bIntercept = true); 1672 virtual ~CSG_Regression_Multiple(void); 1673 1674 void Destroy (void); 1675 1676 bool Set_Data (const CSG_Matrix &Samples, CSG_Strings *pNames = NULL); 1677 1678 void Set_With_Intercept (bool bOn = true) { m_bIntercept = bOn; } Get_With_Intercept(void)1679 bool Get_With_Intercept (void) const { return( m_bIntercept ); } 1680 1681 bool Get_Model (const CSG_Matrix &Samples , CSG_Strings *pNames = NULL); 1682 bool Get_Model_Forward (const CSG_Matrix &Samples, double P_in , CSG_Strings *pNames = NULL); 1683 bool Get_Model_Backward (const CSG_Matrix &Samples, double P_out , CSG_Strings *pNames = NULL); 1684 bool Get_Model_Stepwise (const CSG_Matrix &Samples, double P_in, double P_out, CSG_Strings *pNames = NULL); 1685 1686 bool Get_Model (void); 1687 bool Get_Model_Forward (double P_in); 1688 bool Get_Model_Backward (double P_out); 1689 bool Get_Model_Stepwise (double P_in, double P_out); 1690 1691 bool Get_CrossValidation (int nSubSamples = 0); 1692 1693 CSG_String Get_Info (void) const; Get_Info_Regression(void)1694 class CSG_Table * Get_Info_Regression (void) const { return( m_pRegression ); } Get_Info_Model(void)1695 class CSG_Table * Get_Info_Model (void) const { return( m_pModel ); } Get_Info_Steps(void)1696 class CSG_Table * Get_Info_Steps (void) const { return( m_pSteps ); } 1697 1698 double Get_R2 (void) const; 1699 double Get_R2_Adj (void) const; 1700 double Get_StdError (void) const; 1701 double Get_F (void) const; 1702 double Get_P (void) const; 1703 double Get_CV_RMSE (void) const; 1704 double Get_CV_NRMSE (void) const; 1705 double Get_CV_R2 (void) const; 1706 int Get_CV_nSamples (void) const; 1707 int Get_DegFreedom (void) const; 1708 int Get_nSamples (void) const; 1709 int Get_nPredictors (void) const; Get_Predictor(int i)1710 int Get_Predictor (int i) const { return( i >= 0 && i < Get_nPredictors() ? m_Predictor[i] : -1 ); } 1711 1712 double Get_RConst (void) const; 1713 const SG_Char * Get_Name (int iVariable) const; Get_ID(int iVariable)1714 double Get_ID (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_ID ) ); } Get_RCoeff(int iVariable)1715 double Get_RCoeff (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_RCOEFF) ); } Get_R2_Partial(int iVariable)1716 double Get_R2_Partial (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_R2 ) ); } Get_R2_Partial_Adj(int iVariable)1717 double Get_R2_Partial_Adj (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_R2_ADJ) ); } Get_StdError(int iVariable)1718 double Get_StdError (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_SE ) ); } Get_T(int iVariable)1719 double Get_T (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_T ) ); } Get_P(int iVariable)1720 double Get_P (int iVariable) const { return( Get_Parameter(iVariable, MLR_VAR_SIG ) ); } 1721 1722 double Get_Parameter (int iVariable, int Parameter) const; 1723 1724 double Get_Value (const CSG_Vector &Predictors) const; 1725 bool Get_Value (const CSG_Vector &Predictors, double &Value) const; 1726 1727 double Get_Residual (int iSample) const; 1728 bool Get_Residual (int iSample, double &Residual) const; 1729 1730 bool Get_Residuals (CSG_Vector &Residuals) const; 1731 1732 1733 protected: 1734 1735 bool m_bIntercept; 1736 1737 int *m_bIncluded, *m_Predictor, m_nPredictors; 1738 1739 CSG_Strings m_Names; 1740 1741 CSG_Matrix m_Samples, m_Samples_Model; 1742 1743 class CSG_Table *m_pRegression, *m_pModel, *m_pSteps; 1744 1745 1746 bool _Initialize (bool bInclude); 1747 1748 double _Get_F (int nPredictors, int nSamples, double r2_full, double r2_reduced); 1749 double _Get_P (int nPredictors, int nSamples, double r2_full, double r2_reduced); 1750 1751 bool _Get_Regression (const class CSG_Matrix &Samples); 1752 1753 int _Get_Step_In (CSG_Matrix &X, double P_in , double &R2, const CSG_Matrix &Samples); 1754 int _Get_Step_Out (CSG_Matrix &X, double P_out, double &R2); 1755 1756 bool _Set_Step_Info (const CSG_Matrix &X); 1757 bool _Set_Step_Info (const CSG_Matrix &X, double R2_prev, int iVariable, bool bIn); 1758 1759 }; 1760 1761 1762 /////////////////////////////////////////////////////////// 1763 // // 1764 /////////////////////////////////////////////////////////// 1765 1766 //--------------------------------------------------------- 1767 class SAGA_API_DLL_EXPORT CSG_Regression_Weighted 1768 { 1769 public: 1770 CSG_Regression_Weighted(void); 1771 virtual ~CSG_Regression_Weighted(void); 1772 1773 bool Destroy (void); 1774 1775 bool Add_Sample (double Weight, double Dependent, const CSG_Vector &Predictors); Get_Sample_Count(void)1776 int Get_Sample_Count (void) const { return( m_X.Get_NRows() ); } Get_Predictor_Count(void)1777 int Get_Predictor_Count (void) const { return( m_X.Get_NCols() - 1 ); } 1778 1779 bool Calculate (const CSG_Vector &Weights, const CSG_Vector &Dependents, const CSG_Matrix &Predictors, bool bLogistic = false); 1780 bool Calculate (bool bLogistic = false); 1781 Get_R2(void)1782 double Get_R2 (void) const { return( m_r2 ); } Get_RCoeff(void)1783 const CSG_Vector & Get_RCoeff (void) const { return( m_b ); } Get_RCoeff(int i)1784 double Get_RCoeff (int i) const { return( m_b[i] ); } 1785 double operator [] (int i) const { return( m_b[i] ); } 1786 1787 //----------------------------------------------------- Get_Log_maxIter(void)1788 int Get_Log_maxIter (void) const { return( m_Log_maxIter ); } Get_Log_Epsilon(void)1789 double Get_Log_Epsilon (void) const { return( m_Log_Epsilon ); } Get_Log_Difference(void)1790 double Get_Log_Difference (void) const { return( m_Log_Difference ); } 1791 1792 bool Set_Log_maxIter (int maxIter ); 1793 bool Set_Log_Epsilon (double Epsilon ); 1794 bool Set_Log_Difference (double Difference); 1795 1796 //----------------------------------------------------- 1797 bool Get_CrossValidation (int nSubSamples = 0); 1798 Get_CV_nSamples(void)1799 int Get_CV_nSamples (void) const { return( m_CV_nSamples ); } Get_CV_RMSE(void)1800 double Get_CV_RMSE (void) const { return( m_CV_RMSE ); } Get_CV_NRMSE(void)1801 double Get_CV_NRMSE (void) const { return( m_CV_NRMSE ); } Get_CV_R2(void)1802 double Get_CV_R2 (void) const { return( m_CV_R2 ); } 1803 1804 1805 private: 1806 1807 int m_Log_maxIter, m_CV_nSamples; 1808 1809 double m_r2, m_Log_Epsilon, m_Log_Difference, m_CV_RMSE, m_CV_NRMSE, m_CV_R2; 1810 1811 CSG_Vector m_y, m_w, m_b; 1812 1813 CSG_Matrix m_X; 1814 1815 1816 CSG_Vector _Log_Get_Beta (const CSG_Matrix &X, const CSG_Vector &y, const CSG_Vector &w); 1817 CSG_Vector _Log_Get_Beta (const CSG_Vector &b, const CSG_Matrix &X, const CSG_Vector &y, const CSG_Vector &w, const CSG_Vector &p); 1818 CSG_Matrix _Log_Get_Xwp (const CSG_Vector &p, const CSG_Matrix &X, const CSG_Vector &w); 1819 CSG_Vector _Log_Get_Ywp (const CSG_Vector &p, const CSG_Vector &y, const CSG_Vector &w); 1820 CSG_Vector _Log_Get_Props (const CSG_Matrix &X, const CSG_Vector &b); 1821 bool _Log_NoChange (const CSG_Vector &b_old, const CSG_Vector &b_new); 1822 bool _Log_OutOfControl (const CSG_Vector &b_old, const CSG_Vector &b_new); 1823 1824 }; 1825 1826 1827 /////////////////////////////////////////////////////////// 1828 // // 1829 // Formula Parser (A. Ringeler) // 1830 // // 1831 /////////////////////////////////////////////////////////// 1832 1833 //--------------------------------------------------------- 1834 typedef double (*TSG_Formula_Function_0)(void); 1835 typedef double (*TSG_Formula_Function_1)(double); 1836 typedef double (*TSG_Formula_Function_2)(double, double); 1837 typedef double (*TSG_Formula_Function_3)(double, double, double); 1838 1839 //--------------------------------------------------------- 1840 class SAGA_API_DLL_EXPORT CSG_Formula 1841 { 1842 public: 1843 CSG_Formula(void); 1844 virtual ~CSG_Formula(void); 1845 1846 bool Destroy (void); 1847 1848 static CSG_String Get_Help_Operators (bool bHTML = true, const CSG_String Additional[][2] = NULL); 1849 1850 bool Get_Error (CSG_String &Message); 1851 1852 bool Add_Function (const char *Name, TSG_Formula_Function_1 Function, int nParameters, bool bVarying = false); 1853 1854 bool Set_Formula (const CSG_String &Formula); Get_Formula(void)1855 CSG_String Get_Formula (void) const { return( m_sFormula ); } 1856 1857 void Set_Variable (char Variable, double Value); 1858 1859 double Get_Value (void ) const; 1860 double Get_Value (double x ) const; 1861 double Get_Value (const CSG_Vector &Values ) const; 1862 double Get_Value (double *Values, int nValues) const; 1863 double Get_Value (const char *Arguments, ... ) const; 1864 1865 const char * Get_Used_Variables (void); 1866 1867 1868 //----------------------------------------------------- 1869 typedef struct SSG_Function 1870 { 1871 const char *Name; 1872 1873 TSG_Formula_Function_1 Function; 1874 1875 int nParameters; 1876 1877 bool bVarying; 1878 } 1879 TSG_Function; 1880 1881 1882 private: 1883 1884 //----------------------------------------------------- 1885 typedef struct SSG_Formula 1886 { 1887 char *code; 1888 1889 double *ctable; 1890 } 1891 TSG_Formula; 1892 1893 1894 //----------------------------------------------------- 1895 bool m_bError, m_Vars_Used[256]; 1896 1897 int m_Error_Position, m_Length; 1898 1899 TSG_Formula m_Formula; 1900 1901 TSG_Function *m_Functions; 1902 1903 1904 CSG_String m_sFormula, m_sError; 1905 1906 const char *m_error; 1907 1908 int m_pctable; 1909 1910 double m_Parameters[32], *m_ctable; 1911 1912 1913 void _Set_Error (const CSG_String &Error = ""); 1914 1915 double _Get_Value (const double *Parameters, TSG_Formula Function) const; 1916 1917 int _is_Operand (char c); 1918 int _is_Operand_Code (char c); 1919 int _is_Number (char c); 1920 1921 int _Get_Function (int i, char *Name, int *nParameters, int *bVarying); 1922 int _Get_Function (const char *Name); 1923 1924 TSG_Formula _Translate (const char *source, const char *args, int *length, int *error); 1925 1926 char * _i_trans (char *function, char *begin, char *end); 1927 char * _comp_time (char *function, char *fend, int npars); 1928 int _max_size (const char *source); 1929 char * _my_strtok (char *s); 1930 1931 }; 1932 1933 1934 /////////////////////////////////////////////////////////// 1935 // // 1936 // // 1937 // // 1938 /////////////////////////////////////////////////////////// 1939 1940 //--------------------------------------------------------- 1941 enum ESG_Trend_String 1942 { 1943 SG_TREND_STRING_Formula = 0, 1944 SG_TREND_STRING_Function, 1945 SG_TREND_STRING_Formula_Parameters, 1946 SG_TREND_STRING_Complete, 1947 SG_TREND_STRING_Compact 1948 }; 1949 1950 //--------------------------------------------------------- 1951 class SAGA_API_DLL_EXPORT CSG_Trend 1952 { 1953 public: 1954 CSG_Trend(void); 1955 1956 bool Set_Formula (const CSG_String &Formula); 1957 CSG_String Get_Formula (int Type = SG_TREND_STRING_Complete); 1958 Get_Parameter_Count(void)1959 int Get_Parameter_Count (void) const { return( m_Params .Get_Count() ); } Get_Parameters(void)1960 double * Get_Parameters (void) const { return( m_Params.m_A.Get_Data() ); } 1961 bool Init_Parameter (const SG_Char &Variable, double Value); 1962 1963 void Clr_Data (void); 1964 bool Add_Data (double x, double y); 1965 void Set_Data (double *x, double *y, int n, bool bAdd = false); 1966 void Set_Data (const CSG_Points &Data , bool bAdd = false); Get_Data_Count(void)1967 int Get_Data_Count (void) const { return( (int)m_Data[0].Get_Count() ); } Get_Data_X(int i)1968 double Get_Data_X (int i) const { return( m_Data[0][i] ); } Get_Data_XMin(void)1969 double Get_Data_XMin (void) { return( m_Data[0].Get_Minimum() ); } Get_Data_XMax(void)1970 double Get_Data_XMax (void) { return( m_Data[0].Get_Maximum() ); } Get_Data_XStats(void)1971 CSG_Simple_Statistics & Get_Data_XStats (void) { return( m_Data[0] ); } Get_Data_Y(int i)1972 double Get_Data_Y (int i) const { return( m_Data[1][i] ); } Get_Data_YMin(void)1973 double Get_Data_YMin (void) { return( m_Data[1].Get_Minimum() ); } Get_Data_YMax(void)1974 double Get_Data_YMax (void) { return( m_Data[1].Get_Maximum() ); } Get_Data_YStats(void)1975 CSG_Simple_Statistics & Get_Data_YStats (void) { return( m_Data[1] ); } 1976 1977 bool Set_Max_Iterations (int Iterations); Get_Max_Iterations(void)1978 int Get_Max_Iterations (void) const { return( m_Iter_Max); } 1979 bool Set_Max_Lambda (double Lambda); Get_Max_Lambda(void)1980 double Get_Max_Lambda (void) const { return( m_Lambda_Max); } 1981 1982 bool Get_Trend (double *x, double *y, int n, const CSG_String &Formula); 1983 bool Get_Trend (const CSG_Points &Data , const CSG_String &Formula); 1984 bool Get_Trend (const CSG_String &Formula); 1985 bool Get_Trend (void); 1986 is_Okay(void)1987 bool is_Okay (void) const { return( m_bOkay ); } 1988 1989 CSG_String Get_Error (void); 1990 Get_ChiSquare(void)1991 double Get_ChiSquare (void) const { return( m_bOkay ? m_ChiSqr : 0. ); } Get_R2(void)1992 double Get_R2 (void) const { return( m_bOkay ? m_ChiSqr_o : 0. ); } 1993 Get_Value(double x)1994 double Get_Value (double x) const { return( m_bOkay ? m_Formula.Get_Value(x) : 0. ); } 1995 1996 1997 private: 1998 1999 //----------------------------------------------------- 2000 class SAGA_API_DLL_EXPORT CParams 2001 { 2002 public: CParams(void)2003 CParams(void) {} 2004 2005 bool Create (const CSG_String &Variables); 2006 bool Destroy (void); 2007 Get_Count(void)2008 int Get_Count (void) const { return( (int)m_Variables.Length() ); } 2009 2010 2011 CSG_String m_Variables; 2012 2013 CSG_Vector m_A, m_Atry, m_dA, m_dA2, m_Beta; 2014 2015 CSG_Matrix m_Alpha, m_Covar; 2016 2017 }; 2018 2019 2020 //----------------------------------------------------- 2021 bool m_bOkay; 2022 2023 int m_Iter_Max; 2024 2025 double m_ChiSqr, m_ChiSqr_o, m_Lambda, m_Lambda_Max; 2026 2027 CParams m_Params; 2028 2029 CSG_Simple_Statistics m_Data[2]; 2030 2031 CSG_Formula m_Formula; 2032 2033 2034 bool _Fit_Function (void); 2035 bool _Get_Gaussj (void); 2036 bool _Get_mrqcof (CSG_Vector &Parameters, CSG_Matrix &Alpha, CSG_Vector &Beta); 2037 2038 void _Get_Function (double &y, double *dy_da, double x, const double *Parameters); 2039 2040 }; 2041 2042 2043 /////////////////////////////////////////////////////////// 2044 // // 2045 // // 2046 // // 2047 /////////////////////////////////////////////////////////// 2048 2049 //--------------------------------------------------------- 2050 class SAGA_API_DLL_EXPORT CSG_Trend_Polynom 2051 { 2052 public: 2053 CSG_Trend_Polynom(void); 2054 2055 bool Destroy (void); 2056 2057 bool Set_Order (int Order = 1); 2058 2059 bool Clr_Data (void); 2060 bool Set_Data (double *x, double *y, int n, bool bAdd = false); 2061 bool Add_Data (double x, double y); Get_Data_Count(void)2062 int Get_Data_Count (void) const { return( m_x.Get_N() ); } Get_Data_X(int i)2063 double Get_Data_X (int i) const { return( m_x(i) ); } Get_Data_Y(int i)2064 double Get_Data_Y (int i) const { return( m_y(i) ); } 2065 2066 bool Get_Trend (void); 2067 Get_Order(void)2068 int Get_Order (void) const { return( m_Order ); } Get_nCoefficients(void)2069 int Get_nCoefficients (void) const { return( m_Order + 1 ); } Get_Coefficient(int i)2070 double Get_Coefficient (int i) const { return( m_a(i) ); } Get_R2(void)2071 double Get_R2 (void) const { return( m_r2 ); } 2072 2073 double Get_Value (double x) const; 2074 2075 2076 private: 2077 2078 double m_r2; 2079 2080 int m_Order; 2081 2082 CSG_Vector m_x, m_y, m_a; 2083 2084 }; 2085 2086 2087 /////////////////////////////////////////////////////////// 2088 // // 2089 // // 2090 // // 2091 /////////////////////////////////////////////////////////// 2092 2093 //--------------------------------------------------------- 2094 #endif // #ifndef HEADER_INCLUDED__SAGA_API__mat_tools_H 2095