SAGA API  v9.7
mat_tools.cpp
Go to the documentation of this file.
1 
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.cpp //
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 // //
51 
52 //---------------------------------------------------------
53 #include <time.h>
54 #include <cfloat>
55 
56 #include "mat_tools.h"
57 
58 #include "table.h"
59 #include "grid.h"
60 #include "grids.h"
61 
62 
64 // //
65 // //
66 // //
68 
69 //---------------------------------------------------------
70 double SG_Get_Square(double Value)
71 {
72  return( Value * Value );
73 }
74 
75 
77 // //
78 // //
79 // //
81 
82 //---------------------------------------------------------
83 double SG_Get_Rounded(double Value, int Decimals)
84 {
85  if( Decimals < 0 )
86  {
87  return( Value );
88  }
89 
90  if( Decimals == 0 )
91  {
92  return( floor(0.5 + Value) );
93  }
94 
95  double d = pow(10., Decimals);
96  double v = Value * d;
97 
98  if( fabs(v - floor(v)) > 0. )
99  {
100  return( floor(0.5 + v) / d );
101  }
102 
103  return( Value );
104 }
105 
106 //---------------------------------------------------------
107 double SG_Get_Rounded_To_SignificantFigures(double Value, int Decimals)
108 {
109  if( Decimals <= 0 || Value == 0. )
110  {
111  return( (int)(0.5 + Value) );
112  }
113 
114  Decimals = (int)(-(ceil(log10(fabs(Value))) - Decimals));
115 
116  if( Decimals > 0 )
117  {
118  double d = pow(10., Decimals);
119 
120  return( Value < 0.
121  ? -((int)(0.5 - Value * d)) / d
122  : ((int)(0.5 + Value * d)) / d
123  );
124  }
125  else
126  {
127  double d = pow(10., -Decimals);
128 
129  return( Value < 0.
130  ? -((int)(0.5 - Value / d)) * d
131  : ((int)(0.5 + Value / d)) * d
132  );
133  }
134 }
135 
136 
138 // //
139 // //
140 // //
142 
143 //---------------------------------------------------------
144 int SG_Get_Digit_Count(int Number)
145 {
146  Number = abs(Number);
147 
148  return( Number < 10 ? 1 : 1 + (int)log10((double)Number) );
149 }
150 
151 
153 // //
154 // //
155 // //
157 
158 //---------------------------------------------------------
159 CSG_String SG_Get_Double_asString(double Number, int Width, int Precision, bool bScientific)
160 {
161  if( bScientific )
162  {
163  if( Width > 0 && Precision >= 0 ) return( CSG_String::Format("%*.*e", Width, Precision, Number) );
164  if( Width > 0 ) return( CSG_String::Format("%*e" , Width , Number) );
165  if( Precision >= 0 ) return( CSG_String::Format("%.*e" , Precision, Number) );
166 
167  return( CSG_String::Format("%e", Number) );
168  }
169  else
170  {
171  if( Width > 0 && Precision >= 0 ) return( CSG_String::Format("%*.*f", Width, Precision, Number) );
172  if( Width > 0 ) return( CSG_String::Format("%*f" , Width , Number) );
173  if( Precision >= 0 ) return( CSG_String::Format("%.*f" , Precision, Number) );
174 
175  return( CSG_String::Format("%f", Number) );
176  }
177 }
178 
179 
181 // //
182 // //
183 // //
185 
186 //---------------------------------------------------------
187 int SG_Compare_Int(const void *a, const void *b)
188 {
189  if( *((int *)a) < *((int *)b) )
190  return( -1 );
191 
192  if( *((int *)a) > *((int *)b) )
193  return( 1 );
194 
195  return( 0 );
196 }
197 
198 //---------------------------------------------------------
199 int SG_Compare_Double(const void *a, const void *b)
200 {
201  if( *((double *)a) < *((double *)b) )
202  return( -1 );
203 
204  if( *((double *)a) > *((double *)b) )
205  return( 1 );
206 
207  return( 0 );
208 }
209 
210 //---------------------------------------------------------
211 int SG_Compare_Char_Ptr(const void *a, const void *b)
212 {
213  return( strcmp((const char *)a, (const char *)b) );
214 }
215 
216 
218 // //
219 // //
220 // //
222 
223 //---------------------------------------------------------
224 double SG_Degree_To_Decimal(double Deg, double Min, double Sec)
225 {
226  return( Deg > 0.
227  ? (Deg + Min / 60. + Sec / 3600.)
228  : (Deg - Min / 60. - Sec / 3600.)
229  );
230 }
231 
232 //---------------------------------------------------------
233 void SG_Decimal_To_Degree(double Value, double &Deg, double &Min, double &Sec)
234 {
235  Sec = fmod(Value < 0. ? -Value : Value, 360.);
236 
237  Deg = (int)Sec; Sec = 60. * (Sec - Deg);
238  Min = (int)Sec; Sec = 60. * (Sec - Min);
239 
240  if( Value < 0. )
241  {
242  Deg = -Deg;
243  }
244 }
245 
246 
248 // //
249 // //
250 // //
252 
253 //---------------------------------------------------------
255 {
256  Initialize();
257 }
258 
259 //---------------------------------------------------------
261 {
262  Initialize((unsigned)time(NULL));
263 }
264 
265 //---------------------------------------------------------
266 void CSG_Random::Initialize(unsigned int Value)
267 {
268  srand(Value);
269 }
270 
271 //---------------------------------------------------------
272 // Uniform distributed pseudo-random numbers in the range from 0 to 1.
273 //
275 {
276  return( 1. * rand() / (double)RAND_MAX );
277 }
278 
279 //---------------------------------------------------------
280 // Uniform distributed pseudo-random numbers in the range from min to max.
281 //
282 double CSG_Random::Get_Uniform(double min, double max)
283 {
284  return( min + (max - min) * rand() / (double)RAND_MAX );
285 }
286 
287 //---------------------------------------------------------
288 // Generating Gaussian pseudo-random numbers using
289 // the polar form of the Box-Muller transformation.
290 //
291 // Box, G.E.P, Muller, M.E. (1958):
292 // 'A note on the generation of random normal deviates',
293 // Annals Math. Stat, V. 29, pp. 610-611
294 //
295 // Link: http://www.taygeta.com/random/gaussian.html
296 //
297 //---------------------------------------------------------
298 double CSG_Random::Get_Gaussian(double mean, double stddev)
299 {
300  double x1, x2, w;
301 
302  do
303  {
304  x1 = 2. * Get_Uniform() - 1.;
305  x2 = 2. * Get_Uniform() - 1.;
306 
307  w = x1 * x1 + x2 * x2;
308  }
309  while( w >= 1. );
310 
311  w = sqrt((-2. * log(w)) / w);
312 
313  return( mean + stddev * x1 * w );
314 }
315 
316 
318 // //
319 // //
320 // //
322 
323 //---------------------------------------------------------
325 {
326  Create(false);
327 }
328 
330 {
331  Create(bHoldValues);
332 }
333 
335 {
336  Create(Statistics);
337 }
338 
339 CSG_Simple_Statistics::CSG_Simple_Statistics(double Mean, double StdDev, sLong Count)
340 {
341  Create(Mean, StdDev, Count);
342 }
343 
345 {
346  Create(Values, bHoldValues);
347 }
348 
349 //---------------------------------------------------------
350 bool CSG_Simple_Statistics::Create(bool bHoldValues)
351 {
352  Invalidate();
353 
354  m_Values.Create(bHoldValues ? sizeof(double) : 0, 0, TSG_Array_Growth::SG_ARRAY_GROWTH_1);
355 
356  return( true );
357 }
358 
360 {
361  m_bEvaluated = Statistics.m_bEvaluated;
362 
363  m_nValues = Statistics.m_nValues;
364  m_Weights = Statistics.m_Weights;
365  m_Sum = Statistics.m_Sum;
366  m_Sum2 = Statistics.m_Sum2;
367 
368  m_Minimum = Statistics.m_Minimum;
369  m_Maximum = Statistics.m_Maximum;
370  m_Range = Statistics.m_Range;
371  m_Mean = Statistics.m_Mean;
372  m_Variance = Statistics.m_Variance;
373  m_StdDev = Statistics.m_StdDev;
374 
375  m_Kurtosis = Statistics.m_Kurtosis;
376  m_Skewness = Statistics.m_Skewness;
377 
378  m_Gini = Statistics.m_Gini;
379 
380  m_bSorted = Statistics.m_bSorted;
381  m_Values .Create(Statistics.m_Values);
382 
383  return( true );
384 }
385 
386 bool CSG_Simple_Statistics::Create(double Mean, double StdDev, sLong Count)
387 {
388  Invalidate();
389 
390  m_bEvaluated = 1;
391 
392  m_Mean = Mean;
393  m_StdDev = StdDev;
394  m_Variance = StdDev*StdDev;
395  m_nValues = Count;
396  m_Weights = (double)Count;
397 
398  m_Sum = m_Weights * m_Mean;
400 
401  m_Minimum = m_Mean - 1.5 * m_StdDev;
402  m_Maximum = m_Mean + 1.5 * m_StdDev;
404 
405  return( true );
406 }
407 
408 bool CSG_Simple_Statistics::Create(const CSG_Vector &Values, bool bHoldValues)
409 {
410  if( Create(bHoldValues) )
411  {
412  for(sLong i=0; i<Values.Get_Size(); i++)
413  {
414  Add_Value(Values[i]);
415  }
416 
417  return( true );
418  }
419 
420  return( false );
421 }
422 
423 //---------------------------------------------------------
425 {
426  if( m_nValues < 1 || nValues < 1 || m_nValues == nValues )
427  {
428  return( false );
429  }
430 
431  double Scale = nValues / (double)m_nValues;
432 
433  m_Weights *= Scale;
434  m_Sum *= Scale;
435  m_Sum2 *= Scale;
436 
437  m_nValues = nValues;
438 
439  m_bEvaluated = 0;
440 
441  m_Values.Destroy();
442 
443  return( true );
444 }
445 
446 //---------------------------------------------------------
448 {
449  m_bEvaluated = 0;
450 
451  m_nValues = 0;
452  m_Weights = 0.;
453  m_Sum = 0.;
454  m_Sum2 = 0.;
455 
456  m_Minimum = 0.;
457  m_Maximum = 0.;
458  m_Range = 0.;
459  m_Mean = 0.;
460  m_Variance = 0.;
461  m_StdDev = 0.;
462 
463  m_Kurtosis = 0.;
464  m_Skewness = 0.;
465 
466  m_Gini = -1.;
467 
468  m_bSorted = false;
469  m_Values .Destroy();
470 }
471 
472 //---------------------------------------------------------
474 {
475  _Evaluate();
476 
477  return( is_Evaluated() > 0 );
478 }
479 
480 //---------------------------------------------------------
482 {
483  if( Statistics.m_nValues <= 0 )
484  {
485  return;
486  }
487 
488  if( m_nValues == 0 )
489  {
490  Create(Statistics);
491 
492  return;
493  }
494 
495  //--------------------------------------------------------
496  if( (sLong)m_Values.Get_Size() == m_nValues && (sLong)Statistics.m_Values.Get_Size() == Statistics.m_nValues && m_Values.Set_Array((size_t)(m_nValues + Statistics.m_nValues)) )
497  {
498  for(sLong i=0, j=m_nValues; i<Statistics.m_nValues; i++, j++)
499  {
500  ((double *)m_Values.Get_Array())[j] = Statistics.Get_Value(i);
501  }
502  }
503  else
504  {
505  m_Values.Destroy();
506  }
507 
508  m_nValues += Statistics.m_nValues;
509  m_Weights += Statistics.m_Weights;
510  m_Sum += Statistics.m_Sum;
511  m_Sum2 += Statistics.m_Sum2;
512 
513  if( m_Minimum > Statistics.m_Minimum )
514  m_Minimum = Statistics.m_Minimum;
515 
516  if( m_Maximum < Statistics.m_Maximum )
517  m_Maximum = Statistics.m_Maximum;
518 
519  m_Kurtosis = 0.;
520  m_Skewness = 0.;
521 
522  m_bEvaluated = 0;
523  m_bSorted = false;
524 }
525 
526 //---------------------------------------------------------
527 void CSG_Simple_Statistics::Add_Value(double Value, double Weight)
528 {
529  if( m_nValues == 0 )
530  {
531  m_Minimum = m_Maximum = Value;
532  }
533  else if( m_Minimum > Value )
534  {
535  m_Minimum = Value;
536  }
537  else if( m_Maximum < Value )
538  {
539  m_Maximum = Value;
540  }
541 
542  if( Weight )
543  {
544  m_Weights += fabs(Weight);
545  m_Sum += Weight * Value;
546  m_Sum2 += Weight * Value*Value;
547 
548  m_bEvaluated = 0;
549 
550  if( m_Values.Get_Value_Size() > 0 && m_Values.Inc_Array() )
551  {
552  m_bSorted = false;
553 
554  ((double *)m_Values.Get_Array())[m_nValues] = Value;
555  }
556 
557  m_nValues++;
558  }
559 }
560 
561 //---------------------------------------------------------
563 {
564  if( m_bEvaluated == 0 && m_Weights > 0. )
565  {
566  m_bEvaluated = 1;
567 
569  m_Mean = m_Sum / m_Weights;
571  m_StdDev = m_Variance > 0. ? sqrt(m_Variance) : 0.;
572  }
573 
574  //-----------------------------------------------------
575  if( m_bEvaluated == 1 && Level > 1 )
576  {
577  m_bEvaluated = 2;
578 
579  m_Kurtosis = 0.;
580  m_Skewness = 0.;
581 
582  if( Get_StdDev() > 0. && m_Values.Get_Size() > 0 )
583  {
584  for(sLong i=0; i<Get_Count(); i++)
585  {
586  double d = (Get_Value(i) - Get_Mean()) / Get_StdDev();
587 
588  m_Kurtosis += d*d*d*d;
589  m_Skewness += d*d*d;
590  }
591 
592  m_Kurtosis /= Get_Count();
593  m_Skewness /= Get_Count();
594  // m_Skewness *= Get_Count() / ((Get_Count() - 1) * (Get_Count() - 2));
595  }
596  }
597 }
598 
599 //---------------------------------------------------------
607 {
608  return( Get_StdDev() != 0. ? (Get_Mean() - Get_Median()) / Get_StdDev() : 0. );
609 }
610 
611 //---------------------------------------------------------
618 double CSG_Simple_Statistics::Get_Quantile(double Quantile)
619 {
620  if( m_Values.Get_Size() < 1 )
621  {
622  return( m_Mean );
623  }
624 
625  //-----------------------------------------------------
626  if( !m_bSorted )
627  {
628  qsort(m_Values.Get_Array(), m_Values.Get_Size(), sizeof(double), SG_Compare_Double);
629 
630  m_bSorted = true;
631  }
632 
633  if( Quantile <= 0. || m_Values.Get_Size() == 1 )
634  {
635  return( Get_Values()[0] );
636  }
637 
638  if( Quantile >= 1. )
639  {
640  return( Get_Values()[m_Values.Get_Size() - 1] );
641  }
642 
643  //-----------------------------------------------------
644  double r = Quantile * (m_Values.Get_Size() - 1);
645 
646  sLong i = (sLong)r; r -= i;
647 
648  return( r == 0. ? Get_Values()[i] : ((1. - r) * Get_Values()[i] + r * Get_Values()[i + 1]) );
649 }
650 
651 //---------------------------------------------------------
658 double CSG_Simple_Statistics::Get_Percentile(double Percentile)
659 {
660  return( Get_Quantile(Percentile / 100.) );
661 }
662 
663 //---------------------------------------------------------
670 {
671  return( Get_Quantile(0.5) );
672 }
673 
674 //---------------------------------------------------------
682 {
683  if( m_Gini < 0. && m_Values.Get_Size() > 1 )
684  {
685  if( !m_bSorted )
686  {
687  qsort(m_Values.Get_Array(), m_Values.Get_Size(), sizeof(double), SG_Compare_Double);
688 
689  m_bSorted = true;
690  }
691 
692  m_Gini = 0.;
693 
694  for(sLong i=0; i<Get_Count(); i++)
695  {
696  m_Gini += (i + 1.) * Get_Value(i);
697  }
698 
699  m_Gini = 2. * m_Gini / (Get_Count() * Get_Sum()) - (Get_Count() + 1.) / Get_Count();
700  }
701 
702  return( m_Gini );
703 }
704 
705 //---------------------------------------------------------
711 {
712  if( m_Values.Get_Size() == 0 ) { return( -1 ); }
713 
714  size_t Index = 0;
715  double Value = Get_Values()[Index];
716 
717  for(size_t i=1; i<(size_t)m_Values.Get_Size(); i++)
718  {
719  if( Value > Get_Values()[i] )
720  {
721  Index = i;
722  Value = Get_Values()[i];
723  }
724  }
725 
726  return( (sLong)Index );
727 }
728 
729 //---------------------------------------------------------
735 {
736  if( m_Values.Get_Size() == 0 ) { return( -1 ); }
737 
738  size_t Index = 0;
739  double Value = Get_Values()[Index];
740 
741  for(size_t i=1; i<(size_t)m_Values.Get_Size(); i++)
742  {
743  if( Value < Get_Values()[i] )
744  {
745  Index = i;
746  Value = Get_Values()[i];
747  }
748  }
749 
750  return( (sLong)Index );
751 }
752 
753 //---------------------------------------------------------
758 sLong CSG_Simple_Statistics::Get_nValues_Above(double Threshold, bool bEquals)
759 {
760  if( m_Values.Get_Size() == 0 ) { return( -1 ); }
761 
762  sLong n = 0;
763 
764  for(sLong i=0; i<Get_Count(); i++)
765  {
766  if( (bEquals && Get_Value(i) >= Threshold) || Get_Value(i) > Threshold )
767  {
768  n++;
769  }
770  }
771 
772  return( n );
773 }
774 
775 //---------------------------------------------------------
780 sLong CSG_Simple_Statistics::Get_nValues_Below(double Threshold, bool bEquals)
781 {
782  if( m_Values.Get_Size() == 0 ) { return( -1 ); }
783 
784  sLong n = 0;
785 
786  for(sLong i=0; i<Get_Count(); i++)
787  {
788  if( (bEquals && Get_Value(i) <= Threshold) || Get_Value(i) < Threshold )
789  {
790  n++;
791  }
792  }
793 
794  return( n );
795 }
796 
797 
799 // //
800 // //
801 // //
803 
804 //---------------------------------------------------------
806 {
807  int Index = 0;
808 
809  bWeighted = bWeighted && m_bWeights;
810 
811  for(int i=1; i<Get_Count(); i++)
812  {
813  if( bWeighted )
814  {
815  if( m_Weight[i] > m_Weight[Index] )
816  {
817  Index = i;
818  }
819  }
820  else
821  {
822  if( m_Count[i] > m_Count[Index] )
823  {
824  Index = i;
825  }
826  }
827  }
828 
829  return( Index );
830 }
831 
832 //---------------------------------------------------------
834 {
835  int Index = 0;
836 
837  bWeighted = bWeighted && m_bWeights;
838 
839  for(int i=1; i<Get_Count(); i++)
840  {
841  if( bWeighted )
842  {
843  if( m_Weight[i] < m_Weight[Index] )
844  {
845  Index = i;
846  }
847  }
848  else
849  {
850  if( m_Count[i] < m_Count[Index] )
851  {
852  Index = i;
853  }
854  }
855  }
856 
857  return( Index );
858 }
859 
860 
862 // //
864 
865 //---------------------------------------------------------
867 {
868  m_bWeights = bWeights;
869 
870  m_Count.Destroy();
871  m_Value.Destroy();
872 }
873 
874 //---------------------------------------------------------
875 void CSG_Unique_Number_Statistics::Add_Value(double Value, double Weight)
876 {
877  for(int i=0; i<Get_Count(); i++)
878  {
879  if( Value == m_Value[i] )
880  {
881  m_Count[i]++;
882 
883  if( m_bWeights && Weight > 0. )
884  {
885  m_Weight[i] += Weight;
886  }
887 
888  return;
889  }
890  }
891 
892  m_Count.Add(1);
893  m_Value.Add_Row(Value);
894 
895  if( m_bWeights && Weight > 0. )
896  {
897  m_Weight.Add_Row(Weight);
898  }
899 }
900 
901 //---------------------------------------------------------
903 {
904  for(int i=0; i<Get_Count(); i++)
905  {
906  if( Value == m_Value[i] )
907  {
908  return( i );
909  }
910  }
911 
912  return( -1 );
913 }
914 
915 
917 // //
919 
920 //---------------------------------------------------------
922 {
923  m_bWeights = bWeights;
924 
925  m_Count.Destroy();
926  m_Value.Clear();
927 }
928 
929 //---------------------------------------------------------
930 void CSG_Unique_String_Statistics::Add_Value(const CSG_String &Value, double Weight)
931 {
932  for(int i=0; i<Get_Count(); i++)
933  {
934  if( Value.Cmp(m_Value[i]) == 0 )
935  {
936  m_Count[i]++;
937 
938  if( m_bWeights && Weight > 0. )
939  {
940  m_Weight[i] += Weight;
941  }
942 
943  return;
944  }
945  }
946 
947  m_Count.Add(1);
948  m_Value.Add(Value);
949 
950  if( m_bWeights && Weight > 0. )
951  {
952  m_Weight.Add_Row(Weight);
953  }
954 }
955 
956 //---------------------------------------------------------
958 {
959  for(int i=0; i<Get_Count(); i++)
960  {
961  if( Value.Cmp(m_Value[i]) == 0 )
962  {
963  return( i );
964  }
965  }
966 
967  return( -1 );
968 }
969 
970 
972 // //
973 // //
974 // //
976 
977 //---------------------------------------------------------
979 {
980  m_pTable = new CSG_Table;
981 
982  Create(Type);
983 }
984 
985 //---------------------------------------------------------
987 {
988  delete(m_pTable);
989 }
990 
991 //---------------------------------------------------------
993 {
994  m_pTable->Destroy();
995 
996  m_pTable->Add_Field("VALUE", Type);
997  m_pTable->Add_Field("COUNT", SG_DATATYPE_ULong);
998 }
999 
1000 //---------------------------------------------------------
1002 {
1003  m_pTable->Del_Records();
1004 }
1005 
1006 //---------------------------------------------------------
1008 {
1009  return( m_pTable->Get_Field_Type(0) );
1010 }
1011 
1012 
1014 // //
1016 
1017 //---------------------------------------------------------
1019 {
1020  CSG_Table_Record *pRecord = m_pTable->Find_Record(0, Value);
1021 
1022  return( pRecord ? (int)pRecord->Get_Index() : -1);
1023 }
1024 
1025 //---------------------------------------------------------
1027 {
1028  CSG_Table_Record *pRecord = m_pTable->Find_Record(0, Value);
1029 
1030  return( pRecord ? (int)pRecord->Get_Index() : -1);
1031 }
1032 
1033 //---------------------------------------------------------
1035 {
1036  CSG_Table_Record *pRecord = m_pTable->Find_Record(0, Value);
1037 
1038  return( pRecord ? (int)pRecord->Get_Index() : -1);
1039 }
1040 
1041 
1043 // //
1045 
1046 //---------------------------------------------------------
1048 {
1049  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(Get_Category(Value));
1050 
1051  if( !pRecord )
1052  {
1053  (pRecord = m_pTable->Add_Record())->Set_Value(0, Value);
1054 
1055  if( m_pTable->Get_Count() > 16 || m_pTable->is_Indexed() )
1056  {
1057  Sort();
1058  }
1059  }
1060 
1061  pRecord->Add_Value(1, 1);
1062 
1063  return( (int)(m_pTable->Get_Count() - 1) );
1064 }
1065 
1066 //---------------------------------------------------------
1068 {
1069  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(Get_Category(Value));
1070 
1071  if( !pRecord )
1072  {
1073  (pRecord = m_pTable->Add_Record())->Set_Value(0, Value);
1074 
1075  if( m_pTable->Get_Count() > 16 || m_pTable->is_Indexed() )
1076  {
1077  Sort();
1078  }
1079  }
1080 
1081  pRecord->Add_Value(1, 1);
1082 
1083  return( (int)(m_pTable->Get_Count() - 1) );
1084 }
1085 
1086 //---------------------------------------------------------
1088 {
1089  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(Get_Category(Value));
1090 
1091  if( !pRecord )
1092  {
1093  (pRecord = m_pTable->Add_Record())->Set_Value(0, Value);
1094 
1095  if( m_pTable->Get_Count() > 16 || m_pTable->is_Indexed() )
1096  {
1097  Sort();
1098  }
1099  }
1100 
1101  pRecord->Add_Value(1, 1);
1102 
1103  return( (int)(m_pTable->Get_Count() - 1) );
1104 }
1105 
1106 //---------------------------------------------------------
1107 // sort categories ascending
1109 {
1110  return( m_pTable->Set_Index(0, TABLE_INDEX_Ascending) );
1111 }
1112 
1113 
1115 // //
1117 
1118 //---------------------------------------------------------
1119 // returns the number of categories.
1121 {
1122  return( (int)m_pTable->Get_Count() );
1123 }
1124 
1125 //---------------------------------------------------------
1126 // returns the number of observations for the i'th category.
1128 {
1129  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(i);
1130 
1131  return( pRecord ? pRecord->asInt(1) : 0 );
1132 }
1133 
1134 //---------------------------------------------------------
1136 {
1137  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(i);
1138 
1139  return( pRecord ? pRecord->asInt(0) : 0 );
1140 }
1141 
1142 //---------------------------------------------------------
1144 {
1145  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(i);
1146 
1147  return( pRecord ? pRecord->asDouble(0) : 0 );
1148 }
1149 
1150 //---------------------------------------------------------
1152 {
1153  CSG_Table_Record *pRecord = m_pTable->Get_Record_byIndex(i);
1154 
1155  return( pRecord ? pRecord->asString(0) : SG_T("") );
1156 }
1157 
1158 
1160 // //
1162 
1163 //---------------------------------------------------------
1165 {
1166  if( m_pTable->Get_Count() > 0 )
1167  {
1168  int Index = 0, Count = m_pTable->Get_Record_byIndex(0)->asInt(1);
1169 
1170  for(int i=1; i<m_pTable->Get_Count(); i++)
1171  {
1172  if( Count < m_pTable->Get_Record_byIndex(i)->asInt(1) )
1173  {
1174  Index = i;
1175  Count = m_pTable->Get_Record_byIndex(i)->asInt(1);
1176  }
1177  }
1178 
1179  return( Index );
1180  }
1181 
1182  return( -1 );
1183 }
1184 
1185 //---------------------------------------------------------
1187 {
1188  if( m_pTable->Get_Count() > 0 )
1189  {
1190  int Index = 0, Count = m_pTable->Get_Record_byIndex(0)->asInt(1);
1191 
1192  for(int i=1; i<m_pTable->Get_Count(); i++)
1193  {
1194  if( Count > m_pTable->Get_Record_byIndex(i)->asInt(1) )
1195  {
1196  Index = i;
1197  Count = m_pTable->Get_Record_byIndex(i)->asInt(1);
1198  }
1199  }
1200 
1201  return( Index );
1202  }
1203 
1204  return( -1 );
1205 }
1206 
1207 
1209 // //
1210 // //
1211 // //
1213 
1214 //---------------------------------------------------------
1216 {
1217  _On_Construction();
1218 }
1219 
1220 //---------------------------------------------------------
1222 {
1223  _On_Construction();
1224 
1225  Create(Histogram);
1226 }
1227 
1228 //---------------------------------------------------------
1229 CSG_Histogram::CSG_Histogram(size_t nClasses, double Minimum, double Maximum)
1230 {
1231  _On_Construction();
1232 
1233  Create(nClasses, Minimum, Maximum);
1234 }
1235 
1236 //---------------------------------------------------------
1237 CSG_Histogram::CSG_Histogram(size_t nClasses, double Minimum, double Maximum, const CSG_Vector &Values, size_t maxSamples)
1238 {
1239  _On_Construction();
1240 
1241  Create(nClasses, Minimum, Maximum, Values, maxSamples);
1242 }
1243 
1244 //---------------------------------------------------------
1245 CSG_Histogram::CSG_Histogram(size_t nClasses, double Minimum, double Maximum, CSG_Table *pTable, int Field, size_t maxSamples)
1246 {
1247  _On_Construction();
1248 
1249  Create(nClasses, Minimum, Maximum, pTable, Field, maxSamples);
1250 }
1251 
1252 //---------------------------------------------------------
1253 CSG_Histogram::CSG_Histogram(size_t nClasses, double Minimum, double Maximum, class CSG_Grid *pGrid, size_t maxSamples)
1254 {
1255  _On_Construction();
1256 
1257  Create(nClasses, Minimum, Maximum, pGrid, maxSamples);
1258 }
1259 
1260 //---------------------------------------------------------
1261 CSG_Histogram::CSG_Histogram(size_t nClasses, double Minimum, double Maximum, CSG_Grids *pGrids, size_t maxSamples)
1262 {
1263  _On_Construction();
1264 
1265  Create(nClasses, Minimum, Maximum, pGrids, maxSamples);
1266 }
1267 
1268 //---------------------------------------------------------
1270 {
1271  Destroy();
1272 }
1273 
1274 //---------------------------------------------------------
1276 {
1277  m_Statistics.Create();
1278 
1279  SG_FREE_SAFE(m_Elements );
1280  SG_FREE_SAFE(m_Cumulative);
1281 
1282  _On_Construction();
1283 
1284  return( true );
1285 }
1286 
1287 
1289 // //
1291 
1292 //---------------------------------------------------------
1293 void CSG_Histogram::_On_Construction(void)
1294 {
1295  m_nClasses = 0;
1296  m_Elements = NULL;
1297  m_Cumulative = NULL;
1298  m_Minimum = 0.;
1299  m_Maximum = 0.;
1300  m_ClassWidth = 1.;
1301 }
1302 
1303 //---------------------------------------------------------
1304 bool CSG_Histogram::_Create(size_t nClasses, double Minimum, double Maximum)
1305 {
1306  if( nClasses > 0 && Minimum < Maximum )
1307  {
1308  Destroy();
1309 
1310  m_Elements = (size_t *)SG_Calloc(nClasses, sizeof(size_t));
1311  m_Cumulative = (size_t *)SG_Calloc(nClasses, sizeof(size_t));
1312 
1313  if( m_Elements && m_Cumulative )
1314  {
1315  m_nClasses = nClasses;
1316  m_Minimum = Minimum;
1317  m_Maximum = Maximum;
1318  m_ClassWidth = (Maximum - Minimum) / (double)m_nClasses;
1319 
1320  return( true );
1321  }
1322  }
1323 
1324  Destroy();
1325 
1326  return( false );
1327 }
1328 
1329 //---------------------------------------------------------
1330 void CSG_Histogram::Add_Value(double Value)
1331 {
1332  m_Statistics += Value;
1333 
1334  if( m_nClasses > 0 && m_Minimum <= Value && Value <= m_Maximum )
1335  {
1336  size_t Class = (size_t)((Value - m_Minimum) / m_ClassWidth);
1337 
1338  if( Class >= m_nClasses )
1339  {
1340  Class = m_nClasses - 1;
1341  }
1342 
1343  m_Elements[Class]++;
1344  }
1345 }
1346 
1347 //---------------------------------------------------------
1349 {
1350  if( m_nClasses > 0 && Scale > 0. )
1351  {
1352  m_Statistics.Set_Count((sLong)(Scale * Get_Element_Count()));
1353 
1354  for(size_t i=0; i<m_nClasses; i++)
1355  {
1356  m_Elements[i] = (size_t)(Scale * m_Elements[i]);
1357  }
1358 
1359  return( Update() );
1360  }
1361 
1362  return( false );
1363 }
1364 
1365 //---------------------------------------------------------
1367 {
1368  if( m_nClasses > 0 )
1369  {
1370  m_Statistics.Get_Mean(); // _Evaluate()
1371 
1372  m_nMaximum = m_Cumulative[0] = m_Elements[0];
1373 
1374  for(size_t i=1; i<m_nClasses; i++)
1375  {
1376  m_Cumulative[i] = m_Cumulative[i - 1] + m_Elements[i];
1377 
1378  if( m_nMaximum < m_Elements[i] )
1379  {
1380  m_nMaximum = m_Elements[i];
1381  }
1382  }
1383 
1384  return( Get_Element_Count() > 0 );
1385  }
1386 
1387  return( false );
1388 }
1389 
1390 //---------------------------------------------------------
1391 bool CSG_Histogram::_Update(sLong nElements)
1392 {
1393  if( nElements > 0 && m_Statistics.Get_Count() > 0 )
1394  {
1395  double Scale = (double)nElements / (double)m_Statistics.Get_Count();
1396 
1397  m_Statistics.Create(m_Statistics.Get_Mean(), m_Statistics.Get_StdDev(), nElements);
1398 
1399  for(size_t i=1; i<m_nClasses; i++)
1400  {
1401  m_Elements[i] = (size_t)(0.5 + Scale * m_Elements[i]);
1402  }
1403  }
1404 
1405  return( Update() );
1406 }
1407 
1408 //---------------------------------------------------------
1412 double CSG_Histogram::Get_Quantile(double Quantile) const
1413 {
1414  if( m_nClasses < 2 ) { return( 0. ); }
1415 
1416  if( Quantile <= 0. ) { return( m_Minimum ); }
1417  if( Quantile >= 1. ) { return( m_Maximum ); }
1418 
1419  size_t n = (size_t)(Quantile * Get_Element_Count()); // number of elements
1420 
1421  for(size_t i=0, n0=0; i<m_nClasses; n0=m_Cumulative[i++])
1422  {
1423  if( n < m_Cumulative[i] )
1424  {
1425  if( m_Cumulative[i] >= n0 )
1426  {
1427  return( Get_Center(i) );
1428  }
1429 
1430  double d = (n - n0) / (double)(m_Cumulative[i] - n0);
1431 
1432  return( Get_Break(i) + d * m_ClassWidth );
1433  }
1434  else if( n == m_Cumulative[i] )
1435  {
1436  return( Get_Break(i + 1) );
1437  }
1438  }
1439 
1440  return( m_Maximum );
1441 }
1442 
1443 //---------------------------------------------------------
1447 double CSG_Histogram::Get_Percentile(double Percentile) const
1448 {
1449  return( Get_Quantile(Percentile / 100.) );
1450 }
1451 
1452 //---------------------------------------------------------
1456 double CSG_Histogram::Get_Quantile_Value(double Value) const
1457 {
1458  if( m_nClasses < 2 ) { return( 0. ); }
1459 
1460  if( Value <= m_Minimum ) { return( 0. ); }
1461  if( Value >= m_Maximum ) { return( 1. ); }
1462 
1463  size_t Class = (size_t)(m_nClasses * (Value - m_Minimum) / (m_Maximum - m_Minimum));
1464 
1465  if( Class >= m_nClasses )
1466  {
1467  return( 1. );
1468  }
1469 
1470  if( Class < 1 )
1471  {
1472  double dq = m_Cumulative[Class] / (double)Get_Element_Count();
1473 
1474  return( dq * (Value - m_Minimum) / m_ClassWidth );
1475  }
1476 
1477  double q0 = m_Cumulative[Class - 1] / (double)Get_Element_Count();
1478  double dq = (m_Cumulative[Class ] / (double)Get_Element_Count()) - q0;
1479 
1480  return( q0 + dq * (Value - Get_Break(Class)) / m_ClassWidth );
1481 }
1482 
1483 //---------------------------------------------------------
1487 double CSG_Histogram::Get_Percentile_Value(double Value) const
1488 {
1489  return( Get_Quantile_Value(Value) * 100. );
1490 }
1491 
1492 
1494 // //
1496 
1497 //---------------------------------------------------------
1498 bool CSG_Histogram::Create(const CSG_Histogram &Histogram)
1499 {
1500  if( !_Create(Histogram.m_nClasses, Histogram.m_Minimum, Histogram.m_Maximum) )
1501  {
1502  return( false );
1503  }
1504 
1505  m_Statistics = Histogram.m_Statistics;
1506  m_ClassWidth = Histogram.m_ClassWidth;
1507  m_nMaximum = Histogram.m_nMaximum ;
1508 
1509  for(size_t i=0; i<m_nClasses; i++)
1510  {
1511  m_Cumulative[i] = Histogram.m_Cumulative[i];
1512  m_Elements [i] = Histogram.m_Elements [i];
1513  }
1514 
1515  return( true );
1516 }
1517 
1518 //---------------------------------------------------------
1519 bool CSG_Histogram::Create(size_t nClasses, double Minimum, double Maximum)
1520 {
1521  return( _Create(nClasses, Minimum, Maximum) );
1522 }
1523 
1524 //---------------------------------------------------------
1525 bool CSG_Histogram::Create(size_t nClasses, double Minimum, double Maximum, const CSG_Vector &Values, size_t maxSamples)
1526 {
1527  if( Minimum >= Maximum )
1528  {
1529  CSG_Simple_Statistics s(Values);
1530 
1531  Minimum = s.Get_Minimum();
1532  Maximum = s.Get_Maximum();
1533  }
1534 
1535  if( !_Create(nClasses, Minimum, Maximum) )
1536  {
1537  return( false );
1538  }
1539 
1540  //-----------------------------------------------------
1541  if( maxSamples > 0 && maxSamples < (size_t)Values.Get_N() )
1542  {
1543  double d = (double)Values.Get_N() / (double)maxSamples;
1544 
1545  for(double i=0; i<(double)Values.Get_N(); i+=d)
1546  {
1547  Add_Value(Values[(sLong)i]);
1548  }
1549 
1550  d = (double)m_Statistics.Get_Count() / (double)maxSamples;
1551 
1552  return( _Update(d < 1. ? (int)(d * (double)Values.Get_N()) : Values.Get_N()) );
1553  }
1554 
1555  //-----------------------------------------------------
1556  for(int i=0; i<Values.Get_N(); i++)
1557  {
1558  Add_Value(Values[i]);
1559  }
1560 
1561  return( Update() );
1562 }
1563 
1564 //---------------------------------------------------------
1565 bool CSG_Histogram::Create(size_t nClasses, double Minimum, double Maximum, CSG_Table *pTable, int Field, size_t maxSamples)
1566 {
1567  if( !pTable || Field < 0 || Field >= pTable->Get_Field_Count() || !_Create(nClasses,
1568  Minimum < Maximum ? Minimum : pTable->Get_Minimum(Field),
1569  Minimum < Maximum ? Maximum : pTable->Get_Maximum(Field)) )
1570  {
1571  return( false );
1572  }
1573 
1574  //-----------------------------------------------------
1575  if( maxSamples > 0 && maxSamples < (size_t)pTable->Get_Count() )
1576  {
1577  double d = (double)pTable->Get_Count() / (double)maxSamples;
1578 
1579  for(double i=0; i<(double)pTable->Get_Count(); i+=d)
1580  {
1581  double Value = pTable->Get_Record((int)i)->asDouble(Field);
1582 
1583  if( !pTable->is_NoData_Value(Value) )
1584  {
1585  Add_Value(Value);
1586  }
1587  }
1588 
1589  d = (double)m_Statistics.Get_Count() / (double)maxSamples;
1590 
1591  return( _Update(d < 1. ? (int)(d * pTable->Get_Count()) : pTable->Get_Count()) );
1592  }
1593 
1594  //-----------------------------------------------------
1595  for(sLong i=0; i<pTable->Get_Count(); i++)
1596  {
1597  double Value = pTable->Get_Record(i)->asDouble(Field);
1598 
1599  if( !pTable->is_NoData_Value(Value) )
1600  {
1601  Add_Value(Value);
1602  }
1603  }
1604 
1605  return( Update() );
1606 }
1607 
1608 //---------------------------------------------------------
1609 bool CSG_Histogram::Create(size_t nClasses, double Minimum, double Maximum, CSG_Grid *pGrid, size_t maxSamples)
1610 {
1611  if( !pGrid || !_Create(nClasses,
1612  Minimum < Maximum ? Minimum : pGrid->Get_Min(),
1613  Minimum < Maximum ? Maximum : pGrid->Get_Max()) )
1614  {
1615  return( false );
1616  }
1617 
1618  //-----------------------------------------------------
1619  if( maxSamples > 0 && (sLong)maxSamples < pGrid->Get_NCells() )
1620  {
1621  double d = (double)pGrid->Get_NCells() / (double)maxSamples;
1622 
1623  for(double i=0; i<(double)pGrid->Get_NCells(); i+=d)
1624  {
1625  double Value = pGrid->asDouble((sLong)i);
1626 
1627  if( !pGrid->is_NoData_Value(Value) )
1628  {
1629  Add_Value(Value);
1630  }
1631  }
1632 
1633  d = (double)m_Statistics.Get_Count() / (double)maxSamples;
1634 
1635  return( _Update(d < 1. ? (sLong)(d * (double)pGrid->Get_NCells()) : pGrid->Get_NCells()) );
1636  }
1637 
1638  //-----------------------------------------------------
1639  for(sLong i=0; i<pGrid->Get_NCells(); i++)
1640  {
1641  if( !pGrid->is_NoData(i) )
1642  {
1643  Add_Value(pGrid->asDouble(i));
1644  }
1645  }
1646 
1647  return( Update() );
1648 }
1649 
1650 //---------------------------------------------------------
1651 bool CSG_Histogram::Create(size_t nClasses, double Minimum, double Maximum, CSG_Grids *pGrids, size_t maxSamples)
1652 {
1653  if( !pGrids || !_Create(nClasses,
1654  Minimum < Maximum ? Minimum : pGrids->Get_Min(),
1655  Minimum < Maximum ? Maximum : pGrids->Get_Max()) )
1656  {
1657  return( false );
1658  }
1659 
1660  //-----------------------------------------------------
1661  if( maxSamples > 0 && (sLong)maxSamples < pGrids->Get_NCells() )
1662  {
1663  double d = (double)pGrids->Get_NCells() / (double)maxSamples;
1664 
1665  for(double i=0; i<(double)pGrids->Get_NCells(); i+=d)
1666  {
1667  double Value = pGrids->asDouble((sLong)i);
1668 
1669  if( !pGrids->is_NoData_Value(Value) )
1670  {
1671  Add_Value(Value);
1672  }
1673  }
1674 
1675  d = (double)m_Statistics.Get_Count() / (double)maxSamples;
1676 
1677  return( _Update(d < 1. ? (sLong)(d * (double)pGrids->Get_NCells()) : pGrids->Get_NCells()) );
1678  }
1679 
1680  //-----------------------------------------------------
1681  for(sLong i=0; i<pGrids->Get_NCells(); i++)
1682  {
1683  if( !pGrids->is_NoData(i) )
1684  {
1685  Add_Value(pGrids->asDouble(i));
1686  }
1687  }
1688 
1689  return( Update() );
1690 }
1691 
1692 
1694 // //
1696 
1697 //---------------------------------------------------------
1699 {
1700  Create(Histogram);
1701 
1702  return( *this );
1703 }
1704 
1705 
1707 // //
1708 // //
1709 // //
1711 
1712 //---------------------------------------------------------
1714 {}
1715 
1716 //---------------------------------------------------------
1718 {}
1719 
1720 //---------------------------------------------------------
1721 CSG_Natural_Breaks::CSG_Natural_Breaks(CSG_Table *pTable, int Field, int nClasses, int Histogram)
1722 {
1723  Create(pTable, Field, nClasses, Histogram);
1724 }
1725 
1726 //---------------------------------------------------------
1727 CSG_Natural_Breaks::CSG_Natural_Breaks(CSG_Grid *pGrid, int nClasses, int Histogram)
1728 {
1729  Create(pGrid, nClasses, Histogram);
1730 }
1731 
1732 //---------------------------------------------------------
1733 CSG_Natural_Breaks::CSG_Natural_Breaks(CSG_Grids *pGrids, int nClasses, int Histogram)
1734 {
1735  Create(pGrids, nClasses, Histogram);
1736 }
1737 
1738 //---------------------------------------------------------
1739 CSG_Natural_Breaks::CSG_Natural_Breaks(const CSG_Vector &Values, int nClasses, int Histogram)
1740 {
1741  Create(Values, nClasses, Histogram);
1742 }
1743 
1744 
1746 // //
1748 
1749 //---------------------------------------------------------
1750 bool CSG_Natural_Breaks::Create(CSG_Table *pTable, int Field, int nClasses, int Histogram)
1751 {
1752  bool bResult = false;
1753 
1754  if( Histogram > 0 )
1755  {
1756  bResult = m_Histogram.Create(Histogram, 0, 0, pTable, Field) && _Histogram(nClasses);
1757  }
1758  else if( Field >= 0 && Field < pTable->Get_Field_Count() )
1759  {
1760  for(sLong i=0; i<pTable->Get_Count(); i++)
1761  {
1762  CSG_Table_Record *pRecord = pTable->Get_Record(i);
1763 
1764  if( !pRecord->is_NoData(Field) )
1765  {
1766  m_Values.Add_Row(pRecord->asDouble(Field));
1767  }
1768  }
1769 
1770  bResult = m_Values.Sort() && _Calculate(nClasses);
1771 
1772  m_Values.Destroy();
1773  }
1774 
1775  return( bResult );
1776 }
1777 
1778 //---------------------------------------------------------
1779 bool CSG_Natural_Breaks::Create(CSG_Grid *pGrid, int nClasses, int Histogram)
1780 {
1781  bool bResult = false;
1782 
1783  if( Histogram > 0 )
1784  {
1785  bResult = m_Histogram.Create(Histogram, 0, 0, pGrid) && _Histogram(nClasses);
1786  }
1787  else
1788  {
1789  for(sLong i=0; i<pGrid->Get_NCells(); i++)
1790  {
1791  if( !pGrid->is_NoData(i) )
1792  {
1793  m_Values.Add_Row(pGrid->asDouble(i));
1794  }
1795  }
1796 
1797  bResult = m_Values.Sort() && _Calculate(nClasses);
1798 
1799  m_Values.Destroy();
1800  }
1801 
1802  return( bResult );
1803 }
1804 
1805 //---------------------------------------------------------
1806 bool CSG_Natural_Breaks::Create(CSG_Grids *pGrids, int nClasses, int Histogram)
1807 {
1808  bool bResult = false;
1809 
1810  if( Histogram > 0 )
1811  {
1812  bResult = m_Histogram.Create(Histogram, 0, 0, pGrids) && _Histogram(nClasses);
1813  }
1814  else
1815  {
1816  for(sLong i=0; i<pGrids->Get_NCells(); i++)
1817  {
1818  if( !pGrids->is_NoData(i) )
1819  {
1820  m_Values.Add_Row(pGrids->asDouble(i));
1821  }
1822  }
1823 
1824  bResult = m_Values.Sort() && _Calculate(nClasses);
1825 
1826  m_Values.Destroy();
1827  }
1828 
1829  return( bResult );
1830 }
1831 
1832 //---------------------------------------------------------
1833 bool CSG_Natural_Breaks::Create(const CSG_Vector &Values, int nClasses, int Histogram)
1834 {
1835  bool bResult = false;
1836 
1837  if( Histogram > 0 )
1838  {
1839  bResult = m_Histogram.Create(Histogram, 0, 0, Values) && _Histogram(nClasses);
1840  }
1841  else
1842  {
1843  bResult = m_Values.Create(Values) && m_Values.Sort() && _Calculate(nClasses);
1844 
1845  m_Values.Destroy();
1846  }
1847 
1848  return( bResult );
1849 }
1850 
1851 
1853 // //
1855 
1856 //---------------------------------------------------------
1857 bool CSG_Natural_Breaks::_Histogram(int nClasses)
1858 {
1859  if( _Calculate(nClasses) )
1860  {
1861  double d = (double)m_Histogram.Get_Class_Count() / m_Histogram.Get_Cumulative((int)(m_Histogram.Get_Class_Count() - 1));
1862 
1863  m_Breaks[0] = m_Histogram.Get_Break(0);
1864 
1865  for(int i=1; i<Get_Count(); i++)
1866  {
1867  m_Breaks[i] = m_Histogram.Get_Value(m_Breaks[i] * d);
1868  }
1869 
1870  m_Breaks[nClasses] = m_Histogram.Get_Break((int)m_Histogram.Get_Class_Count());
1871 
1872  m_Histogram.Destroy();
1873 
1874  return( true );
1875  }
1876 
1877  m_Histogram.Destroy();
1878 
1879  return( false );
1880 }
1881 
1882 //---------------------------------------------------------
1883 inline double CSG_Natural_Breaks::_Get_Value(int i)
1884 {
1885  if( m_Histogram.Get_Class_Count() > 0 )
1886  {
1887  return( (double)m_Histogram.Get_Cumulative(i) );
1888  }
1889 
1890  return( m_Values[i] );
1891 }
1892 
1893 //---------------------------------------------------------
1894 bool CSG_Natural_Breaks::_Calculate(int nClasses)
1895 {
1896  if( m_Histogram.Get_Class_Count() == 0 && m_Values.Get_Size() == 0 )
1897  {
1898  return( false );
1899  }
1900 
1901  int nValues = m_Histogram.Get_Class_Count() > 0 ? (int)m_Histogram.Get_Class_Count() : m_Values.Get_N();
1902 
1903  CSG_Matrix mv(nClasses, nValues); mv.Assign(FLT_MAX);
1904 
1905  int **mc = (int **)SG_Malloc(nValues * sizeof(int *));
1906 
1907  mc[0] = (int *)SG_Calloc((size_t)nClasses * nValues, sizeof(int));
1908 
1909  for(int i=0; i<nValues; i++)
1910  {
1911  mc[i] = mc[0] + i * (size_t)nClasses;
1912  }
1913 
1914  //-----------------------------------------------------
1915  for(int i=1; i<nValues; i++)
1916  {
1917  double v = 0., s1 = 0., s2 = 0., w = 0.;
1918 
1919  for(int m=0, n=i+1; m<=i; m++, n--)
1920  {
1921  v = _Get_Value(n);
1922  s2 += v*v;
1923  s1 += v;
1924  w ++;
1925  v = s2 - (s1 * s1) / w;
1926 
1927  if( n > 0 )
1928  {
1929  for(int j=1; j<nClasses; j++)
1930  {
1931  if( mv[i][j] >= (v + mv[n - 1][j - 1]) )
1932  {
1933  mc[i][j] = n;
1934  mv[i][j] = v + mv[n - 1][j - 1];
1935  }
1936  }
1937  }
1938  }
1939 
1940  mc[i][0] = 0;
1941  mv[i][0] = v;
1942  }
1943 
1944  //-----------------------------------------------------
1945  CSG_Array_Int Class(nClasses);
1946 
1947  for(int i=0; i<nClasses; i++)
1948  {
1949  Class[i] = i;
1950  }
1951 
1952  int j = Class[(size_t)nClasses - 1] = nValues - 1;
1953 
1954  for(int i=nClasses-1; i>0; i--)
1955  {
1956  Class[(size_t)i - 1] = j = mc[j - 1][i];
1957  }
1958 
1959  //-----------------------------------------------------
1960  m_Breaks.Create((size_t)nClasses + 1);
1961 
1962  m_Breaks[0] = _Get_Value(0);
1963 
1964  for(int i=1; i<nClasses; i++)
1965  {
1966  m_Breaks[i] = _Get_Value(Class[i - 1]);
1967  }
1968 
1969  m_Breaks[nClasses] = _Get_Value(nValues - 1);
1970 
1971  SG_Free(mc[0]); SG_Free(mc);
1972 
1973  return( true );
1974 }
1975 
1976 
1978 // //
1979 // //
1980 // //
1982 
1983 //---------------------------------------------------------
1985 {
1986  m_nFeatures = 0;
1987  m_Iteration = 0;
1988 }
1989 
1990 //---------------------------------------------------------
1992 {
1993  Destroy();
1994 }
1995 
1996 //---------------------------------------------------------
1998 {
1999  m_Centroid.Destroy();
2000  m_Variance.Destroy();
2001  m_nMembers.Destroy();
2002  m_Clusters.Destroy();
2003  m_Features.Destroy();
2004  m_nFeatures = 0;
2005  m_Iteration = 0;
2006 
2007  return( true );
2008 }
2009 
2010 //---------------------------------------------------------
2011 bool CSG_Cluster_Analysis::Create(int nFeatures)
2012 {
2013  Destroy();
2014 
2015  if( nFeatures > 0 )
2016  {
2017  m_nFeatures = nFeatures;
2018 
2019  m_Features.Create(m_nFeatures * sizeof(double), 0, TSG_Array_Growth::SG_ARRAY_GROWTH_3);
2020 
2021  return( true );
2022  }
2023 
2024  return( false );
2025 }
2026 
2027 //---------------------------------------------------------
2029 {
2030  return( m_nFeatures > 0 && m_Features.Inc_Array() );
2031 }
2032 
2033 //---------------------------------------------------------
2034 bool CSG_Cluster_Analysis::Set_Feature(sLong iElement, int iFeature, double Value)
2035 {
2036  if( iElement >= 0 && iElement < Get_nElements() && iFeature >= 0 && iFeature < m_nFeatures )
2037  {
2038  ((double *)m_Features.Get_Entry(iElement))[iFeature] = Value;
2039 
2040  return( true );
2041  }
2042 
2043  return( false );
2044 }
2045 
2046 //---------------------------------------------------------
2056 //---------------------------------------------------------
2057 bool CSG_Cluster_Analysis::Execute(int Method, int nClusters, int nMaxIterations, int Initialization)
2058 {
2059  if( Get_nElements() < 2 || nClusters < 2 )
2060  {
2061  return( false );
2062  }
2063 
2064  //-----------------------------------------------------
2065  m_nMembers.Create(nClusters);
2066  m_Variance.Create(nClusters);
2067  m_Centroid.Create(m_nFeatures, nClusters);
2068 
2069  //-----------------------------------------------------
2070  m_Clusters.Create(Get_nElements());
2071 
2072  for(int iElement=0; iElement<Get_nElements(); iElement++)
2073  {
2074  switch( Initialization )
2075  {
2076  default: // random
2077  if( (m_Clusters[iElement] = (int)CSG_Random::Get_Uniform(0, nClusters)) >= nClusters )
2078  {
2079  m_Clusters[iElement] = nClusters - 1;
2080  }
2081  break;
2082 
2083  case 1: // periodic
2084  {
2085  m_Clusters[iElement] = iElement % nClusters;
2086  }
2087  break;
2088 
2089  case 2: // keep as is, but check for valid cluster ids
2090  if( 0 > m_Clusters[iElement] || m_Clusters[iElement] >= nClusters )
2091  {
2092  m_Clusters[iElement] = iElement % nClusters;
2093  }
2094  break;
2095  }
2096  }
2097 
2098  //-----------------------------------------------------
2099  bool bResult;
2100 
2101  m_Iteration = 0;
2102 
2103  switch( Method )
2104  {
2105  default: bResult = _Minimum_Distance(true , nMaxIterations); break;
2106  case 1: bResult = _Hill_Climbing (true , nMaxIterations); break;
2107  case 2: bResult = _Minimum_Distance(true , nMaxIterations)
2108  && _Hill_Climbing (false, nMaxIterations); break;
2109  }
2110 
2111  //-----------------------------------------------------
2112  if( bResult )
2113  {
2114  for(int iCluster=0; iCluster<nClusters; iCluster++)
2115  {
2116  m_Variance[iCluster] = m_nMembers[iCluster] <= 0 ? 0. : m_Variance[iCluster] / m_nMembers[iCluster];
2117  }
2118  }
2119 
2120  return( bResult );
2121 }
2122 
2123 //---------------------------------------------------------
2124 bool CSG_Cluster_Analysis::_Minimum_Distance(bool bInitialize, int nMaxIterations)
2125 {
2126  int iElement, iCluster, nClusters = m_Variance.Get_N();
2127 
2128  double SP_Last = -1.;
2129 
2130  //-----------------------------------------------------
2131  for(m_Iteration=1; SG_UI_Process_Get_Okay(); m_Iteration++)
2132  {
2133  m_Variance = 0.;
2134  m_Centroid = 0.;
2135  m_nMembers = 0;
2136 
2137  //-------------------------------------------------
2138  for(iElement=0; iElement<Get_nElements(); iElement++)
2139  {
2140  m_nMembers[iCluster = m_Clusters[iElement]]++;
2141 
2142  double *Feature = (double *)m_Features.Get_Entry(iElement);
2143 
2144  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2145  {
2146  m_Centroid[iCluster][iFeature] += Feature[iFeature];
2147  }
2148  }
2149 
2150  //-------------------------------------------------
2151  for(iCluster=0; iCluster<nClusters; iCluster++)
2152  {
2153  double d = m_nMembers[iCluster] > 0 ? 1. / m_nMembers[iCluster] : 0.;
2154 
2155  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2156  {
2157  m_Centroid[iCluster][iFeature] *= d;
2158  }
2159  }
2160 
2161  //-------------------------------------------------
2162  int nShifts = 0; m_SP = 0.;
2163 
2164  for(iElement=0; iElement<Get_nElements(); iElement++)
2165  {
2166  double *Feature = (double *)m_Features.Get_Entry(iElement);
2167 
2168  double minVariance = -1.;
2169  int minCluster = -1;
2170 
2171  for(iCluster=0; iCluster<nClusters; iCluster++)
2172  {
2173  double Variance = 0.;
2174 
2175  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2176  {
2177  Variance += SG_Get_Square(m_Centroid[iCluster][iFeature] - Feature[iFeature]);
2178  }
2179 
2180  if( minVariance < 0. || Variance < minVariance )
2181  {
2182  minVariance = Variance;
2183  minCluster = iCluster;
2184  }
2185  }
2186 
2187  if( m_Clusters[iElement] != minCluster )
2188  {
2189  m_Clusters[iElement] = minCluster;
2190 
2191  nShifts++;
2192  }
2193 
2194  m_SP += minVariance;
2195  m_Variance[minCluster] += minVariance;
2196  }
2197 
2198  //-------------------------------------------------
2199  m_SP /= Get_nElements();
2200 
2201  SG_UI_Process_Set_Text(CSG_String::Format("%s: %d >> %s %f",
2202  _TL("pass" ), m_Iteration,
2203  _TL("change"), m_Iteration < 2 ? m_SP : SP_Last - m_SP
2204  ));
2205 
2206  SP_Last = m_SP;
2207 
2208  if( nShifts == 0 || (nMaxIterations > 0 && nMaxIterations <= m_Iteration) )
2209  {
2210  return( true );
2211  }
2212  }
2213 
2214  return( true );
2215 }
2216 
2217 //---------------------------------------------------------
2218 bool CSG_Cluster_Analysis::_Hill_Climbing(bool bInitialize, int nMaxIterations)
2219 {
2220  int iElement, iCluster, nClusters = m_Variance.Get_N();
2221 
2222  //-----------------------------------------------------
2223  m_Variance = 0.;
2224  m_Centroid = 0.;
2225  m_nMembers = 0;
2226 
2227  for(iElement=0; iElement<Get_nElements(); iElement++)
2228  {
2229  m_nMembers[iCluster = m_Clusters[iElement]]++;
2230 
2231  double *Feature = (double *)m_Features.Get_Entry(iElement);
2232 
2233  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2234  {
2235  double d = Feature[iFeature];
2236 
2237  m_Centroid[iCluster][iFeature] += d;
2238  m_Variance[iCluster] += d*d;
2239  }
2240  }
2241 
2242  //-----------------------------------------------------
2243  for(iCluster=0; iCluster<nClusters; iCluster++)
2244  {
2245  double v = 0., d = m_nMembers[iCluster] <= 0 ? 0. : 1. / (double)m_nMembers[iCluster];
2246 
2247  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2248  {
2249  m_Centroid[iCluster][iFeature] *= d;
2250  v += SG_Get_Square(m_Centroid[iCluster][iFeature]);
2251  }
2252 
2253  m_Variance[iCluster] -= v * m_nMembers[iCluster];
2254  }
2255 
2256  //-----------------------------------------------------
2257  double SP_Last = -1.; int noShift = 0;
2258 
2259  for(m_Iteration=1; SG_UI_Process_Get_Okay(false); m_Iteration++)
2260  {
2261  for(iElement=0; iElement<Get_nElements(); iElement++)
2262  {
2263  iCluster = m_Clusters[iElement];
2264 
2265  if( noShift++ < Get_nElements() && m_nMembers[iCluster] > 1 )
2266  {
2267  double *Feature = (double *)m_Features.Get_Entry(iElement);
2268 
2269  double Variance = 0.;
2270 
2271  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2272  {
2273  Variance += SG_Get_Square(m_Centroid[iCluster][iFeature] - Feature[iFeature]);
2274  }
2275 
2276  double V1 = Variance * m_nMembers[iCluster] / (m_nMembers[iCluster] - 1.);
2277 
2278  //-----------------------------------------
2279  int kCluster = 0; double VMin = -1.;
2280 
2281  for(int jCluster=0; jCluster<nClusters; jCluster++)
2282  {
2283  if( jCluster != iCluster )
2284  {
2285  Variance = 0.;
2286 
2287  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2288  {
2289  Variance += SG_Get_Square(m_Centroid[jCluster][iFeature] - Feature[iFeature]);
2290  }
2291 
2292  double V2 = Variance * m_nMembers[jCluster] / (m_nMembers[jCluster] + 1.);
2293 
2294  if( VMin < 0. || V2 < VMin )
2295  {
2296  VMin = V2;
2297  kCluster = jCluster;
2298  }
2299  }
2300  }
2301 
2302  //-----------------------------------------
2303  if( VMin >= 0 && VMin < V1 )
2304  {
2305  noShift = 0;
2306  m_Variance[iCluster] -= V1;
2307  m_Variance[kCluster] += VMin;
2308  V1 = 1. / (m_nMembers[iCluster] - 1.);
2309  double V2 = 1. / (m_nMembers[kCluster] + 1.);
2310 
2311  for(int iFeature=0; iFeature<m_nFeatures; iFeature++)
2312  {
2313  double d = Feature[iFeature];
2314 
2315  m_Centroid[iCluster][iFeature] = (m_nMembers[iCluster] * m_Centroid[iCluster][iFeature] - d) * V1;
2316  m_Centroid[kCluster][iFeature] = (m_nMembers[kCluster] * m_Centroid[kCluster][iFeature] + d) * V2;
2317  }
2318 
2319  m_Clusters[iElement] = kCluster;
2320 
2321  m_nMembers[iCluster]--;
2322  m_nMembers[kCluster]++;
2323  }
2324  }
2325  }
2326 
2327  //-------------------------------------------------
2328  for(iCluster=0, m_SP=0.; iCluster<nClusters; iCluster++)
2329  {
2330  m_SP += m_Variance[iCluster];
2331  }
2332 
2333  m_SP /= Get_nElements();
2334 
2335  SG_UI_Process_Set_Text(CSG_String::Format("%s: %d >> %s %f",
2336  _TL("pass" ), m_Iteration,
2337  _TL("change"), m_Iteration <= 1 ? m_SP : SP_Last - m_SP
2338  ));
2339 
2340  SP_Last = m_SP;
2341 
2342  if( noShift >= Get_nElements() || (nMaxIterations > 0 && nMaxIterations <= m_Iteration) )
2343  {
2344  return( true );
2345  }
2346  }
2347 
2348  return( true );
2349 }
2350 
2351 
2353 // //
2354 // //
2355 // //
2357 
2358 //---------------------------------------------------------
2360 {
2361  m_nFeatures = 0; m_pClasses = NULL; m_nClasses = 0;
2362 
2363  m_Threshold_Distance = 0.;
2364  m_Threshold_Angle = 0.;
2365  m_Threshold_Probability = 0.;
2366  m_Probability_Relative = false;
2367 
2368  for(int i=0; i<SG_CLASSIFY_SUPERVISED_WTA; i++)
2369  {
2371  // || i == SG_CLASSIFY_SUPERVISED_Mahalonobis
2374  }
2375 }
2376 
2377 //---------------------------------------------------------
2379 {
2380  Destroy();
2381 }
2382 
2383 //---------------------------------------------------------
2385 {
2386  Destroy();
2387 
2388  if( nFeatures > 0 )
2389  {
2390  m_nFeatures = nFeatures;
2391  }
2392 }
2393 
2394 //---------------------------------------------------------
2396 {
2397  if( m_nClasses > 0 )
2398  {
2399  for(int i=0; i<m_nClasses; i++)
2400  {
2401  delete(m_pClasses[i]);
2402  }
2403 
2404  SG_FREE_SAFE(m_pClasses);
2405  }
2406 
2407  m_nFeatures = 0;
2408 
2409  m_Info.Clear();
2410 }
2411 
2412 
2414 // //
2416 
2417 //---------------------------------------------------------
2418 void CSG_Classifier_Supervised::Set_Threshold_Distance (double Value) { m_Threshold_Distance = Value; }
2419 double CSG_Classifier_Supervised::Get_Threshold_Distance (void) { return( m_Threshold_Distance ); }
2420 
2421 //---------------------------------------------------------
2422 void CSG_Classifier_Supervised::Set_Threshold_Angle (double Value) { m_Threshold_Angle = Value; }
2423 double CSG_Classifier_Supervised::Get_Threshold_Angle (void) { return( m_Threshold_Angle ); }
2424 
2425 //---------------------------------------------------------
2426 void CSG_Classifier_Supervised::Set_Threshold_Probability(double Value) { m_Threshold_Probability = Value; }
2427 double CSG_Classifier_Supervised::Get_Threshold_Probability(void) { return( m_Threshold_Probability ); }
2428 
2429 //---------------------------------------------------------
2430 void CSG_Classifier_Supervised::Set_Probability_Relative (bool Value) { m_Probability_Relative = Value; }
2431 bool CSG_Classifier_Supervised::Get_Probability_Relative (void) { return( m_Probability_Relative ); }
2432 
2433 //---------------------------------------------------------
2434 void CSG_Classifier_Supervised::Set_WTA(int Method, bool bOn)
2435 {
2436  if( Method >= 0 && Method < SG_CLASSIFY_SUPERVISED_WTA )
2437  {
2438  m_bWTA[Method] = bOn;
2439  }
2440 }
2441 
2443 {
2444  return( Method >= 0 && Method < SG_CLASSIFY_SUPERVISED_WTA ? m_bWTA[Method] : false );
2445 }
2446 
2447 
2449 // //
2451 
2452 //---------------------------------------------------------
2453 #include "saga_api.h"
2454 
2455 //---------------------------------------------------------
2457 {
2458  int nFeatures = m_nFeatures; Destroy(); m_nFeatures = nFeatures;
2459 
2460  //-----------------------------------------------------
2461  CSG_MetaData Data;
2462 
2463  if( !Data.Load(File) || !Data.Cmp_Name("supervised_classifier") || SG_Compare_Version(Data.Get_Property("saga-version"), "2.1.4") < 0 )
2464  {
2465  return( false );
2466  }
2467 
2468  if( !Data("classes") || !Data("features") || !Data["features"]("count") || Data["features"]["count"].Get_Content().asInt() != m_nFeatures || m_nFeatures == 0 )
2469  {
2470  return( false );
2471  }
2472 
2473  if( Data["features"]("info") )
2474  {
2475  m_Info = Data["features"]["info"].Get_Content();
2476  }
2477 
2478  //-----------------------------------------------------
2479  CSG_MetaData &Classes = *Data.Get_Child("CLASSES");
2480 
2481  for(int i=0; i<Classes.Get_Children_Count(); i++)
2482  {
2483  if( Classes[i].Cmp_Name("class") && Classes[i].Get_Child("id") )
2484  {
2485  bool bAdd = true;
2486 
2487  CClass *pClass = new CClass(Classes[i]["id"].Get_Content());
2488 
2489  if( !pClass->m_Cov .from_String(Classes[i]["cov" ].Get_Content()) || pClass->m_Cov .Get_NX() != m_nFeatures || !pClass->m_Cov.is_Square() ) { bAdd = false; }
2490  if( !pClass->m_Mean.from_String(Classes[i]["mean"].Get_Content()) || pClass->m_Mean.Get_N () != m_nFeatures ) { bAdd = false; }
2491  if( !pClass->m_Min .from_String(Classes[i]["min" ].Get_Content()) || pClass->m_Min .Get_N () != m_nFeatures ) { bAdd = false; }
2492  if( !pClass->m_Max .from_String(Classes[i]["max" ].Get_Content()) || pClass->m_Max .Get_N () != m_nFeatures ) { bAdd = false; }
2493 
2494  //---------------------------------------------
2495  if( !bAdd )
2496  {
2497  delete(pClass);
2498  }
2499  else
2500  {
2501  m_pClasses = (CClass **)SG_Realloc(m_pClasses, ((size_t)m_nClasses + 1) * sizeof(CClass *));
2502  m_pClasses[m_nClasses++] = pClass;
2503 
2504  pClass->m_Cov_Det = pClass->m_Cov.Get_Determinant();
2505  pClass->m_Cov_Inv = pClass->m_Cov.Get_Inverse();
2506 
2507  pClass->m_Mean_Spectral = CSG_Simple_Statistics(pClass->m_Mean).Get_Mean();
2508  }
2509  }
2510  }
2511 
2512  return( m_nClasses > 0 );
2513 }
2514 
2515 //---------------------------------------------------------
2516 bool CSG_Classifier_Supervised::Save(const CSG_String &File, const SG_Char *Feature_Info)
2517 {
2518  if( m_nFeatures < 1 || m_nClasses < 1 || File.is_Empty() )
2519  {
2520  return( false );
2521  }
2522 
2523  CSG_MetaData Data;
2524 
2525  Data.Set_Name ("supervised_classifier");
2526  Data.Add_Property("saga-version", SAGA_VERSION);
2527 
2528  CSG_MetaData &Features = *Data.Add_Child("features");
2529 
2530  Features.Add_Child("count", m_nFeatures);
2531 
2532  if( Feature_Info && *Feature_Info )
2533  {
2534  Features.Add_Child("info", Feature_Info);
2535  }
2536 
2537  CSG_MetaData &Classes = *Data.Add_Child("classes");
2538 
2539  Classes.Add_Property("count", m_nClasses);
2540 
2541  for(int i=0; i<m_nClasses; i++)
2542  {
2543  CSG_MetaData &Class = *Classes.Add_Child("class");
2544 
2545  CClass *pClass = m_pClasses[i];
2546 
2547  Class.Add_Child("id" , pClass->m_ID );
2548  Class.Add_Child("mean", pClass->m_Mean.to_String());
2549  Class.Add_Child("min" , pClass->m_Min .to_String());
2550  Class.Add_Child("max" , pClass->m_Max .to_String());
2551  Class.Add_Child("cov" , pClass->m_Cov .to_String());
2552  }
2553 
2554  return( Data.Save(File) );
2555 }
2556 
2557 
2559 // //
2561 
2562 //---------------------------------------------------------
2564 {
2565  CSG_String s;
2566 
2567  if( m_nFeatures > 0 && m_nClasses > 0 )
2568  {
2569  s += "\n";
2570 
2571  for(int iClass=0; iClass<m_nClasses; iClass++)
2572  {
2573  CClass *pClass = m_pClasses[iClass];
2574 
2575  s += "\n____\n" + pClass->m_ID + "\nFeature\tMean\tMin\tMax\tStdDev";
2576 
2577  for(int i=0; i<m_nFeatures; i++)
2578  {
2579  s += CSG_String::Format("\n%3d.", i + 1);
2580  s += "\t" + SG_Get_String(pClass->m_Mean[i]);
2581  s += "\t" + SG_Get_String(pClass->m_Min [i]);
2582  s += "\t" + SG_Get_String(pClass->m_Max [i]);
2583  s += "\t" + SG_Get_String(sqrt(pClass->m_Cov[i][i]));
2584  }
2585 
2586  s += "\n";
2587  }
2588  }
2589 
2590  return( s );
2591 }
2592 
2593 
2595 // //
2597 
2598 //---------------------------------------------------------
2599 bool CSG_Classifier_Supervised::Add_Class(const CSG_String &Class_ID, const CSG_Vector &Mean, const CSG_Vector &Min, const CSG_Vector &Max, const CSG_Matrix &Cov)
2600 {
2601  if( m_nFeatures < 1 || Mean.Get_N() != m_nFeatures || Min.Get_N() != m_nFeatures || Max.Get_N() != m_nFeatures || Cov.Get_NCols() != m_nFeatures || Cov.Get_NRows() != m_nFeatures )
2602  {
2603  return( false );
2604  }
2605 
2606  CClass *pClass, **pClasses = (CClass **)SG_Realloc(m_pClasses, ((size_t)m_nClasses + 1) * sizeof(CClass *));
2607 
2608  if( pClasses )
2609  {
2610  m_pClasses = pClasses;
2611 
2612  m_pClasses[m_nClasses++] = pClass = new CClass(Class_ID);
2613 
2614  pClass->m_ID = Class_ID;
2615  pClass->m_Mean = Mean;
2616  pClass->m_Min = Min;
2617  pClass->m_Max = Max;
2618  pClass->m_Cov = Cov;
2619  pClass->m_Cov_Inv = Cov.Get_Inverse();
2620  pClass->m_Cov_Det = Cov.Get_Determinant();
2621 
2622  pClass->m_Mean_Spectral = CSG_Simple_Statistics(Mean).Get_Mean();
2623 
2624  return( true );
2625  }
2626 
2627  return( false );
2628 }
2629 
2630 
2632 // //
2634 
2635 //---------------------------------------------------------
2637 {
2638  for(int i=0; i<m_nClasses; i++)
2639  {
2640  m_pClasses[i]->m_Samples.Destroy();
2641  }
2642 
2643  return( true );
2644 }
2645 
2646 //---------------------------------------------------------
2648 {
2649  if( m_nFeatures > 0 && m_nFeatures == Features.Get_N() )
2650  {
2651  int iClass = Get_Class(Class_ID);
2652 
2653  if( iClass < 0 )
2654  {
2655  CClass **pClasses = (CClass **)SG_Realloc(m_pClasses, ((size_t)m_nClasses + 1) * sizeof(CClass *));
2656 
2657  if( pClasses )
2658  {
2659  m_pClasses = pClasses;
2660 
2661  m_pClasses[iClass = m_nClasses++] = new CClass(Class_ID);
2662  }
2663  }
2664 
2665  if( iClass >= 0 )
2666  {
2667  return( m_pClasses[iClass]->m_Samples.Add_Row(Features) );
2668  }
2669  }
2670 
2671  return( false );
2672 }
2673 
2674 //---------------------------------------------------------
2675 bool CSG_Classifier_Supervised::Train(bool bClear_Samples)
2676 {
2677  if( m_nFeatures < 1 || m_nClasses < 1 )
2678  {
2679  return( false );
2680  }
2681 
2682  for(int iClass=0; iClass<m_nClasses; iClass++)
2683  {
2684  if( !m_pClasses[iClass]->Train() )
2685  {
2686  return( false );
2687  }
2688  }
2689 
2690  if( bClear_Samples )
2691  {
2693  }
2694 
2695  return( true );
2696 }
2697 
2698 
2700 // //
2702 
2703 //---------------------------------------------------------
2704 bool CSG_Classifier_Supervised::CClass::Train(void)
2705 {
2706  if( m_Samples.Get_NCols() < 1 || m_Samples.Get_NRows() < 1 )
2707  {
2708  return( false );
2709  }
2710 
2711  //-----------------------------------------------------
2712  m_Mean.Create(m_Samples.Get_NCols());
2713  m_Min .Create(m_Samples.Get_NCols());
2714  m_Max .Create(m_Samples.Get_NCols());
2715 
2716  for(int iFeature=0; iFeature<m_Samples.Get_NCols(); iFeature++)
2717  {
2719 
2720  for(int iSample=0; iSample<m_Samples.Get_NRows(); iSample++)
2721  {
2722  s += m_Samples[iSample][iFeature];
2723  }
2724 
2725  m_Mean[iFeature] = s.Get_Mean ();
2726  m_Min [iFeature] = s.Get_Minimum();
2727  m_Max [iFeature] = s.Get_Maximum();
2728  }
2729 
2730  //-----------------------------------------------------
2731  m_Cov.Create(m_Samples.Get_NCols(), m_Samples.Get_NCols());
2732 
2733  for(int iFeature=0; iFeature<m_Samples.Get_NCols(); iFeature++)
2734  {
2735  for(int jFeature=iFeature; jFeature<m_Samples.Get_NCols(); jFeature++)
2736  {
2737  double cov = 0.;
2738 
2739  for(int iSample=0; iSample<m_Samples.Get_NRows(); iSample++)
2740  {
2741  cov += (m_Samples[iSample][iFeature] - m_Mean[iFeature]) * (m_Samples[iSample][jFeature] - m_Mean[jFeature]);
2742  }
2743 
2744  if( m_Samples.Get_NRows() > 1 )
2745  {
2746  cov /= m_Samples.Get_NRows() - 1;
2747  }
2748 
2749  m_Cov[iFeature][jFeature] = m_Cov[jFeature][iFeature] = cov;
2750  }
2751  }
2752 
2753  m_Cov_Inv = m_Cov.Get_Inverse ();
2754  m_Cov_Det = m_Cov.Get_Determinant();
2755 
2756  m_Mean_Spectral = CSG_Simple_Statistics(m_Mean).Get_Mean();
2757 
2758  //-----------------------------------------------------
2759  return( true );
2760 }
2761 
2762 
2764 // //
2766 
2767 //---------------------------------------------------------
2769 {
2770  if( m_nFeatures > 0 )
2771  {
2772  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2773  {
2774  if( !Get_Class_ID(iClass).Cmp(Class_ID) )
2775  {
2776  return( iClass );
2777  }
2778  }
2779  }
2780 
2781  return( -1 );
2782 }
2783 
2784 //---------------------------------------------------------
2785 bool CSG_Classifier_Supervised::Get_Class(const CSG_Vector &Features, int &Class, double &Quality, int Method)
2786 {
2787  Class = -1; Quality = 0.;
2788 
2789  if( Get_Feature_Count() == Features.Get_N() )
2790  {
2791  switch( Method )
2792  {
2793  case SG_CLASSIFY_SUPERVISED_BinaryEncoding : _Get_Binary_Encoding (Features, Class, Quality); break;
2794  case SG_CLASSIFY_SUPERVISED_ParallelEpiped : _Get_Parallel_Epiped (Features, Class, Quality); break;
2795  case SG_CLASSIFY_SUPERVISED_MinimumDistance : _Get_Minimum_Distance (Features, Class, Quality); break;
2796  case SG_CLASSIFY_SUPERVISED_Mahalonobis : _Get_Mahalanobis_Distance (Features, Class, Quality); break;
2797  case SG_CLASSIFY_SUPERVISED_MaximumLikelihood: _Get_Maximum_Likelihood (Features, Class, Quality); break;
2798  case SG_CLASSIFY_SUPERVISED_SAM : _Get_Spectral_Angle_Mapping(Features, Class, Quality); break;
2799  case SG_CLASSIFY_SUPERVISED_SID : _Get_Spectral_Divergence (Features, Class, Quality); break;
2800  case SG_CLASSIFY_SUPERVISED_WTA : _Get_Winner_Takes_All (Features, Class, Quality); break;
2801  }
2802 
2803  return( Class >= 0 );
2804  }
2805 
2806  return( false );
2807 }
2808 
2809 
2811 // //
2813 
2814 //---------------------------------------------------------
2816 {
2817  switch( Method )
2818  {
2819  case SG_CLASSIFY_SUPERVISED_BinaryEncoding : return( _TL("Binary Encoding") );
2820  case SG_CLASSIFY_SUPERVISED_ParallelEpiped : return( _TL("Parallelepiped") );
2821  case SG_CLASSIFY_SUPERVISED_MinimumDistance : return( _TL("Minimum Distance") );
2822  case SG_CLASSIFY_SUPERVISED_Mahalonobis : return( _TL("Mahalanobis Distance") );
2823  case SG_CLASSIFY_SUPERVISED_MaximumLikelihood: return( _TL("Maximum Likelihood") );
2824  case SG_CLASSIFY_SUPERVISED_SAM : return( _TL("Spectral Angle Mapping") );
2825  case SG_CLASSIFY_SUPERVISED_SID : return( _TL("Spectral Information Divergence") );
2826  case SG_CLASSIFY_SUPERVISED_SVM : return( _TL("Support Vector Machine") );
2827  case SG_CLASSIFY_SUPERVISED_WTA : return( _TL("Winner Takes All") );
2828  }
2829 
2830  return( SG_T("") );
2831 }
2832 
2833 //---------------------------------------------------------
2835 {
2836  switch( Method )
2837  {
2838  case SG_CLASSIFY_SUPERVISED_BinaryEncoding : return( _TL("Difference") );
2839  case SG_CLASSIFY_SUPERVISED_ParallelEpiped : return( _TL("Memberships") );
2840  case SG_CLASSIFY_SUPERVISED_MinimumDistance : return( _TL("Distance") );
2841  case SG_CLASSIFY_SUPERVISED_Mahalonobis : return( _TL("Distance") );
2842  case SG_CLASSIFY_SUPERVISED_MaximumLikelihood: return( _TL("Proximity") );
2843  case SG_CLASSIFY_SUPERVISED_SAM : return( _TL("Angle") );
2844  case SG_CLASSIFY_SUPERVISED_SID : return( _TL("Divergence") );
2845  case SG_CLASSIFY_SUPERVISED_SVM : return( _TL("") );
2846  case SG_CLASSIFY_SUPERVISED_WTA : return( _TL("Votes") );
2847  }
2848 
2849  return( SG_T("") );
2850 }
2851 
2852 
2854 // //
2856 
2857 //---------------------------------------------------------
2858 // Mazer, A. S., Martin, M., Lee, M., and Solomon, J. E. (1988):
2859 // Image Processing Software for Imaging Spectrometry Analysis.
2860 // Remote Sensing of Environment, v. 24, no. 1, p. 201-210.
2861 //
2862 void CSG_Classifier_Supervised::_Get_Binary_Encoding(const CSG_Vector &Features, int &Class, double &Quality)
2863 {
2864  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2865  {
2866  CClass *pClass = m_pClasses[iClass];
2867 
2868  double Mean_Spectral = CSG_Simple_Statistics(Features).Get_Mean();
2869 
2870  int d = 0;
2871 
2872  for(int iFeature=0; iFeature<Get_Feature_Count(); iFeature++)
2873  {
2874  d += (Features(iFeature) < Mean_Spectral) == (pClass->m_Mean[iFeature] < pClass->m_Mean_Spectral) ? 0 : 1;
2875 
2876  if( iFeature == 0 ) // spectral slopes
2877  {
2878  d += (Features[iFeature ] < Features[iFeature + 1]) == (pClass->m_Mean[iFeature ] < pClass->m_Mean[iFeature + 1]) ? 0 : 1;
2879  }
2880  else if( iFeature == Get_Feature_Count() - 1 )
2881  {
2882  d += (Features[iFeature - 1] < Features[iFeature ]) == (pClass->m_Mean[iFeature - 1] < pClass->m_Mean[iFeature ]) ? 0 : 1;
2883  }
2884  else
2885  {
2886  d += (Features[iFeature - 1] < Features[iFeature + 1]) == (pClass->m_Mean[iFeature - 1] < pClass->m_Mean[iFeature + 1]) ? 0 : 1;
2887  }
2888  }
2889 
2890  if( Class < 0 || Quality > d ) // find the minimum 'Hamming' distance
2891  {
2892  Class = iClass; Quality = d;
2893  }
2894  }
2895 }
2896 
2897 //---------------------------------------------------------
2898 void CSG_Classifier_Supervised::_Get_Parallel_Epiped(const CSG_Vector &Features, int &Class, double &Quality)
2899 {
2900  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2901  {
2902  CClass *pClass = m_pClasses[iClass];
2903 
2904  bool bMember = true;
2905 
2906  for(int iFeature=0; bMember && iFeature<Get_Feature_Count(); iFeature++)
2907  {
2908  bMember = pClass->m_Min[iFeature] <= Features[iFeature] && Features[iFeature] <= pClass->m_Max[iFeature];
2909  }
2910 
2911  if( bMember )
2912  {
2913  Class = iClass; Quality++;
2914  }
2915  }
2916 }
2917 
2918 //---------------------------------------------------------
2919 void CSG_Classifier_Supervised::_Get_Minimum_Distance(const CSG_Vector &Features, int &Class, double &Quality)
2920 {
2921  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2922  {
2923  CClass *pClass = m_pClasses[iClass];
2924 
2925  double Distance = (Features - pClass->m_Mean).Get_Length();
2926 
2927  if( Class < 0 || Quality > Distance )
2928  {
2929  Class = iClass; Quality = Distance;
2930  }
2931  }
2932 
2933  if( m_Threshold_Distance > 0. && Quality > m_Threshold_Distance )
2934  {
2935  Class = -1;
2936  }
2937 }
2938 
2939 //---------------------------------------------------------
2940 void CSG_Classifier_Supervised::_Get_Mahalanobis_Distance(const CSG_Vector &Features, int &Class, double &Quality)
2941 {
2942  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2943  {
2944  CClass *pClass = m_pClasses[iClass];
2945 
2946  CSG_Vector D = Features - pClass->m_Mean;
2947 
2948  double Distance = D * (pClass->m_Cov_Inv * D);
2949 
2950  if( Class < 0 || Quality > Distance )
2951  {
2952  Class = iClass; Quality = Distance;
2953  }
2954  }
2955 
2956  if( m_Threshold_Distance > 0. && Quality > m_Threshold_Distance )
2957  {
2958  Class = -1;
2959  }
2960 }
2961 
2962 //---------------------------------------------------------
2963 void CSG_Classifier_Supervised::_Get_Maximum_Likelihood(const CSG_Vector &Features, int &Class, double &Quality)
2964 {
2965  double dSum = 0.;
2966 
2967  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
2968  {
2969  CClass *pClass = m_pClasses[iClass];
2970 
2971  CSG_Vector D = Features - pClass->m_Mean;
2972 
2973  double Distance = D * (pClass->m_Cov_Inv * D);
2974 
2975  double Probability = pow(2. * M_PI, -0.5 * m_nFeatures) * pow(pClass->m_Cov_Det, -0.5) * exp(-0.5 * Distance);
2976  // double Probability = -log(pClass->m_Cov_Det) - Distance;
2977 
2978  dSum += Probability;
2979 
2980  if( Class < 0 || Quality < Probability )
2981  {
2982  Class = iClass; Quality = Probability;
2983  }
2984  }
2985 
2986  if( Class >= 0 )
2987  {
2988  if( m_Probability_Relative )
2989  {
2990  Quality = 100. * Quality / dSum;
2991  }
2992 
2993  if( m_Threshold_Probability > 0. && Quality < m_Threshold_Probability )
2994  {
2995  Class = -1;
2996  }
2997  }
2998 }
2999 
3000 //---------------------------------------------------------
3001 void CSG_Classifier_Supervised::_Get_Spectral_Angle_Mapping(const CSG_Vector &Features, int &Class, double &Quality)
3002 {
3003  for(int iClass=0; iClass<Get_Class_Count(); iClass++)
3004  {
3005  CClass *pClass = m_pClasses[iClass];
3006 
3007  double Angle = Features.Get_Angle(pClass->m_Mean);
3008 
3009  if( Class < 0 || Quality > Angle )
3010  {
3011  Class = iClass; Quality = Angle;
3012  }
3013  }
3014 
3015  Quality *= M_RAD_TO_DEG;
3016 
3017  if( m_Threshold_Angle > 0. && Quality > m_Threshold_Angle )
3018  {
3019  Class = -1;
3020  }
3021 }
3022 
3023 //---------------------------------------------------------
3024 void CSG_Classifier_Supervised::_Get_Spectral_Divergence(const CSG_Vector &Features, int &Class, double &Quality)
3025 {
3026 }
3027 
3028 //---------------------------------------------------------
3029 void CSG_Classifier_Supervised::_Get_Winner_Takes_All(const CSG_Vector &Features, int &Class, double &Quality)
3030 {
3031  int *Votes = (int *)SG_Calloc(Get_Class_Count(), sizeof(int));
3032 
3033  for(int iMethod=0; iMethod<SG_CLASSIFY_SUPERVISED_WTA; iMethod++)
3034  {
3035  int iClass; double iQuality;
3036 
3037  if( m_bWTA[iMethod] && Get_Class(Features, iClass, iQuality, iMethod) && ++Votes[iClass] > Quality )
3038  {
3039  Class = iClass; Quality = Votes[iClass];
3040  }
3041  }
3042 
3043  SG_Free(Votes);
3044 }
3045 
3046 
3048 // //
3049 // //
3050 // //
3052 
3053 //---------------------------------------------------------
3054 // source: http://psydok.sulb.uni-saarland.de/volltexte/2004/268/html/
3055 
3056 //---------------------------------------------------------
3058 { // Hill's approx. to cumulative t-dist, Commun.A.C.M. 13,617-619.
3059  // See: J.H.Maindonald, Computational Statistics, p.295.
3060  // Calculates p given t and tail type.
3061 
3062  if( !T || !df || df < 1. )
3063  {
3064  return( -1. );
3065  }
3066 
3067  return( _Change_Tail_Type(Get_T_P(T, df), TESTDIST_TYPE_TwoTail, Type, T < 0.) );
3068 }
3069 
3070 //---------------------------------------------------------
3072 { // Keith Dear & Robert Brennan.
3073  // Returns an accurate t to tol sig. fig.'s given p & df.
3074 
3075  if( p <= 0. || p >= 1. || df < 1 )
3076  {
3077  return( -1. );
3078  }
3079 
3080  bool bNegative = (Type == TESTDIST_TYPE_Left && p < 0.5) || (Type == TESTDIST_TYPE_Right && p > 0.5);
3081  double t, p0, p1, diff = 1.;
3082 
3083  p0 = p1 = _Change_Tail_Type(p, Type, TESTDIST_TYPE_TwoTail, bNegative);
3084 
3085  while( fabs(diff) > 0.0001 )
3086  {
3087  t = Get_T_Inv(p1, df); // initial rough value
3088  diff = Get_T_P(t, df) - p0; // compare result with forward fn
3089  p1 = p1 - diff; // small adjustment to p1
3090  }
3091 
3092  return( bNegative ? -t : t );
3093 }
3094 
3095 //---------------------------------------------------------
3096 double CSG_Test_Distribution::_Change_Tail_Type(double p, TSG_Test_Distribution_Type from, TSG_Test_Distribution_Type to, bool bNegative)
3097 {
3098  if( from != to )
3099  {
3100  switch( from ) // convert any tail type to 'left'
3101  {
3102  case TESTDIST_TYPE_Left : break;
3103  case TESTDIST_TYPE_Right : p = 1. - p; break;
3104  case TESTDIST_TYPE_Middle : p = p / 2. + 0.5; if( bNegative ) p = 1. - p; break;
3105  case TESTDIST_TYPE_TwoTail: p = 1. - p / 2. ; if( bNegative ) p = 1. - p; break;
3106  // case TESTDIST_TYPE_Half : p = p + 0.5 ; if( bNegative ) p = 1. - p; break;
3107  }
3108 
3109  switch( to ) // convert p from tail type 'left' to any other
3110  {
3111  case TESTDIST_TYPE_Left : break;
3112  case TESTDIST_TYPE_Right : p = 1. - p; break;
3113  case TESTDIST_TYPE_Middle : if( bNegative ) p = 1. - p; p = 2. * (1. - p); break;
3114  case TESTDIST_TYPE_TwoTail: if( bNegative ) p = 1. - p; p = 2. * p - 1. ; break;
3115  // case TESTDIST_TYPE_Half : if( bNegative ) p = 1. - p; p = p - 0.5 ; break;
3116  }
3117  }
3118 
3119  return( p );
3120 }
3121 
3122 //---------------------------------------------------------
3124 { // Returns the two-tailed standard normal probability of z
3125  const double a1 = 0.0000053830, a2 = 0.0000488906, a3 = 0.0000380036;
3126  const double a4 = 0.0032776263, a5 = 0.0211410061, a6 = 0.0498673470;
3127 
3128  z = fabs(z);
3129 
3130  double p = (((((a1 * z + a2) * z + a3) * z + a4) * z + a5) * z + a6) * z + 1.;
3131 
3132  return( pow(p, -16) );
3133 }
3134 
3135 //---------------------------------------------------------
3137 { // Returns z given a half-middle tail type p.
3138  const double a0 = 2.5066282, a1 = -18.6150006, a2 = 41.3911977, a3 = -25.4410605;
3139  const double b1 = -8.4735109, b2 = 23.0833674, b3 = -21.0622410, b4 = 3.1308291;
3140  const double c0 = -2.7871893, c1 = -2.2979648, c2 = 4.8501413, c3 = 2.3212128;
3141  const double d1 = 3.5438892, d2 = 1.6370678;
3142 
3143  if( p > 0.42 )
3144  {
3145  double r = sqrt(-log(0.5 - p));
3146 
3147  return( (((c3 * r + c2) * r + c1) * r + c0) / ((d2 * r + d1) * r + 1.) );
3148  }
3149  else
3150  {
3151  double r = p * p;
3152 
3153  return( p * (((a3 * r + a2) * r + a1) * r + a0) / ((((b4 * r + b3) * r + b2) * r + b1) * r + 1.) );
3154  }
3155 }
3156 
3157 //---------------------------------------------------------
3158 double CSG_Test_Distribution::Get_T_P(double T, int df)
3159 { // Returns two-tail probability level given t and df.
3160  return( df == 1 ? 1. - 2. * atan(fabs(T)) / M_PI
3161  : df == 2 ? 1. - fabs(T) / sqrt(T*T + 2.)
3162  : df == 3 ? 1. - 2. * (atan(fabs(T) / sqrt(3.)) + fabs(T) * sqrt(3.) / (T*T + 3.)) / M_PI
3163  : df == 4 ? 1. - fabs(T) * (1. + 2. / (T*T + 4.)) / sqrt(T*T + 4.)
3164  : Get_Norm_P(Get_T_Z(fabs(T), df))
3165  );
3166 }
3167 
3168 //---------------------------------------------------------
3169 double CSG_Test_Distribution::Get_T_Z(double T, int df)
3170 { // Converts a t value to an approximate z value w.r.t the given df
3171  // s.t. std.norm.(z) = t(z, df) at the two-tail probability level.
3172 
3173  double A9, B9, T9, Z8, P7, B7, z;
3174 
3175  A9 = df - 0.5;
3176  B9 = 48. * A9*A9,
3177  T9 = T*T / df;
3178  Z8 = T9 >= 0.04
3179  ? A9 * log(1. + T9)
3180  : A9 * (((1. - T9 * 0.75) * T9 / 3. - 0.5) * T9 + 1.) * T9;
3181  P7 = ((0.4 * Z8 + 3.3) * Z8 + 24.) * Z8 + 85.5;
3182  B7 = 0.8 * pow(Z8, 2.) + 100. + B9;
3183  z = (1. + (-P7 / B7 + Z8 + 3.) / B9) * sqrt(Z8);
3184 
3185  return( z );
3186 }
3187 
3188 //---------------------------------------------------------
3189 double CSG_Test_Distribution::Get_T_Inv(double p, int df)
3190 { // Hill's approx. inverse t-dist.: Comm. of A.C.M Vol.13 No.10 1970 pg 620.
3191  // Calculates t given df and two-tail probability.
3192 
3193  if( df == 1 )
3194  {
3195  return( cos(p * M_PI / 2.) / sin(p * M_PI / 2.) );
3196  }
3197 
3198  if( df == 2 )
3199  {
3200  return( sqrt(2. / (p * (2. - p)) - 2.) );
3201  }
3202 
3203  double a = 1. / (df - 0.5);
3204  double b = 48. / (a*a);
3205  double c = ((20700. * a / b - 98.) * a - 16.) * a + 96.36;
3206  double d = ((94.5 / (b + c) - 3.) / b + 1.) * sqrt(a * M_PI / 2.) * df;
3207  double x = d * p;
3208  double y = pow(x, 2. / df);
3209 
3210  if( y > 0.05 + a )
3211  {
3212  x = Get_Norm_Z(0.5 * (1. - p));
3213  y = x*x;
3214 
3215  if( df < 5 )
3216  {
3217  c = c + 0.3 * (df - 4.5) * (x + 0.6);
3218  }
3219 
3220  c = (((0.05 * d * x - 5) * x - 7.) * x - 2.) * x + b + c;
3221  y = (((((0.4 * y + 6.3) * y + 36.) * y + 94.5) / c - y - 3.) / b + 1.) * x;
3222  y = a * y*y;
3223 
3224  if( y > 0.002 )
3225  {
3226  y = exp(y) - 1.;
3227  }
3228  else
3229  {
3230  y = 0.5 * y*y + y;
3231  }
3232  }
3233  else
3234  {
3235  y = ((1. / (((df + 6.) / (df * y) - 0.089 * d - 0.822) * (df + 2.) * 3.)
3236  + 0.5 / (df + 4.)) * y - 1.) * (df + 1.) / (df + 2.) + 1. / y;
3237  }
3238 
3239  return( sqrt(df * y) );
3240 }
3241 
3242 
3244 // //
3246 
3247 //---------------------------------------------------------
3248 double CSG_Test_Distribution::Get_F_Tail_from_R2(double R2, int nPredictors, int nSamples, TSG_Test_Distribution_Type Type)
3249 {
3250  double F = ((sLong)nSamples - (sLong)nPredictors - 1) * (R2 / nPredictors) / (1. - R2);
3251 
3252  return( CSG_Test_Distribution::Get_F_Tail(F, nPredictors, nSamples - nPredictors - 1, Type) );
3253 }
3254 
3255 //---------------------------------------------------------
3256 double CSG_Test_Distribution::Get_F_Tail(double F, int dfn, int dfd, TSG_Test_Distribution_Type Type)
3257 {
3258  // calculates for F, dfn(ominator) and dfd(enominator) the "tail" of the F-distribution
3259 
3260  double p = 1.;
3261 
3262  if( F >= 0.00001 && dfn > 0 && dfd > 0 )
3263  {
3264  if( F * dfn >= dfd || F > 1. + 20. / dfn + 10. / sqrt((double)dfn) )
3265  {
3266  p = Get_Gamma(F, dfn, dfd);
3267  }
3268  else
3269  {
3270  p = 1. - Get_Gamma(1. / F, dfd, dfn);
3271  }
3272  }
3273 
3274  if( p <= 0. || p >= 1. )
3275  {
3276  p = F > 1. ? 0. : F < 1. ? 1. : 0.5;
3277  }
3278 
3279  return( Type == TESTDIST_TYPE_Right ? p : 1. - p );
3280 }
3281 
3282 //---------------------------------------------------------
3283 double CSG_Test_Distribution::Get_F_Inverse(double alpha, int dfn, int dfd, TSG_Test_Distribution_Type Type)
3284 {
3285  if( alpha < 0. || alpha > 1. || dfd < 0 || dfn < 0 )
3286  {
3287  return( -1 );
3288  }
3289 
3290  if( Type != TESTDIST_TYPE_Right )
3291  {
3292  alpha = 1. - alpha;
3293  }
3294 
3295  const int ITERMAX = 100;
3296  const double EPSILON = 0.0001;
3297 
3298  int i;
3299  double lo, hi, mid, p;
3300 
3301  if( alpha <= 0.5 )
3302  {
3303  lo = 0.5;
3304  hi = lo;
3305 
3306  for(i=0; i<ITERMAX; i++)
3307  {
3308  hi *= 2.;
3309  p = Get_F_Tail(hi, dfn, dfd);
3310 
3311  if( p > alpha )
3312  {
3313  lo = hi;
3314  }
3315  else
3316  {
3317  break;
3318  }
3319  }
3320 
3321  if( p > alpha )
3322  {
3323  return( hi );
3324  }
3325  }
3326  else
3327  {
3328  hi = 2;
3329  lo = hi;
3330 
3331  for(i=0; i<ITERMAX; i++)
3332  {
3333  lo /= 2.;
3334  p = Get_F_Tail(lo, dfn, dfd);
3335 
3336  if( p < alpha )
3337  {
3338  hi = lo;
3339  }
3340  else
3341  {
3342  break;
3343  }
3344  }
3345 
3346  if( p < alpha )
3347  {
3348  return( lo );
3349  }
3350  }
3351 
3352  mid = (hi + lo) / 2.;
3353 
3354  for(i=0; i<ITERMAX && (hi-lo)>EPSILON*mid; i++)
3355  {
3356  mid = (hi + lo) / 2.;
3357  p = Get_F_Tail(mid, dfn, dfd);
3358 
3359  if( p < alpha )
3360  hi = mid;
3361  else if( p > alpha )
3362  lo = mid;
3363  else
3364  break;
3365  }
3366 
3367  return( mid );
3368 }
3369 
3370 //---------------------------------------------------------
3371 double CSG_Test_Distribution::Get_Gamma(double F, double dfn, double dfd)
3372 {
3373  // calculates for F, dfn(ominator) and dfd(enominator) the incomplete Gamma-function
3374 
3375  const double EXPMIN = -30.;
3376  const double SMALL = 0.00000000001;
3377 
3378  double x, c, er, s, n, t1, t;
3379 
3380  dfn /= 2.;
3381  dfd /= 2.;
3382 
3383  x = dfd / (dfd + dfn * F);
3384  c = Get_Log_Gamma(dfn + dfd) - Get_Log_Gamma(dfn) - Get_Log_Gamma(dfd + 1.) + dfd * log(x) + dfn * log(1. - x);
3385 
3386  if( c < EXPMIN )
3387  {
3388  return( -1. );
3389  }
3390 
3391  dfn += dfd;
3392  dfd += 1.;
3393  c = exp(c);
3394  er = SMALL / c;
3395  t = dfn * x / dfd;
3396  t1 = 0.;
3397  s = t + 1.;
3398  n = 0;
3399 
3400  while( t > er || t > t1 )
3401  {
3402  n += 1;
3403  t1 = t;
3404  t *= ((dfn + n) * x / (dfd + n));
3405  s += t;
3406  }
3407 
3408  return( s * c );
3409 }
3410 
3411 //---------------------------------------------------------
3412 double CSG_Test_Distribution::Get_Log_Gamma(double a)
3413 {
3414  // calculates the logarithm of the Gamma-function
3415 
3416  const int ARGMIN = 6;
3417 
3418  const double HL2PI = 0.91893853320467275; // = log(2. * M_PI) / 2.
3419 
3420  int n = (int)floor(ARGMIN - a + 0.0001);
3421 
3422  if( n > 0 )
3423  {
3424  a += n;
3425  }
3426 
3427  double g;
3428 
3429  g = 1. / (a*a);
3430  g = (1. - g * (1. / 30. - g * (1. / 105. - g * (1. / 140. - g / 99.)))) / (12. * a);
3431  g = g + ((a - 0.5) * log(a) - a + HL2PI);
3432 
3433  for(int i=0; i<n; i++)
3434  {
3435  a = a - 1.;
3436  g = g - log(a);
3437  }
3438 
3439  return( g );
3440 }
3441 
3442 
3444 // //
3445 // //
3446 // //
3448 
3449 //---------------------------------------------------------
3450 CSG_Matrix SG_Get_Correlation_Matrix (const CSG_Matrix &Values, bool bCovariances)
3451 {
3452  int nVariables = Values.Get_NX();
3453  int nSamples = Values.Get_NY();
3454 
3455  //-----------------------------------------------------
3456  int i, j, k;
3458  CSG_Matrix C;
3459 
3460  C.Create(nVariables, nVariables);
3461 
3462  //-----------------------------------------------------
3463  S = new CSG_Simple_Statistics[nVariables];
3464 
3465  for(j=0; j<nVariables; j++)
3466  {
3467  for(i=0; i<nSamples; i++)
3468  {
3469  S[j] += Values[i][j];
3470  }
3471  }
3472 
3473  //-----------------------------------------------------
3474  for(k=0; k<nVariables; k++)
3475  {
3476  for(j=k; j<nVariables; j++)
3477  {
3478  double cov = 0.;
3479 
3480  for(i=0; i<nSamples; i++)
3481  {
3482  cov += (Values[i][j] - S[j].Get_Mean()) * (Values[i][k] - S[k].Get_Mean());
3483  }
3484 
3485  cov /= nSamples;
3486 
3487  if( !bCovariances )
3488  {
3489  cov /= (S[j].Get_StdDev() * S[k].Get_StdDev());
3490  }
3491 
3492  C[j][k] = C[k][j] = cov;
3493  }
3494  }
3495 
3496  //-----------------------------------------------------
3497  delete[](S);
3498 
3499  return( C );
3500 }
3501 
3502 
3504 // //
3505 // //
3506 // //
3508 
3509 //---------------------------------------------------------
CSG_Classifier_Supervised::Destroy
void Destroy(void)
Definition: mat_tools.cpp:2395
SG_Get_Correlation_Matrix
CSG_Matrix SG_Get_Correlation_Matrix(const CSG_Matrix &Values, bool bCovariances)
Definition: mat_tools.cpp:3450
CSG_Table::Find_Record
virtual bool Find_Record(sLong &Index, int iField, const CSG_String &Value, bool bCreateIndex=false)
Definition: table.cpp:939
CSG_Table_Record::asDouble
double asDouble(int Field) const
Definition: table_record.cpp:527
CSG_Test_Distribution::Get_F_Tail
static double Get_F_Tail(double F, int dfn, int dfd, TSG_Test_Distribution_Type Type=TESTDIST_TYPE_Right)
Definition: mat_tools.cpp:3256
CSG_Array::Set_Array
bool Set_Array(sLong nValues, bool bShrink=true)
Definition: api_memory.cpp:310
SG_Compare_Int
int SG_Compare_Int(const void *a, const void *b)
Definition: mat_tools.cpp:187
CSG_Histogram::Get_Cumulative
size_t Get_Cumulative(int i) const
Definition: mat_tools.h:1051
CSG_Matrix::Get_Inverse
CSG_Matrix Get_Inverse(bool bSilent=true, int nSubSquare=0) const
Definition: mat_matrix.cpp:1845
SG_FREE_SAFE
#define SG_FREE_SAFE(PTR)
Definition: api_core.h:205
CSG_Cluster_Analysis::Set_Feature
bool Set_Feature(sLong iElement, int iFeature, double Value)
Definition: mat_tools.cpp:2034
SG_Get_Double_asString
CSG_String SG_Get_Double_asString(double Number, int Width, int Precision, bool bScientific)
Definition: mat_tools.cpp:159
SG_T
#define SG_T(s)
Definition: api_core.h:537
CSG_Simple_Statistics::Get_SkewnessPearson
double Get_SkewnessPearson(void)
Definition: mat_tools.cpp:606
CSG_Histogram::Update
bool Update(void)
Definition: mat_tools.cpp:1366
_TL
#define _TL(s)
Definition: api_core.h:1507
CSG_Table::Del_Records
virtual bool Del_Records(void)
Definition: table.cpp:901
CSG_Cluster_Analysis::Execute
bool Execute(int Method, int nClusters, int nMaxIterations=0, int Initialization=0)
Definition: mat_tools.cpp:2057
CSG_Array::Get_Size
sLong Get_Size(void) const
Definition: api_core.h:327
CSG_Category_Statistics::Destroy
void Destroy(void)
Definition: mat_tools.cpp:1001
CSG_Unique_Value_Statistics::m_Count
CSG_Array_Int m_Count
Definition: mat_tools.h:839
SAGA_VERSION
#define SAGA_VERSION
Definition: saga_api.h:90
CSG_Cluster_Analysis::Create
bool Create(int nFeatures)
Definition: mat_tools.cpp:2011
CSG_Classifier_Supervised::Print
CSG_String Print(void)
Definition: mat_tools.cpp:2563
CSG_Simple_Statistics::m_Minimum
double m_Minimum
Definition: mat_tools.h:792
SG_CLASSIFY_SUPERVISED_Mahalonobis
@ SG_CLASSIFY_SUPERVISED_Mahalonobis
Definition: mat_tools.h:1222
CSG_Simple_Statistics::Get_Median
double Get_Median(void)
Definition: mat_tools.cpp:669
TESTDIST_TYPE_Right
@ TESTDIST_TYPE_Right
Definition: mat_tools.h:1521
SG_Compare_Char_Ptr
int SG_Compare_Char_Ptr(const void *a, const void *b)
Definition: mat_tools.cpp:211
M_RAD_TO_DEG
#define M_RAD_TO_DEG
Definition: mat_tools.h:108
CSG_Simple_Statistics::CSG_Simple_Statistics
CSG_Simple_Statistics(void)
Definition: mat_tools.cpp:324
CSG_MetaData::Get_Children_Count
int Get_Children_Count(void) const
Definition: metadata.h:147
CSG_Classifier_Supervised::Get_Threshold_Angle
double Get_Threshold_Angle(void)
Definition: mat_tools.cpp:2423
CSG_Histogram::Get_Percentile
double Get_Percentile(double Percentile) const
Definition: mat_tools.cpp:1447
SG_Get_Rounded
double SG_Get_Rounded(double Value, int Decimals)
Definition: mat_tools.cpp:83
CSG_MetaData::Get_Content
const CSG_String & Get_Content(void) const
Definition: metadata.h:132
CSG_Matrix::Get_NRows
sLong Get_NRows(void) const
Definition: mat_tools.h:523
CSG_Table_Record
Definition: table.h:130
CSG_Array_Int::Add
bool Add(int Value)
Definition: api_memory.cpp:567
CSG_Classifier_Supervised::Get_Probability_Relative
bool Get_Probability_Relative(void)
Definition: mat_tools.cpp:2431
CSG_Histogram::Get_Break
double Get_Break(int i) const
Definition: mat_tools.h:1056
CSG_Classifier_Supervised::Train_Add_Sample
bool Train_Add_Sample(const CSG_String &Class_ID, const CSG_Vector &Features)
Definition: mat_tools.cpp:2647
TESTDIST_TYPE_Middle
@ TESTDIST_TYPE_Middle
Definition: mat_tools.h:1522
SG_Decimal_To_Degree
void SG_Decimal_To_Degree(double Value, double &Deg, double &Min, double &Sec)
Definition: mat_tools.cpp:233
CSG_Category_Statistics::~CSG_Category_Statistics
virtual ~CSG_Category_Statistics(void)
Definition: mat_tools.cpp:986
CSG_Classifier_Supervised::Set_Threshold_Angle
void Set_Threshold_Angle(double Value)
Definition: mat_tools.cpp:2422
SG_Get_String
SAGA_API_DLL_EXPORT CSG_String SG_Get_String(double Value, int Precision=-99)
Definition: api_string.cpp:1342
CSG_Unique_Value_Statistics::Get_Minority
virtual int Get_Minority(bool bWeighted=false) const
Definition: mat_tools.cpp:833
CSG_Table::Get_Record
virtual CSG_Table_Record * Get_Record(sLong Index) const
Definition: table.h:399
CSG_Histogram
Definition: mat_tools.h:1011
CSG_Classifier_Supervised::Add_Class
bool Add_Class(const CSG_String &Class_ID, const CSG_Vector &Mean, const CSG_Vector &Min, const CSG_Vector &Max, const CSG_Matrix &Cov)
Definition: mat_tools.cpp:2599
CSG_Random::Get_Uniform
static double Get_Uniform(void)
Definition: mat_tools.cpp:274
C
#define C
SG_Get_Rounded_To_SignificantFigures
double SG_Get_Rounded_To_SignificantFigures(double Value, int Decimals)
Definition: mat_tools.cpp:107
SG_Malloc
SAGA_API_DLL_EXPORT void * SG_Malloc(size_t size)
Definition: api_memory.cpp:65
SG_UI_Process_Get_Okay
bool SG_UI_Process_Get_Okay(bool bBlink)
Definition: api_callback.cpp:207
CSG_Simple_Statistics::m_Variance
double m_Variance
Definition: mat_tools.h:792
CSG_Simple_Statistics::Get_nValues_Above
sLong Get_nValues_Above(double Threshold, bool bEquals=false)
Definition: mat_tools.cpp:758
CSG_Random::Get_Gaussian
static double Get_Gaussian(double mean, double stddev)
Definition: mat_tools.cpp:298
CSG_Simple_Statistics::m_StdDev
double m_StdDev
Definition: mat_tools.h:792
CSG_Array_Int::Create
int * Create(const CSG_Array_Int &Array)
Definition: api_memory.cpp:551
CSG_Data_Object::is_NoData_Value
bool is_NoData_Value(double Value) const
Definition: dataobject.h:255
CSG_Category_Statistics::Get_Count
int Get_Count(void) const
Definition: mat_tools.cpp:1120
CSG_Classifier_Supervised::Get_Feature_Count
int Get_Feature_Count(void)
Definition: mat_tools.h:1252
saga_api.h
CSG_Table::Destroy
virtual bool Destroy(void)
Definition: table.cpp:325
grid.h
CSG_Category_Statistics::Get_Minority
int Get_Minority(void)
Definition: mat_tools.cpp:1186
CSG_Simple_Statistics::Get_Sum
double Get_Sum(void)
Definition: mat_tools.h:749
CSG_Histogram::Create
bool Create(const CSG_Histogram &Histogram)
Definition: mat_tools.cpp:1498
CSG_Classifier_Supervised::CSG_Classifier_Supervised
CSG_Classifier_Supervised(void)
Definition: mat_tools.cpp:2359
CSG_Grids::Get_NCells
sLong Get_NCells(void) const
Definition: grids.h:188
CSG_Unique_String_Statistics::Get_Class_Index
int Get_Class_Index(const CSG_String &Value) const
Definition: mat_tools.cpp:957
CSG_Vector::Add_Row
bool Add_Row(double Value=0.)
Definition: mat_matrix.cpp:188
CSG_Category_Statistics::Sort
bool Sort(void)
Definition: mat_tools.cpp:1108
CSG_Table::Get_Field_Count
int Get_Field_Count(void) const
Definition: table.h:361
CSG_Simple_Statistics::m_Gini
double m_Gini
Definition: mat_tools.h:792
CSG_Cluster_Analysis::Add_Element
bool Add_Element(void)
Definition: mat_tools.cpp:2028
CSG_Simple_Statistics::Evaluate
bool Evaluate(void)
Definition: mat_tools.cpp:473
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
CSG_Simple_Statistics::m_Skewness
double m_Skewness
Definition: mat_tools.h:792
CSG_Cluster_Analysis::~CSG_Cluster_Analysis
virtual ~CSG_Cluster_Analysis(void)
Definition: mat_tools.cpp:1991
CSG_Classifier_Supervised::Get_Class
int Get_Class(const CSG_String &Class_ID)
Definition: mat_tools.cpp:2768
CSG_MetaData::Save
bool Save(const CSG_String &File, const SG_Char *Extension=NULL) const
Definition: metadata.cpp:879
CSG_MetaData::Get_Child
CSG_MetaData * Get_Child(int Index) const
Definition: metadata.h:148
CSG_Test_Distribution::Get_F_Inverse
static double Get_F_Inverse(double alpha, int dfn, int dfd, TSG_Test_Distribution_Type Type=TESTDIST_TYPE_Right)
Definition: mat_tools.cpp:3283
CSG_Classifier_Supervised::Set_Threshold_Distance
void Set_Threshold_Distance(double Value)
Definition: mat_tools.cpp:2418
CSG_Histogram::~CSG_Histogram
virtual ~CSG_Histogram(void)
Definition: mat_tools.cpp:1269
SG_Compare_Double
int SG_Compare_Double(const void *a, const void *b)
Definition: mat_tools.cpp:199
CSG_Unique_Number_Statistics::Add_Value
void Add_Value(double Value, double Weight=1.)
Definition: mat_tools.cpp:875
CSG_Random::CSG_Random
CSG_Random(void)
Definition: mat_tools.cpp:254
SG_Calloc
SAGA_API_DLL_EXPORT void * SG_Calloc(size_t num, size_t size)
Definition: api_memory.cpp:71
CSG_Table_Record::is_NoData
bool is_NoData(int Field) const
Definition: table_record.cpp:416
CSG_String::Cmp
int Cmp(const CSG_String &String) const
Definition: api_string.cpp:515
CSG_Histogram::Get_Element_Count
size_t Get_Element_Count(void) const
Definition: mat_tools.h:1045
CSG_Natural_Breaks::Create
bool Create(class CSG_Table *pTable, int Field, int nClasses, int Histogram=0)
Definition: mat_tools.cpp:1750
CSG_Histogram::Get_Center
double Get_Center(int i) const
Definition: mat_tools.h:1059
CSG_Matrix::Get_NCols
sLong Get_NCols(void) const
Definition: mat_tools.h:522
SG_CLASSIFY_SUPERVISED_SVM
@ SG_CLASSIFY_SUPERVISED_SVM
Definition: mat_tools.h:1227
mat_tools.h
CSG_Category_Statistics::Get_Majority
int Get_Majority(void)
Definition: mat_tools.cpp:1164
CSG_Simple_Statistics::Get_Maximum
double Get_Maximum(void)
Definition: mat_tools.h:747
SG_CLASSIFY_SUPERVISED_SID
@ SG_CLASSIFY_SUPERVISED_SID
Definition: mat_tools.h:1226
CSG_Matrix::Create
bool Create(const CSG_Matrix &Matrix)
Definition: mat_matrix.cpp:836
CSG_Random::Initialize
static void Initialize(void)
Definition: mat_tools.cpp:260
CSG_Classifier_Supervised::Get_Name_of_Quality
static CSG_String Get_Name_of_Quality(int Method)
Definition: mat_tools.cpp:2834
CSG_Category_Statistics::Get_Category_Type
TSG_Data_Type Get_Category_Type(void) const
Definition: mat_tools.cpp:1007
CSG_MetaData::Cmp_Name
bool Cmp_Name(const CSG_String &String, bool bNoCase=true) const
Definition: metadata.cpp:461
CSG_Strings::Clear
void Clear(void)
Definition: api_core.h:735
CSG_Simple_Statistics::Get_nValues_Below
sLong Get_nValues_Below(double Threshold, bool bEquals=false)
Definition: mat_tools.cpp:780
CSG_Table_Record::asString
const SG_Char * asString(int Field, int Decimals=-99) const
Definition: table_record.cpp:461
CSG_Natural_Breaks::~CSG_Natural_Breaks
virtual ~CSG_Natural_Breaks(void)
Definition: mat_tools.cpp:1717
SG_CLASSIFY_SUPERVISED_WTA
@ SG_CLASSIFY_SUPERVISED_WTA
Definition: mat_tools.h:1225
CSG_Vector
Definition: mat_tools.h:360
TSG_Test_Distribution_Type
TSG_Test_Distribution_Type
Definition: mat_tools.h:1519
CSG_Test_Distribution::Get_Norm_P
static double Get_Norm_P(double Z)
Definition: mat_tools.cpp:3123
CSG_Simple_Statistics::Get_Count
sLong Get_Count(void) const
Definition: mat_tools.h:743
CSG_Natural_Breaks::Get_Count
int Get_Count(void) const
Definition: mat_tools.h:1122
CSG_Cluster_Analysis::Get_nElements
sLong Get_nElements(void) const
Definition: mat_tools.h:1175
CSG_Unique_String_Statistics::Add_Value
void Add_Value(const CSG_String &Value, double Weight=1.)
Definition: mat_tools.cpp:930
CSG_Strings::Add
bool Add(const CSG_Strings &Strings)
Definition: api_string.cpp:1046
CSG_Cluster_Analysis::Destroy
bool Destroy(void)
Definition: mat_tools.cpp:1997
CSG_Classifier_Supervised::Get_WTA
bool Get_WTA(int Method)
Definition: mat_tools.cpp:2442
CSG_Classifier_Supervised::Get_Class_ID
const CSG_String & Get_Class_ID(int iClass)
Definition: mat_tools.h:1256
CSG_Vector::Create
bool Create(const CSG_Vector &Vector)
Definition: mat_matrix.cpp:87
CSG_Simple_Statistics::Get_Minimum
double Get_Minimum(void)
Definition: mat_tools.h:746
CSG_Array::Destroy
bool Destroy(void)
Definition: api_memory.cpp:291
CSG_Histogram::Get_Quantile_Value
double Get_Quantile_Value(double Value) const
Definition: mat_tools.cpp:1456
CSG_Simple_Statistics::_Evaluate
void _Evaluate(int Level=1)
Definition: mat_tools.cpp:562
CSG_Simple_Statistics::m_Mean
double m_Mean
Definition: mat_tools.h:792
CSG_Classifier_Supervised::Load
bool Load(const CSG_String &File)
Definition: mat_tools.cpp:2456
CSG_Array::Get_Value_Size
size_t Get_Value_Size(void) const
Definition: api_core.h:326
CSG_Classifier_Supervised::Save
bool Save(const CSG_String &File, const SG_Char *Feature_Info=NULL)
Definition: mat_tools.cpp:2516
CSG_Grids::is_NoData
virtual bool is_NoData(int x, int y, int z) const
Definition: grids.h:370
sLong
signed long long sLong
Definition: api_core.h:158
CSG_Category_Statistics::asString
CSG_String asString(int iCategory) const
Definition: mat_tools.cpp:1151
CSG_MetaData::Add_Property
bool Add_Property(const CSG_String &Name, const CSG_String &Value)
Definition: metadata.cpp:559
CSG_Simple_Statistics::m_bEvaluated
int m_bEvaluated
Definition: mat_tools.h:788
CSG_Simple_Statistics::Get_StdDev
double Get_StdDev(void)
Definition: mat_tools.h:753
CSG_Table::is_Indexed
bool is_Indexed(void) const
Definition: table.h:451
CSG_Classifier_Supervised::Get_Class_Count
int Get_Class_Count(void)
Definition: mat_tools.h:1254
CSG_Table::Get_Count
sLong Get_Count(void) const
Definition: table.h:397
SG_Get_Square
double SG_Get_Square(double Value)
Definition: mat_tools.cpp:70
CSG_Simple_Statistics::Invalidate
void Invalidate(void)
Definition: mat_tools.cpp:447
CSG_Test_Distribution::Get_Norm_Z
static double Get_Norm_Z(double P)
Definition: mat_tools.cpp:3136
CSG_Category_Statistics::Add_Value
int Add_Value(int Value)
Definition: mat_tools.cpp:1047
SG_UI_Process_Set_Text
void SG_UI_Process_Set_Text(const CSG_String &Text)
Definition: api_callback.cpp:323
SG_CLASSIFY_SUPERVISED_SAM
@ SG_CLASSIFY_SUPERVISED_SAM
Definition: mat_tools.h:1224
CSG_Vector::Get_Angle
double Get_Angle(const CSG_Vector &Vector) const
Definition: mat_matrix.cpp:703
CSG_Array_Int
Definition: api_core.h:423
CSG_Simple_Statistics::is_Evaluated
int is_Evaluated(void) const
Definition: mat_tools.h:739
CSG_Vector::Get_N
int Get_N(void) const
Definition: mat_tools.h:382
CSG_Unique_Number_Statistics::Get_Class_Index
int Get_Class_Index(double Value) const
Definition: mat_tools.cpp:902
CSG_Unique_Value_Statistics::m_Weight
CSG_Vector m_Weight
Definition: mat_tools.h:841
CSG_Classifier_Supervised::Set_WTA
void Set_WTA(int Method, bool bOn)
Definition: mat_tools.cpp:2434
CSG_Classifier_Supervised::Get_Name_of_Method
static CSG_String Get_Name_of_Method(int Method)
Definition: mat_tools.cpp:2815
SG_CLASSIFY_SUPERVISED_ParallelEpiped
@ SG_CLASSIFY_SUPERVISED_ParallelEpiped
Definition: mat_tools.h:1220
CSG_Array::Create
void * Create(const CSG_Array &Array)
Definition: api_memory.cpp:250
CSG_Simple_Statistics::Get_Percentile
double Get_Percentile(double Percentile)
Definition: mat_tools.cpp:658
CSG_Category_Statistics::Get_Category
int Get_Category(int Value) const
Definition: mat_tools.cpp:1018
CSG_Histogram::Scale_Element_Count
bool Scale_Element_Count(double Scale)
Definition: mat_tools.cpp:1348
CSG_Simple_Statistics::Get_Mean
double Get_Mean(void)
Definition: mat_tools.h:751
CSG_Array::Get_Array
void * Get_Array(void) const
Definition: api_core.h:336
CSG_Table::Get_Record_byIndex
CSG_Table_Record * Get_Record_byIndex(sLong Index) const
Definition: table.h:404
CSG_Unique_Value_Statistics::Get_Count
int Get_Count(void) const
Definition: mat_tools.h:817
CSG_Category_Statistics::asDouble
double asDouble(int iCategory) const
Definition: mat_tools.cpp:1143
CSG_Table_Record::Add_Value
bool Add_Value(int Field, double Value)
Definition: table_record.cpp:329
SG_CLASSIFY_SUPERVISED_MaximumLikelihood
@ SG_CLASSIFY_SUPERVISED_MaximumLikelihood
Definition: mat_tools.h:1223
CSG_Histogram::Get_Class_Count
size_t Get_Class_Count(void) const
Definition: mat_tools.h:1043
CSG_Simple_Statistics::Set_Count
bool Set_Count(sLong Count)
Definition: mat_tools.cpp:424
CSG_Simple_Statistics::Add
void Add(const CSG_Simple_Statistics &Statistics)
Definition: mat_tools.cpp:481
CSG_String::Format
static CSG_String Format(const char *Format,...)
Definition: api_string.cpp:270
CSG_Category_Statistics::CSG_Category_Statistics
CSG_Category_Statistics(TSG_Data_Type Type=SG_DATATYPE_String)
Definition: mat_tools.cpp:978
CSG_Classifier_Supervised::Get_Threshold_Probability
double Get_Threshold_Probability(void)
Definition: mat_tools.cpp:2427
CSG_Table::Add_Field
virtual bool Add_Field(const CSG_String &Name, TSG_Data_Type Type, int Position=-1)
Definition: table.cpp:474
CSG_Histogram::Add_Value
void Add_Value(double Value)
Definition: mat_tools.cpp:1330
CSG_Table
Definition: table.h:285
CSG_Vector::Get_Size
sLong Get_Size(void) const
Definition: mat_tools.h:380
CSG_Classifier_Supervised::~CSG_Classifier_Supervised
virtual ~CSG_Classifier_Supervised(void)
Definition: mat_tools.cpp:2378
CSG_Histogram::Get_Percentile_Value
double Get_Percentile_Value(double Value) const
Definition: mat_tools.cpp:1487
CSG_String::Clear
void Clear(void)
Definition: api_string.cpp:259
CSG_Simple_Statistics::m_Maximum
double m_Maximum
Definition: mat_tools.h:792
CSG_Simple_Statistics::m_Sum
double m_Sum
Definition: mat_tools.h:792
CSG_Table_Record::asInt
int asInt(int Field) const
Definition: table_record.cpp:494
CSG_Vector::Destroy
bool Destroy(void)
Definition: mat_matrix.cpp:130
CSG_Simple_Statistics::m_nValues
sLong m_nValues
Definition: mat_tools.h:790
TESTDIST_TYPE_TwoTail
@ TESTDIST_TYPE_TwoTail
Definition: mat_tools.h:1523
CSG_Grids::asDouble
virtual double asDouble(sLong i, bool bScaled=true) const
Definition: grids.h:399
SG_Char
#define SG_Char
Definition: api_core.h:536
CSG_Array::Inc_Array
bool Inc_Array(sLong nValues=1)
Definition: api_memory.cpp:414
M_PI
#define M_PI
Definition: mat_tools.h:96
CSG_Unique_Value_Statistics::m_bWeights
bool m_bWeights
Definition: mat_tools.h:837
CSG_String
Definition: api_core.h:563
CSG_Simple_Statistics::m_Sum2
double m_Sum2
Definition: mat_tools.h:792
CSG_Histogram::Destroy
bool Destroy(void)
Definition: mat_tools.cpp:1275
SG_Get_Digit_Count
int SG_Get_Digit_Count(int Number)
Definition: mat_tools.cpp:144
CSG_Simple_Statistics::Get_Values
double * Get_Values(void) const
Definition: mat_tools.h:774
CSG_Simple_Statistics::Add_Value
void Add_Value(double Value, double Weight=1.)
Definition: mat_tools.cpp:527
CSG_Grid::asDouble
virtual double asDouble(sLong i, bool bScaled=true) const
Definition: grid.h:767
CSG_Simple_Statistics::Get_Value
double Get_Value(sLong i) const
Definition: mat_tools.h:775
CSG_Unique_String_Statistics::Create
virtual void Create(bool bWeights=false)
Definition: mat_tools.cpp:921
CSG_MetaData
Definition: metadata.h:88
CSG_Histogram::operator=
CSG_Histogram & operator=(const CSG_Histogram &Histogram)
Definition: mat_tools.cpp:1698
CSG_MetaData::Load
bool Load(const CSG_String &File, const SG_Char *Extension=NULL)
Definition: metadata.cpp:786
CSG_Unique_Value_Statistics::Get_Majority
virtual int Get_Majority(bool bWeighted=false) const
Definition: mat_tools.cpp:805
CSG_String::is_Empty
bool is_Empty(void) const
Definition: api_string.cpp:178
SG_CLASSIFY_SUPERVISED_BinaryEncoding
@ SG_CLASSIFY_SUPERVISED_BinaryEncoding
Definition: mat_tools.h:1219
CSG_Simple_Statistics::Get_Gini
double Get_Gini(void)
Definition: mat_tools.cpp:681
CSG_Matrix::Destroy
bool Destroy(void)
Definition: mat_matrix.cpp:923
CSG_Unique_Number_Statistics::Create
virtual void Create(bool bWeights=false)
Definition: mat_tools.cpp:866
CSG_Simple_Statistics::Create
bool Create(bool bHoldValues=false)
Definition: mat_tools.cpp:350
CSG_Histogram::CSG_Histogram
CSG_Histogram(void)
Definition: mat_tools.cpp:1215
CSG_Category_Statistics::asInt
int asInt(int iCategory) const
Definition: mat_tools.cpp:1135
CSG_Matrix::Get_Determinant
double Get_Determinant(void) const
Definition: mat_matrix.cpp:1806
CSG_Grid
Definition: grid.h:475
CSG_MetaData::Set_Name
void Set_Name(const CSG_String &Name)
Definition: metadata.h:129
CSG_Simple_Statistics::Get_IndexOfMinimum
sLong Get_IndexOfMinimum(void)
Definition: mat_tools.cpp:710
SG_Compare_Version
int SG_Compare_Version(const CSG_String &Version, int Major, int Minor, int Release)
Definition: saga_api.cpp:82
CSG_Classifier_Supervised::Train
bool Train(bool bClr_Samples=false)
Definition: mat_tools.cpp:2675
CSG_Table_Record::Get_Index
sLong Get_Index(void) const
Definition: table.h:136
CSG_Simple_Statistics::m_Kurtosis
double m_Kurtosis
Definition: mat_tools.h:792
CSG_Histogram::Get_Quantile
double Get_Quantile(double Quantile) const
Definition: mat_tools.cpp:1412
TABLE_INDEX_Ascending
@ TABLE_INDEX_Ascending
Definition: table.h:105
SG_CLASSIFY_SUPERVISED_MinimumDistance
@ SG_CLASSIFY_SUPERVISED_MinimumDistance
Definition: mat_tools.h:1221
CSG_Test_Distribution::Get_F_Tail_from_R2
static double Get_F_Tail_from_R2(double R2, int nPredictors, int nSamples, TSG_Test_Distribution_Type Type=TESTDIST_TYPE_Right)
Definition: mat_tools.cpp:3248
TSG_Data_Type
TSG_Data_Type
Definition: api_core.h:994
TESTDIST_TYPE_Left
@ TESTDIST_TYPE_Left
Definition: mat_tools.h:1520
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_Simple_Statistics::Get_Quantile
double Get_Quantile(double Quantile)
Definition: mat_tools.cpp:618
CSG_Table::Set_Index
bool Set_Index(CSG_Index &Index, int Field, bool bAscending=true) const
Definition: table.cpp:1457
CSG_MetaData::Add_Child
CSG_MetaData * Add_Child(void)
Definition: metadata.cpp:166
CSG_Table::Get_Field_Type
TSG_Data_Type Get_Field_Type(int iField) const
Definition: table.h:363
CSG_Classifier_Supervised::Set_Threshold_Probability
void Set_Threshold_Probability(double Value)
Definition: mat_tools.cpp:2426
CSG_Classifier_Supervised::Set_Probability_Relative
void Set_Probability_Relative(bool Value)
Definition: mat_tools.cpp:2430
CSG_Table::Add_Record
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition: table.cpp:788
CSG_Classifier_Supervised::Train_Clr_Samples
bool Train_Clr_Samples(void)
Definition: mat_tools.cpp:2636
CSG_Grid::is_NoData
virtual bool is_NoData(int x, int y) const
Definition: grid.h:701
EPSILON
#define EPSILON
Definition: mat_formula.cpp:92
CSG_Category_Statistics::Create
void Create(TSG_Data_Type Type=SG_DATATYPE_String)
Definition: mat_tools.cpp:992
CSG_Matrix
Definition: mat_tools.h:478
CSG_Grids
Definition: grids.h:119
CSG_Test_Distribution::Get_T_Tail
static double Get_T_Tail(double T, int df, TSG_Test_Distribution_Type Type=TESTDIST_TYPE_Right)
Definition: mat_tools.cpp:3057
CSG_Test_Distribution::Get_T_Inverse
static double Get_T_Inverse(double alpha, int df, TSG_Test_Distribution_Type Type=TESTDIST_TYPE_Right)
Definition: mat_tools.cpp:3071
CSG_Simple_Statistics::Get_IndexOfMaximum
sLong Get_IndexOfMaximum(void)
Definition: mat_tools.cpp:734
CSG_Array_Int::Destroy
void Destroy(void)
Definition: api_core.h:431
CSG_Simple_Statistics
Definition: mat_tools.h:723
CSG_Simple_Statistics::m_Values
CSG_Array m_Values
Definition: mat_tools.h:794
CSG_Simple_Statistics::m_Range
double m_Range
Definition: mat_tools.h:792
CSG_Natural_Breaks::CSG_Natural_Breaks
CSG_Natural_Breaks(void)
Definition: mat_tools.cpp:1713
table.h
CSG_Histogram::Get_Value
double Get_Value(double i) const
Definition: mat_tools.h:1054
CSG_Vector::Sort
bool Sort(bool bAscending=true)
Definition: mat_matrix.cpp:642
CSG_MetaData::Get_Property
const SG_Char * Get_Property(int Index) const
Definition: metadata.h:180
CSG_Classifier_Supervised::Get_Threshold_Distance
double Get_Threshold_Distance(void)
Definition: mat_tools.cpp:2419
CSG_Matrix::Get_NY
int Get_NY(void) const
Definition: mat_tools.h:521
SG_DATATYPE_ULong
@ SG_DATATYPE_ULong
Definition: api_core.h:1002
CSG_Classifier_Supervised::Create
void Create(int nFeatures)
Definition: mat_tools.cpp:2384
CSG_Array::Get_Entry
void * Get_Entry(sLong Index) const
Returns a pointer to the memory address of the requested variable. You have to type cast and derefere...
Definition: api_core.h:331
SG_Degree_To_Decimal
double SG_Degree_To_Decimal(double Deg, double Min, double Sec)
Definition: mat_tools.cpp:224
CSG_Cluster_Analysis::CSG_Cluster_Analysis
CSG_Cluster_Analysis(void)
Definition: mat_tools.cpp:1984
CSG_Grid::Get_NCells
sLong Get_NCells(void) const
Definition: grid.h:539
CSG_Simple_Statistics::m_Weights
double m_Weights
Definition: mat_tools.h:792
CSG_Matrix::Get_NX
int Get_NX(void) const
Definition: mat_tools.h:520
CSG_Simple_Statistics::m_bSorted
bool m_bSorted
Definition: mat_tools.h:786
grids.h