stats.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <grass/gis.h>
  2. #include <grass/gmath.h>
  3. #include "local_proto.h"
  4. int
  5. within(int samptot, int nclass, double *nsamp, double ***cov,
  6. double **w, int bands)
  7. {
  8. int i, j, k;
  9. /* Initialize within class covariance matrix */
  10. for (i = 0; i < bands; i++)
  11. for (j = 0; j < bands; j++)
  12. w[i][j] = 0.0;
  13. for (i = 0; i < nclass; i++)
  14. for (j = 0; j < bands; j++)
  15. for (k = 0; k < bands; k++)
  16. w[j][k] += (nsamp[i] - 1) * cov[i][j][k];
  17. for (i = 0; i < bands; i++)
  18. for (j = 0; j < bands; j++)
  19. w[i][j] = (1.0 / ((double)(samptot - nclass))) * w[i][j];
  20. return 0;
  21. }
  22. int
  23. between(int samptot, int nclass, double *nsamp, double **mu,
  24. double **p, int bands)
  25. {
  26. int i, j, k;
  27. double **tmp0, **tmp1, **tmp2;
  28. double *newvec;
  29. tmp0 = G_alloc_matrix(bands, bands);
  30. tmp1 = G_alloc_matrix(bands, bands);
  31. tmp2 = G_alloc_matrix(bands, bands);
  32. newvec = G_alloc_vector(bands);
  33. for (i = 0; i < nclass; i++)
  34. for (j = 0; j < bands; j++)
  35. newvec[j] += nsamp[i] * mu[i][j];
  36. for (i = 0; i < bands; i++)
  37. for (j = 0; j < bands; j++)
  38. tmp1[i][j] = (newvec[i] * newvec[j]) / samptot;
  39. for (k = 0; k < nclass; k++) {
  40. product(mu[k], nsamp[k], tmp0, bands);
  41. for (i = 0; i < bands; i++)
  42. for (j = 0; j < bands; j++)
  43. tmp2[i][j] += tmp0[i][j] - tmp1[i][j];
  44. }
  45. for (i = 0; i < bands; i++)
  46. for (j = 0; j < bands; j++)
  47. p[i][j] = tmp2[i][j] / (nclass - 1);
  48. G_free_matrix(tmp0);
  49. G_free_matrix(tmp1);
  50. G_free_matrix(tmp2);
  51. G_free_vector(newvec);
  52. return 0;
  53. }