solvers_classic_iter.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <math.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include <grass/gmath.h>
  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 maximum is reached, the
  31. * solver will abort the calculation and writes the current result into the vector x.
  32. * The parameter <i>err</i> defines the error break criteria for the solver.
  33. *
  34. * \param Asp G_math_spvector ** -- the sparse matrix
  35. * \param x double * -- the vector of unknowns
  36. * \param b double * -- the right side vector
  37. * \param rows int -- number of rows
  38. * \param maxit int -- the maximum number of iterations
  39. * \param sor double -- defines the successive overrelaxion parameter [0:1]
  40. * \param error double -- defines the error break criteria
  41. * \return int -- 1=success, -1=could not solve the les
  42. *
  43. * */
  44. int G_math_solver_sparse_jacobi(G_math_spvector ** Asp, double *x, double *b,
  45. int rows, int maxit, double sor, double error)
  46. {
  47. int i, j, k, center, finished = 0;
  48. double *Enew;
  49. double E, err = 0;
  50. Enew = G_alloc_vector(rows);
  51. for (k = 0; k < maxit; k++) {
  52. err = 0;
  53. {
  54. if (k == 0) {
  55. for (j = 0; j < rows; j++) {
  56. Enew[j] = x[j];
  57. }
  58. }
  59. for (i = 0; i < rows; i++) {
  60. E = 0;
  61. center = 0;
  62. for (j = 0; j < Asp[i]->cols; j++) {
  63. E += Asp[i]->values[j] * x[Asp[i]->index[j]];
  64. if (Asp[i]->index[j] == i)
  65. center = j;
  66. }
  67. Enew[i] = x[i] - sor * (E - b[i]) / Asp[i]->values[center];
  68. }
  69. for (j = 0; j < rows; j++) {
  70. err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
  71. x[j] = Enew[j];
  72. }
  73. }
  74. G_message(_("sparse Jacobi -- iteration %5i error %g\n"), k, err);
  75. if (err < error) {
  76. finished = 1;
  77. break;
  78. }
  79. }
  80. G_free(Enew);
  81. return finished;
  82. }
  83. /*!
  84. * \brief The iterative gauss seidel solver for sparse matrices
  85. *
  86. * The Jacobi solver solves the linear equation system Ax = b
  87. * The result is written to the vector x.
  88. *
  89. * The parameter <i>maxit</i> specifies the maximum number of iterations. If the maximum is reached, the
  90. * solver will abort the calculation and writes the current result into the vector x.
  91. * The parameter <i>err</i> defines the error break criteria for the solver.
  92. *
  93. * \param Asp G_math_spvector ** -- the sparse matrix
  94. * \param x double * -- the vector of unknowns
  95. * \param b double * -- the right side vector
  96. * \param rows int -- number of rows
  97. * \param maxit int -- the maximum number of iterations
  98. * \param sor double -- defines the successive overrelaxion parameter [0:2]
  99. * \param error double -- defines the error break criteria
  100. * \return int -- 1=success, -1=could not solve the les
  101. *
  102. * */
  103. int G_math_solver_sparse_gs(G_math_spvector ** Asp, double *x, double *b,
  104. int rows, int maxit, double sor, double error)
  105. {
  106. int i, j, k, finished = 0;
  107. double *Enew;
  108. double E, err = 0;
  109. int center;
  110. Enew = G_alloc_vector(rows);
  111. for (k = 0; k < maxit; k++) {
  112. err = 0;
  113. {
  114. if (k == 0) {
  115. for (j = 0; j < rows; j++) {
  116. Enew[j] = x[j];
  117. }
  118. }
  119. for (i = 0; i < rows; i++) {
  120. E = 0;
  121. center = 0;
  122. for (j = 0; j < Asp[i]->cols; j++) {
  123. E += Asp[i]->values[j] * Enew[Asp[i]->index[j]];
  124. if (Asp[i]->index[j] == i)
  125. center = j;
  126. }
  127. Enew[i] = x[i] - sor * (E - b[i]) / Asp[i]->values[center];
  128. }
  129. for (j = 0; j < rows; j++) {
  130. err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
  131. x[j] = Enew[j];
  132. }
  133. }
  134. G_message(_("sparse SOR -- iteration %5i error %g\n"), k, err);
  135. if (err < error) {
  136. finished = 1;
  137. break;
  138. }
  139. }
  140. G_free(Enew);
  141. return finished;
  142. }
  143. /*!
  144. * \brief The iterative jacobi solver for quadratic matrices
  145. *
  146. * The Jacobi solver solves the linear equation system Ax = b
  147. * The result is written to the vector x.
  148. *
  149. * The parameter <i>maxit</i> specifies the maximum number of iterations. If the maximum is reached, the
  150. * solver will abort the calculation and writes the current result into the vector x.
  151. * The parameter <i>err</i> defines the error break criteria for the solver.
  152. *
  153. * \param A double ** -- the dense matrix
  154. * \param x double * -- the vector of unknowns
  155. * \param b double * -- the right side vector
  156. * \param rows int -- number of rows
  157. * \param maxit int -- the maximum number of iterations
  158. * \param sor double -- defines the successive overrelaxion parameter [0:1]
  159. * \param error double -- defines the error break criteria
  160. * \return int -- 1=success, -1=could not solve the les
  161. *
  162. * */
  163. int G_math_solver_jacobi(double **A, double *x, double *b, int rows,
  164. int maxit, double sor, double error)
  165. {
  166. int i, j, k;
  167. double *Enew;
  168. double E, err = 0;
  169. Enew = G_alloc_vector(rows);
  170. for (j = 0; j < rows; j++) {
  171. Enew[j] = x[j];
  172. }
  173. for (k = 0; k < maxit; k++) {
  174. for (i = 0; i < rows; i++) {
  175. E = 0;
  176. for (j = 0; j < rows; j++) {
  177. E += A[i][j] * x[j];
  178. }
  179. Enew[i] = x[i] - sor * (E - b[i]) / A[i][i];
  180. }
  181. err = 0;
  182. for (j = 0; j < rows; j++) {
  183. err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
  184. x[j] = Enew[j];
  185. }
  186. G_message(_("Jacobi -- iteration %5i error %g\n"), k, err);
  187. if (err < error)
  188. break;
  189. }
  190. return 1;
  191. }
  192. /*!
  193. * \brief The iterative gauss seidel solver for quadratic matrices
  194. *
  195. * The Jacobi solver solves the linear equation system Ax = b
  196. * The result is written to the vector x.
  197. *
  198. * The parameter <i>maxit</i> specifies the maximum number of iterations. If the maximum is reached, the
  199. * solver will abort the calculation and writes the current result into the vector x.
  200. * The parameter <i>err</i> defines the error break criteria for the solver.
  201. *
  202. * \param A double ** -- the dense matrix
  203. * \param x double * -- the vector of unknowns
  204. * \param b double * -- the right side vector
  205. * \param rows int -- number of rows
  206. * \param maxit int -- the maximum number of iterations
  207. * \param sor double -- defines the successive overrelaxion parameter [0:2]
  208. * \param error double -- defines the error break criteria
  209. * \return int -- 1=success, -1=could not solve the les
  210. *
  211. * */
  212. int G_math_solver_gs(double **A, double *x, double *b, int rows, int maxit,
  213. double sor, double error)
  214. {
  215. int i, j, k;
  216. double *Enew;
  217. double E, err = 0;
  218. Enew = G_alloc_vector(rows);
  219. for (j = 0; j < rows; j++) {
  220. Enew[j] = x[j];
  221. }
  222. for (k = 0; k < maxit; k++) {
  223. for (i = 0; i < rows; i++) {
  224. E = 0;
  225. for (j = 0; j < rows; j++) {
  226. E += A[i][j] * Enew[j];
  227. }
  228. Enew[i] = x[i] - sor * (E - b[i]) / A[i][i];
  229. }
  230. err = 0;
  231. for (j = 0; j < rows; j++) {
  232. err += (x[j] - Enew[j]) * (x[j] - Enew[j]);
  233. x[j] = Enew[j];
  234. }
  235. G_message(_("SOR -- iteration %5i error %g\n"), k, err);
  236. if (err < error)
  237. break;
  238. }
  239. return 1;
  240. }