GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
solvers_direct.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 <math.h>
16#include <unistd.h>
17#include <stdio.h>
18#include <string.h>
19#include <grass/gis.h>
20#include <grass/gmath.h>
21#include <grass/glocale.h>
22
23#define TINY 1.0e-20
24#define COMP_PIVOT 100
25
26/*!
27 * \brief The gauss elimination solver for quardatic matrices
28 *
29 * This solver does not support sparse matrices
30 * The matrix A will be overwritten.
31 * The result is written to the vector x
32 *
33 * \param A double **
34 * \param x double *
35 * \param b double *
36 * \param rows int
37 * \return int -- 1 success
38 * */
39int G_math_solver_gauss(double **A, double *x, double *b, int rows)
40{
41 G_message(_("Starting direct gauss elimination solver"));
42
45
46 return 1;
47}
48
49/*!
50 * \brief The LU solver for quardatic matrices
51 *
52 * This solver does not support sparse matrices
53 * The matrix A will be overwritten.
54 * The result is written to the vector x in the G_math_les structure
55 *
56 *
57 * \param A double **
58 * \param x double *
59 * \param b double *
60 * \param rows int
61 * \return int -- 1 success
62 * */
63int G_math_solver_lu(double **A, double *x, double *b, int rows)
64{
65 int i;
66
67 double *c, *tmpv;
68
69 G_message(_("Starting direct lu decomposition solver"));
70
71 tmpv = G_alloc_vector(rows);
72 c = G_alloc_vector(rows);
73
74 G_math_lu_decomposition(A, b, rows);
75
76#pragma omp parallel
77 {
78
79#pragma omp for schedule(static) private(i)
80 for (i = 0; i < rows; i++) {
81 tmpv[i] = A[i][i];
82 A[i][i] = 1;
83 }
84
85#pragma omp single
86 {
88 }
89
90#pragma omp for schedule(static) private(i)
91 for (i = 0; i < rows; i++) {
92 A[i][i] = tmpv[i];
93 }
94
95#pragma omp single
96 {
98 }
99 }
100
101 G_free(c);
102 G_free(tmpv);
103
104 return 1;
105}
106
107/*!
108 * \brief The choleksy decomposition solver for quardatic, symmetric
109 * positive definite matrices
110 *
111 * This solver does not support sparse matrices
112 * The matrix A will be overwritten.
113 * The result is written to the vector x
114 *
115 * \param A double **
116 * \param x double *
117 * \param b double *
118 * \param bandwidth int -- the bandwidth of the band matrix, if unsure set to
119 * rows \param rows int \return int -- 1 success
120 * */
121int G_math_solver_cholesky(double **A, double *x, double *b, int bandwidth,
122 int rows)
123{
124
125 G_message(_("Starting cholesky decomposition solver"));
126
127 if (G_math_cholesky_decomposition(A, rows, bandwidth) != 1) {
128 G_warning(_("Unable to solve the linear equation system"));
129 return -2;
130 }
131
134
135 return 1;
136}
137
138/*!
139 * \brief Gauss elimination
140 *
141 * To run this solver efficiently,
142 * no pivoting is supported.
143 * The matrix will be overwritten with the decomposite form
144 * \param A double **
145 * \param b double *
146 * \param rows int
147 * \return void
148 *
149 * */
150void G_math_gauss_elimination(double **A, double *b, int rows)
151{
152 int i, j, k;
153
154 double tmpval = 0.0;
155
156 for (k = 0; k < rows - 1; k++) {
157#pragma omp parallel for schedule(static) private(i, j, tmpval) \
158 shared(k, A, b, rows)
159 for (i = k + 1; i < rows; i++) {
160 tmpval = A[i][k] / A[k][k];
161 b[i] = b[i] - tmpval * b[k];
162 for (j = k + 1; j < rows; j++) {
163 A[i][j] = A[i][j] - tmpval * A[k][j];
164 }
165 }
166 }
167
168 return;
169}
170
171/*!
172 * \brief lu decomposition
173 *
174 * To run this solver efficiently,
175 * no pivoting is supported.
176 * The matrix will be overwritten with the decomposite form
177 *
178 * \param A double **
179 * \param b double * -- this vector is needed if its part of the linear equation
180 * system, otherwise set it to NULL \param rows int \return void
181 *
182 * */
183void G_math_lu_decomposition(double **A, double *b G_UNUSED, int rows)
184{
185
186 int i, j, k;
187
188 for (k = 0; k < rows - 1; k++) {
189#pragma omp parallel for schedule(static) private(i, j) shared(k, A, rows)
190 for (i = k + 1; i < rows; i++) {
191 A[i][k] = A[i][k] / A[k][k];
192 for (j = k + 1; j < rows; j++) {
193 A[i][j] = A[i][j] - A[i][k] * A[k][j];
194 }
195 }
196 }
197
198 return;
199}
200
201/*!
202 * \brief cholesky decomposition for symmetric, positive definite matrices
203 * with bandwidth optimization
204 *
205 * The provided matrix will be overwritten with the lower and
206 * upper triangle matrix A = LL^T
207 *
208 * \param A double **
209 * \param rows int
210 * \param bandwidth int -- the bandwidth of the matrix (0 > bandwidth <= cols)
211 * \return void
212 *
213 * */
214int G_math_cholesky_decomposition(double **A, int rows, int bandwidth)
215{
216
217 int i = 0, j = 0, k = 0;
218
219 double sum_1 = 0.0;
220
221 double sum_2 = 0.0;
222
223 int colsize;
224
225 if (bandwidth <= 0)
226 bandwidth = rows;
227
229
230 for (k = 0; k < rows; k++) {
231#pragma omp parallel for schedule(static) private(i, j, sum_2) shared(A, k) \
232 reduction(+ : sum_1)
233 for (j = 0; j < k; j++) {
234 sum_1 += A[k][j] * A[k][j];
235 }
236
237 if (0 > (A[k][k] - sum_1)) {
238 G_warning("Matrix is not positive definite. break.");
239 return -1;
240 }
241 A[k][k] = sqrt(A[k][k] - sum_1);
242 sum_1 = 0.0;
243
244 if ((k + bandwidth) > rows) {
245 colsize = rows;
246 }
247 else {
248 colsize = k + bandwidth;
249 }
250
251#pragma omp parallel for schedule(static) private(i, j, sum_2) \
252 shared(A, k, sum_1, colsize)
253
254 for (i = k + 1; i < colsize; i++) {
255 sum_2 = 0.0;
256 for (j = 0; j < k; j++) {
257 sum_2 += A[i][j] * A[k][j];
258 }
259 A[i][k] = (A[i][k] - sum_2) / A[k][k];
260 }
261 }
262 /* we need to copy the lower triangle matrix to the upper triangle */
263#pragma omp parallel for schedule(static) private(i, k) shared(A, rows)
264 for (k = 0; k < rows; k++) {
265 for (i = k + 1; i < rows; i++) {
266 A[k][i] = A[i][k];
267 }
268 }
269
270 return 1;
271}
272
273/*!
274 * \brief backward substitution
275 *
276 * \param A double **
277 * \param x double *
278 * \param b double *
279 * \param rows int
280 * \return void
281 *
282 * */
283void G_math_backward_substitution(double **A, double *x, double *b, int rows)
284{
285 int i, j;
286
287 for (i = rows - 1; i >= 0; i--) {
288 for (j = i + 1; j < rows; j++) {
289 b[i] = b[i] - A[i][j] * x[j];
290 }
291 x[i] = (b[i]) / A[i][i];
292 }
293
294 return;
295}
296
297/*!
298 * \brief forward substitution
299 *
300 * \param A double **
301 * \param x double *
302 * \param b double *
303 * \param rows int
304 * \return void
305 *
306 * */
307void G_math_forward_substitution(double **A, double *x, double *b, int rows)
308{
309 int i, j;
310
311 double tmpval = 0.0;
312
313 for (i = 0; i < rows; i++) {
314 tmpval = 0;
315 for (j = 0; j < i; j++) {
316 tmpval += A[i][j] * x[j];
317 }
318 x[i] = (b[i] - tmpval) / A[i][i];
319 }
320
321 return;
322}
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
double * G_alloc_vector(size_t)
Vector matrix memory allocation.
Definition dalloc.c:38
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define _(str)
Definition glocale.h:10
double b
Definition r_raster.c:37
int G_math_solver_lu(double **A, double *x, double *b, int rows)
The LU solver for quardatic matrices.
void G_math_lu_decomposition(double **A, double *b, int rows)
lu decomposition
int G_math_solver_gauss(double **A, double *x, double *b, int rows)
The gauss elimination solver for quardatic matrices.
void G_math_forward_substitution(double **A, double *x, double *b, int rows)
forward substitution
int G_math_solver_cholesky(double **A, double *x, double *b, int bandwidth, int rows)
The choleksy decomposition solver for quardatic, symmetric positive definite matrices.
void G_math_backward_substitution(double **A, double *x, double *b, int rows)
backward substitution
void G_math_gauss_elimination(double **A, double *b, int rows)
Gauss elimination.
int G_math_cholesky_decomposition(double **A, int rows, int bandwidth)
cholesky decomposition for symmetric, positive definite matrices with bandwidth optimization
#define x