Skip to content

Commit 49b8066

Browse files
committed
Added a new library to grass called ccmath (LGPL license) to replace the
NR algorithms of the gmath library. Moved the linear equation solver code from gpde lib to gmath lib. Added blas level 1, 2 and 3 algorithm in gmath lib. Modified the gmath solver to use the grass blas implementation. Added wrapper for ATLAS blas level 1 algorithms. Updated the gpde library tests. Added gmath library tests for the numerical part and ccmath wrapper. Modified the groundwater flow modules (raster, raster3d)to use the gmath solver. Patched i.cca, i.pca and i.smap to use gmath vecotr and matrix functions and the ccmath wrapper for eigen value computation. Removed NR svd and eigen value code. git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39389 15284696-431f-4ddb-bdfa-cd5b030d7da7
1 parent 664305e commit 49b8066

File tree

138 files changed

+13923
-3816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+13923
-3816
lines changed

imagery/i.cca/local_proto.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33

44
#include <grass/raster.h>
55

6-
#define MX 9
7-
#define MC 50
8-
96
/* matrix.c */
10-
int product(double[MX], double, double[MX][MX], int);
11-
int setdiag(double[MX], int, double[MX][MX]);
12-
int getsqrt(double[MX][MX], int, double[MX][MX], double[MX][MX]);
13-
int solveq(double[MX][MX], int, double[MX][MX], double[MX][MX]);
14-
int matmul(double[MX][MX], double[MX][MX], double[MX][MX], int);
7+
int product(double*, double, double**, int);
8+
int setdiag(double*, int, double**);
9+
int getsqrt(double**, int, double**, double**);
10+
int solveq(double**, int, double**, double**);
11+
int print_matrix(double **matrix, int bands);
1512

1613
/* stats.c */
17-
int within(int, int, double[MC], double[MC][MX][MX], double[MX][MX], int);
18-
int between(int, int, double[MC], double[MC][MX], double[MX][MX], int);
14+
int within(int, int, double*, double***, double**, int);
15+
int between(int, int, double*, double**, double**, int);
1916

2017
/* transform.c */
21-
int transform(int[MX], int[MX], int, int, double[MX][MX], int, CELL[MX],
22-
CELL[MX]);
18+
int transform(int*, int*, int, int, double**, int, CELL*,
19+
CELL*);
2320

2421
#endif /* __LOCAL_PROTO_H__ */

imagery/i.cca/main.c

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ int main(int argc, char *argv[])
5353
int bands; /* Number of image bands */
5454
int nclass; /* Number of classes */
5555
int samptot; /* Total number of sample points */
56-
double mu[MC][MX]; /* Mean vector for image classes */
57-
double w[MX][MX]; /* Within Class Covariance Matrix */
58-
double p[MX][MX]; /* Between class Covariance Matrix */
59-
double l[MX][MX]; /* Diagonal matrix of eigenvalues */
60-
double q[MX][MX]; /* Transformation matrix */
61-
double cov[MC][MX][MX]; /* Individual class Covariance Matrix */
62-
double nsamp[MC]; /* Number of samples in a given class */
63-
double eigval[MX]; /* Eigen value vector */
64-
double eigmat[MX][MX]; /* Eigen Matrix */
65-
char tempname[50];
56+
double **mu; /* Mean vector for image classes */
57+
double **w; /* Within Class Covariance Matrix */
58+
double **p; /* Between class Covariance Matrix */
59+
double **l; /* Diagonal matrix of eigenvalues */
60+
double **q; /* Transformation matrix */
61+
double ***cov; /* Individual class Covariance Matrix */
62+
double *nsamp; /* Number of samples in a given class */
63+
double *eigval; /* Eigen value vector */
64+
double **eigmat; /* Eigen Matrix */
65+
char tempname[1024];
6666

6767
/* used to make the color tables */
68-
CELL outbandmax[MX]; /* will hold the maximums found in the out maps */
69-
CELL outbandmin[MX]; /* will hold the minimums found in the out maps */
68+
CELL *outbandmax; /* will hold the maximums found in the out maps */
69+
CELL *outbandmin; /* will hold the minimums found in the out maps */
7070
struct Colors color_tbl;
7171
struct Signature sigs;
7272
FILE *sigfp;
7373
struct Ref refs;
74-
int datafds[MX];
75-
int outfds[MX];
74+
int *datafds;
75+
int *outfds;
7676

