home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / meschach / !Meschach / h / matrix < prev    next >
Encoding:
Text File  |  1994-04-15  |  18.8 KB  |  642 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. /*
  28.         Type definitions for general purpose maths package
  29. */
  30.  
  31. #ifndef    MATRIXH
  32.  
  33. /* RCS id: $Id: matrix.h,v 1.18 1994/04/16 00:33:37 des Exp $ */
  34.  
  35. #define    MATRIXH    
  36.  
  37. #include    "machine.h"
  38. #include        "err.h"
  39. #include     "meminfo.h"
  40.  
  41. /* unsigned integer type */
  42. #ifndef U_INT_DEF
  43. typedef    unsigned int    u_int;
  44. #define U_INT_DEF
  45. #endif
  46.  
  47. /* vector definition */
  48. typedef    struct    {
  49.         u_int    dim, max_dim;
  50.         Real    *ve;
  51.         } VEC;
  52.  
  53. /* matrix definition */
  54. typedef    struct    {
  55.         u_int    m, n;
  56.         u_int    max_m, max_n, max_size;
  57.         Real    **me,*base;    /* base is base of alloc'd mem */
  58.         } MAT;
  59.  
  60. /* band matrix definition */
  61. typedef struct {
  62.                MAT   *mat;       /* matrix */
  63.                int   lb,ub;    /* lower and upper bandwidth */
  64.                } BAND;
  65.  
  66.  
  67. /* permutation definition */
  68. typedef    struct    {
  69.         u_int    size, max_size, *pe;
  70.         } PERM;
  71.  
  72. /* integer vector definition */
  73. typedef struct    {
  74.         u_int    dim, max_dim;
  75.         int    *ive;
  76.             } IVEC;
  77.  
  78.  
  79. #ifndef MALLOCDECL
  80. #ifndef ANSI_C
  81. extern    char    *malloc(), *calloc(), *realloc();
  82. #else
  83. extern    void    *malloc(size_t),
  84.         *calloc(size_t,size_t),
  85.         *realloc(void *,size_t);
  86. #endif
  87. #endif
  88.  
  89. #ifndef ANSI_C
  90. extern void m_version();
  91. #else
  92. void    m_version( void );
  93. #endif
  94.  
  95. #ifndef ANSI_C
  96. /* allocate one object of given type */
  97. #define    NEW(type)    ((type *)calloc(1,sizeof(type)))
  98.  
  99. /* allocate num objects of given type */
  100. #define    NEW_A(num,type)    ((type *)calloc((unsigned)(num),sizeof(type)))
  101.  
  102.  /* re-allocate arry to have num objects of the given type */
  103. #define    RENEW(var,num,type) \
  104.     ((var)=(type *)((var) ? \
  105.             realloc((char *)(var),(unsigned)(num)*sizeof(type)) : \
  106.             calloc((unsigned)(num),sizeof(type))))
  107.  
  108. #define    MEMCOPY(from,to,n_items,type) \
  109.     MEM_COPY((char *)(from),(char *)(to),(unsigned)(n_items)*sizeof(type))
  110.  
  111. #else
  112. /* allocate one object of given type */
  113. #define    NEW(type)    ((type *)calloc((size_t)1,(size_t)sizeof(type)))
  114.  
  115. /* allocate num objects of given type */
  116. #define    NEW_A(num,type)    ((type *)calloc((size_t)(num),(size_t)sizeof(type)))
  117.  
  118.  /* re-allocate arry to have num objects of the given type */
  119. #define    RENEW(var,num,type) \
  120.     ((var)=(type *)((var) ? \
  121.             realloc((char *)(var),(size_t)((num)*sizeof(type))) : \
  122.             calloc((size_t)(num),(size_t)sizeof(type))))
  123.  
  124. #define    MEMCOPY(from,to,n_items,type) \
  125.  MEM_COPY((char *)(from),(char *)(to),(unsigned)(n_items)*sizeof(type))
  126.  
  127. #endif
  128.  
  129. /* type independent min and max operations */
  130. #ifndef max
  131. #define    max(a,b)    ((a) > (b) ? (a) : (b))
  132. #endif
  133. #ifndef min
  134. #define    min(a,b)    ((a) > (b) ? (b) : (a))
  135. #endif
  136.  
  137.  
  138. #undef TRUE
  139. #define    TRUE    1
  140. #undef FALSE
  141. #define    FALSE    0
  142.  
  143.  
  144. /* for input routines */
  145. #define MAXLINE 81
  146.  
  147.  
  148. /* Dynamic memory allocation */
  149.  
  150. /* Should use M_FREE/V_FREE/PX_FREE in programs instead of m/v/px_free()
  151.    as this is considerably safer -- also provides a simple type check ! */
  152.  
  153. #ifndef ANSI_C
  154.  
  155. extern    VEC *v_get(), *v_resize();
  156. extern    MAT *m_get(), *m_resize();
  157. extern    PERM *px_get(), *px_resize();
  158. extern    IVEC *iv_get(), *iv_resize();
  159. extern    int m_free(),v_free();
  160. extern  int px_free();
  161. extern  int iv_free();
  162. extern  BAND *bd_get(), *bd_resize();
  163. extern  int bd_free();
  164.  
  165. #else
  166.  
  167. /* get/resize vector to given dimension */
  168. extern    VEC *v_get(int), *v_resize(VEC *,int);
  169. /* get/resize matrix to be m x n */
  170. extern    MAT *m_get(int,int), *m_resize(MAT *,int,int);
  171. /* get/resize permutation to have the given size */
  172. extern    PERM *px_get(int), *px_resize(PERM *,int);
  173. /* get/resize an integer vector to given dimension */
  174. extern    IVEC *iv_get(int), *iv_resize(IVEC *,int);
  175. /* get/resize a band matrix to given dimension */
  176. extern  BAND *bd_get(int,int,int), *bd_resize(BAND *,int,int,int);
  177.  
  178. /* free (de-allocate) (band) matrices, vectors, permutations and 
  179.    integer vectors */
  180. extern  int iv_free(IVEC *);
  181. extern    m_free(MAT *),v_free(VEC *),px_free(PERM *);
  182. extern   int bd_free(BAND *);
  183.  
  184. #endif
  185.  
  186.  
  187. /* MACROS */
  188.  
  189. /* macros that also check types and sets pointers to NULL */
  190. #define    M_FREE(mat)    ( m_free(mat),    (mat)=(MAT *)NULL )
  191. #define V_FREE(vec)    ( v_free(vec),    (vec)=(VEC *)NULL )
  192. #define    PX_FREE(px)    ( px_free(px),    (px)=(PERM *)NULL )
  193. #define    IV_FREE(iv)    ( iv_free(iv),    (iv)=(IVEC *)NULL )
  194.  
  195. #define MAXDIM      2001
  196.  
  197.  
  198. /* Entry level access to data structures */
  199. #ifdef DEBUG
  200.  
  201. /* returns x[i] */
  202. #define    v_entry(x,i)    (((i) < 0 || (i) >= (x)->dim) ? \
  203.              error(E_BOUNDS,"v_entry"), 0.0 : (x)->ve[i] )
  204.  
  205. /* x[i] <- val */
  206. #define    v_set_val(x,i,val) ((x)->ve[i] = ((i) < 0 || (i) >= (x)->dim) ? \
  207.                 error(E_BOUNDS,"v_set_val"), 0.0 : (val))
  208.  
  209. /* x[i] <- x[i] + val */
  210. #define    v_add_val(x,i,val) ((x)->ve[i] += ((i) < 0 || (i) >= (x)->dim) ? \
  211.                 error(E_BOUNDS,"v_add_val"), 0.0 : (val))
  212.  
  213. /* x[i] <- x[i] - val */
  214. #define    v_sub_val(x,i,val) ((x)->ve[i] -= ((i) < 0 || (i) >= (x)->dim) ? \
  215.                 error(E_BOUNDS,"v_sub_val"), 0.0 : (val))
  216.  
  217. /* returns A[i][j] */
  218. #define    m_entry(A,i,j)    (((i) < 0 || (i) >= (A)->m || \
  219.               (j) < 0 || (j) >= (A)->n) ? \
  220.              error(E_BOUNDS,"m_entry"), 0.0 : (A)->me[i][j] )
  221.  
  222. /* A[i][j] <- val */
  223. #define    m_set_val(A,i,j,val) ((A)->me[i][j] = ((i) < 0 || (i) >= (A)->m || \
  224.                            (j) < 0 || (j) >= (A)->n) ? \
  225.                   error(E_BOUNDS,"m_set_val"), 0.0 : (val) )
  226.  
  227. /* A[i][j] <- A[i][j] + val */
  228. #define    m_add_val(A,i,j,val) ((A)->me[i][j] += ((i) < 0 || (i) >= (A)->m || \
  229.                         (j) < 0 || (j) >= (A)->n) ? \
  230.                   error(E_BOUNDS,"m_add_val"), 0.0 : (val) )
  231.  
  232. /* A[i][j] <- A[i][j] - val */
  233. #define    m_sub_val(A,i,j,val) ((A)->me[i][j] -= ((i) < 0 || (i) >= (A)->m || \
  234.                         (j) < 0 || (j) >= (A)->n) ? \
  235.                   error(E_BOUNDS,"m_sub_val"), 0.0 : (val) )
  236. #else
  237.  
  238. /* returns x[i] */
  239. #define    v_entry(x,i)        ((x)->ve[i])
  240.  
  241. /* x[i] <- val */
  242. #define    v_set_val(x,i,val)    ((x)->ve[i]  = (val))
  243.  
  244. /* x[i] <- x[i] + val */
  245. #define    v_add_val(x,i,val)    ((x)->ve[i] += (val))
  246.  
  247.  /* x[i] <- x[i] - val */
  248. #define    v_sub_val(x,i,val)    ((x)->ve[i] -= (val))
  249.  
  250. /* returns A[i][j] */
  251. #define    m_entry(A,i,j)        ((A)->me[i][j])
  252.  
  253. /* A[i][j] <- val */
  254. #define    m_set_val(A,i,j,val)    ((A)->me[i][j]  = (val) )
  255.  
  256. /* A[i][j] <- A[i][j] + val */
  257. #define    m_add_val(A,i,j,val)    ((A)->me[i][j] += (val) )
  258.  
  259. /* A[i][j] <- A[i][j] - val */
  260. #define    m_sub_val(A,i,j,val)    ((A)->me[i][j] -= (val) )
  261.  
  262. #endif
  263.  
  264.  
  265. /* I/O routines */
  266. #ifndef ANSI_C
  267.  
  268. extern    void v_foutput(),m_foutput(),px_foutput();
  269. extern  void iv_foutput();
  270. extern    VEC *v_finput();
  271. extern    MAT *m_finput();
  272. extern    PERM *px_finput();
  273. extern    IVEC *iv_finput();
  274. extern    int fy_or_n(), fin_int(), yn_dflt(), skipjunk();
  275. extern    double fin_double();
  276.  
  277. #else
  278.  
  279. /* print x on file fp */
  280. void v_foutput(FILE *fp,VEC *x),
  281.        /* print A on file fp */
  282.     m_foutput(FILE *fp,MAT *A),
  283.        /* print px on file fp */
  284.     px_foutput(FILE *fp,PERM *px);
  285. /* print ix on file fp */
  286. void iv_foutput(FILE *fp,IVEC *ix);
  287.  
  288. /* Note: if out is NULL, then returned object is newly allocated;
  289.         Also: if out is not NULL, then that size is assumed */
  290.  
  291. /* read in vector from fp */
  292. VEC *v_finput(FILE *fp,VEC *out);
  293. /* read in matrix from fp */
  294. MAT *m_finput(FILE *fp,MAT *out);
  295. /* read in permutation from fp */
  296. PERM *px_finput(FILE *fp,PERM *out);
  297. /* read in int vector from fp */
  298. IVEC *iv_finput(FILE *fp,IVEC *out);
  299.  
  300. /* fy_or_n -- yes-or-no to question in string s
  301.         -- question written to stderr, input from fp 
  302.         -- if fp is NOT a tty then return y_n_dflt */
  303. int fy_or_n(FILE *fp,char *s);
  304.  
  305. /* yn_dflt -- sets the value of y_n_dflt to val */
  306. int yn_dflt(int val);
  307.  
  308. /* fin_int -- return integer read from file/stream fp
  309.         -- prompt s on stderr if fp is a tty
  310.         -- check that x lies between low and high: re-prompt if
  311.                 fp is a tty, error exit otherwise
  312.         -- ignore check if low > high           */
  313. int fin_int(FILE *fp,char *s,int low,int high);
  314.  
  315. /* fin_double -- return double read from file/stream fp
  316.         -- prompt s on stderr if fp is a tty
  317.         -- check that x lies between low and high: re-prompt if
  318.                 fp is a tty, error exit otherwise
  319.         -- ignore check if low > high           */
  320. double fin_double(FILE *fp,char *s,double low,double high);
  321.  
  322. /* it skips white spaces and strings of the form #....\n
  323.    Here .... is a comment string */
  324. int skipjunk(FILE *fp);
  325.  
  326. #endif
  327.  
  328.  
  329. /* MACROS */
  330.  
  331. /* macros to use stdout and stdin instead of explicit fp */
  332. #define    v_output(vec)    v_foutput(stdout,vec)
  333. #define    v_input(vec)    v_finput(stdin,vec)
  334. #define    m_output(mat)    m_foutput(stdout,mat)
  335. #define    m_input(mat)    m_finput(stdin,mat)
  336. #define    px_output(px)    px_foutput(stdout,px)
  337. #define    px_input(px)    px_finput(stdin,px)
  338. #define    iv_output(iv)    iv_foutput(stdout,iv)
  339. #define    iv_input(iv)    iv_finput(stdin,iv)
  340.  
  341. /* general purpose input routine; skips comments # ... \n */
  342. #define    finput(fp,prompt,fmt,var) \
  343.     ( ( isatty(fileno(fp)) ? fprintf(stderr,prompt) : skipjunk(fp) ), \
  344.                             fscanf(fp,fmt,var) )
  345. #define    input(prompt,fmt,var)    finput(stdin,prompt,fmt,var)
  346. #define    fprompter(fp,prompt) \
  347.     ( isatty(fileno(fp)) ? fprintf(stderr,prompt) : skipjunk(fp) )
  348. #define    prompter(prompt)    fprompter(stdin,prompt)
  349. #define    y_or_n(s)    fy_or_n(stdin,s)
  350. #define    in_int(s,lo,hi)    fin_int(stdin,s,lo,hi)
  351. #define    in_double(s,lo,hi)    fin_double(stdin,s,lo,hi)
  352.  
  353. /* Copying routines */
  354. #ifndef ANSI_C
  355. extern    MAT    *_m_copy(), *m_move(), *vm_move();
  356. extern    VEC    *_v_copy(), *v_move(), *mv_move();
  357. extern    PERM    *px_copy();
  358. extern    IVEC    *iv_copy(), *iv_move();
  359. extern  BAND    *bd_copy();
  360.  
  361. #else
  362.  
  363. /* copy in to out starting at out[i0][j0] */
  364. extern    MAT    *_m_copy(MAT *in,MAT *out,u_int i0,u_int j0),
  365.         * m_move(MAT *in, int, int, int, int, MAT *out, int, int),
  366.         *vm_move(VEC *in, int, MAT *out, int, int, int, int);
  367. /* copy in to out starting at out[i0] */
  368. extern    VEC    *_v_copy(VEC *in,VEC *out,u_int i0),
  369.         * v_move(VEC *in, int, int, VEC *out, int),
  370.         *mv_move(MAT *in, int, int, int, int, VEC *out, int);
  371. extern    PERM    *px_copy(PERM *in,PERM *out);
  372. extern    IVEC    *iv_copy(IVEC *in,IVEC *out),
  373.         *iv_move(IVEC *in, int, int, IVEC *out, int);
  374. extern  BAND    *bd_copy(BAND *in,BAND *out);
  375.  
  376. #endif
  377.  
  378.  
  379. /* MACROS */
  380. #define    m_copy(in,out)    _m_copy(in,out,0,0)
  381. #define    v_copy(in,out)    _v_copy(in,out,0)
  382.  
  383.  
  384. /* Initialisation routines -- to be zero, ones, random or identity */
  385. #ifndef ANSI_C
  386. extern    VEC     *v_zero(), *v_rand(), *v_ones();
  387. extern    MAT     *m_zero(), *m_ident(), *m_rand(), *m_ones();
  388. extern    PERM    *px_ident();
  389. extern  IVEC    *iv_zero();
  390. #else
  391. extern    VEC     *v_zero(VEC *), *v_rand(VEC *), *v_ones(VEC *);
  392. extern    MAT     *m_zero(MAT *), *m_ident(MAT *), *m_rand(MAT *),
  393.                         *m_ones(MAT *);
  394. extern    PERM    *px_ident(PERM *);
  395. extern  IVEC    *iv_zero(IVEC *);
  396. #endif
  397.  
  398. /* Basic vector operations */
  399. #ifndef ANSI_C
  400. extern    VEC *sv_mlt(), *mv_mlt(), *vm_mlt(), *v_add(), *v_sub(),
  401.         *px_vec(), *pxinv_vec(), *v_mltadd(), *v_map(), *_v_map(),
  402.         *v_lincomb(), *v_linlist();
  403. extern    double    v_min(), v_max(), v_sum();
  404. extern    VEC    *v_star(), *v_slash(), *v_sort();
  405. extern    double _in_prod(), __ip__();
  406. extern    void    __mltadd__(), __add__(), __sub__(), 
  407.                 __smlt__(), __zero__();
  408. #else
  409.  
  410. extern    VEC    *sv_mlt(double,VEC *,VEC *),    /* out <- s.x */
  411.         *mv_mlt(MAT *,VEC *,VEC *),    /* out <- A.x */
  412.         *vm_mlt(MAT *,VEC *,VEC *),    /* oKPsolve(MAT *A,PERM *pivot,PERM *blocks,VEC *b,VEC *x),
  413.         *CHsolve(MAT *A,VEC *b,VEC *x),
  414.         *LDLsolve(MAT *A,VEC *b,VEC *x),
  415.         *LUsolve(MAT *A,PERM *pivot,VEC *b,VEC *x),
  416.         *_Qsolve(MAT *A,VEC *,VEC *,VEC *, VEC *),
  417.         *QRsolve(MAT *A,VEC *,VEC *b,VEC *x),
  418.             *QRTsolve(MAT *A,VEC *,VEC *b,VEC *x),
  419.  
  420.  
  421.      /* Triangular equations solve routines;
  422.         U for upper triangular, L for lower traingular, D for diagonal
  423.         if diag_val == 0.0 use that values in the matrix */
  424.  
  425.         *Usolve(MAT *A,VEC *b,VEC *x,double diag_val),
  426.         *Lsolve(MAT *A,VEC *b,VEC *x,double diag_val),
  427.         *Dsolve(MAT *A,VEC *b,VEC *x),
  428.         *LTsolve(MAT *A,VEC *b,VEC *x,double diag_val),
  429.         *UTsolve(MAT *A,VEC *b,VEC *x,double diag_val),
  430.                 *LUTsolve(MAT *A,PERM *,VEC *,VEC *),
  431.                 *QRCPsolve(MAT *QR,VEC *diag,PERM *pivot,VEC *b,VEC *x);
  432.  
  433. extern  BAND    *bdLUfactor(BAND *A,PERM *pivot),
  434.                 *bdLDLfactor(BAND *A);
  435. extern  VEC     *bdLUsolve(BAND *A,PERM *pivot,VEC *b,VEC *x),
  436.                 *bdLDLsolve(BAND *A,VEC *b,VEC *x);
  437.  
  438.  
  439.  
  440. extern    VEC    *hhvec(VEC *,u_int,Real *,VEC *,Real *);
  441. extern    VEC    *hhtrvec(VEC *,double,u_int,VEC *,VEC *);
  442. extern    MAT    *hhtrrows(MAT *,u_int,u_int,VEC *,double);
  443. extern    MAT    *hhtrcols(MAT *,u_int,u_int,VEC *,double);
  444.  
  445. extern    void    givens(double,double,Real *,Real *);
  446. extern    VEC    *rot_vec(VEC *,u_int,u_int,double,double,VEC *); /* in situ */
  447. extern    MAT    *rot_rows(MAT *,u_int,u_int,double,double,MAT *); /* in situ */
  448. extern    MAT    *rot_cols(MAT *,u_int,u_int,double,double,MAT *); /* in situ */
  449.  
  450.  
  451. /* eigenvalue routines */
  452.  
  453.                /* compute eigenvalues of tridiagonal matrix
  454.                   with diagonal entries a[i], super & sub diagonal entries
  455.                   b[i]; eigenvectors stored in Q (if not NULL) */
  456. extern    VEC    *trieig(VEC *a,VEC *b,MAT *Q),
  457.                  /* sets out to be vector of eigenvectors; eigenvectors
  458.                    stored in Q (if not NULL). A is unchanged */
  459.         *symmeig(MAT *A,MAT *Q,VEC *out);
  460.  
  461.                /* computes real Schur form = Q^T.A.Q */
  462. extern    MAT    *schur(MAT *A,MAT *Q);
  463.          /* computes real and imaginary parts of the eigenvalues
  464.                         of A after schur() */
  465. extern    void    schur_evals(MAT *A,VEC *re_part,VEC *im_part);
  466.           /* computes real and imaginary parts of the eigenvectors
  467.                         of A after schur() */
  468. extern    MAT    *schur_vecs(MAT *T,MAT *Q,MAT *X_re,MAT *X_im);
  469.  
  470.  
  471. /* singular value decomposition */
  472.  
  473.         /* computes singular values of bi-diagonal matrix with
  474.                    diagonal entries a[i] and superdiagonal entries b[i];
  475.                    singular vectors stored in U and V (if not NULL) */
  476. VEC    *bisvd(VEC *a,VEC *b,MAT *U,MAT *V),
  477.                /* sets out to be vector of singular values;
  478.                    singular vectors stored in U and V */
  479.     *svd(MAT *A,MAT *U,MAT *V,VEC *out);
  480.  
  481. /* matrix powers and exponent */
  482. MAT  *_m_pow(MAT *,int,MAT *,MAT *);
  483. MAT  *m_pow(MAT *,int, MAT *);
  484. MAT  *m_exp(MAT *,double,MAT *);
  485. MAT  *_m_exp(MAT *,double,MAT *,int *,int *);
  486. MAT  *m_poly(MAT *,VEC *,MAT *);
  487.  
  488. /* FFT */
  489. void fft(VEC *,VEC *);
  490. void ifft(VEC *,VEC *);
  491.  
  492. #endif
  493.  
  494.  
  495. #endif
  496. FileDataŵmeminfo4EÿÿÿÀ·?OÔ
  497. /**************************************************************************
  498. **
  499. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  500. **
  501. **                 Meschach Library
  502. ** 
  503. ** This Meschach Library is provided "as is" without any express 
  504. ** or implied warranty of any kind with respect to this software. 
  505. ** In particular the authors shall not be liable for any direct, 
  506. ** indirect, special, incidental or consequential damages arising 
  507. ** in any way from use of the software.
  508. ** 
  509. ** Everyone is granted permission to copy, modify and redistribute this
  510. ** Meschach Library, provided:
  511. **  1.  All copies contain this copyright notice.
  512. **  2.  All modified copies shall carry a notice stating who
  513. **      made the last modification and the date of such modification.
  514. **  3.  No charge is made for this software or works derived from it.  
  515. **      This clause shall not be construed as constraining other software
  516. **      distributed on the same medium as this software, nor is a
  517. **      distribution fee considered a charge.
  518. **
  519. ***************************************************************************/
  520.  
  521.  
  522. /* meminfo.h  26/08/93 */
  523. /* changed  11/12/93 */
  524.  
  525.  
  526. #ifndef MEM_INFOH
  527. #define MEM_INFOH
  528.  
  529.  
  530.  
  531. /* for hash table in mem_stat.c */
  532. /* Note: the hash size should be a prime, or at very least odd */
  533. #define MEM_HASHSIZE         509
  534. #define MEM_HASHSIZE_FILE    "meminfo.h"
  535.  
  536.  
  537. /* default: memory information is off */
  538. /* set it to 1 if you want it all the time */
  539. #define MEM_SWITCH_ON_DEF    0
  540.  
  541.  
  542. /* available standard types */
  543. #define TYPE_NULL              (-1)
  544. #define TYPE_MAT                0
  545. #define TYPE_BAND               1
  546. #define TYPE_PERM        2
  547. #define TYPE_VEC        3
  548. #define TYPE_IVEC        4
  549.  
  550. #ifdef SPARSE
  551. #define TYPE_ITER        5
  552. #define TYPE_SPROW              6
  553. #define TYPE_SPMAT        7
  554. #endif
  555.  
  556. #ifdef COMPLEX
  557. #ifdef SPARSE
  558. #define TYPE_ZVEC        8
  559. #define TYPE_ZMAT        9
  560. #else
  561. #define TYPE_ZVEC        5
  562. #define TYPE_ZMAT        6
  563. #endif
  564. #endif
  565.  
  566. /* structure for memory information */
  567. typedef struct {
  568.    long bytes;       /* # of allocated bytes for each type (summary) */
  569.    int  numvar;      /* # of allocated variables for each type */
  570. } MEM_ARRAY;
  571.  
  572.  
  573.  
  574. #ifdef ANSI_C
  575.  
  576. int  mem_info_is_on(void);
  577. int mem_info_on(int sw);
  578.  
  579. long mem_info_bytes(int type,int list);
  580. int mem_info_numvar(int type,int list);
  581. void mem_info_file(FILE * fp,int list);
  582.  
  583. void mem_bytes_list(int type,int old_size,int new_size,
  584.                int list);
  585. void mem_numvar_list(int type, int num, int list);
  586.  
  587. int mem_stat_reg_list(void **var,int type,int list);
  588. int mem_stat_mark(int mark);
  589. int mem_stat_free_list(int mark,int list);
  590. int mem_stat_show_mark(void);
  591. void mem_stat_dump(FILE *fp,int list);
  592. int mem_attach_list(int list,int ntypes,char *type_names[],
  593.     int (*free_funcs[])(), MEM_ARRAY info_sum[]);
  594. int mem_free_vars(int list);
  595. int mem_is_list_attached(int list);
  596. void mem_dump_list(FILE *fp,int list);
  597. int mem_stat_reg_vars(int list,int type,...);
  598.  
  599. #else
  600. int mem_info_is_on();
  601. int mem_info_on();
  602.  
  603. long mem_info_bytes();
  604. int mem_info_numvar();
  605. void mem_info_file();
  606.  
  607. void mem_bytes_list();
  608. void mem_numvar_list();
  609.  
  610. int mem_stat_reg_list();
  611. int mem_stat_mark();
  612. int mem_stat_free_list();
  613. int mem_stat_show_mark();
  614. void mem_stat_dump();
  615. int mem_attach_list();
  616. int mem_free_vars();
  617. int mem_is_list_attached();
  618. void mem_dump_list();
  619. int mem_stat_reg_vars();
  620.  
  621. #endif 
  622.  
  623. /* macros */
  624.  
  625. #define mem_info()   mem_info_file(stdout,0)
  626.  
  627. #define mem_stat_reg(var,type)  mem_stat_reg_list((void **)var,type,0)
  628. #define MEM_STAT_REG(var,type)  mem_stat_reg_list((void **)&(var),type,0)
  629. #define mem_stat_free(mark)   mem_stat_free_list(mark,0)
  630.  
  631. #define mem_bytes(type,old_size,new_size)  \
  632.   mem_bytes_list(type,old_size,new_size,0)
  633.  
  634. #define mem_numvar(type,num) mem_numvar_list(type,num,0)
  635.  
  636.  
  637. /* internal type */
  638.  
  639. typedef struct {
  640.    char **type_names;        /* array of names of types (strings) */
  641.    int  (**free_funcs)();    /* array of functions for releasing types */
  642.