GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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  int i;
307 
308  if (les->type == N_SPARSE_LES)
309  G_debug(2, "Releasing memory of a sparse linear equation system\n");
310  else
311  G_debug(2, "Releasing memory of a regular linear equation system\n");
312 
313  if (les) {
314 
315  if (les->x)
316  G_free(les->x);
317  if (les->b)
318  G_free(les->b);
319 
320  if (les->type == N_SPARSE_LES) {
321 
322  if (les->Asp) {
323  G_math_free_spmatrix(les->Asp, les->rows);
324  }
325  }
326  else {
327 
328  if (les->A) {
329  G_free_matrix(les->A);
330  }
331  }
332 
333  free(les);
334  }
335 
336  return;
337 }
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
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int quad
Definition: N_pde.h:94
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_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
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
Definition: dalloc.c:60
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 N_SPARSE_LES
Definition: N_pde.h:43
double * x
Definition: N_pde.h:88
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
double ** A
Definition: N_pde.h:90
G_math_spvector ** G_math_alloc_spmatrix(int rows)
Allocate memory for a sparse matrix.
Definition: sparse_matrix.c:58
G_math_spvector ** Asp
Definition: N_pde.h:91
void G_free_matrix(double **m)
Matrix memory deallocation.
Definition: dalloc.c:169
void G_math_free_spmatrix(G_math_spvector **spmatrix, int rows)
Release the memory of the sparse matrix.
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
return NULL
Definition: dbfopen.c:1394
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:93
tuple cols
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
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
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
void free(void *)
double * b
Definition: N_pde.h:89
#define N_NORMAL_LES
Definition: N_pde.h:42
int rows
Definition: N_pde.h:92
void N_free_les(N_les *les)
Release the memory of the linear equation system.
Definition: N_les.c:304
The linear equation system (les) structure.
Definition: N_pde.h:86
int type
Definition: N_pde.h:95
void N_print_les(N_les *les)
prints the linear equation system to stdout
Definition: N_les.c:252