GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-57a646b4a4
eigen_tools.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <math.h>
3 #include <grass/gis.h>
4 #include <grass/gmath.h>
5 
6 static int egcmp(const void *pa, const void *pb);
7 
8 int G_math_egvorder(double *d, double **z, long bands)
9 {
10  double *buff;
11  double **tmp;
12  int i, j;
13 
14  /* allocate temporary matrix */
15  buff = (double *)G_malloc(bands * (bands + 1) * sizeof(double));
16  tmp = (double **)G_malloc(bands * sizeof(double *));
17  for (i = 0; i < bands; i++)
18  tmp[i] = &buff[i * (bands + 1)];
19 
20  /* concatenate (vertically) z and d into tmp */
21  for (i = 0; i < bands; i++) {
22  for (j = 0; j < bands; j++)
23  tmp[i][j + 1] = z[j][i];
24  tmp[i][0] = d[i];
25  }
26 
27  /* sort the combined matrix */
28  qsort(tmp, bands, sizeof(double *), egcmp);
29 
30  /* split tmp into z and d */
31  for (i = 0; i < bands; i++) {
32  for (j = 0; j < bands; j++)
33  z[j][i] = tmp[i][j + 1];
34  d[i] = tmp[i][0];
35  }
36 
37  /* free temporary matrix */
38  G_free(tmp);
39  G_free(buff);
40 
41  return 0;
42 }
43 
44 /***************************************************************************/
45 
46 static int egcmp(const void *pa, const void *pb)
47 {
48  const double *a = *(const double *const *)pa;
49  const double *b = *(const double *const *)pb;
50 
51  if (*a > *b)
52  return -1;
53  if (*a < *b)
54  return 1;
55 
56  return 0;
57 }
58 
59 /***************************************************************************/
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
int G_math_egvorder(double *d, double **z, long bands)
Definition: eigen_tools.c:8
double b
Definition: r_raster.c:39