: \$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 } :* 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= 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=cols) { hoc_execerror("Indices out of bounds", 0); } for (i=0;i= 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 }