NEURON
sparse.h
Go to the documentation of this file.
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  Header for sparse matrix stuff.
29  Basic sparse routines to be held in sparse.c
30 */
31 
32 /* RCS id: sparse.h,v 1.1 1997/11/03 16:15:55 hines Exp */
33 
34 #ifndef SPARSEH
35 
36 #define SPARSEH
37 
38 
39 #include "matrix.h"
40 
41 
42 /* basic sparse types */
43 
44 typedef struct row_elt {
48 
49 typedef struct SPROW {
50  int len, maxlen, diag;
51  row_elt *elt; /* elt[maxlen] */
52  } SPROW;
53 
54 typedef struct SPMAT {
55  int m, n, max_m, max_n;
57  SPROW *row; /* row[max_m] */
58  int *start_row; /* start_row[max_n] */
59  int *start_idx; /* start_idx[max_n] */
60  } SPMAT;
61 
62 /* Note that the first allocated entry in column j is start_row[j];
63  This starts the chain down the columns using the nxt_row and nxt_idx
64  fields of each entry in each row. */
65 
66 /* hines: change pair to mesch_pair so no conflict with c++ standard */
67 typedef struct mesch_pair { int pos; Real val; } mesch_pair;
68 
69 typedef struct SPVEC {
70  int dim, max_dim;
71  mesch_pair *elt; /* elt[max_dim] */
72  } SPVEC;
73 
74 #define SMNULL ((SPMAT*)NULL)
75 #define SVNULL ((SPVEC*)NULL)
76 
77 /* Macro for speedup */
78 #define sprow_idx2(r,c,hint) \
79  ( ( (hint) >= 0 && (hint) < (r)->len && \
80  (r)->elt[hint].col == (c)) ? (hint) : sprow_idx((r),(c)) )
81 
82 
83 
84 /* memory functions */
85 
86 #ifdef ANSI_C
87 int sp_get_vars(int m,int n,int deg,...);
88 int sp_resize_vars(int m,int n,...);
89 int sp_free_vars(SPMAT **,...);
90 #elif VARARGS
91 int sp_get_vars();
92 int sp_resize_vars();
93 int sp_free_vars();
94 
95 #endif
96 
97 /* Sparse Matrix Operations and Utilities */
98 #ifndef ANSI_C
99 extern SPMAT *sp_get(), *sp_copy(), *sp_copy2(),
100  *sp_zero(), *sp_resize(), *sp_compact();
101 extern double sp_get_val(), sp_set_val();
102 extern VEC *sp_mv_mlt(), *sp_vm_mlt();
103 extern int sp_free();
104 
105 /* Access path operations */
106 extern SPMAT *sp_col_access();
107 extern SPMAT *sp_diag_access();
108 extern int chk_col_access();
109 
110 /* Input/output operations */
111 extern SPMAT *sp_finput();
112 extern void sp_foutput(), sp_foutput2();
113 
114 /* algebraic operations */
115 extern SPMAT *sp_smlt(), *sp_add(), *sp_sub(), *sp_mltadd();
116 
117 
118 /* sparse row operations */
119 extern SPROW *sprow_get(), *sprow_xpd(), *sprow_merge(), *sprow_mltadd(),
120  *sprow_resize(), *sprow_copy();
121 extern SPROW *sprow_add(), *sprow_sub(), *sprow_smlt();
122 extern double sprow_set_val();
123 extern void sprow_foutput();
124 extern int sprow_idx(), sprow_free();
125 
126 /* dump */
127 extern void sp_dump(), sprow_dump();
128 extern MAT *sp_m2dense();
129 
130 #else
131 SPMAT *sp_get(int,int,int), *sp_copy(SPMAT *),
133  *sp_zero(SPMAT *), *sp_resize(SPMAT *,int,int),
134  *sp_compact(SPMAT *,double);
135 double sp_get_val(SPMAT *,int,int), sp_set_val(SPMAT *,int,int,double);
137 int sp_free(SPMAT *);
138 
139 /* Access path operations */
142 int chk_col_access(SPMAT *);
143 
144 /* Input/output operations */
145 SPMAT *sp_finput(FILE *);
146 void sp_foutput(FILE *,SPMAT *), sp_foutput2(FILE *,SPMAT *);
147 
148 /* algebraic operations */
152  *sp_mltadd(SPMAT *A,SPMAT *B,double alpha,SPMAT *C);
153 
154 /* sparse row operations */
155 SPROW *sprow_get(int), *sprow_xpd(SPROW *r,int n,int type),
156  *sprow_resize(SPROW *r,int n,int type),
159  *sprow_mltadd(SPROW *,SPROW *,double,int,SPROW *,int type);
160 SPROW *sprow_add(SPROW *r1,SPROW *r2, int j0,SPROW *r_out, int type),
161  *sprow_sub(SPROW *r1,SPROW *r2, int j0,SPROW *r_out, int type),
162  *sprow_smlt(SPROW *r1,double alpha, int j0,SPROW *r_out, int type);
163 double sprow_set_val(SPROW *,int,double);
164 int sprow_free(SPROW *);
165 int sprow_idx(SPROW *,int);
166 void sprow_foutput(FILE *,SPROW *);
167 
168 /* dump */
169 void sp_dump(FILE *fp, SPMAT *A);
170 void sprow_dump(FILE *fp, SPROW *r);
171 MAT *sp_m2dense(SPMAT *A,MAT *out);
172 
173 #endif
174 
175 /* MACROS */
176 
177 #define sp_input() sp_finput(stdin)
178 #define sp_output(A) sp_foutput(stdout,(A))
179 #define sp_output2(A) sp_foutput2(stdout,(A))
180 #define row_mltadd(r1,r2,alpha,out) sprow_mltadd(r1,r2,alpha,0,out)
181 #define out_row(r) sprow_foutput(stdout,(r))
182 
183 #define SP_FREE(A) ( sp_free((A)), (A)=(SPMAT *)NULL)
184 
185 /* utility for index computations -- ensures index returned >= 0 */
186 #define fixindex(idx) ((idx) == -1 ? (error(E_BOUNDS,"fixindex"),0) : \
187  (idx) < 0 ? -((idx)+2) : (idx))
188 
189 
190 /* NOT USED */
191 
192 /* loop over the columns in a row */
193 /*
194 #define loop_cols(r,e,code) \
195  do { int _r_idx; row_elt *e; SPROW *_t_row; \
196  _t_row = (r); e = &(_t_row->elt); \
197  for ( _r_idx = 0; _r_idx < _t_row->len; _r_idx++, e++ ) \
198  { code; } } while ( 0 )
199 */
200 /* loop over the rows in a column */
201 /*
202 #define loop_cols(A,col,e,code) \
203  do { int _r_num, _r_idx, _c; SPROW *_r; row_elt *e; \
204  if ( ! (A)->flag_col ) sp_col_access((A)); \
205  col_num = (col); \
206  if ( col_num < 0 || col_num >= A->n ) \
207  error(E_BOUNDS,"loop_cols"); \
208  _r_num = (A)->start_row[_c]; _r_idx = (A)->start_idx[_c]; \
209  while ( _r_num >= 0 ) { \
210  _r = &((A)->row[_r_num]); \
211  _r_idx = sprow_idx2(_r,_c,_r_idx); \
212  if ( _r_idx < 0 ) continue; \
213  e = &(_r->elt[_r_idx]); code; \
214  _r_num = e->nxt_row; _r_idx = e->nxt_idx; \
215  } } while ( 0 )
216 
217 */
218 
219 #endif
220 
#define alpha
Definition: bkpfacto.c:43
short type
Definition: cabvars.h:9
static Frame * fp
Definition: code.cpp:161
#define Real
Definition: machine.h:189
#define A(i)
Definition: multisplit.cpp:63
#define B(i)
Definition: multisplit.cpp:64
int const size_t const size_t n
Definition: nrngsl.h:11
void sprow_dump(FILE *fp, SPROW *r)
Definition: sprow.c:45
SPROW * sprow_mltadd(SPROW *, SPROW *, double, int, SPROW *, int type)
Definition: sprow.c:399
SPMAT * sp_copy(SPMAT *)
SPMAT * sp_mltadd(SPMAT *A, SPMAT *B, double alpha, SPMAT *C)
Definition: sparse.c:559
void sp_dump(FILE *fp, SPMAT *A)
Definition: sparseio.c:127
SPROW * sprow_copy(SPROW *, SPROW *, SPROW *, int type)
void sp_foutput(FILE *, SPMAT *)
int sp_resize_vars(int m, int n,...)
Definition: sparse.c:891
SPROW * sprow_sub(SPROW *r1, SPROW *r2, int j0, SPROW *r_out, int type)
int sp_free(SPMAT *)
Definition: sparse.c:260
SPMAT * sp_zero(SPMAT *)
SPROW * sprow_xpd(SPROW *r, int n, int type)
SPMAT * sp_copy2(SPMAT *, SPMAT *)
SPMAT * sp_finput(FILE *)
Definition: sparseio.c:185
double sp_set_val(SPMAT *, int, int, double)
Definition: sparse.c:66
VEC * sp_vm_mlt(SPMAT *, VEC *, VEC *)
Definition: sparse.c:159
SPROW * sprow_resize(SPROW *r, int n, int type)
SPMAT * sp_col_access(SPMAT *)
Definition: sparse.c:376
struct row_elt row_elt
MAT * sp_m2dense(SPMAT *A, MAT *out)
Definition: sparse.c:438
struct SPROW SPROW
SPROW * sprow_merge(SPROW *, SPROW *, SPROW *, int type)
int sp_get_vars(int m, int n, int deg,...)
Definition: sparse.c:861
SPMAT * sp_compact(SPMAT *, double)
Definition: sparse.c:810
int sprow_free(SPROW *)
Definition: sprow.c:259
struct SPMAT SPMAT
SPMAT * sp_get(int, int, int)
void sp_foutput2(FILE *, SPMAT *)
SPROW * sprow_smlt(SPROW *r1, double alpha, int j0, SPROW *r_out, int type)
Definition: sprow.c:605
SPMAT * sp_sub(SPMAT *A, SPMAT *B, SPMAT *C)
VEC * sp_mv_mlt(SPMAT *, VEC *, VEC *)
int sp_free_vars(SPMAT **,...)
Definition: sparse.c:918
struct SPVEC SPVEC
SPROW * sprow_get(int)
SPMAT * sp_add(SPMAT *A, SPMAT *B, SPMAT *C)
SPMAT * sp_diag_access(SPMAT *)
Definition: sparse.c:417
SPMAT * sp_smlt(SPMAT *A, double alpha, SPMAT *B)
SPMAT * sp_resize(SPMAT *, int, int)
SPROW * sprow_add(SPROW *r1, SPROW *r2, int j0, SPROW *r_out, int type)
int sprow_idx(SPROW *, int)
Definition: sprow.c:75
struct mesch_pair mesch_pair
double sp_get_val(SPMAT *, int, int)
int chk_col_access(SPMAT *)
Definition: spbkp.c:512
void sprow_foutput(FILE *, SPROW *)
Definition: sprow.c:644
double sprow_set_val(SPROW *, int, double)
Definition: sprow.c:666
Definition: matrix.h:73
Definition: sparse.h:54
char flag_diag
Definition: sparse.h:56
int max_n
Definition: sparse.h:55
SPROW * row
Definition: sparse.h:57
int max_m
Definition: sparse.h:55
int n
Definition: sparse.h:55
int * start_row
Definition: sparse.h:58
char flag_col
Definition: sparse.h:56
int m
Definition: sparse.h:55
int * start_idx
Definition: sparse.h:59
Definition: sparse.h:49
int diag
Definition: sparse.h:50
int maxlen
Definition: sparse.h:50
int len
Definition: sparse.h:50
row_elt * elt
Definition: sparse.h:51
Definition: sparse.h:69
int dim
Definition: sparse.h:70
mesch_pair * elt
Definition: sparse.h:71
int max_dim
Definition: sparse.h:70
Definition: matrix.h:67
int pos
Definition: sparse.h:67
Real val
Definition: sparse.h:67
Definition: sparse.h:44
int nxt_row
Definition: sparse.h:45
int nxt_idx
Definition: sparse.h:45
int col
Definition: sparse.h:45
Real val
Definition: sparse.h:46