n_les.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: functions to manage linear equation systems
  8. * part of the gpde library
  9. *
  10. * COPYRIGHT: (C) 2000 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 <stdlib.h>
  18. #include <grass/N_pde.h>
  19. #include <grass/gmath.h>
  20. /*!
  21. * \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A, vector x and vector b
  22. *
  23. * This function calls #N_alloc_les_param
  24. *
  25. * \param cols int
  26. * \param rows int
  27. * \param type int
  28. * \return N_les *
  29. *
  30. * */
  31. N_les *N_alloc_nquad_les(int cols, int rows, int type)
  32. {
  33. return N_alloc_les_param(cols, rows, type, 2);
  34. }
  35. /*!
  36. * \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A and vector x
  37. *
  38. * This function calls #N_alloc_les_param
  39. *
  40. * \param cols int
  41. * \param rows int
  42. * \param type int
  43. * \return N_les *
  44. *
  45. * */
  46. N_les *N_alloc_nquad_les_Ax(int cols, int rows, int type)
  47. {
  48. return N_alloc_les_param(cols, rows, type, 1);
  49. }
  50. /*!
  51. * \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A
  52. *
  53. * This function calls #N_alloc_les_param
  54. *
  55. * \param cols int
  56. * \param rows int
  57. * \param type int
  58. * \return N_les *
  59. *
  60. * */
  61. N_les *N_alloc_nquad_les_A(int cols, int rows, int type)
  62. {
  63. return N_alloc_les_param(cols, rows, type, 0);
  64. }
  65. /*!
  66. * \brief Allocate memory for a (not) quadratic linear equation system which includes the Matrix A, vector x and vector b
  67. *
  68. * This function calls #N_alloc_les_param
  69. *
  70. * \param cols int
  71. * \param rows int
  72. * \param type int
  73. * \return N_les *
  74. *
  75. * */
  76. N_les *N_alloc_nquad_les_Ax_b(int cols, int rows, int type)
  77. {
  78. return N_alloc_les_param(cols, rows, type, 2);
  79. }
  80. /*!
  81. * \brief Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b
  82. *
  83. * This function calls #N_alloc_les_param
  84. *
  85. * \param rows int
  86. * \param type int
  87. * \return N_les *
  88. *
  89. * */
  90. N_les *N_alloc_les(int rows, int type)
  91. {
  92. return N_alloc_les_param(rows, rows, type, 2);
  93. }
  94. /*!
  95. * \brief Allocate memory for a quadratic linear equation system which includes the Matrix A and vector x
  96. *
  97. * This function calls #N_alloc_les_param
  98. *
  99. * \param rows int
  100. * \param type int
  101. * \return N_les *
  102. *
  103. * */
  104. N_les *N_alloc_les_Ax(int rows, int type)
  105. {
  106. return N_alloc_les_param(rows, rows, type, 1);
  107. }
  108. /*!
  109. * \brief Allocate memory for a quadratic linear equation system which includes the Matrix A
  110. *
  111. * This function calls #N_alloc_les_param
  112. *
  113. * \param rows int
  114. * \param type int
  115. * \return N_les *
  116. *
  117. * */
  118. N_les *N_alloc_les_A(int rows, int type)
  119. {
  120. return N_alloc_les_param(rows, rows, type, 0);
  121. }
  122. /*!
  123. * \brief Allocate memory for a quadratic linear equation system which includes the Matrix A, vector x and vector b
  124. *
  125. * This function calls #N_alloc_les_param
  126. *
  127. * \param rows int
  128. * \param type int
  129. * \return N_les *
  130. *
  131. * */
  132. N_les *N_alloc_les_Ax_b(int rows, int type)
  133. {
  134. return N_alloc_les_param(rows, rows, type, 2);
  135. }
  136. /*!
  137. * \brief Allocate memory for a quadratic or not quadratic linear equation system
  138. *
  139. * The type of the linear equation system must be N_NORMAL_LES for
  140. * a regular quadratic matrix or N_SPARSE_LES for a sparse matrix
  141. *
  142. * <p>
  143. * In case of N_NORMAL_LES
  144. *
  145. * A quadratic matrix of size rows*rows*sizeof(double) will allocated
  146. *
  147. * <p>
  148. * In case of N_SPARSE_LES
  149. *
  150. * a vector of size row will be allocated, ready to hold additional allocated sparse vectors.
  151. * each sparse vector may have a different size.
  152. *
  153. * Parameter parts defines which parts of the les should be allocated.
  154. * The number of columns and rows defines if the matrix is quadratic.
  155. *
  156. * \param cols int
  157. * \param rows int
  158. * \param type int
  159. * \param parts int -- 2 = A, x and b; 1 = A and x; 0 = A allocated
  160. * \return N_les *
  161. *
  162. * */
  163. N_les *N_alloc_les_param(int cols, int rows, int type, int parts)
  164. {
  165. N_les *les;
  166. int i;
  167. if (type == N_SPARSE_LES)
  168. G_debug(2,
  169. "Allocate memory for a sparse linear equation system with %i rows\n",
  170. rows);
  171. else
  172. G_debug(2,
  173. "Allocate memory for a regular linear equation system with %i rows\n",
  174. rows);
  175. les = (N_les *) G_calloc(1, sizeof(N_les));
  176. if (parts > 0) {
  177. les->x = (double *)G_calloc(cols, sizeof(double));
  178. for (i = 0; i < cols; i++)
  179. les->x[i] = 0.0;
  180. }
  181. if (parts > 1) {
  182. les->b = (double *)G_calloc(cols, sizeof(double));
  183. for (i = 0; i < cols; i++)
  184. les->b[i] = 0.0;
  185. }
  186. les->A = NULL;
  187. les->Asp = NULL;
  188. les->rows = rows;
  189. les->cols = cols;
  190. if (rows == cols)
  191. les->quad = 1;
  192. else
  193. les->quad = 0;
  194. if (type == N_SPARSE_LES) {
  195. les->Asp = G_math_alloc_spmatrix(rows);
  196. les->type = N_SPARSE_LES;
  197. }
  198. else {
  199. les->A = G_alloc_matrix(rows, cols);
  200. les->type = N_NORMAL_LES;
  201. }
  202. return les;
  203. }
  204. /*!
  205. *
  206. * \brief prints the linear equation system to stdout
  207. *
  208. * <p>
  209. * Format:
  210. * A*x = b
  211. *
  212. * <p>
  213. * Example
  214. \verbatim
  215. 2 1 1 1 * 2 = 0.1
  216. 1 2 0 0 * 3 = 0.2
  217. 1 0 2 0 * 3 = 0.2
  218. 1 0 0 2 * 2 = 0.1
  219. \endverbatim
  220. *
  221. * \param les N_les *
  222. * \return void
  223. *
  224. * */
  225. void N_print_les(N_les * les)
  226. {
  227. int i, j, k, out;
  228. if (les->type == N_SPARSE_LES) {
  229. for (i = 0; i < les->rows; i++) {
  230. for (j = 0; j < les->cols; j++) {
  231. out = 0;
  232. for (k = 0; k < les->Asp[i]->cols; k++) {
  233. if (les->Asp[i]->index[k] == j) {
  234. fprintf(stdout, "%4.5f ", les->Asp[i]->values[k]);
  235. out = 1;
  236. }
  237. }
  238. if (!out)
  239. fprintf(stdout, "%4.5f ", 0.0);
  240. }
  241. if (les->x)
  242. fprintf(stdout, " * %4.5f", les->x[i]);
  243. if (les->b)
  244. fprintf(stdout, " = %4.5f ", les->b[i]);
  245. fprintf(stdout, "\n");
  246. }
  247. }
  248. else {
  249. for (i = 0; i < les->rows; i++) {
  250. for (j = 0; j < les->cols; j++) {
  251. fprintf(stdout, "%4.5f ", les->A[i][j]);
  252. }
  253. if (les->x)
  254. fprintf(stdout, " * %4.5f", les->x[i]);
  255. if (les->b)
  256. fprintf(stdout, " = %4.5f ", les->b[i]);
  257. fprintf(stdout, "\n");
  258. }
  259. }
  260. return;
  261. }
  262. /*!
  263. * \brief Release the memory of the linear equation system
  264. *
  265. * \param les N_les *
  266. * \return void
  267. *
  268. * */
  269. void N_free_les(N_les * les)
  270. {
  271. if (les->type == N_SPARSE_LES)
  272. G_debug(2, "Releasing memory of a sparse linear equation system\n");
  273. else
  274. G_debug(2, "Releasing memory of a regular linear equation system\n");
  275. if (les) {
  276. if (les->x)
  277. G_free(les->x);
  278. if (les->b)
  279. G_free(les->b);
  280. if (les->type == N_SPARSE_LES) {
  281. if (les->Asp) {
  282. G_math_free_spmatrix(les->Asp, les->rows);
  283. }
  284. }
  285. else {
  286. if (les->A) {
  287. G_free_matrix(les->A);
  288. }
  289. }
  290. free(les);
  291. }
  292. return;
  293. }