GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
solvers_krylov.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/gmath.h>
22#include <grass/glocale.h>
23
24static G_math_spvector **create_diag_precond_matrix(double **A,
25 G_math_spvector **Asp,
26 int rows, int prec);
27static int solver_pcg(double **A, G_math_spvector **Asp, double *x, double *b,
28 int rows, int maxit, double err, int prec, int has_band,
29 int bandwidth);
30static int solver_cg(double **A, G_math_spvector **Asp, double *x, double *b,
31 int rows, int maxit, double err, int has_band,
32 int bandwidth);
33static int solver_bicgstab(double **A, G_math_spvector **Asp, double *x,
34 double *b, int rows, int maxit, double err);
35
36/*!
37 * \brief The iterative preconditioned conjugate gradients solver for symmetric
38 * positive definite matrices
39 *
40 * This iterative solver works with symmetric positive definite regular
41 * quadratic matrices.
42 *
43 * This solver solves the linear equation system:
44 * A x = b
45 *
46 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
47 * maximum is reached, the solver will abort the calculation and writes the
48 * current result into the vector x. The parameter <i>err</i> defines the error
49 * break criteria for the solver.
50 *
51 * \param A (double **) -- the matrix
52 * \param x (double *) -- the value vector
53 * \param b (double *) -- the right hand side
54 * \param rows (int)
55 * \param maxit (int) -- the maximum number of iterations
56 * \param err (double) -- defines the error break criteria
57 * \param prec (int) -- the preconditioner which should be used 1,2 or 3
58 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
59 * singular, -1 - could not solve the les
60 *
61 * */
62int G_math_solver_pcg(double **A, double *x, double *b, int rows, int maxit,
63 double err, int prec)
64{
65
66 return solver_pcg(A, NULL, x, b, rows, maxit, err, prec, 0, 0);
67}
68
69/*!
70 * \brief The iterative preconditioned conjugate gradients solver for symmetric
71 * positive definite band matrices
72 *
73 * WARNING: The preconditioning of symmetric band matrices is not implemented
74 * yet
75 *
76 * This iterative solver works with symmetric positive definite band matrices.
77 *
78 * This solver solves the linear equation system:
79 * A x = b
80 *
81 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
82 * maximum is reached, the solver will abort the calculation and writes the
83 * current result into the vector x. The parameter <i>err</i> defines the error
84 * break criteria for the solver.
85 *
86 * \param A (double **) -- the positive definite band matrix
87 * \param x (double *) -- the value vector
88 * \param b (double *) -- the right hand side
89 * \param rows (int)
90 * \param bandwidth (int) -- bandwidth of matrix A
91 * \param maxit (int) -- the maximum number of iterations
92 * \param err (double) -- defines the error break criteria
93 * \param prec (int) -- the preconditioner which should be used 1,2 or 3
94 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
95 * singular, -1 - could not solve the les
96 *
97 * */
98int G_math_solver_pcg_sband(double **A, double *x, double *b, int rows,
99 int bandwidth, int maxit, double err, int prec)
100{
101 G_fatal_error("Preconditioning of band matrices is not implemented yet");
102 return solver_pcg(A, NULL, x, b, rows, maxit, err, prec, 1, bandwidth);
103}
104
105/*!
106 * \brief The iterative preconditioned conjugate gradients solver for sparse
107 * symmetric positive definite matrices
108 *
109 * This iterative solver works with symmetric positive definite sparse matrices.
110 *
111 * This solver solves the linear equation system:
112 * A x = b
113 *
114 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
115 * maximum is reached, the solver will abort the calculation and writes the
116 * current result into the vector x. The parameter <i>err</i> defines the error
117 * break criteria for the solver.
118 *
119 * \param Asp (G_math_spvector **) -- the sparse matrix
120 * \param x (double *) -- the value vector
121 * \param b (double *) -- the right hand side
122 * \param rows (int)
123 * \param maxit (int) -- the maximum number of iterations
124 * \param err (double) -- defines the error break criteria
125 * \param prec (int) -- the preconditioner which should be used 1,2 or 3
126 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
127 * singular, -1 - could not solve the les
128 *
129 * */
130int G_math_solver_sparse_pcg(G_math_spvector **Asp, double *x, double *b,
131 int rows, int maxit, double err, int prec)
132{
133
134 return solver_pcg(NULL, Asp, x, b, rows, maxit, err, prec, 0, 0);
135}
136
137int solver_pcg(double **A, G_math_spvector **Asp, double *x, double *b,
138 int rows, int maxit, double err, int prec, int has_band,
139 int bandwidth)
140{
141 double *r, *z;
142
143 double *p;
144
145 double *v;
146
147 double s = 0.0;
148
149 double a0 = 0, a1 = 0, mygamma, tmp = 0;
150
151 int m, i;
152
153 int finished = 2;
154
155 int error_break;
156
158
159 r = G_alloc_vector(rows);
160 p = G_alloc_vector(rows);
161 v = G_alloc_vector(rows);
162 z = G_alloc_vector(rows);
163
164 error_break = 0;
165
166 /*compute the preconditioning matrix, this is a sparse matrix */
167 M = create_diag_precond_matrix(A, Asp, rows, prec);
168
169 /*
170 * residual calculation
171 */
172#pragma omp parallel
173 {
174 if (Asp)
175 G_math_Ax_sparse(Asp, x, v, rows);
176 else if (has_band)
177 G_math_Ax_sband(A, x, v, rows, bandwidth);
178 else
179 G_math_d_Ax(A, x, v, rows, rows);
180
181 G_math_d_ax_by(b, v, r, 1.0, -1.0, rows);
182 /*perform the preconditioning */
183 G_math_Ax_sparse(M, r, p, rows);
184
185 /* scalar product */
186#pragma omp for schedule(static) private(i) reduction(+ : s)
187 for (i = 0; i < rows; i++) {
188 s += p[i] * r[i];
189 }
190 }
191
192 a0 = s;
193 s = 0.0;
194
195 /* ******************* */
196 /* start the iteration */
197 /* ******************* */
198 for (m = 0; m < maxit; m++) {
199#pragma omp parallel default(shared)
200 {
201 if (Asp)
202 G_math_Ax_sparse(Asp, p, v, rows);
203 else if (has_band)
204 G_math_Ax_sband(A, p, v, rows, bandwidth);
205 else
206 G_math_d_Ax(A, p, v, rows, rows);
207
208 /* scalar product */
209#pragma omp for schedule(static) private(i) reduction(+ : s)
210 for (i = 0; i < rows; i++) {
211 s += v[i] * p[i];
212 }
213
214 /* barrier */
215#pragma omp single
216 {
217 tmp = s;
218 mygamma = a0 / tmp;
219 s = 0.0;
220 }
221
222 G_math_d_ax_by(p, x, x, mygamma, 1.0, rows);
223
224 if (m % 50 == 1) {
225 if (Asp)
226 G_math_Ax_sparse(Asp, x, v, rows);
227 else if (has_band)
228 G_math_Ax_sband(A, x, v, rows, bandwidth);
229 else
230 G_math_d_Ax(A, x, v, rows, rows);
231
232 G_math_d_ax_by(b, v, r, 1.0, -1.0, rows);
233 }
234 else {
235 G_math_d_ax_by(r, v, r, 1.0, -1.0 * mygamma, rows);
236 }
237
238 /*perform the preconditioning */
239 G_math_Ax_sparse(M, r, z, rows);
240
241 /* scalar product */
242#pragma omp for schedule(static) private(i) reduction(+ : s)
243 for (i = 0; i < rows; i++) {
244 s += z[i] * r[i];
245 }
246
247 /* barrier */
248#pragma omp single
249 {
250 a1 = s;
251 tmp = a1 / a0;
252 a0 = a1;
253 s = 0.0;
254
256 ;
257 }
258 else {
259 G_warning(_("Unable to solve the linear equation system"));
260 error_break = 1;
261 }
262 }
263 G_math_d_ax_by(p, z, p, tmp, 1.0, rows);
264 }
265
266 if (Asp != NULL)
267 G_message(_("Sparse PCG -- iteration %i error %g\n"), m, a0);
268 else
269 G_message(_("PCG -- iteration %i error %g\n"), m, a0);
270
271 if (error_break == 1) {
272 finished = -1;
273 break;
274 }
275
276 if (a0 < err) {
277 finished = 1;
278 break;
279 }
280 }
281
282 G_free(r);
283 G_free(p);
284 G_free(v);
285 G_free(z);
286 G_math_free_spmatrix(M, rows);
287
288 return finished;
289}
290
291/*!
292 * \brief The iterative conjugate gradients solver for symmetric positive
293 * definite matrices
294 *
295 * This iterative solver works with symmetric positive definite regular
296 * quadratic matrices.
297 *
298 * This solver solves the linear equation system:
299 * A x = b
300 *
301 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
302 * maximum is reached, the solver will abort the calculation and writes the
303 * current result into the vector x. The parameter <i>err</i> defines the error
304 * break criteria for the solver.
305 *
306 * \param A (double **) -- the matrix
307 * \param x (double *) -- the value vector
308 * \param b (double *) -- the right hand side
309 * \param rows (int)
310 * \param maxit (int) -- the maximum number of iterations
311 * \param err (double) -- defines the error break criteria
312 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
313 * singular, -1 - could not solve the les
314 *
315 * */
316int G_math_solver_cg(double **A, double *x, double *b, int rows, int maxit,
317 double err)
318{
319 return solver_cg(A, NULL, x, b, rows, maxit, err, 0, 0);
320}
321
322/*!
323 * \brief The iterative conjugate gradients solver for symmetric positive
324 * definite band matrices
325 *
326 * This iterative solver works with symmetric positive definite band matrices.
327 *
328 * This solver solves the linear equation system:
329 * A x = b
330 *
331 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
332 * maximum is reached, the solver will abort the calculation and writes the
333 * current result into the vector x. The parameter <i>err</i> defines the error
334 * break criteria for the solver.
335 *
336 * \param A (double **) -- the symmetric positive definite band matrix
337 * \param x (double *) -- the value vector
338 * \param b (double *) -- the right hand side
339 * \param rows (int)
340 * \param bandwidth (int) -- the bandwidth of matrix A
341 * \param maxit (int) -- the maximum number of iterations
342 * \param err (double) -- defines the error break criteria
343 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
344 * singular, -1 - could not solve the les
345 *
346 * */
347int G_math_solver_cg_sband(double **A, double *x, double *b, int rows,
348 int bandwidth, int maxit, double err)
349{
350 return solver_cg(A, NULL, x, b, rows, maxit, err, 1, bandwidth);
351}
352
353/*!
354 * \brief The iterative conjugate gradients solver for sparse symmetric positive
355 * definite matrices
356 *
357 * This iterative solver works with symmetric positive definite sparse matrices.
358 *
359 * This solver solves the linear equation system:
360 * A x = b
361 *
362 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
363 * maximum is reached, the solver will abort the calculation and writes the
364 * current result into the vector x. The parameter <i>err</i> defines the error
365 * break criteria for the solver.
366 *
367 * \param Asp (G_math_spvector **) -- the sparse matrix
368 * \param x (double *) -- the value vector
369 * \param b (double *) -- the right hand side
370 * \param rows (int)
371 * \param maxit (int) -- the maximum number of iterations
372 * \param err (double) -- defines the error break criteria
373 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
374 * singular, -1 - could not solve the les
375 *
376 * */
377int G_math_solver_sparse_cg(G_math_spvector **Asp, double *x, double *b,
378 int rows, int maxit, double err)
379{
380 return solver_cg(NULL, Asp, x, b, rows, maxit, err, 0, 0);
381}
382
383int solver_cg(double **A, G_math_spvector **Asp, double *x, double *b, int rows,
384 int maxit, double err, int has_band, int bandwidth)
385{
386 double *r;
387
388 double *p;
389
390 double *v;
391
392 double s = 0.0;
393
394 double a0 = 0, a1 = 0, mygamma, tmp = 0;
395
396 int m, i;
397
398 int finished = 2;
399
400 int error_break;
401
402 r = G_alloc_vector(rows);
403 p = G_alloc_vector(rows);
404 v = G_alloc_vector(rows);
405
406 error_break = 0;
407 /*
408 * residual calculation
409 */
410#pragma omp parallel
411 {
412 if (Asp)
413 G_math_Ax_sparse(Asp, x, v, rows);
414 else if (has_band)
415 G_math_Ax_sband(A, x, v, rows, bandwidth);
416 else
417 G_math_d_Ax(A, x, v, rows, rows);
418
419 G_math_d_ax_by(b, v, r, 1.0, -1.0, rows);
420 G_math_d_copy(r, p, rows);
421
422 /* scalar product */
423#pragma omp for schedule(static) private(i) reduction(+ : s)
424 for (i = 0; i < rows; i++) {
425 s += r[i] * r[i];
426 }
427 }
428
429 a0 = s;
430 s = 0.0;
431
432 /* ******************* */
433 /* start the iteration */
434 /* ******************* */
435 for (m = 0; m < maxit; m++) {
436#pragma omp parallel default(shared)
437 {
438 if (Asp)
439 G_math_Ax_sparse(Asp, p, v, rows);
440 else if (has_band)
441 G_math_Ax_sband(A, p, v, rows, bandwidth);
442 else
443 G_math_d_Ax(A, p, v, rows, rows);
444
445 /* scalar product */
446#pragma omp for schedule(static) private(i) reduction(+ : s)
447 for (i = 0; i < rows; i++) {
448 s += v[i] * p[i];
449 }
450
451 /* barrier */
452#pragma omp single
453 {
454 tmp = s;
455 mygamma = a0 / tmp;
456 s = 0.0;
457 }
458
459 G_math_d_ax_by(p, x, x, mygamma, 1.0, rows);
460
461 if (m % 50 == 1) {
462 if (Asp)
463 G_math_Ax_sparse(Asp, x, v, rows);
464 else if (has_band)
465 G_math_Ax_sband(A, x, v, rows, bandwidth);
466 else
467 G_math_d_Ax(A, x, v, rows, rows);
468
469 G_math_d_ax_by(b, v, r, 1.0, -1.0, rows);
470 }
471 else {
472 G_math_d_ax_by(r, v, r, 1.0, -1.0 * mygamma, rows);
473 }
474
475 /* scalar product */
476#pragma omp for schedule(static) private(i) reduction(+ : s)
477 for (i = 0; i < rows; i++) {
478 s += r[i] * r[i];
479 }
480
481 /* barrier */
482#pragma omp single
483 {
484 a1 = s;
485 tmp = a1 / a0;
486 a0 = a1;
487 s = 0.0;
488
490 ;
491 }
492 else {
493 G_warning(_("Unable to solve the linear equation system"));
494 error_break = 1;
495 }
496 }
497 G_math_d_ax_by(p, r, p, tmp, 1.0, rows);
498 }
499
500 if (Asp != NULL)
501 G_message(_("Sparse CG -- iteration %i error %g\n"), m, a0);
502 else
503 G_message(_("CG -- iteration %i error %g\n"), m, a0);
504
505 if (error_break == 1) {
506 finished = -1;
507 break;
508 }
509
510 if (a0 < err) {
511 finished = 1;
512 break;
513 }
514 }
515
516 G_free(r);
517 G_free(p);
518 G_free(v);
519
520 return finished;
521}
522
523/*!
524 * \brief The iterative biconjugate gradients solver with stabilization for
525 * unsymmetric non-definite matrices
526 *
527 * This iterative solver works with regular quadratic matrices.
528 *
529 * This solver solves the linear equation system:
530 * A x = b
531 *
532 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
533 * maximum is reached, the solver will abort the calculation and writes the
534 * current result into the vector x. The parameter <i>err</i> defines the error
535 * break criteria for the solver.
536 *
537 * \param A (double **) -- the matrix
538 * \param x (double *) -- the value vector
539 * \param b (double *) -- the right hand side
540 * \param rows (int)
541 * \param maxit (int) -- the maximum number of iterations
542 * \param err (double) -- defines the error break criteria
543 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
544 * singular, -1 - could not solve the les
545 *
546 * */
547int G_math_solver_bicgstab(double **A, double *x, double *b, int rows,
548 int maxit, double err)
549{
550 return solver_bicgstab(A, NULL, x, b, rows, maxit, err);
551}
552
553/*!
554 * \brief The iterative biconjugate gradients solver with stabilization for
555 * unsymmetric non-definite matrices
556 *
557 * This iterative solver works with sparse matrices.
558 *
559 * This solver solves the linear equation system:
560 * A x = b
561 *
562 * The parameter <i>maxit</i> specifies the maximum number of iterations. If the
563 * maximum is reached, the solver will abort the calculation and writes the
564 * current result into the vector x. The parameter <i>err</i> defines the error
565 * break criteria for the solver.
566 *
567 * \param Asp (G_math_spvector **) -- the sparse matrix
568 * \param x (double *) -- the value vector
569 * \param b (double *) -- the right hand side
570 * \param rows (int)
571 * \param maxit (int) -- the maximum number of iterations
572 * \param err (double) -- defines the error break criteria
573 * \return (int) -- 1 - success, 2 - not finished but success, 0 - matrix
574 * singular, -1 - could not solve the les
575 *
576 * */
577int G_math_solver_sparse_bicgstab(G_math_spvector **Asp, double *x, double *b,
578 int rows, int maxit, double err)
579{
580 return solver_bicgstab(NULL, Asp, x, b, rows, maxit, err);
581}
582
583int solver_bicgstab(double **A, G_math_spvector **Asp, double *x, double *b,
584 int rows, int maxit, double err)
585{
586 double *r;
587
588 double *r0;
589
590 double *p;
591
592 double *v;
593
594 double *s;
595
596 double *t;
597
598 double s1 = 0.0, s2 = 0.0, s3 = 0.0;
599
600 double alpha = 0, beta = 0, omega, rr0 = 0, error;
601
602 int m, i;
603
604 int finished = 2;
605
606 int error_break;
607
608 r = G_alloc_vector(rows);
609 r0 = G_alloc_vector(rows);
610 p = G_alloc_vector(rows);
611 v = G_alloc_vector(rows);
612 s = G_alloc_vector(rows);
613 t = G_alloc_vector(rows);
614
615 error_break = 0;
616
617#pragma omp parallel
618 {
619 if (Asp)
620 G_math_Ax_sparse(Asp, x, v, rows);
621 else
622 G_math_d_Ax(A, x, v, rows, rows);
623
624 G_math_d_ax_by(b, v, r, 1.0, -1.0, rows);
625 G_math_d_copy(r, r0, rows);
626 G_math_d_copy(r, p, rows);
627 }
628
629 s1 = s2 = s3 = 0.0;
630
631 /* ******************* */
632 /* start the iteration */
633 /* ******************* */
634 for (m = 0; m < maxit; m++) {
635
636#pragma omp parallel default(shared)
637 {
638 if (Asp)
639 G_math_Ax_sparse(Asp, p, v, rows);
640 else
641 G_math_d_Ax(A, p, v, rows, rows);
642
643 /* scalar product */
644#pragma omp for schedule(static) private(i) reduction(+ : s1, s2, s3)
645 for (i = 0; i < rows; i++) {
646 s1 += r[i] * r[i];
647 s2 += r[i] * r0[i];
648 s3 += v[i] * r0[i];
649 }
650
651#pragma omp single
652 {
653 error = s1;
654
656 ;
657 }
658 else {
659 G_warning(_("Unable to solve the linear equation system"));
660 error_break = 1;
661 }
662
663 rr0 = s2;
664 alpha = rr0 / s3;
665 s1 = s2 = s3 = 0.0;
666 }
667
668 G_math_d_ax_by(r, v, s, 1.0, -1.0 * alpha, rows);
669 if (Asp)
670 G_math_Ax_sparse(Asp, s, t, rows);
671 else
672 G_math_d_Ax(A, s, t, rows, rows);
673
674 /* scalar product */
675#pragma omp for schedule(static) private(i) reduction(+ : s1, s2)
676 for (i = 0; i < rows; i++) {
677 s1 += t[i] * s[i];
678 s2 += t[i] * t[i];
679 }
680
681#pragma omp single
682 {
683 omega = s1 / s2;
684 s1 = s2 = 0.0;
685 }
686
687 G_math_d_ax_by(p, s, r, alpha, omega, rows);
688 G_math_d_ax_by(x, r, x, 1.0, 1.0, rows);
689 G_math_d_ax_by(s, t, r, 1.0, -1.0 * omega, rows);
690
691#pragma omp for schedule(static) private(i) reduction(+ : s1)
692 for (i = 0; i < rows; i++) {
693 s1 += r[i] * r0[i];
694 }
695
696#pragma omp single
697 {
698 beta = alpha / omega * s1 / rr0;
699 s1 = s2 = s3 = 0.0;
700 }
701
702 G_math_d_ax_by(p, v, p, 1.0, -1.0 * omega, rows);
703 G_math_d_ax_by(p, r, p, beta, 1.0, rows);
704 }
705
706 if (Asp != NULL)
707 G_message(_("Sparse BiCGStab -- iteration %i error %g\n"), m,
708 error);
709 else
710 G_message(_("BiCGStab -- iteration %i error %g\n"), m, error);
711
712 if (error_break == 1) {
713 finished = -1;
714 break;
715 }
716
717 if (error < err) {
718 finished = 1;
719 break;
720 }
721 }
722
723 G_free(r);
724 G_free(r0);
725 G_free(p);
726 G_free(v);
727 G_free(s);
728 G_free(t);
729
730 return finished;
731}
732
733/*!
734 * \brief Compute a diagonal preconditioning matrix for krylov space solver
735 *
736 * \param A (double **) -- the matrix for which the precondition should be
737 * computed (if the sparse matrix is used, set it to NULL) \param Asp
738 * (G_math_spvector **) -- the matrix for which the precondition should be
739 * computed \param rows (int) \param prec (int) -- which preconditioner should
740 * be used 1, 2 or 3
741 *
742 * */
743G_math_spvector **create_diag_precond_matrix(double **A, G_math_spvector **Asp,
744 int rows, int prec)
745{
747
748 unsigned int i, j, cols = (unsigned int)rows;
749
750 double sum;
751
752 assert(rows >= 0);
753
755
756 if (A != NULL) {
757#pragma omp parallel for schedule(static) private(i, j, sum) \
758 shared(A, Msp, rows, cols, prec)
759 for (i = 0; i < (unsigned int)rows; i++) {
761
762 switch (prec) {
764 sum = 0;
765 for (j = 0; j < cols; j++)
766 sum += A[i][j] * A[i][j];
767 spvect->values[0] = 1.0 / sqrt(sum);
768 break;
770 sum = 0;
771 for (j = 0; j < cols; j++)
772 sum += fabs(A[i][j]);
773 spvect->values[0] = 1.0 / (sum);
774 break;
776 default:
777 spvect->values[0] = 1.0 / A[i][i];
778 break;
779 }
780
781 spvect->index[0] = i;
782 spvect->cols = 1;
783 ;
785 }
786 }
787 else {
788#pragma omp parallel for schedule(static) private(i, j, sum) \
789 shared(Asp, Msp, rows, cols, prec)
790 for (i = 0; i < (unsigned int)rows; i++) {
792
793 switch (prec) {
795 sum = 0;
796 for (j = 0; j < Asp[i]->cols; j++)
797 sum += Asp[i]->values[j] * Asp[i]->values[j];
798 spvect->values[0] = 1.0 / sqrt(sum);
799 break;
801 sum = 0;
802 for (j = 0; j < Asp[i]->cols; j++)
803 sum += fabs(Asp[i]->values[j]);
804 spvect->values[0] = 1.0 / (sum);
805 break;
807 default:
808 for (j = 0; j < Asp[i]->cols; j++)
809 if (i == Asp[i]->index[j])
810 spvect->values[0] = 1.0 / Asp[i]->values[j];
811 break;
812 }
813
814 spvect->index[0] = i;
815 spvect->cols = 1;
816 ;
818 }
819 }
820 return Msp;
821}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
G_math_spvector * G_math_alloc_spvector(int)
Allocate memory for a sparse vector.
void G_math_d_ax_by(double *, double *, double *, double, double, int)
Scales vectors x and y with the scalars a and b and adds them.
void G_math_Ax_sband(double **A, double *x, double *y, int rows, int bandwidth)
Compute the matrix - vector product of symmetric band matrix A and vector x.
void G_math_free_spmatrix(G_math_spvector **, int)
Release the memory of the sparse matrix.
G_math_spvector ** G_math_alloc_spmatrix(int)
Allocate memory for a sparse matrix.
double * G_alloc_vector(size_t)
Vector matrix memory allocation.
Definition dalloc.c:38
void G_math_Ax_sparse(G_math_spvector **, double *, double *, int)
Compute the matrix - vector product of sparse matrix **Asp and vector x.
int G_math_add_spvector(G_math_spvector **, G_math_spvector *, int)
Adds a sparse vector to a sparse matrix at position row.
void G_math_d_Ax(double **, double *, double *, int, int)
Compute the matrix - vector product of matrix A and vector x.
void G_math_d_copy(double *, double *, int)
Copy the vector x to y.
#define M(row, col)
Definition georef.c:43
#define _(str)
Definition glocale.h:10
#define G_MATH_ROWSCALE_ABSSUMNORM_PRECONDITION
Definition gmath.h:47
#define G_MATH_ROWSCALE_EUKLIDNORM_PRECONDITION
Definition gmath.h:48
#define G_MATH_DIAGONAL_PRECONDITION
Definition gmath.h:46
#define assert(condition)
Definition lz4.c:291
double b
Definition r_raster.c:37
double t
Definition r_raster.c:37
double r
Definition r_raster.c:37
int G_math_solver_pcg(double **A, double *x, double *b, int rows, int maxit, double err, int prec)
The iterative preconditioned conjugate gradients solver for symmetric positive definite matrices.
int G_math_solver_cg(double **A, double *x, double *b, int rows, int maxit, double err)
The iterative conjugate gradients solver for symmetric positive definite matrices.
int G_math_solver_bicgstab(double **A, double *x, double *b, int rows, int maxit, double err)
The iterative biconjugate gradients solver with stabilization for unsymmetric non-definite matrices.
int G_math_solver_sparse_cg(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err)
The iterative conjugate gradients solver for sparse symmetric positive definite matrices.
int G_math_solver_cg_sband(double **A, double *x, double *b, int rows, int bandwidth, int maxit, double err)
The iterative conjugate gradients solver for symmetric positive definite band matrices.
int G_math_solver_pcg_sband(double **A, double *x, double *b, int rows, int bandwidth, int maxit, double err, int prec)
The iterative preconditioned conjugate gradients solver for symmetric positive definite band matrices...
int G_math_solver_sparse_bicgstab(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err)
The iterative biconjugate gradients solver with stabilization for unsymmetric non-definite matrices.
int G_math_solver_sparse_pcg(G_math_spvector **Asp, double *x, double *b, int rows, int maxit, double err, int prec)
The iterative preconditioned conjugate gradients solver for sparse symmetric positive definite matric...
The row vector of the sparse matrix.
Definition gmath.h:54
double * values
Definition gmath.h:55
unsigned int cols
Definition gmath.h:56
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define x