GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
n_les.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Grass PDE Numerical Library
5 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6 * soerengebbert <at> gmx <dot> de
7 *
8 * PURPOSE: functions to manage linear equation systems
9 * part of the gpde library
10 *
11 * COPYRIGHT: (C) 2000 by the GRASS Development Team
12 *
13 * This program is free software under the GNU General Public
14 * License (>=v2). Read the file COPYING that comes with GRASS
15 * for details.
16 *
17 *****************************************************************************/
18 
19 #include <stdlib.h>
20 #include <grass/N_pde.h>
21 #include <grass/gmath.h>
22 
23 
35 N_les *N_alloc_nquad_les(int cols, int rows, int type)
36 {
37  return N_alloc_les_param(cols, rows, type, 2);
38 }
39 
51 N_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 
67 N_les *N_alloc_nquad_les_A(int cols, int rows, int type)
68 {
69  return N_alloc_les_param(cols, rows, type, 0);
70 }
71 
83 N_les *N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
84 {
85  return N_alloc_les_param(cols, rows, type, 2);
86 }
87 
88 
89 
100 N_les *N_alloc_les(int rows, int type)
101 {
102  return N_alloc_les_param(rows, rows, type, 2);
103 }
104 
115 N_les *N_alloc_les_Ax(int rows, int type)
116 {
117  return N_alloc_les_param(rows, rows, type, 1);
118 }
119 
130 N_les *N_alloc_les_A(int rows, int type)
131 {
132  return N_alloc_les_param(rows, rows, type, 0);
133 }
134 
145 N_les *N_alloc_les_Ax_b(int rows, int type)
146 {
147  return N_alloc_les_param(rows, rows, type, 2);
148 }
149 
150 
178 N_les *N_alloc_les_param(int cols, int rows, int type, int parts)
179 {
180  N_les *les;
181 
182  int i;
183 
184  if (type == N_SPARSE_LES)
185  G_debug(2,
186  "Allocate memory for a sparse linear equation system with %i rows\n",
187  rows);
188  else
189  G_debug(2,
190  "Allocate memory for a regular linear equation system with %i rows\n",
191  rows);
192 
193  les = (N_les *) G_calloc(1, sizeof(N_les));
194 
195  if (parts > 0) {
196  les->x = (double *)G_calloc(cols, sizeof(double));
197  for (i = 0; i < cols; i++)
198  les->x[i] = 0.0;
199  }
200 
201 
202  if (parts > 1) {
203  les->b = (double *)G_calloc(cols, sizeof(double));
204  for (i = 0; i < cols; i++)
205  les->b[i] = 0.0;
206  }
207 
208  les->A = NULL;
209  les->Asp = NULL;
210  les->rows = rows;
211  les->cols = cols;
212  if (rows == cols)
213  les->quad = 1;
214  else
215  les->quad = 0;
216 
217  if (type == N_SPARSE_LES) {
218  les->Asp = G_math_alloc_spmatrix(rows);
219  les->type = N_SPARSE_LES;
220  }
221  else {
222  les->A = G_alloc_matrix(rows, cols);
223  les->type = N_NORMAL_LES;
224  }
225 
226  return les;
227 }
228 
252 void N_print_les(N_les * les)
253 {
254  int i, j, k, out;
255 
256 
257  if (les->type == N_SPARSE_LES) {
258  for (i = 0; i < les->rows; i++) {
259  for (j = 0; j < les->cols; j++) {
260  out = 0;
261  for (k = 0; k < les->Asp[i]->cols; k++) {
262  if (les->Asp[i]->index[k] == j) {
263  fprintf(stdout, "%4.5f ", les->Asp[i]->values[k]);
264  out = 1;
265  }
266  }
267  if (!out)
268  fprintf(stdout, "%4.5f ", 0.0);
269  }
270  if (les->x)
271  fprintf(stdout, " * %4.5f", les->x[i]);
272  if (les->b)
273  fprintf(stdout, " = %4.5f ", les->b[i]);
274 
275  fprintf(stdout, "\n");
276  }
277  }
278  else {
279 
280  for (i = 0; i < les->rows; i++) {
281  for (j = 0; j < les->cols; j++) {
282  fprintf(stdout, "%4.5f ", les->A[i][j]);
283  }
284  if (les->x)
285  fprintf(stdout, " * %4.5f", les->x[i]);
286  if (les->b)
287  fprintf(stdout, " = %4.5f ", les->b[i]);
288 
289  fprintf(stdout, "\n");
290  }
291 
292  }
293  return;
294 }
295 
304 void N_free_les(N_les * les)
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 }
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:83
N_les * N_alloc_les_Ax_b(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b.
Definition: n_les.c:145
int quad
Definition: N_pde.h:80
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
Definition: dalloc.c:60
#define N_SPARSE_LES
Definition: N_pde.h:27
N_les * N_alloc_les(int rows, int type)
Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b.
Definition: n_les.c:100
#define NULL
Definition: ccmath.h:32
double * x
Definition: N_pde.h:74
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_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:115
double ** A
Definition: N_pde.h:76
G_math_spvector ** G_math_alloc_spmatrix(int rows)
Allocate memory for a sparse matrix.
Definition: sparse_matrix.c:58
void N_print_les(N_les *les)
prints the linear equation system to stdout
Definition: n_les.c:252
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_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:67
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
G_math_spvector ** Asp
Definition: N_pde.h:77
void G_free_matrix(double **m)
Matrix memory deallocation.
Definition: dalloc.c:169
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:178
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:35
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
int cols
Definition: N_pde.h:79
double * b
Definition: N_pde.h:75
#define N_NORMAL_LES
Definition: N_pde.h:26
int rows
Definition: N_pde.h:78
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
void G_math_free_spmatrix(G_math_spvector **Asp, int rows)
Release the memory of the sparse matrix.
The linear equation system (les) structure.
Definition: N_pde.h:72
int type
Definition: N_pde.h:81