GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
solvers_classic_iter.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Grass numerical math interface
4 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5 * soerengebbert <at> googlemail <dot> com
6 *
7 * PURPOSE: linear equation system solvers
8 * part of the gmath library
9 *
10 * COPYRIGHT: (C) 2010 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 <assert.h>
19#include <math.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <string.h>
23#include <grass/gis.h>
24#include <grass/glocale.h>
25#include <grass/gmath.h>
26
27/*!
28 * \brief The iterative jacobi solver for sparse matrices
29 *
30 * The Jacobi solver solves the linear equation system Ax = b
31 * The result is written to the vector x.
32 *
33 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
34 * maximum is reached, the solver will abort the calculation and writes the
35 * current result into the vector x. The parameter <i>err</i> defines the error
36 * break criteria for the solver.
37 *
38 * \param Asp G_math_spvector ** -- the sparse matrix
39 * \param x double * -- the vector of unknowns
40 * \param b double * -- the right side vector
41 * \param rows int -- number of rows
42 * \param maxit int -- the maximum number of iterations
43 * \param sor double -- defines the successive overrelaxion parameter [0:1]
44 * \param error double -- defines the error break criteria
45 * \return int -- 1=success, -1=could not solve the les
46 *
47 * */
48int G_math_solver_sparse_jacobi(G_math_spvector **Asp, double *x, double *b,
49 int rows, int maxit, double sor, double error)
50{
51 unsigned int i, j, center, finished = 0;
52
53 int k;
54
55 double *Enew;
56
57 double E, err = 0;
58
59 assert(rows >= 0);
60
61 Enew = G_alloc_vector(rows);
62
63 for (k = 0; k < maxit; k++) {
64 err = 0;
65 {
66 if (k == 0) {
67 for (j = 0; j < (unsigned int)rows; j++) {
68 Enew[j] = x[j];
69 }
70 }
71 for (i = 0; i < (unsigned int)rows; i++) {
72 E = 0;
73 center = 0;
74 for (j = 0; j < Asp[i]->cols; j++) {
75 E += Asp[i]->values[j] * x[Asp[i]->index[j]];
76 if (Asp[i]->index[j] == i)
77 center = j;
78 }
79 Enew[i] = x[i] - sor * (E - b[i]) / Asp[i]->values[center];
80 }
81 for (j = 0; j < (unsigned int)rows; j++) {
82 err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
83
84 x[j] = Enew[j];
85 }
86 }
87
88 G_message(_("sparse Jacobi -- iteration %5i error %g\n"), k, err);
89
90 if (err < error) {
91 finished = 1;
92 break;
93 }
94 }
95
96 G_free(Enew);
97
98 return finished;
99}
100
101/*!
102 * \brief The iterative gauss seidel solver for sparse matrices
103 *
104 * The Jacobi solver solves the linear equation system Ax = b
105 * The result is written to the vector x.
106 *
107 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
108 * maximum is reached, the solver will abort the calculation and writes the
109 * current result into the vector x. The parameter <i>err</i> defines the error
110 * break criteria for the solver.
111 *
112 * \param Asp G_math_spvector ** -- the sparse matrix
113 * \param x double * -- the vector of unknowns
114 * \param b double * -- the right side vector
115 * \param rows int -- number of rows
116 * \param maxit int -- the maximum number of iterations
117 * \param sor double -- defines the successive overrelaxion parameter [0:2]
118 * \param error double -- defines the error break criteria
119 * \return int -- 1=success, -1=could not solve the les
120 *
121 * */
122int G_math_solver_sparse_gs(G_math_spvector **Asp, double *x, double *b,
123 int rows, int maxit, double sor, double error)
124{
125 unsigned int i, j, finished = 0;
126
127 int k;
128
129 double *Enew;
130
131 double E, err = 0;
132
133 int center;
134
135 assert(rows >= 0);
136
137 Enew = G_alloc_vector(rows);
138
139 for (k = 0; k < maxit; k++) {
140 err = 0;
141 {
142 if (k == 0) {
143 for (j = 0; j < (unsigned int)rows; j++) {
144 Enew[j] = x[j];
145 }
146 }
147 for (i = 0; i < (unsigned int)rows; i++) {
148 E = 0;
149 center = 0;
150 for (j = 0; j < Asp[i]->cols; j++) {
151 E += Asp[i]->values[j] * Enew[Asp[i]->index[j]];
152 if (Asp[i]->index[j] == i)
153 center = j;
154 }
155 Enew[i] = x[i] - sor * (E - b[i]) / Asp[i]->values[center];
156 }
157 for (j = 0; j < (unsigned int)rows; j++) {
158 err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
159
160 x[j] = Enew[j];
161 }
162 }
163
164 G_message(_("sparse SOR -- iteration %5i error %g\n"), k, err);
165
166 if (err < error) {
167 finished = 1;
168 break;
169 }
170 }
171
172 G_free(Enew);
173
174 return finished;
175}
176
177/*!
178 * \brief The iterative jacobi solver for quadratic matrices
179 *
180 * The Jacobi solver solves the linear equation system Ax = b
181 * The result is written to the vector x.
182 *
183 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
184 * maximum is reached, the solver will abort the calculation and writes the
185 * current result into the vector x. The parameter <i>err</i> defines the error
186 * break criteria for the solver.
187 *
188 * \param A double ** -- the dense matrix
189 * \param x double * -- the vector of unknowns
190 * \param b double * -- the right side vector
191 * \param rows int -- number of rows
192 * \param maxit int -- the maximum number of iterations
193 * \param sor double -- defines the successive overrelaxion parameter [0:1]
194 * \param error double -- defines the error break criteria
195 * \return int -- 1=success, -1=could not solve the les
196 *
197 * */
198int G_math_solver_jacobi(double **A, double *x, double *b, int rows, int maxit,
199 double sor, double error)
200{
201 int i, j, k;
202
203 double *Enew;
204
205 double E, err = 0;
206
207 Enew = G_alloc_vector(rows);
208
209 for (j = 0; j < rows; j++) {
210 Enew[j] = x[j];
211 }
212
213 for (k = 0; k < maxit; k++) {
214 for (i = 0; i < rows; i++) {
215 E = 0;
216 for (j = 0; j < rows; j++) {
217 E += A[i][j] * x[j];
218 }
219 Enew[i] = x[i] - sor * (E - b[i]) / A[i][i];
220 }
221 err = 0;
222 for (j = 0; j < rows; j++) {
223 err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
224 x[j] = Enew[j];
225 }
226 G_message(_("Jacobi -- iteration %5i error %g\n"), k, err);
227 if (err < error)
228 break;
229 }
230
231 G_free(Enew);
232
233 return 1;
234}
235
236/*!
237 * \brief The iterative gauss seidel solver for quadratic matrices
238 *
239 * The Jacobi solver solves the linear equation system Ax = b
240 * The result is written to the vector x.
241 *
242 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
243 * maximum is reached, the solver will abort the calculation and writes the
244 * current result into the vector x. The parameter <i>err</i> defines the error
245 * break criteria for the solver.
246 *
247 * \param A double ** -- the dense matrix
248 * \param x double * -- the vector of unknowns
249 * \param b double * -- the right side vector
250 * \param rows int -- number of rows
251 * \param maxit int -- the maximum number of iterations
252 * \param sor double -- defines the successive overrelaxion parameter [0:2]
253 * \param error double -- defines the error break criteria
254 * \return int -- 1=success, -1=could not solve the les
255 *
256 * */
257int G_math_solver_gs(double **A, double *x, double *b, int rows, int maxit,
258 double sor, double error)
259{
260 int i, j, k;
261
262 double *Enew;
263
264 double E, err = 0;
265
266 Enew = G_alloc_vector(rows);
267
268 for (j = 0; j < rows; j++) {
269 Enew[j] = x[j];
270 }
271
272 for (k = 0; k < maxit; k++) {
273 for (i = 0; i < rows; i++) {
274 E = 0;
275 for (j = 0; j < rows; j++) {
276 E += A[i][j] * Enew[j];
277 }
278 Enew[i] = x[i] - sor * (E - b[i]) / A[i][i];
279 }
280 err = 0;
281 for (j = 0; j < rows; j++) {
282 err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
283 x[j] = Enew[j];
284 }
285 G_message(_("SOR -- iteration %5i error %g\n"), k, err);
286 if (err < error)
287 break;
288 }
289
290 G_free(Enew);
291
292 return 1;
293}
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void G_message(const char *,...) __attribute__((format(printf
double * G_alloc_vector(size_t)
Vector matrix memory allocation.
Definition dalloc.c:38
#define _(str)
Definition glocale.h:10
#define assert(condition)
Definition lz4.c:291
double b
Definition r_raster.c:39
int G_math_solver_sparse_jacobi(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double sor, double error)
The iterative jacobi solver for sparse matrices.
int G_math_solver_sparse_gs(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double sor, double error)
The iterative gauss seidel solver for sparse matrices.
int G_math_solver_jacobi(double **A, double *x, double *b, int rows, int maxit, double sor, double error)
The iterative jacobi solver for quadratic matrices.
int G_math_solver_gs(double **A, double *x, double *b, int rows, int maxit, double sor, double error)
The iterative gauss seidel solver for quadratic matrices.
The row vector of the sparse matrix.
Definition gmath.h:54
double * values
Definition gmath.h:55
unsigned int cols
Definition gmath.h:56
unsigned int * index
Definition gmath.h:57
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define x