sparse.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /*
  25. Header for sparse matrix stuff.
  26. Basic sparse routines to be held in sparse.c
  27. */
  28. /* RCS id: $Id: sparse.h,v 1.2 1994/01/13 05:33:36 des Exp $ */
  29. #ifndef SPARSEH
  30. #define SPARSEH
  31. #include "matrix.h"
  32. /* basic sparse types */
  33. typedef struct row_elt {
  34. int col, nxt_row, nxt_idx;
  35. Real val;
  36. } row_elt;
  37. typedef struct SPROW {
  38. int len, maxlen, diag;
  39. row_elt *elt; /* elt[maxlen] */
  40. } SPROW;
  41. typedef struct SPMAT {
  42. int m, n, max_m, max_n;
  43. char flag_col, flag_diag;
  44. SPROW *row; /* row[max_m] */
  45. int *start_row; /* start_row[max_n] */
  46. int *start_idx; /* start_idx[max_n] */
  47. } SPMAT;
  48. /* Note that the first allocated entry in column j is start_row[j];
  49. This starts the chain down the columns using the nxt_row and nxt_idx
  50. fields of each entry in each row. */
  51. typedef struct pair { int pos; Real val; } pair;
  52. typedef struct SPVEC {
  53. int dim, max_dim;
  54. pair *elt; /* elt[max_dim] */
  55. } SPVEC;
  56. #define SMNULL ((SPMAT*)NULL)
  57. #define SVNULL ((SPVEC*)NULL)
  58. /* Macro for speedup */
  59. #define sprow_idx2(r,c,hint) \
  60. ( ( (hint) >= 0 && (hint) < (r)->len && \
  61. (r)->elt[hint].col == (c)) ? (hint) : sprow_idx((r),(c)) )
  62. /* memory functions */
  63. #ifdef ANSI_C
  64. int sp_get_vars(int m,int n,int deg,...);
  65. int sp_resize_vars(int m,int n,...);
  66. int sp_free_vars(SPMAT **,...);
  67. #elif VARARGS
  68. int sp_get_vars();
  69. int sp_resize_vars();
  70. int sp_free_vars();
  71. #endif /* ANSI_C */
  72. /* Sparse Matrix Operations and Utilities */
  73. #ifndef ANSI_C
  74. extern SPMAT *sp_get(), *sp_copy(), *sp_copy2(),
  75. *sp_zero(), *sp_resize(), *sp_compact();
  76. extern double sp_get_val(), sp_set_val();
  77. extern VEC *sp_mv_mlt(), *sp_vm_mlt();
  78. extern int sp_free();
  79. /* Access path operations */
  80. extern SPMAT *sp_col_access();
  81. extern SPMAT *sp_diag_access();
  82. extern int chk_col_access();
  83. /* Input/output operations */
  84. extern SPMAT *sp_finput();
  85. extern void sp_foutput(), sp_foutput2();
  86. /* algebraic operations */
  87. extern SPMAT *sp_smlt(), *sp_add(), *sp_sub(), *sp_mltadd();
  88. /* sparse row operations */
  89. extern SPROW *sprow_get(), *sprow_xpd(), *sprow_merge(), *sprow_mltadd(),
  90. *sprow_resize(), *sprow_copy();
  91. extern SPROW *sprow_add(), *sprow_sub(), *sprow_smlt();
  92. extern double sprow_set_val();
  93. extern void sprow_foutput();
  94. extern int sprow_idx(), sprow_free();
  95. /* dump */
  96. extern void sp_dump(), sprow_dump();
  97. extern MAT *sp_m2dense();
  98. #else
  99. SPMAT *sp_get(int,int,int), *sp_copy(const SPMAT *),
  100. *sp_copy2(const SPMAT *,SPMAT *),
  101. *sp_zero(SPMAT *), *sp_resize(SPMAT *,int,int),
  102. *sp_compact(SPMAT *,double);
  103. double sp_get_val(const SPMAT *,int,int), sp_set_val(SPMAT *,int,int,double);
  104. VEC *sp_mv_mlt(const SPMAT *, const VEC *, VEC *),
  105. *sp_vm_mlt(const SPMAT *, const VEC *, VEC *);
  106. int sp_free(SPMAT *);
  107. /* Access path operations */
  108. SPMAT *sp_col_access(SPMAT *);
  109. SPMAT *sp_diag_access(SPMAT *);
  110. int chk_col_access(const SPMAT *);
  111. /* Input/output operations */
  112. SPMAT *sp_finput(FILE *);
  113. void sp_foutput(FILE *, const SPMAT *);
  114. /* algebraic operations */
  115. SPMAT *sp_smlt(const SPMAT *A,double alpha,SPMAT *B),
  116. *sp_add(const SPMAT *A,const SPMAT *B,SPMAT *C),
  117. *sp_sub(const SPMAT *A,const SPMAT *B,SPMAT *C),
  118. *sp_mltadd(const SPMAT *A,const SPMAT *B,double alpha,SPMAT *C);
  119. /* sparse row operations */
  120. SPROW *sprow_get(int), *sprow_xpd(SPROW *r,int n,int type),
  121. *sprow_resize(SPROW *r,int n,int type),
  122. *sprow_merge(const SPROW *,const SPROW *,SPROW *,int type),
  123. *sprow_copy(const SPROW *,const SPROW *,SPROW *,int type),
  124. *sprow_mltadd(const SPROW *r1,const SPROW *r2, double alpha,
  125. int j0, SPROW *r_out, int type);
  126. SPROW *sprow_add(const SPROW *r1,const SPROW *r2, int j0,SPROW *r_out, int type),
  127. *sprow_sub(const SPROW *r1,const SPROW *r2, int j0,SPROW *r_out, int type),
  128. *sprow_smlt(const SPROW *r1,double alpha, int j0,SPROW *r_out, int type);
  129. double sprow_set_val(SPROW *,int,double);
  130. int sprow_free(SPROW *);
  131. int sprow_idx(const SPROW *,int);
  132. void sprow_foutput(FILE *,const SPROW *);
  133. /* dump */
  134. void sp_dump(FILE *fp, const SPMAT *A);
  135. void sprow_dump(FILE *fp, const SPROW *r);
  136. MAT *sp_m2dense(const SPMAT *A,MAT *out);
  137. #endif /* ANSI_C */
  138. /* MACROS */
  139. #define sp_input() sp_finput(stdin)
  140. #define sp_output(A) sp_foutput(stdout,(A))
  141. #define sp_output2(A) sp_foutput2(stdout,(A))
  142. #define row_mltadd(r1,r2,alpha,out) sprow_mltadd(r1,r2,alpha,0,out)
  143. #define out_row(r) sprow_foutput(stdout,(r))
  144. #define SP_FREE(A) ( sp_free((A)), (A)=(SPMAT *)NULL)
  145. /* utility for index computations -- ensures index returned >= 0 */
  146. #define fixindex(idx) ((idx) == -1 ? (error(E_BOUNDS,"fixindex"),0) : \
  147. (idx) < 0 ? -((idx)+2) : (idx))
  148. /* NOT USED */
  149. /* loop over the columns in a row */
  150. /*
  151. #define loop_cols(r,e,code) \
  152. do { int _r_idx; row_elt *e; SPROW *_t_row; \
  153. _t_row = (r); e = &(_t_row->elt); \
  154. for ( _r_idx = 0; _r_idx < _t_row->len; _r_idx++, e++ ) \
  155. { code; } } while ( 0 )
  156. */
  157. /* loop over the rows in a column */
  158. /*
  159. #define loop_cols(A,col,e,code) \
  160. do { int _r_num, _r_idx, _c; SPROW *_r; row_elt *e; \
  161. if ( ! (A)->flag_col ) sp_col_access((A)); \
  162. col_num = (col); \
  163. if ( col_num < 0 || col_num >= A->n ) \
  164. error(E_BOUNDS,"loop_cols"); \
  165. _r_num = (A)->start_row[_c]; _r_idx = (A)->start_idx[_c]; \
  166. while ( _r_num >= 0 ) { \
  167. _r = &((A)->row[_r_num]); \
  168. _r_idx = sprow_idx2(_r,_c,_r_idx); \
  169. if ( _r_idx < 0 ) continue; \
  170. e = &(_r->elt[_r_idx]); code; \
  171. _r_num = e->nxt_row; _r_idx = e->nxt_idx; \
  172. } } while ( 0 )
  173. */
  174. #endif /* SPARSEH */