7777
struct GModule *module;
7878
struct Option *grp_opt, *subgrp_opt, *sig_opt, *out_opt;
@@ -129,9 +129,28 @@ int main(int argc, char *argv[])
129129

130130
/* check the number of input bands */
131131
bands = refs.nfiles;
132-
if (bands > MX - 1)
133-
G_fatal_error(_("Subgroup too large. Maximum number of bands is %d\n."),
134-
MX - 1);
132+
133+
/*memory allocation*/
134+
mu = G_alloc_matrix(nclass, bands);
135+
w = G_alloc_matrix(bands, bands);
136+
p = G_alloc_matrix(bands, bands);
137+
l = G_alloc_matrix(bands, bands);
138+
q = G_alloc_matrix(bands, bands);
139+
eigmat = G_alloc_matrix(bands, bands);
140+
nsamp = G_alloc_vector(nclass);
141+
eigval = G_alloc_vector(bands);
142+
143+
cov = (double***)G_calloc(nclass, sizeof(double**));
144+
for(i = 0; i < nclass; i++)
145+
{
146+
cov[i] = G_alloc_matrix(bands,bands);
147+
}
148+
149+
outbandmax = (CELL*)G_calloc(nclass, sizeof(CELL));
150+
outbandmin = (CELL*)G_calloc(nclass, sizeof(CELL));
151+
datafds = (int*)G_calloc(nclass, sizeof(int));
152+
outfds = (int*)G_calloc(nclass, sizeof(int));
153+
135154

136155
/*
137156
Here is where the information regarding
@@ -154,14 +173,26 @@ int main(int argc, char *argv[])
154173

155174
within(samptot, nclass, nsamp, cov, w, bands);
156175
between(samptot, nclass, nsamp, mu, p, bands);
157-
jacobi(w, (long)bands, eigval, eigmat);
158-
egvorder(eigval, eigmat, (long)bands);
176+
G_math_d_copy(w[0], eigmat[0], bands*bands);
177+
G_math_eigen(eigmat, eigval, bands);
178+
G_math_egvorder(eigval, eigmat, bands);
159179
setdiag(eigval, bands, l);
160180
getsqrt(w, bands, l, eigmat);
161181
solveq(q, bands, w, p);
162-
jacobi(q, (long)bands, eigval, eigmat);
163-
egvorder(eigval, eigmat, (long)bands);
164-
matmul(q, eigmat, w, bands);
182+
G_math_d_copy(q[0], eigmat[0], bands*bands);
183+
G_math_eigen(eigmat, eigval, bands);
184+
G_math_egvorder(eigval, eigmat, bands);
185+
G_math_d_AB(eigmat, w, q, bands, bands, bands);
186+
187+
for(i = 0; i < bands; i++)
188+
{
189+
G_verbose_message("%i. eigen value: %+6.5f", i, eigval[i]);
190+
G_verbose_message("eigen vector:");
191+
for(j = 0; j < bands; j++)
192+
G_verbose_message("%+6.5f ", eigmat[i][j]);
193+
194+
}
195+
165196

166197
/* open the cell maps */
167198
for (i = 1; i <= bands; i++) {
@@ -205,6 +236,25 @@ int main(int argc, char *argv[])
205236

206237
I_free_signatures(&sigs);
207238
I_free_group_ref(&refs);
239+
240+
/*free memory*/
241+
G_free_matrix(mu);
242+
G_free_matrix(w);
243+
G_free_matrix(p);
244+
G_free_matrix(l);
245+
G_free_matrix(q);
246+
G_free_matrix(eigmat);
247+
for(i = 0; i < nclass; i++)
248+
G_free_matrix(cov[i]);
249+
G_free(cov);
250+
251+
G_free_vector(nsamp);
252+
G_free_vector(eigval);
253+
254+
G_free(outbandmax);
255+
G_free(outbandmin);
256+
G_free(datafds);
257+
G_free(outfds);
208258

209259
exit(EXIT_SUCCESS);
210260
}

imagery/i.cca/matrix.c

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,39 @@
44
#include "local_proto.h"
55

66

