eigen_tools.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <grass/gis.h>
  4. #include <grass/gmath.h>
  5. static int egcmp(const void *pa, const void *pb);
  6. int G_math_egvorder(double *d, double **z, long bands)
  7. {
  8. double *buff;
  9. double **tmp;
  10. int i, j;
  11. /* allocate temporary matrix */
  12. buff = (double *)G_malloc(bands * (bands + 1) * sizeof(double));
  13. tmp = (double **)G_malloc(bands * sizeof(double *));
  14. for (i = 0; i < bands; i++)
  15. tmp[i] = &buff[i * (bands + 1)];
  16. /* concatenate (vertically) z and d into tmp */
  17. for (i = 0; i < bands; i++) {
  18. for (j = 0; j < bands; j++)
  19. tmp[i][j + 1] = z[j][i];
  20. tmp[i][0] = d[i];
  21. }
  22. /* sort the combined matrix */
  23. qsort(tmp, bands, sizeof(double *), egcmp);
  24. /* split tmp into z and d */
  25. for (i = 0; i < bands; i++) {
  26. for (j = 0; j < bands; j++)
  27. z[j][i] = tmp[i][j + 1];
  28. d[i] = tmp[i][0];
  29. }
  30. /* free temporary matrix */
  31. G_free(tmp);
  32. G_free(buff);
  33. return 0;
  34. }
  35. /***************************************************************************/
  36. static int egcmp(const void *pa, const void *pb)
  37. {
  38. const double *a = *(const double *const *)pa;
  39. const double *b = *(const double *const *)pb;
  40. if (*a > *b)
  41. return -1;
  42. if (*a < *b)
  43. return 1;
  44. return 0;
  45. }
  46. /***************************************************************************/