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