2
/*****************************************************************************
4
* MODULE: Grass PDE Numerical Library
5
* AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6
* soerengebbert <at> gmx <dot> de
8
* PURPOSE: functions to manage linear equation systems
9
* part of the gpde library
11
* COPYRIGHT: (C) 2000 by the GRASS Development Team
13
* This program is free software under the GNU General Public
14
* License (>=v2). Read the file COPYING that comes with GRASS
17
*****************************************************************************/
20
#include <grass/N_pde.h>
21
#include <grass/gmath.h>
25
* \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A, vector x and vector b
27
* This function calls #N_alloc_les_param
35
N_les *N_alloc_nquad_les(int cols, int rows, int type)
37
return N_alloc_les_param(cols, rows, type, 2);
41
* \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A and vector x
43
* This function calls #N_alloc_les_param
51
N_les *N_alloc_nquad_les_Ax(int cols, int rows, int type)
53
return N_alloc_les_param(cols, rows, type, 1);
57
* \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A
59
* This function calls #N_alloc_les_param
67
N_les *N_alloc_nquad_les_A(int cols, int rows, int type)
69
return N_alloc_les_param(cols, rows, type, 0);
73
* \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A, vector x and vector b
75
* This function calls #N_alloc_les_param
83
N_les *N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
85
return N_alloc_les_param(cols, rows, type, 2);
91
* \brief Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b
93
* This function calls #N_alloc_les_param
100
N_les *N_alloc_les(int rows, int type)
102
return N_alloc_les_param(rows, rows, type, 2);
106
* \brief Allocate memory for a quadratic linear equation system which includes the Matrix A and vector x
108
* This function calls #N_alloc_les_param
115
N_les *N_alloc_les_Ax(int rows, int type)
117
return N_alloc_les_param(rows, rows, type, 1);
121
* \brief Allocate memory for a quadratic linear equation system which includes the Matrix A
123
* This function calls #N_alloc_les_param
130
N_les *N_alloc_les_A(int rows, int type)
132
return N_alloc_les_param(rows, rows, type, 0);
136
* \brief Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b
138
* This function calls #N_alloc_les_param
145
N_les *N_alloc_les_Ax_b(int rows, int type)
147
return N_alloc_les_param(rows, rows, type, 2);
152
* \brief Allocate memory for a quadratic or not quadratic linear equation system
154
* The type of the linear equation system must be N_NORMAL_LES for
155
* a regular quadratic matrix or N_SPARSE_LES for a sparse matrix
158
* In case of N_NORMAL_LES
160
* A quadratic matrix of size rows*rows*sizeof(double) will allocated
163
* In case of N_SPARSE_LES
165
* a vector of size row will be allocated, ready to hold additional allocated sparse vectors.
166
* each sparse vector may have a different size.
168
* Parameter parts defines which parts of the les should be allocated.
169
* The number of columns and rows defines if the matrix is quadratic.
174
* \param parts int -- 2 = A, x and b; 1 = A and x; 0 = A allocated
178
N_les *N_alloc_les_param(int cols, int rows, int type, int parts)
184
if (type == N_SPARSE_LES)
186
"Allocate memory for a sparse linear equation system with %i rows\n",
190
"Allocate memory for a regular linear equation system with %i rows\n",
193
les = (N_les *) G_calloc(1, sizeof(N_les));
196
les->x = (double *)G_calloc(cols, sizeof(double));
197
for (i = 0; i < cols; i++)
203
les->b = (double *)G_calloc(cols, sizeof(double));
204
for (i = 0; i < cols; i++)
217
if (type == N_SPARSE_LES) {
218
les->Asp = G_math_alloc_spmatrix(rows);
219
les->type = N_SPARSE_LES;
222
les->A = G_alloc_matrix(rows, cols);
223
les->type = N_NORMAL_LES;
231
* \brief prints the linear equation system to stdout
252
void N_print_les(N_les * les)
257
if (les->type == N_SPARSE_LES) {
258
for (i = 0; i < les->rows; i++) {
259
for (j = 0; j < les->cols; j++) {
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]);
268
fprintf(stdout, "%4.5f ", 0.0);
271
fprintf(stdout, " * %4.5f", les->x[i]);
273
fprintf(stdout, " = %4.5f ", les->b[i]);
275
fprintf(stdout, "\n");
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]);
285
fprintf(stdout, " * %4.5f", les->x[i]);
287
fprintf(stdout, " = %4.5f ", les->b[i]);
289
fprintf(stdout, "\n");
297
* \brief Release the memory of the linear equation system
304
void N_free_les(N_les * les)
306
if (les->type == N_SPARSE_LES)
307
G_debug(2, "Releasing memory of a sparse linear equation system\n");
309
G_debug(2, "Releasing memory of a regular linear equation system\n");
318
if (les->type == N_SPARSE_LES) {
321
G_math_free_spmatrix(les->Asp, les->rows);
327
G_free_matrix(les->A);