1 /*================================================================
3 * This routine computes a sparse matrix times a diagonal matrix
4 * whose diagonal entries are stored in a full vector.
7 * spmtimesd(m,d,[]) = diag(d) * m,
8 * spmtimesd(m,[],d) = m * diag(d)
9 * spmtimesd(m,d1,d2) = diag(d1) * m * diag(d2)
10 * m could be complex, but d is assumed real
12 * Stella X. Yu's first MEX function, Nov 9, 2001.
21 tic; b=spmtimesd(a,d1,d2); toc
22 tic; bb = spdiags(d1,0,m,m) * a * spdiags(d2,0,n,n); toc
26 *=================================================================*/
37 /* declare variables */
38 int i, j, k, m, n, nzmax, xm, yn;
39 mwIndex *pir, *pjc, *qir, *qjc;
40 double *x, *y, *pr, *pi, *qr, *qi;
44 mexErrMsgTxt("Three input arguments required");
47 mexErrMsgTxt("Too many output arguments.");
49 if (!(mxIsSparse(in[0]))) {
50 mexErrMsgTxt("Input argument #1 must be of type sparse");
52 if ( mxIsSparse(in[1]) || mxIsSparse(in[2]) ) {
53 mexErrMsgTxt("Input argument #2 & #3 must be of type full");
56 /* computation starts */
72 if ( xm>0 && xm != m) {
73 mexErrMsgTxt("Row multiplication dimension mismatch.");
75 if ( yn>0 && yn != n) {
76 mexErrMsgTxt("Column multiplication dimension mismatch.");
80 nzmax = mxGetNzmax(in[0]);
81 mxComplexity cmplx = (pi==NULL ? mxREAL : mxCOMPLEX);
82 out[0] = mxCreateSparse(m,n,nzmax,cmplx);
84 mexErrMsgTxt("Not enough space for the output matrix.");
89 qir = mxGetIr(out[0]);
90 qjc = mxGetJc(out[0]);
92 /* left multiplication */
97 for (k=pjc[j]; k<pjc[j+1]; k++) {
100 qr[k] = x[i] * pr[k];
102 qi[k] = x[i] * pi[k];
110 /* right multiplication */
113 for (j=0; j<n; j++) {
115 for (k=pjc[j]; k<pjc[j+1]; k++) {
117 qr[k] = pr[k] * y[j];
119 qi[k] = qi[k] * y[j];
128 for (j=0; j<n; j++) {
130 for (k=pjc[j]; k<pjc[j+1]; k++) {
133 qr[k] = x[i] * pr[k] * y[j];
135 qi[k] = x[i] * qi[k] * y[j];