GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
9 int G_math_egvorder(double *d, double **z, long bands)
10 {
11  double *buff;
12  double **tmp;
13  int i, j;
14 
15  /* allocate temporary matrix */
16  buff = (double *)G_malloc(bands * (bands + 1) * sizeof(double));
17  tmp = (double **)G_malloc(bands * sizeof(double *));
18  for (i = 0; i < bands; i++)
19  tmp[i] = &buff[i * (bands + 1)];
20 
21  /* concatenate (vertically) z and d into tmp */
22  for (i = 0; i < bands; i++) {
23  for (j = 0; j < bands; j++)
24  tmp[i][j + 1] = z[j][i];
25  tmp[i][0] = d[i];
26  }
27 
28  /* sort the combined matrix */
29  qsort(tmp, bands, sizeof(double *), egcmp);
30 
31  /* split tmp into z and d */
32  for (i = 0; i < bands; i++) {
33  for (j = 0; j < bands; j++)
34  z[j][i] = tmp[i][j + 1];
35  d[i] = tmp[i][0];
36  }
37 
38  /* free temporary matrix */
39  G_free(tmp);
40  G_free(buff);
41 
42  return 0;
43 }
44 
45 /***************************************************************************/
46 
47 static int egcmp(const void *pa, const void *pb)
48 {
49  const double *a = *(const double *const *)pa;
50  const double *b = *(const double *const *)pb;
51 
52  if (*a > *b)
53  return -1;
54  if (*a < *b)
55  return 1;
56 
57  return 0;
58 }
59 /***************************************************************************/
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
int G_math_egvorder(double *d, double **z, long bands)
Definition: eigen_tools.c:9
char buff[1024]
Definition: g3dcats.c:89