-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.mod
339 lines (317 loc) · 8.42 KB
/
matrix.mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
: $Id: matrix.mod,v 1.25 2000/12/15 20:09:51 billl Exp $
:* COMMENT
COMMENT
NB: only minimal error checking done
NB: no dynamic allocation - eg m1.transpose(m1) will give a wrong result
NB: matrix and vec sizes must be correct before using: use .resize()
================ USAGE ================
objref mat
mat = new Vector(rows*cols)
mat.mprintf(M,N) // print out as M rows and N columns
mat2.transpose(mat) // transpose of matrix
y.mmult(mat,x) // y = mat*x
y.spmult(pre,post,mat,x) // y = mat*x using "sparse matrix"
w.spget(pre,post,row,col) // ie pre,post,post,pre!!
wt.mkspcp/chkspcp(pre,post) // copy the indices into integer arrays
mat.outprod(x,y) // mat = outer product of vectors x and y
mat.mget(i,j,cols) // i=row#; j=col#
mat.mset(i,j,cols,val)
y.mrow(mat,i,cols)
y.mcol(mat,j,cols)
================================================================
ENDCOMMENT
NEURON {
SUFFIX nothing
}
VERBATIM
#ifndef NRN_VERSION_GTEQ_8_2_0
extern double hoc_call_func(Symbol*, int narg);
#endif
ENDVERBATIM
:* mat.outprod(x,y) // mat = outer product of vectors x and y
VERBATIM
static double outprod(void* vv) {
int i, j, nx, ny, nz;
double *x, *y, *z;
/* this will be the outer product */
nx = vector_instance_px(vv, &x);
/* these are the two vectors that make it up */
ny = vector_arg_px(1, &y); // will be number of columns
nz = vector_arg_px(2, &z); // will be number of rows
if (nx != ny*nz) {
hoc_execerror("Vector size mismatch", 0);
}
for (i=0;i<ny;i++) {
for (j=0;j<nz;j++) {
x[i*nz+j] = y[i]*z[j];
}
}
return nx;
}
ENDVERBATIM
:* mmult
VERBATIM
static double mmult(void* vv) {
int i, j, nx, ny, nz;
double *x, *y, *z;
/* x will be the product of matrix y and vec z */
nx = vector_instance_px(vv, &x);
ny = vector_arg_px(1, &y);
nz = vector_arg_px(2, &z);
if (ny != nx*nz) {
hoc_execerror("Vector size mismatch", 0);
}
for (i=0;i<nx;i++) {
x[i] = 0.;
for (j=0;j<nz;j++) {
x[i] += y[i*nz+j]*z[j];
}
}
return nx;
}
ENDVERBATIM
:* ST[PO].spltp(pr,po,wt,ST[PRE])
VERBATIM
static double spltp(void* vv) {
int ii, jj, nstpr, nstpo, nw, npr, npo, flag, cnt;
double *stpr, *stpo, *w, *pr, *po;
char func[4] = "ltp";
Symbol* s = hoc_lookup(func);
if (! s) { hoc_execerror("Can't find ltp() func", 0); }
nstpo = vector_instance_px(vv, &stpo);
npr = vector_arg_px(1, &pr);
npo = vector_arg_px(2, &po);
nw = vector_arg_px(3, &w);
nstpr = vector_arg_px(4, &stpr);
for (ii=0,jj=0,cnt=0;ii<nstpo;ii++) {
if (stpo[ii]==1.0) { /* connections to these will be changed */
for (;po[jj]<ii;jj++) ; /* move forward till find a po */
for (;po[jj]==ii;jj++) { /* move through these po's */
if (stpr[(int)pr[jj]]==1.) { /* did the presyn spike? */
cnt++; hoc_pushx(1.0);
} else {
cnt--; hoc_pushx(-1.0);
}
hoc_pushx(w[jj]);
w[jj]=hoc_call_func(s, 2);
}
}
}
return cnt;
}
ENDVERBATIM
VERBATIM
/* Maintain a parallel vector of ints to avoid the slowness of repeated casts in spmult */
static int *pr_int;
static int *po_int;
static int cpfl=0;
ENDVERBATIM
:* wt.mkspcp(pr,po)
VERBATIM
static double mkspcp(void* vv) {
int j, nw, npr, npo;
double *w, *pr, *po;
if (! ifarg(1)) {
cpfl=0;
if (po_int!=NULL) free(po_int);
if (pr_int!=NULL) free(pr_int);
po_int=(int *)NULL; pr_int=(int *)NULL;
return 0;
}
nw = vector_instance_px(vv, &w);
npr = vector_arg_px(1, &pr);
npo = vector_arg_px(2, &po);
pr_int=(int *)ecalloc(nw, sizeof(int));
po_int=(int *)ecalloc(nw, sizeof(int));
for (j=0;j<nw;j++) {
po_int[j]=(int)po[j];
pr_int[j]=(int)pr[j];
}
cpfl=nw;
return cpfl;
}
ENDVERBATIM
:* wt.chkspcp(pr,po)
VERBATIM
static double chkspcp(void* vv) {
int j, nw, npr, npo, flag;
double *w, *pr, *po;
nw = vector_instance_px(vv, &w);
npr = vector_arg_px(1, &pr);
npo = vector_arg_px(2, &po);
flag=1;
if (po_int==NULL || pr_int==NULL) { cpfl=0; return 0; }
if (cpfl!=nw) { flag=0;
} else for (j=0;j<nw;j++) {
if (po_int[j]!=(int)po[j] || pr_int[j]!=(int)pr[j]) {flag=0; continue;}
}
if (flag==0) {
cpfl=0; free(po_int); free(pr_int);
po_int=(int *)NULL; pr_int=(int *)NULL;
}
return flag;
}
ENDVERBATIM
:* y.spmult(pr,po,wt,x[,flag])
: y=W*x, y will be the product of matrix w with pre/post indices and vec x
: optional flag (5th arg present) - do not clear dest vector initially
VERBATIM
static double spmult(void* vv) {
int i, j, nx, ny, nw, npr, npo, flag;
double *x, *y, *w, *pr, *po, xx;
ny = vector_instance_px(vv, &y);
npr = vector_arg_px(1, &pr);
npo = vector_arg_px(2, &po);
nw = vector_arg_px(3, &w);
nx = vector_arg_px(4, &x);
if (ifarg(5)) {flag=1;} else {flag=0;}
if (nw!=npr || nw!=npo) {
hoc_execerror("Sparse mat must have 3 identical size vecs for pre/post/wt", 0);
}
if (flag==0) for (i=0;i<ny;i++) y[i] = 0.; // clear dest vec
if (cpfl==0) {
for (j=0;j<nw;j++) y[(int)po[j]] += (x[(int)pr[j]]*w[j]);
} else if (cpfl!=nw) { hoc_execerror("cpfl!=nw in spmult", 0); } else {
for (j=0;j<nw;j++) if (x[pr_int[j]]!=0) { y[po_int[j]] += ((x[pr_int[j]])*w[j]); }
}
return nx;
}
ENDVERBATIM
:* wt.spget(pr,po,row,col) returns weight value
VERBATIM
static double spget(void* vv) {
int j, nw, npr, npo;
double *w, *pr, *po, row, col;
nw = vector_instance_px(vv, &w);
npr = vector_arg_px(1, &pr);
npo = vector_arg_px(2, &po);
row = *getarg(3);
col = *getarg(4);
for (j=0;j<nw;j++) if (row==po[j]&&col==pr[j]) break;
if (j==nw) return 0.; else return w[j];
}
ENDVERBATIM
:* transpose
VERBATIM
static double transpose(void* vv) {
int i, j, nx, ny, rows, cols;
double *x, *y;
/* x will be the transpose of matrix y */
nx = vector_instance_px(vv, &x);
ny = vector_arg_px(1, &y);
rows = (int)*getarg(2);
cols = (int)*getarg(3);
if (ny != nx) {
hoc_execerror("Vector size mismatch", 0);
}
for (i=0;i<rows;i++) {
for (j=0;j<cols;j++) {
x[j*rows+i] = y[i*cols+j];
}
}
return nx;
}
ENDVERBATIM
:* mprintf
VERBATIM
static double mprintf(void* vv) {
int i, j, nx, rows, cols;
double *x;
/* x will be printed out */
nx = vector_instance_px(vv, &x);
rows = (int)*getarg(1);
cols = (int)*getarg(2);
if (nx != rows*cols) {
hoc_execerror("Vector size mismatch", 0);
}
for (i=0;i<rows;i++) {
for (j=0;j<cols;j++) {
printf("%g\t",x[i*cols+j]);
}
printf("\n");
}
return nx;
}
ENDVERBATIM
:* mget(i,j,cols)
VERBATIM
static double mget(void* vv) {
int i, j, nx, rows, cols;
double *x;
nx = vector_instance_px(vv, &x);
i = (int)*getarg(1);
j = (int)*getarg(2);
cols = (int)*getarg(3);
if (i*cols+j >= nx) {
hoc_execerror("Indices out of bounds", 0);
}
return x[i*cols+j];
}
ENDVERBATIM
:* mrow(mat,i,cols)
VERBATIM
static double mrow(void* vv) {
int i, j, nx, ny, rows, cols;
double *x, *y;
nx = vector_instance_px(vv, &x);
ny = vector_arg_px(1, &y);
i = (int)*getarg(2);
cols = (int)*getarg(3);
if (cols!=nx || i>=ny/cols) {
hoc_execerror("Indices out of bounds", 0);
}
for (j=0;j<nx;j++) { x[j] = y[i*cols+j]; }
return nx;
}
ENDVERBATIM
:* mcol(mat,j,cols)
VERBATIM
static double mcol(void* vv) {
int i, j, nx, ny, rows, cols;
double *x, *y;
nx = vector_instance_px(vv, &x);
ny = vector_arg_px(1, &y);
j = (int)*getarg(2);
cols = (int)*getarg(3);
if (cols!=ny/nx || j>=cols) {
hoc_execerror("Indices out of bounds", 0);
}
for (i=0;i<nx;i++) { x[i] = y[i*cols+j]; }
return nx;
}
ENDVERBATIM
:* mset(i,j,cols,val)
VERBATIM
static double mset(void* vv) {
int i, j, nx, rows, cols;
double *x, val;
nx = vector_instance_px(vv, &x);
i = (int)*getarg(1);
j = (int)*getarg(2);
cols = (int)*getarg(3);
val = *getarg(4);
if (i*cols+j >= nx) {
hoc_execerror("Indices out of bounds", 0);
}
return (x[i*cols+j]=val);
}
ENDVERBATIM
:* PROCEDURE install_matrix()
PROCEDURE install_matrix() {
VERBATIM
/* the list of additional methods */
install_vector_method("outprod", outprod);
install_vector_method("mmult", mmult);
install_vector_method("spmult", spmult);
install_vector_method("spget", spget);
install_vector_method("mkspcp", mkspcp);
install_vector_method("chkspcp", chkspcp);
install_vector_method("spltp", spltp);
install_vector_method("transpose", transpose);
install_vector_method("mprintf", mprintf);
install_vector_method("mget", mget);
install_vector_method("mset", mset);
install_vector_method("mrow", mrow);
install_vector_method("mcol", mcol);
ENDVERBATIM
}