GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
eigen_tools.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <math.h>
00003 #include <grass/gis.h>
00004 #include <grass/gmath.h>
00005 
00006 static int egcmp(const void *pa, const void *pb);
00007 
00008 
00009 int G_math_egvorder(double *d, double **z, long bands)
00010 {
00011     double *buff;
00012     double **tmp;
00013     int i, j;
00014 
00015     /* allocate temporary matrix */
00016     buff = (double *)G_malloc(bands * (bands + 1) * sizeof(double));
00017     tmp = (double **)G_malloc(bands * sizeof(double *));
00018     for (i = 0; i < bands; i++)
00019         tmp[i] = &buff[i * (bands + 1)];
00020 
00021     /* concatenate (vertically) z and d into tmp */
00022     for (i = 0; i < bands; i++) {
00023         for (j = 0; j < bands; j++)
00024             tmp[i][j + 1] = z[j][i];
00025         tmp[i][0] = d[i];
00026     }
00027 
00028     /* sort the combined matrix */
00029     qsort(tmp, bands, sizeof(double *), egcmp);
00030 
00031     /* split tmp into z and d */
00032     for (i = 0; i < bands; i++) {
00033         for (j = 0; j < bands; j++)
00034             z[j][i] = tmp[i][j + 1];
00035         d[i] = tmp[i][0];
00036     }
00037 
00038     /* free temporary matrix */
00039     G_free(tmp);
00040     G_free(buff);
00041 
00042     return 0;
00043 }
00044 
00045 /***************************************************************************/
00046 
00047 static int egcmp(const void *pa, const void *pb)
00048 {
00049     const double *a = *(const double *const *)pa;
00050     const double *b = *(const double *const *)pb;
00051 
00052     if (*a > *b)
00053         return -1;
00054     if (*a < *b)
00055         return 1;
00056 
00057     return 0;
00058 }
00059 /***************************************************************************/