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