GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 1993-1996 Lubos Mitas
23 * SPDX-FileCopyrightText: GRASS Development Team
24 * SPDX-License-Identifier: GPL-2.0-or-later
25 */
26
27#include <stdio.h>
28#include <math.h>
29#include <unistd.h>
30#include <grass/gis.h>
31#include <grass/interpf.h>
32#include <grass/gmath.h>
33
35 struct triple *points, /* points for interpolation */
36 int n_points, /* number of points */
37 double **matrix, /* matrix */
38 int *indx)
39{
40 static double *A = NULL;
41
42 if (!A) {
43 if (!(A = G_alloc_vector((params->KMAX2 + 2) * (params->KMAX2 + 2) +
44 1))) {
45 fprintf(stderr, "Cannot allocate memory for A\n");
46 return -1;
47 }
48 }
49 return IL_matrix_create_alloc(params, points, n_points, matrix, indx, A);
50}
51
52/*!
53 * \brief Creates system of linear equations from interpolated points
54 *
55 * Creates system of linear equations represented by matrix using given
56 * points and interpolating function interp()
57 *
58 * \param params struct interp_params *
59 * \param points points for interpolation as struct triple
60 * \param n_points number of points
61 * \param[out] matrix the matrix
62 * \param indx
63 *
64 * \return -1 on failure, 1 on success
65 */
67 struct triple *points, /* points for interpolation */
68 int n_points, /* number of points */
69 double **matrix, /* matrix */
70 int *indx, double *A
71 /* temporary matrix unique for all threads */)
72{
73 double xx, yy;
74 double rfsta2, r;
75 double d;
76 int n1, k1, k2, k, i1, l, m, i, j;
77 double fstar2 = params->fi * params->fi / 4.;
78 double RO, amaxa;
79 double rsin = 0, rcos = 0, teta,
80 scale = 0; /*anisotropy parameters - added by JH 2002 */
81 double xxr, yyr;
82
83 if (params->theta) {
84 teta = params->theta * (M_PI / 180); /* deg to rad */
85 rsin = sin(teta);
86 rcos = cos(teta);
87 }
88 if (params->scalex)
89 scale = params->scalex;
90
91 n1 = n_points + 1;
92
93 /*
94 C GENERATION OF MATRIX
95 C FIRST COLUMN
96 */
97 A[1] = 0.;
98 for (k = 1; k <= n_points; k++) {
99 i1 = k + 1;
100 A[i1] = 1.;
101 }
102 /*
103 C OTHER COLUMNS
104 */
105 RO = -params->rsm;
106 /* fprintf (stderr, "sm[%d] = %f, ro=%f\n", 1, points[1].smooth, RO); */
107 for (k = 1; k <= n_points; k++) {
108 k1 = k * n1 + 1;
109 k2 = k + 1;
110 i1 = k1 + k;
111 if (params->rsm < 0.) { /*indicates variable smoothing */
112 A[i1] = -points[k - 1].sm; /* added by Mitasova nov. 96 */
113 /* G_debug(5, "sm[%d]=%f, a=%f", k, points[k-1].sm, A[i1]); */
114 }
115 else {
116 A[i1] = RO; /* constant smoothing */
117 }
118 /* if (i1 == 100) fprintf (stderr,i "A[%d] = %f\n", i1, A[i1]); */
119
120 /* A[i1] = RO; */
121 for (l = k2; l <= n_points; l++) {
122 xx = points[k - 1].x - points[l - 1].x;
123 yy = points[k - 1].y - points[l - 1].y;
124
125 if ((params->theta) && (params->scalex)) {
126 /* re run anisotropy */
127 xxr = xx * rcos + yy * rsin;
128 yyr = yy * rcos - xx * rsin;
129 xx = xxr;
130 yy = yyr;
131 r = scale * xx * xx + yy * yy;
132 rfsta2 = fstar2 * (scale * xx * xx + yy * yy);
133 }
134 else {
135 r = xx * xx + yy * yy;
136 rfsta2 = fstar2 * (xx * xx + yy * yy);
137 }
138
139 if (rfsta2 == 0.) {
140 fprintf(stderr, "ident. points in segm.\n");
141 fprintf(stderr, "x[%d]=%f, x[%d]=%f, y[%d]=%f, y[%d]=%f\n",
142 k - 1, points[k - 1].x, l - 1, points[l - 1].x, k - 1,
143 points[k - 1].y, l - 1, points[l - 1].y);
144 return -1;
145 }
146 i1 = k1 + l;
147 A[i1] = params->interp(r, params->fi);
148 }
149 }
150
151 /* C SYMMETRISATION */
152 amaxa = 1.;
153 for (k = 1; k <= n1; k++) {
154 k1 = (k - 1) * n1;
155 k2 = k + 1;
156 for (l = k2; l <= n1; l++) {
157 m = (l - 1) * n1 + k;
158 A[m] = A[k1 + l];
159 amaxa = amax1(A[m], amaxa);
160 }
161 }
162 m = 0;
163 for (i = 0; i <= n_points; i++) {
164 for (j = 0; j <= n_points; j++) {
165 m++;
166 matrix[i][j] = A[m];
167 }
168 }
169
170 G_debug(3, "calling G_ludcmp() n=%d indx=%d", n_points, *indx);
171 if (G_ludcmp(matrix, n_points + 1, indx, &d) <= 0) {
172 /* find the inverse of the matrix */
173 fprintf(stderr, "G_ludcmp() failed! n=%d d=%.2f\n", n_points, d);
174 return -1;
175 }
176
177 /* G_free_vector(A); */
178 return 1;
179}
#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:38
#define M_PI
Definition gis.h:154
double amax1(double, double)
Definition minmax.c:55
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:66
int IL_matrix_create(struct interp_params *params, struct triple *points, int n_points, double **matrix, int *indx)
Definition matrix.c:34
double l
Definition r_raster.c:37
double r
Definition r_raster.c:37
interp_fn * interp
Definition interpf.h:136
double fi
Definition interpf.h:97
double theta
Definition interpf.h:116
double rsm
Definition interpf.h:105
double scalex
Definition interpf.h:119
double sm
Definition dataquad.h:38
double x
Definition dataquad.h:35
double y
Definition dataquad.h:36
#define x