GRASS 8 Programmer's Manual 8.6.0dev(2026)-0f6a7341fc
Loading...
Searching...
No Matches
n_les.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Grass PDE Numerical Library
4 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5 * soerengebbert <at> gmx <dot> de
6 *
7 * PURPOSE: functions to manage linear equation systems
8 * part of the gpde library
9 *
10 * SPDX-FileCopyrightText: 2000 GRASS Development Team
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 *****************************************************************************/
14
15#include <stdlib.h>
16#include <grass/N_pde.h>
17#include <grass/gmath.h>
18
19/*!
20 * \brief Allocate memory for a (not) quadratic linear equation system which
21 * includes the Matrix A, vector x and vector b
22 *
23 * This function calls #N_alloc_les_param
24 *
25 * \param cols int
26 * \param rows int
27 * \param type int
28 * \return N_les *
29 *
30 * */
31N_les *N_alloc_nquad_les(int cols, int rows, int type)
32{
33 return N_alloc_les_param(cols, rows, type, 2);
34}
35
36/*!
37 * \brief Allocate memory for a (not) quadratic linear equation system which
38 * includes the Matrix A and vector x
39 *
40 * This function calls #N_alloc_les_param
41 *
42 * \param cols int
43 * \param rows int
44 * \param type int
45 * \return N_les *
46 *
47 * */
48N_les *N_alloc_nquad_les_Ax(int cols, int rows, int type)
49{
50 return N_alloc_les_param(cols, rows, type, 1);
51}
52
53/*!
54 * \brief Allocate memory for a (not) quadratic linear equation system which
55 * includes the Matrix A
56 *
57 * This function calls #N_alloc_les_param
58 *
59 * \param cols int
60 * \param rows int
61 * \param type int
62 * \return N_les *
63 *
64 * */
65N_les *N_alloc_nquad_les_A(int cols, int rows, int type)
66{
67 return N_alloc_les_param(cols, rows, type, 0);
68}
69
70/*!
71 * \brief Allocate memory for a (not) quadratic linear equation system which
72 * includes the Matrix A, vector x and vector b
73 *
74 * This function calls #N_alloc_les_param
75 *
76 * \param cols int
77 * \param rows int
78 * \param type int
79 * \return N_les *
80 *
81 * */
82N_les *N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
83{
84 return N_alloc_les_param(cols, rows, type, 2);
85}
86
87/*!
88 * \brief Allocate memory for a quadratic linear equation system which includes
89 * the Matrix A, vector x and vector b
90 *
91 * This function calls #N_alloc_les_param
92 *
93 * \param rows int
94 * \param type int
95 * \return N_les *
96 *
97 * */
98N_les *N_alloc_les(int rows, int type)
99{
100 return N_alloc_les_param(rows, rows, type, 2);
101}
102
103/*!
104 * \brief Allocate memory for a quadratic linear equation system which includes
105 * the Matrix A and vector x
106 *
107 * This function calls #N_alloc_les_param
108 *
109 * \param rows int
110 * \param type int
111 * \return N_les *
112 *
113 * */
114N_les *N_alloc_les_Ax(int rows, int type)
115{
116 return N_alloc_les_param(rows, rows, type, 1);
117}
118
119/*!
120 * \brief Allocate memory for a quadratic linear equation system which includes
121 * the Matrix A
122 *
123 * This function calls #N_alloc_les_param
124 *
125 * \param rows int
126 * \param type int
127 * \return N_les *
128 *
129 * */
130N_les *N_alloc_les_A(int rows, int type)
131{
132 return N_alloc_les_param(rows, rows, type, 0);
133}
134
135/*!
136 * \brief Allocate memory for a quadratic linear equation system which includes
137 * the Matrix A, vector x and vector b
138 *
139 * This function calls #N_alloc_les_param
140 *
141 * \param rows int
142 * \param type int
143 * \return N_les *
144 *
145 * */
146N_les *N_alloc_les_Ax_b(int rows, int type)
147{
148 return N_alloc_les_param(rows, rows, type, 2);
149}
150
151/*!
152 * \brief Allocate memory for a quadratic or not quadratic linear equation
153 * system
154 *
155 * The type of the linear equation system must be N_NORMAL_LES for
156 * a regular quadratic matrix or N_SPARSE_LES for a sparse matrix
157 *
158 * <p>
159 * In case of N_NORMAL_LES
160 *
161 * A quadratic matrix of size rows*rows*sizeof(double) will allocated
162 *
163 * <p>
164 * In case of N_SPARSE_LES
165 *
166 * a vector of size row will be allocated, ready to hold additional allocated
167 * sparse vectors. each sparse vector may have a different size.
168 *
169 * Parameter parts defines which parts of the les should be allocated.
170 * The number of columns and rows defines if the matrix is quadratic.
171 *
172 * \param cols int
173 * \param rows int
174 * \param type int
175 * \param parts int -- 2 = A, x and b; 1 = A and x; 0 = A allocated
176 * \return N_les *
177 *
178 * */
179N_les *N_alloc_les_param(int cols, int rows, int type, int parts)
180{
181 N_les *les;
182
183 int i;
184
185 if (type == N_SPARSE_LES)
186 G_debug(2,
187 "Allocate memory for a sparse linear equation system with %i "
188 "rows\n",
189 rows);
190 else
191 G_debug(2,
192 "Allocate memory for a regular linear equation system with %i "
193 "rows\n",
194 rows);
195
196 les = (N_les *)G_calloc(1, sizeof(N_les));
197
198 if (parts > 0) {
199 les->x = (double *)G_calloc(cols, sizeof(double));
200 for (i = 0; i < cols; i++)
201 les->x[i] = 0.0;
202 }
203
204 if (parts > 1) {
205 les->b = (double *)G_calloc(cols, sizeof(double));
206 for (i = 0; i < cols; i++)
207 les->b[i] = 0.0;
208 }
209
210 les->A = NULL;
211 les->Asp = NULL;
212 les->rows = rows;
213 les->cols = cols;
214 if (rows == cols)
215 les->quad = 1;
216 else
217 les->quad = 0;
218
219 if (type == N_SPARSE_LES) {
220 les->Asp = G_math_alloc_spmatrix(rows);
221 les->type = N_SPARSE_LES;
222 }
223 else {
224 les->A = G_alloc_matrix(rows, cols);
225 les->type = N_NORMAL_LES;
226 }
227
228 return les;
229}
230
231/*!
232 *
233 * \brief prints the linear equation system to stdout
234 *
235 * <p>
236 * Format:
237 * A*x = b
238 *
239 * <p>
240 * Example
241 \verbatim
242
243 2 1 1 1 * 2 = 0.1
244 1 2 0 0 * 3 = 0.2
245 1 0 2 0 * 3 = 0.2
246 1 0 0 2 * 2 = 0.1
247
248 \endverbatim
249 *
250 * \param les N_les *
251 * \return void
252 *
253 * */
255{
256 int i, j, k, out;
257
258 if (les->type == N_SPARSE_LES) {
259 for (i = 0; i < les->rows; i++) {
260 for (j = 0; j < les->cols; j++) {
261 out = 0;
262 for (k = 0; (unsigned int)k < les->Asp[i]->cols; k++) {
263 if (les->Asp[i]->index[k] == (unsigned int)j) {
264 fprintf(stdout, "%4.5f ", les->Asp[i]->values[k]);
265 out = 1;
266 }
267 }
268 if (!out)
269 fprintf(stdout, "%4.5f ", 0.0);
270 }
271 if (les->x)
272 fprintf(stdout, " * %4.5f", les->x[i]);
273 if (les->b)
274 fprintf(stdout, " = %4.5f ", les->b[i]);
275
276 fprintf(stdout, "\n");
277 }
278 }
279 else {
280
281 for (i = 0; i < les->rows; i++) {
282 for (j = 0; j < les->cols; j++) {
283 fprintf(stdout, "%4.5f ", les->A[i][j]);
284 }
285 if (les->x)
286 fprintf(stdout, " * %4.5f", les->x[i]);
287 if (les->b)
288 fprintf(stdout, " = %4.5f ", les->b[i]);
289
290 fprintf(stdout, "\n");
291 }
292 }
293 return;
294}
295
296/*!
297 * \brief Release the memory of the linear equation system
298 *
299 * \param les N_les *
300 * \return void
301 *
302 * */
303
305{
306 if (les->type == N_SPARSE_LES)
307 G_debug(2, "Releasing memory of a sparse linear equation system\n");
308 else
309 G_debug(2, "Releasing memory of a regular linear equation system\n");
310
311 if (les) {
312
313 if (les->x)
314 G_free(les->x);
315 if (les->b)
316 G_free(les->b);
317
318 if (les->type == N_SPARSE_LES) {
319
320 if (les->Asp) {
321 G_math_free_spmatrix(les->Asp, les->rows);
322 }
323 }
324 else {
325
326 if (les->A) {
327 G_free_matrix(les->A);
328 }
329 }
330
331 free(les);
332 }
333
334 return;
335}
#define N_NORMAL_LES
Definition N_pde.h:22
#define N_SPARSE_LES
Definition N_pde.h:23
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_calloc(m, n)
Definition defs/gis.h:137
int G_debug(int, const char *,...) __attribute__((format(printf
void G_free_matrix(double **)
Matrix memory deallocation.
Definition dalloc.c:154
void G_math_free_spmatrix(G_math_spvector **, int)
Release the memory of the sparse matrix.
G_math_spvector ** G_math_alloc_spmatrix(int)
Allocate memory for a sparse matrix.
double ** G_alloc_matrix(int, int)
Matrix memory allocation.
Definition dalloc.c:55
void N_print_les(N_les *les)
prints the linear equation system to stdout
Definition n_les.c:254
N_les * N_alloc_les_param(int cols, int rows, int type, int parts)
Allocate memory for a quadratic or not quadratic linear equation system.
Definition n_les.c:179
N_les * N_alloc_les_Ax(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A and vector x.
Definition n_les.c:114
N_les * N_alloc_nquad_les_Ax(int cols, int rows, int type)
Allocate memory for a (not) quadratic linear equation system which includes the Matrix A and vector x...
Definition n_les.c:48
N_les * N_alloc_nquad_les(int cols, int rows, int type)
Allocate memory for a (not) quadratic linear equation system which includes the Matrix A,...
Definition n_les.c:31
N_les * N_alloc_les_A(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A.
Definition n_les.c:130
N_les * N_alloc_nquad_les_A(int cols, int rows, int type)
Allocate memory for a (not) quadratic linear equation system which includes the Matrix A.
Definition n_les.c:65
N_les * N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
Allocate memory for a (not) quadratic linear equation system which includes the Matrix A,...
Definition n_les.c:82
N_les * N_alloc_les_Ax_b(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A,...
Definition n_les.c:146
void N_free_les(N_les *les)
Release the memory of the linear equation system.
Definition n_les.c:304
N_les * N_alloc_les(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A,...
Definition n_les.c:98
void free(void *)
The linear equation system (les) structure.
Definition N_pde.h:68