GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
N_les.c
Go to the documentation of this file.
00001 
00002 /*****************************************************************************
00003 *
00004 * MODULE:       Grass PDE Numerical Library
00005 * AUTHOR(S):    Soeren Gebbert, Berlin (GER) Dec 2006
00006 *               soerengebbert <at> gmx <dot> de
00007 *               
00008 * PURPOSE:      functions to manage linear equation systems
00009 *               part of the gpde library
00010 *               
00011 * COPYRIGHT:    (C) 2000 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *
00017 *****************************************************************************/
00018 
00019 #include <stdlib.h>
00020 #include "grass/N_pde.h"
00021 #include "grass/gmath.h"
00022 
00023 
00035 N_les *N_alloc_nquad_les(int cols, int rows, int type)
00036 {
00037     return N_alloc_les_param(cols, rows, type, 2);
00038 }
00039 
00051 N_les *N_alloc_nquad_les_Ax(int cols, int rows, int type)
00052 {
00053     return N_alloc_les_param(cols, rows, type, 1);
00054 }
00055 
00067 N_les *N_alloc_nquad_les_A(int cols, int rows, int type)
00068 {
00069     return N_alloc_les_param(cols, rows, type, 0);
00070 }
00071 
00083 N_les *N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
00084 {
00085     return N_alloc_les_param(cols, rows, type, 2);
00086 }
00087 
00088 
00089 
00100 N_les *N_alloc_les(int rows, int type)
00101 {
00102     return N_alloc_les_param(rows, rows, type, 2);
00103 }
00104 
00115 N_les *N_alloc_les_Ax(int rows, int type)
00116 {
00117     return N_alloc_les_param(rows, rows, type, 1);
00118 }
00119 
00130 N_les *N_alloc_les_A(int rows, int type)
00131 {
00132     return N_alloc_les_param(rows, rows, type, 0);
00133 }
00134 
00145 N_les *N_alloc_les_Ax_b(int rows, int type)
00146 {
00147     return N_alloc_les_param(rows, rows, type, 2);
00148 }
00149 
00150 
00178 N_les *N_alloc_les_param(int cols, int rows, int type, int parts)
00179 {
00180     N_les *les;
00181 
00182     int i;
00183 
00184     if (type == N_SPARSE_LES)
00185         G_debug(2,
00186                 "Allocate memory for a sparse linear equation system with %i rows\n",
00187                 rows);
00188     else
00189         G_debug(2,
00190                 "Allocate memory for a regular linear equation system with %i rows\n",
00191                 rows);
00192 
00193     les = (N_les *) G_calloc(1, sizeof(N_les));
00194 
00195     if (parts > 0) {
00196         les->x = (double *)G_calloc(cols, sizeof(double));
00197         for (i = 0; i < cols; i++)
00198             les->x[i] = 0.0;
00199     }
00200 
00201 
00202     if (parts > 1) {
00203         les->b = (double *)G_calloc(cols, sizeof(double));
00204         for (i = 0; i < cols; i++)
00205             les->b[i] = 0.0;
00206     }
00207 
00208     les->A = NULL;
00209     les->Asp = NULL;
00210     les->rows = rows;
00211     les->cols = cols;
00212     if (rows == cols)
00213         les->quad = 1;
00214     else
00215         les->quad = 0;
00216 
00217     if (type == N_SPARSE_LES) {
00218         les->Asp = G_math_alloc_spmatrix(rows);
00219         les->type = N_SPARSE_LES;
00220     }
00221     else {
00222         les->A = G_alloc_matrix(rows, cols);
00223         les->type = N_NORMAL_LES;
00224     }
00225 
00226     return les;
00227 }
00228 
00252 void N_print_les(N_les * les)
00253 {
00254     int i, j, k, out;
00255 
00256 
00257     if (les->type == N_SPARSE_LES) {
00258         for (i = 0; i < les->rows; i++) {
00259             for (j = 0; j < les->cols; j++) {
00260                 out = 0;
00261                 for (k = 0; k < les->Asp[i]->cols; k++) {
00262                     if (les->Asp[i]->index[k] == j) {
00263                         fprintf(stdout, "%4.5f ", les->Asp[i]->values[k]);
00264                         out = 1;
00265                     }
00266                 }
00267                 if (!out)
00268                     fprintf(stdout, "%4.5f ", 0.0);
00269             }
00270             if (les->x)
00271                 fprintf(stdout, "  *  %4.5f", les->x[i]);
00272             if (les->b)
00273                 fprintf(stdout, " =  %4.5f ", les->b[i]);
00274 
00275             fprintf(stdout, "\n");
00276         }
00277     }
00278     else {
00279 
00280         for (i = 0; i < les->rows; i++) {
00281             for (j = 0; j < les->cols; j++) {
00282                 fprintf(stdout, "%4.5f ", les->A[i][j]);
00283             }
00284             if (les->x)
00285                 fprintf(stdout, "  *  %4.5f", les->x[i]);
00286             if (les->b)
00287                 fprintf(stdout, " =  %4.5f ", les->b[i]);
00288 
00289             fprintf(stdout, "\n");
00290         }
00291 
00292     }
00293     return;
00294 }
00295 
00304 void N_free_les(N_les * les)
00305 {
00306     int i;
00307 
00308     if (les->type == N_SPARSE_LES)
00309         G_debug(2, "Releasing memory of a sparse linear equation system\n");
00310     else
00311         G_debug(2, "Releasing memory of a regular linear equation system\n");
00312 
00313     if (les) {
00314 
00315         if (les->x)
00316             G_free(les->x);
00317         if (les->b)
00318             G_free(les->b);
00319 
00320         if (les->type == N_SPARSE_LES) {
00321 
00322             if (les->Asp) {
00323                 G_math_free_spmatrix(les->Asp, les->rows);
00324             }
00325         }
00326         else {
00327 
00328             if (les->A) {
00329                 G_free_matrix(les->A);
00330             }
00331         }
00332 
00333         free(les);
00334     }
00335 
00336     return;
00337 }