-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvecxspsubmat.c
62 lines (49 loc) · 1.92 KB
/
vecxspsubmat.c
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
/* Bare bones (full vector).'*sparse submatrix */
/* Produces a full vector result */
/* Assumes all input elements are finite */
/* E.g., does not account for NaN or 0*inf etc. */
/* Based on code by James Tursa, modified by Stefan Guettel */
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
ptrdiff_t nn, j, k, nrow;
double *Mpr, *Vpr, *Cpr; /* pointers to input & output matrices*/
int col_s, col_e; /* start & end indices of matrix columns to use */
mwIndex *Mir, *Mjc;
/*
m = mxGetM(prhs[1]);
n = mxGetN(prhs[1]);
*/
col_s = (int) mxGetScalar(prhs[2]);
col_e = (int) mxGetScalar(prhs[3]);
nn = (ptrdiff_t) (col_e - col_s + 1);
/* Create output */
plhs[0] = mxCreateDoubleMatrix( 1, (ptrdiff_t) nn, mxREAL );
/* Get data pointers */
Vpr = mxGetPr(prhs[0]); /* vector */
Mpr = mxGetPr(prhs[1]); /* sparse matrix */
Mir = mxGetIr(prhs[1]);
Mjc = mxGetJc(prhs[1]);
Cpr = mxGetPr(plhs[0]); /* output vector */
/* Calculate result */
for( j=0; j<col_e; j++ ) {
nrow = Mjc[j+1] - Mjc[j]; /* Number of elements for this column */
/* Cpr[j] = 0; */ /* mxCreateDoubleMatrix takes care of init */
if( j+1<col_s ) {
/* skip this column */
/*
while( nrow-- ) {
*Mpr++;
*Mir++;
}
*/
Mpr += nrow; /* skip matrix element */
Mir += nrow; /* skip row-index position for this element */
continue;
}
k = j + 1 - col_s;
while( nrow-- ) {
Cpr[k] += *Mpr++ * Vpr[*Mir++]; /* Accumulate contribution of j-th column */
}
}
}