home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / iter.h < prev    next >
C/C++ Source or Header  |  1994-01-13  |  7KB  |  244 lines

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