GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
matrix.c
Go to the documentation of this file.
1 /*!
2  * \author
3  * Lubos Mitas (original program and various modifications)
4  *
5  * \author
6  * H. Mitasova,
7  * I. Kosinovsky, D. Gerdes,
8  * D. McCauley
9  * (GRASS4.1 version of the program and GRASS4.2 modifications)
10  *
11  * \author
12  * L. Mitas,
13  * H. Mitasova,
14  * I. Kosinovsky,
15  * D.Gerdes,
16  * D. McCauley
17  * (1993, 1995)
18  *
19  * \author modified by McCauley in August 1995
20  * \author modified by Mitasova in August 1995, Nov. 1996
21  *
22  * \copyright
23  * (C) 1993-1996 by Lubos Mitas and the GRASS Development Team
24  *
25  * \copyright
26  * This program is free software under the GNU General Public License (>=v2).
27  * Read the file COPYING that comes with GRASS for details.
28  */
29 
30 #include <stdio.h>
31 #include <math.h>
32 #include <unistd.h>
33 #include <grass/gis.h>
34 #include <grass/interpf.h>
35 #include <grass/gmath.h>
36 
37 int IL_matrix_create(struct interp_params *params,
38  struct triple *points, /* points for interpolation */
39  int n_points, /* number of points */
40  double **matrix, /* matrix */
41  int *indx)
42 {
43  static double *A = NULL;
44 
45  if (!A) {
46  if (!(A = G_alloc_vector((params->KMAX2 + 2) * (params->KMAX2 + 2) +
47  1))) {
48  fprintf(stderr, "Cannot allocate memory for A\n");
49  return -1;
50  }
51  }
52  return IL_matrix_create_alloc(params, points, n_points, matrix, indx, A);
53 }
54 
55 /*!
56  * \brief Creates system of linear equations from interpolated points
57  *
58  * Creates system of linear equations represented by matrix using given
59  * points and interpolating function interp()
60  *
61  * \param params struct interp_params *
62  * \param points points for interpolation as struct triple
63  * \param n_points number of points
64  * \param[out] matrix the matrix
65  * \param indx
66  *
67  * \return -1 on failure, 1 on success
68  */
70  struct triple *points, /* points for interpolation */
71  int n_points, /* number of points */
72  double **matrix, /* matrix */
73  int *indx, double *A
74  /* temporary matrix unique for all threads */)
75 {
76  double xx, yy;
77  double rfsta2, r;
78  double d;
79  int n1, k1, k2, k, i1, l, m, i, j;
80  double fstar2 = params->fi * params->fi / 4.;
81  double RO, amaxa;
82  double rsin = 0, rcos = 0, teta,
83  scale = 0; /*anisotropy parameters - added by JH 2002 */
84  double xxr, yyr;
85 
86  if (params->theta) {
87  teta = params->theta * (M_PI / 180); /* deg to rad */
88  rsin = sin(teta);
89  rcos = cos(teta);
90  }
91  if (params->scalex)
92  scale = params->scalex;
93 
94  n1 = n_points + 1;
95 
96  /*
97  C GENERATION OF MATRIX
98  C FIRST COLUMN
99  */
100  A[1] = 0.;
101  for (k = 1; k <= n_points; k++) {
102  i1 = k + 1;
103  A[i1] = 1.;
104  }
105  /*
106  C OTHER COLUMNS
107  */
108  RO = -params->rsm;
109  /* fprintf (stderr, "sm[%d] = %f, ro=%f\n", 1, points[1].smooth, RO); */
110  for (k = 1; k <= n_points; k++) {
111  k1 = k * n1 + 1;
112  k2 = k + 1;
113  i1 = k1 + k;
114  if (params->rsm < 0.) { /*indicates variable smoothing */
115  A[i1] = -points[k - 1].sm; /* added by Mitasova nov. 96 */
116  /* G_debug(5, "sm[%d]=%f, a=%f", k, points[k-1].sm, A[i1]); */
117  }
118  else {
119  A[i1] = RO; /* constant smoothing */
120  }
121  /* if (i1 == 100) fprintf (stderr,i "A[%d] = %f\n", i1, A[i1]); */
122 
123  /* A[i1] = RO; */
124  for (l = k2; l <= n_points; l++) {
125  xx = points[k - 1].x - points[l - 1].x;
126  yy = points[k - 1].y - points[l - 1].y;
127 
128  if ((params->theta) && (params->scalex)) {
129  /* re run anisotropy */
130  xxr = xx * rcos + yy * rsin;
131  yyr = yy * rcos - xx * rsin;
132  xx = xxr;
133  yy = yyr;
134  r = scale * xx * xx + yy * yy;
135  rfsta2 = fstar2 * (scale * xx * xx + yy * yy);
136  }
137  else {
138  r = xx * xx + yy * yy;
139  rfsta2 = fstar2 * (xx * xx + yy * yy);
140  }
141 
142  if (rfsta2 == 0.) {
143  fprintf(stderr, "ident. points in segm.\n");
144  fprintf(stderr, "x[%d]=%f, x[%d]=%f, y[%d]=%f, y[%d]=%f\n",
145  k - 1, points[k - 1].x, l - 1, points[l - 1].x, k - 1,
146  points[k - 1].y, l - 1, points[l - 1].y);
147  return -1;
148  }
149  i1 = k1 + l;
150  A[i1] = params->interp(r, params->fi);
151  }
152  }
153 
154  /* C SYMMETRISATION */
155  amaxa = 1.;
156  for (k = 1; k <= n1; k++) {
157  k1 = (k - 1) * n1;
158  k2 = k + 1;
159  for (l = k2; l <= n1; l++) {
160  m = (l - 1) * n1 + k;
161  A[m] = A[k1 + l];
162  amaxa = amax1(A[m], amaxa);
163  }
164  }
165  m = 0;
166  for (i = 0; i <= n_points; i++) {
167  for (j = 0; j <= n_points; j++) {
168  m++;
169  matrix[i][j] = A[m];
170  }
171  }
172 
173  G_debug(3, "calling G_ludcmp() n=%d indx=%d", n_points, *indx);
174  if (G_ludcmp(matrix, n_points + 1, indx, &d) <= 0) {
175  /* find the inverse of the matrix */
176  fprintf(stderr, "G_ludcmp() failed! n=%d d=%.2f\n", n_points, d);
177  return -1;
178  }
179 
180  /* G_free_vector(A); */
181  return 1;
182 }
#define NULL
Definition: ccmath.h:32
int G_debug(int, const char *,...) __attribute__((format(printf
int G_ludcmp(double **, int, int *, double *)
LU decomposition.
Definition: lu.c:17
double * G_alloc_vector(size_t)
Vector matrix memory allocation.
Definition: dalloc.c:39
#define M_PI
Definition: gis.h:158
double amax1(double, double)
Definition: minmax.c:52
int IL_matrix_create_alloc(struct interp_params *params, struct triple *points, int n_points, double **matrix, int *indx, double *A)
Creates system of linear equations from interpolated points.
Definition: matrix.c:69
int IL_matrix_create(struct interp_params *params, struct triple *points, int n_points, double **matrix, int *indx)
Definition: matrix.c:37
double l
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
interp_fn * interp
Definition: interpf.h:131
double fi
Definition: interpf.h:92
double theta
Definition: interpf.h:111
double rsm
Definition: interpf.h:100
double scalex
Definition: interpf.h:114
double sm
Definition: dataquad.h:42
double x
Definition: dataquad.h:39
double y
Definition: dataquad.h:40
#define x