7-
int product(double vector[MX], double factor, double matrix1[MX][MX],
7+
int print_matrix(double **matrix, int bands)
8+
{
9+
int i, j;
10+
11+
for (i = 0; i < bands; i++)
12+
{
13+
for (j = 0; j < bands; j++) {
14+
printf("%g ", matrix[i][j]);
15+
}
16+
printf("\n");
17+
}
18+
return 0;
19+
}
20+
21+
int product(double *vector, double factor, double **matrix1,
822
int bands)
923
{
1024
int i, j;
1125

12-
for (i = 1; i <= bands; i++)
13-
for (j = 1; j <= bands; j++) {
26+
for (i = 0; i < bands; i++)
27+
for (j = 0; j < bands; j++) {
1428
matrix1[i][j] = (double)factor *(vector[i] * vector[j]);
1529
}
1630
return 0;
1731
}
1832

1933

20-
int setdiag(double eigval[MX], int bands, double l[MX][MX])
34+
int setdiag(double *eigval, int bands, double **l)
2135
{
2236
int i, j;
2337

24-
for (i = 1; i <= bands; i++)
25-
for (j = 1; j <= bands; j++)
38+
for (i = 0; i < bands; i++)
39+
for (j = 0; j < bands; j++)
2640
if (i == j)
2741
l[i][j] = eigval[i];
2842
else
@@ -32,43 +46,37 @@ int setdiag(double eigval[MX], int bands, double l[MX][MX])
3246

3347

3448
int
35-
getsqrt(double w[MX][MX], int bands, double l[MX][MX], double eigmat[MX][MX])
49+
getsqrt(double **w, int bands, double **l, double **eigmat)
3650
{
3751
int i;
38-
double tmp[MX][MX];
52+
double **tmp;
53+
54+
tmp = G_alloc_matrix(bands, bands);
3955

40-
for (i = 1; i <= bands; i++)
56+
for (i = 0; i < bands; i++)
4157
l[i][i] = 1.0 / sqrt(l[i][i]);
42-
matmul(tmp, eigmat, l, bands);
43-
transpose(eigmat, bands);
44-
matmul(w, tmp, eigmat, bands);
45-
return 0;
46-
}
4758

59+
G_math_d_AB(eigmat, l, tmp, bands, bands, bands);
60+
G_math_d_A_T(eigmat, bands);
61+
G_math_d_AB(tmp, eigmat, w, bands, bands, bands);
4862

49-
int solveq(double q[MX][MX], int bands, double w[MX][MX], double p[MX][MX])
50-
{
51-
double tmp[MX][MX];
63+
G_free_matrix(tmp);
5264

53-
matmul(tmp, w, p, bands);
54-
matmul(q, tmp, w, bands);
5565
return 0;
5666
}
5767

5868

59-
int matmul(double res[MX][MX], double m1[MX][MX], double m2[MX][MX], int dim)
69+
int solveq(double **q, int bands, double **w, double **p)
6070
{
61-
int i, j, k;
62-
double sum;
63-
64-
for (i = 1; i <= dim; i++) {
65-
for (j = 1; j <= dim; j++) {
66-
sum = 0.0;
67-
for (k = 1; k <= dim; k++)
68-
sum += m1[i][k] * m2[k][j];
69-
res[i][j] = sum;
70-
}
71-
}
71+
double **tmp;
72+
73+
tmp = G_alloc_matrix(bands, bands);
74+
75+
G_math_d_AB(w, p, tmp, bands, bands, bands);
76+
G_math_d_AB(tmp, w, q, bands, bands, bands);
77+
78+
G_free_matrix(tmp);
7279

7380
return 0;
7481
}
82+

imagery/i.cca/stats.c

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,68 @@
11
#include <grass/gis.h>
2-
#include "local_proto.h"
2+
#include <grass/gmath.h>
33

4+
#include "local_proto.h"
45

56
int
6-
within(int samptot, int nclass, double nsamp[MC], double cov[MC][MX][MX],
7-
double w[MX][MX], int bands)
7+
within(int samptot, int nclass, double *nsamp, double ***cov,
8+
double **w, int bands)
89
{
910
int i, j, k;
1011

1112
/* Initialize within class covariance matrix */
12-
for (i = 1; i <= bands; i++)
13-
for (j = 1; j <= bands; j++)
13+
for (i = 0; i < bands; i++)
14+
for (j = 0; j < bands; j++)
1415
w[i][j] = 0.0;
1516

16-
for (i = 1; i <= nclass; i++)
17-
for (j = 1; j <= bands; j++)
18-
for (k = 1; k <= bands; k++)
17+
for (i = 0; i < nclass; i++)
18+
for (j = 0; j < bands; j++)
19+
for (k = 0; k < bands; k++)
1920
w[j][k] += (nsamp[i] - 1) * cov[i][j][k];
2021

21-
for (i = 1; i <= bands; i++)
22-
for (j = 1; j <= bands; j++)
22+
for (i = 0; i < bands; i++)
23+
for (j = 0; j < bands; j++)
2324
w[i][j] = (1.0 / ((double)(samptot - nclass))) * w[i][j];
2425

2526
return 0;
2627
}
2728

2829

2930
int
30-
between(int samptot, int nclass, double nsamp[MC], double mu[MC][MX],
31-
double p[MX][MX], int bands)
31+
between(int samptot, int nclass, double *nsamp, double **mu,
32+
double **p, int bands)
3233
{
3334
int i, j, k;
34-
double tmp0[MX][MX], tmp1[MX][MX], tmp2[MX][MX];
35-
double newvec[MX];
36-
37-
for (i = 0; i < MX; i++)
38-
newvec[i] = 0.0;
35+
double **tmp0, **tmp1, **tmp2;
36+
double *newvec;
3937

40-
for (i = 1; i <= bands; i++)
41-
for (j = 1; j <= bands; j++)
42-
tmp1[i][j] = tmp2[i][j] = 0.0;
38+
tmp0 = G_alloc_matrix(bands, bands);
39+
tmp1 = G_alloc_matrix(bands, bands);
40+
tmp2 = G_alloc_matrix(bands, bands);
41+
newvec = G_alloc_vector(bands);
4342

44-
/* for (i = 1 ; i <= nclass ; i++)
45-
product(mu[i],nsamp[i],tmp0,tmp1,bands);
46-
for (i = 1 ; i <= nclass ; i++)
47-
for (j = 1 ; j <= bands ; j++)
48-
newvec[j] += nsamp[i] * mu[i][j];
49-
for (i = 1 ; i <= bands ; i++)
50-
for (j = 1 ; i <= bands ; j++)
51-
tmp2[i][j] = (newvec[i] * newvec[j]) / samptot;
52-
for (i = 1 ; i <= bands ; i++)
53-
for (j = 1 ; j <= bands ; j++)
54-
p[i][j] = (tmp1[i][j] - tmp2[i][j]) / (nclass - 1);
55-
*/
56-
57-
for (i = 1; i <= nclass; i++)
58-
for (j = 1; j <= bands; j++)
43+
for (i = 0; i < nclass; i++)
44+
for (j = 0; j < bands; j++)
5945
newvec[j] += nsamp[i] * mu[i][j];
60-
for (i = 1; i <= bands; i++)
61-
for (j = 1; j <= bands; j++)
46+
for (i = 0; i < bands; i++)
47+
for (j = 0; j < bands; j++)
6248
tmp1[i][j] = (newvec[i] * newvec[j]) / samptot;
6349

64-
for (k = 1; k <= nclass; k++) {
50+
for (k = 0; k < nclass; k++) {
6551
product(mu[k], nsamp[k], tmp0, bands);
66-
for (i = 1; i <= bands; i++)
67-
for (j = 1; j <= bands; j++)
52+
for (i = 0; i < bands; i++)
53+
for (j = 0; j < bands; j++)
6854
tmp2[i][j] += tmp0[i][j] - tmp1[i][j];
6955
}
7056

71-
for (i = 1; i <= bands; i++)
72-
for (j = 1; j <= bands; j++)
57+
for (i = 0; i < bands; i++)
58+
for (j = 0; j < bands; j++)
7359
p[i][j] = tmp2[i][j] / (nclass - 1);
7460

61+
G_free_matrix(tmp0);
62+
G_free_matrix(tmp1);
63+
G_free_matrix(tmp2);
64+
G_free_vector(newvec);
65+
7566
return 0;
7667
}
68+

0 commit comments

Comments
 (0)