iter.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**************************************************************************
  2. **
  3. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  4. **
  5. ** Meschach Library
  6. **
  7. ** This Meschach Library is provided "as is" without any express
  8. ** or implied warranty of any kind with respect to this software.
  9. ** In particular the authors shall not be liable for any direct,
  10. ** indirect, special, incidental or consequential damages arising
  11. ** in any way from use of the software.
  12. **
  13. ** Everyone is granted permission to copy, modify and redistribute this
  14. ** Meschach Library, provided:
  15. ** 1. All copies contain this copyright notice.
  16. ** 2. All modified copies shall carry a notice stating who
  17. ** made the last modification and the date of such modification.
  18. ** 3. No charge is made for this software or works derived from it.
  19. ** This clause shall not be construed as constraining other software
  20. ** distributed on the same medium as this software, nor is a
  21. ** distribution fee considered a charge.
  22. **
  23. ***************************************************************************/
  24. /* iter.h 14/09/93 */
  25. /*
  26. Structures for iterative methods
  27. */
  28. #ifndef ITERHH
  29. #define ITERHH
  30. /* RCS id: $Id: iter.h,v 1.2 1994/03/08 05:48:27 des Exp $ */
  31. #include "sparse.h"
  32. /* basic structure for iterative methods */
  33. /* type Fun_Ax for functions to get y = A*x */
  34. #ifdef ANSI_C
  35. typedef VEC *(*Fun_Ax)(void *,VEC *,VEC *);
  36. #else
  37. typedef VEC *(*Fun_Ax)();
  38. #endif
  39. /* type ITER */
  40. typedef struct Iter_data {
  41. int shared_x; /* if TRUE then x is shared and it will not be free'd */
  42. int shared_b; /* if TRUE then b is shared and it will not be free'd */
  43. unsigned k; /* no. of direction (search) vectors; =0 - none */
  44. int limit; /* upper bound on the no. of iter. steps */
  45. int steps; /* no. of iter. steps done */
  46. Real eps; /* accuracy required */
  47. VEC *x; /* input: initial guess;
  48. output: approximate solution */
  49. VEC *b; /* right hand side of the equation A*x = b */
  50. Fun_Ax Ax; /* function computing y = A*x */
  51. void *A_par; /* parameters for Ax */
  52. Fun_Ax ATx; /* function computing y = A^T*x;
  53. T = transpose */
  54. void *AT_par; /* parameters for ATx */
  55. Fun_Ax Bx; /* function computing y = B*x; B - preconditioner */
  56. void *B_par; /* parameters for Bx */
  57. Fun_Ax BTx; /* function computing y = B^T*x; B - preconditioner */
  58. void *BT_par; /* parameters for BTx */
  59. #ifdef ANSI_C
  60. #ifdef PROTOTYPES_IN_STRUCT
  61. void (*info)(struct Iter_data *, double, VEC *,VEC *);
  62. /* function giving some information for a user;
  63. nres - a norm of a residual res */
  64. int (*stop_crit)(struct Iter_data *, double, VEC *,VEC *);
  65. /* stopping criterion:
  66. nres - a norm of res;
  67. res - residual;
  68. if returned value == TRUE then stop;
  69. if returned value == FALSE then continue; */
  70. #else
  71. void (*info)();
  72. int (*stop_crit)();
  73. #endif /* PROTOTYPES_IN_STRUCT */
  74. #else
  75. void (*info)();
  76. /* function giving some information for a user */
  77. int (*stop_crit)();
  78. /* stopping criterion:
  79. if returned value == TRUE then stop;
  80. if returned value == FALSE then continue; */
  81. #endif /* ANSI_C */
  82. Real init_res; /* the norm of the initial residual */
  83. } ITER;
  84. #define INULL (ITER *)NULL
  85. /* type Fun_info */
  86. #ifdef ANSI_C
  87. typedef void (*Fun_info)(ITER *, double, VEC *,VEC *);
  88. #else
  89. typedef void (*Fun_info)();
  90. #endif
  91. /* type Fun_stp_crt */
  92. #ifdef ANSI_C
  93. typedef int (*Fun_stp_crt)(ITER *, double, VEC *,VEC *);
  94. #else
  95. typedef int (*Fun_stp_crt)();
  96. #endif
  97. /* macros */
  98. /* default values */
  99. #define ITER_LIMIT_DEF 1000
  100. #define ITER_EPS_DEF 1e-6
  101. /* other macros */
  102. /* set ip->Ax=fun and ip->A_par=fun_par */
  103. #define iter_Ax(ip,fun,fun_par) \
  104. (ip->Ax=(Fun_Ax)(fun),ip->A_par=(void *)(fun_par),0)
  105. #define iter_ATx(ip,fun,fun_par) \
  106. (ip->ATx=(Fun_Ax)(fun),ip->AT_par=(void *)(fun_par),0)
  107. #define iter_Bx(ip,fun,fun_par) \
  108. (ip->Bx=(Fun_Ax)(fun),ip->B_par=(void *)(fun_par),0)
  109. #define iter_BTx(ip,fun,fun_par) \
  110. (ip->BTx=(Fun_Ax)(fun),ip->BT_par=(void *)(fun_par),0)
  111. /* save free macro */
  112. #define ITER_FREE(ip) (iter_free(ip), (ip)=(ITER *)NULL)
  113. /* prototypes from iter0.c */
  114. #ifdef ANSI_C
  115. /* standard information */
  116. void iter_std_info(const ITER *ip,double nres,VEC *res,VEC *Bres);
  117. /* standard stopping criterion */
  118. int iter_std_stop_crit(const ITER *ip, double nres, VEC *res,VEC *Bres);
  119. /* get, resize and free ITER variable */
  120. ITER *iter_get(int lenb, int lenx);
  121. ITER *iter_resize(ITER *ip,int lenb,int lenx);
  122. int iter_free(ITER *ip);
  123. void iter_dump(FILE *fp,ITER *ip);
  124. /* copy ip1 to ip2 copying also elements of x and b */
  125. ITER *iter_copy(const ITER *ip1, ITER *ip2);
  126. /* copy ip1 to ip2 without copying elements of x and b */
  127. ITER *iter_copy2(ITER *ip1,ITER *ip2);
  128. /* functions for generating sparse matrices with random elements */
  129. SPMAT *iter_gen_sym(int n, int nrow);
  130. SPMAT *iter_gen_nonsym(int m,int n,int nrow,double diag);
  131. SPMAT *iter_gen_nonsym_posdef(int n,int nrow);
  132. #else
  133. void iter_std_info();
  134. int iter_std_stop_crit();
  135. ITER *iter_get();
  136. int iter_free();
  137. ITER *iter_resize();
  138. void iter_dump();
  139. ITER *iter_copy();
  140. ITER *iter_copy2();
  141. SPMAT *iter_gen_sym();
  142. SPMAT *iter_gen_nonsym();
  143. SPMAT *iter_gen_nonsym_posdef();
  144. #endif
  145. /* prototypes from iter.c */
  146. /* different iterative procedures */
  147. #ifdef ANSI_C
  148. VEC *iter_cg(ITER *ip);
  149. VEC *iter_cg1(ITER *ip);
  150. VEC *iter_spcg(SPMAT *A,SPMAT *LLT,VEC *b,double eps,VEC *x,int limit,
  151. int *steps);
  152. VEC *iter_cgs(ITER *ip,VEC *r0);
  153. VEC *iter_spcgs(SPMAT *A,SPMAT *B,VEC *b,VEC *r0,double eps,VEC *x,
  154. int limit, int *steps);
  155. VEC *iter_lsqr(ITER *ip);
  156. VEC *iter_splsqr(SPMAT *A,VEC *b,double tol,VEC *x,
  157. int limit,int *steps);
  158. VEC *iter_gmres(ITER *ip);
  159. VEC *iter_spgmres(SPMAT *A,SPMAT *B,VEC *b,double tol,VEC *x,int k,
  160. int limit, int *steps);
  161. MAT *iter_arnoldi_iref(ITER *ip,Real *h,MAT *Q,MAT *H);
  162. MAT *iter_arnoldi(ITER *ip,Real *h,MAT *Q,MAT *H);
  163. MAT *iter_sparnoldi(SPMAT *A,VEC *x0,int k,Real *h,MAT *Q,MAT *H);
  164. VEC *iter_mgcr(ITER *ip);
  165. VEC *iter_spmgcr(SPMAT *A,SPMAT *B,VEC *b,double tol,VEC *x,int k,
  166. int limit, int *steps);
  167. void iter_lanczos(ITER *ip,VEC *a,VEC *b,Real *beta2,MAT *Q);
  168. void iter_splanczos(SPMAT *A,int m,VEC *x0,VEC *a,VEC *b,Real *beta2,
  169. MAT *Q);
  170. VEC *iter_lanczos2(ITER *ip,VEC *evals,VEC *err_est);
  171. VEC *iter_splanczos2(SPMAT *A,int m,VEC *x0,VEC *evals,VEC *err_est);
  172. VEC *iter_cgne(ITER *ip);
  173. VEC *iter_spcgne(SPMAT *A,SPMAT *B,VEC *b,double eps,VEC *x,
  174. int limit,int *steps);
  175. #else
  176. VEC *iter_cg();
  177. VEC *iter_cg1();
  178. VEC *iter_spcg();
  179. VEC *iter_cgs();
  180. VEC *iter_spcgs();
  181. VEC *iter_lsqr();
  182. VEC *iter_splsqr();
  183. VEC *iter_gmres();
  184. VEC *iter_spgmres();
  185. MAT *iter_arnoldi_iref();
  186. MAT *iter_arnoldi();
  187. MAT *iter_sparnoldi();
  188. VEC *iter_mgcr();
  189. VEC *iter_spmgcr();
  190. void iter_lanczos();
  191. void iter_splanczos();
  192. VEC *iter_lanczos2();
  193. VEC *iter_splanczos2();
  194. VEC *iter_cgne();
  195. VEC *iter_spcgne();
  196. #endif
  197. #endif /* ITERHH */