GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — CompEconRecompiled/dprod.c at master · LRondina/CompEconRecompiled Expand file tree/ Copy pathdprod.cMore file actions 189 lines (182 loc) · 5.64 KB / Copy pathdprod.cFile metadata and controls189 lines (182 loc) · 5.64 KB 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 #include "mex.h" #include <math.h> #ifdef old32 #define mwSize int #define mwIndex int #endif /* C code to compute direct products (row-wise tensor products) For real full or sparse matrices only */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *a, *b, *c; mwIndex i, j, k; mwSize m, an, bn; if (nrhs!=2){mexErrMsgTxt("Two parameters must be passed");} if (!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) mexErrMsgTxt("Inputs must be full or sparse double matrices"); if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1])) mexErrMsgTxt("Complex matrices not supported in dprod"); if (mxIsEmpty(prhs[0])) plhs[0]=mxDuplicateArray(prhs[1]); else if (mxIsEmpty(prhs[1])) plhs[0]=mxDuplicateArray(prhs[0]); else { m=mxGetM(prhs[0]); if (mxGetM(prhs[1])!=m) {mexErrMsgTxt("Matrices must have the same number of rows");} /* SPARSE/SPARSE -> SPARSE */ if (mxIsSparse(prhs[0]) && mxIsSparse(prhs[1])) { mwSize *ai, *aj, *bi, *bj, *ci, *cj; mwIndex ak, anj, bk, bjend, ck; mwSize *acounts, *bcounts; a=mxGetPr(prhs[0]); ai=mxGetIr(prhs[0]); aj=mxGetJc(prhs[0]); an=mxGetN(prhs[0]); b=mxGetPr(prhs[1]); bi=mxGetIr(prhs[1]); bj=mxGetJc(prhs[1]); bn=mxGetN(prhs[1]); /* determine # of non-zeros */ acounts=mxCalloc(2*m,sizeof(mwSize)); bcounts=acounts+m; for (k=*(aj+an), i=0; i<k; i++) acounts[ai[i]]++; for (k=*(bj+bn), i=0; i<k; i++) bcounts[bi[i]]++; for (k=0,i=0; i<m; i++) k+=acounts[i]*bcounts[i]; mxFree(acounts); /* create output array */ plhs[0]=mxCreateSparse(m,an*bn,k,mxREAL); ci=mxGetIr(plhs[0]); cj=mxGetJc(plhs[0]); c=mxGetPr(plhs[0]); /* main loop order: columns of a, columns of b, rows */ *cj=0; ck=0; for (i=0; i<an; i++,a+=anj,ai+=anj){ anj=aj[i+1]-aj[i]; bjend=0; for (j=1; j<=bn; j++, *(++cj)=ck){ bk=bjend; bjend=bj[j]; for (ak=0; ak<anj && bk<bjend;){ if (ai[ak]==bi[bk]){ *c++=a[ak]*b[bk++]; *ci++=ai[ak++]; ck++; } else if (ai[ak]<bi[bk]) ak++; else bk++; } } } } /* DOUBLE/SPARSE -> SPARSE */ else if (mxIsDouble(prhs[0]) && mxIsSparse(prhs[1])) { mwIndex *bi, *bj, *ci, *cj; mwSize ak, bk, ck, bjj; mwSize *acounts, *bcounts; a=mxGetPr(prhs[0]); an=mxGetN(prhs[0]); b=mxGetPr(prhs[1]); bi=mxGetIr(prhs[1]); bj=mxGetJc(prhs[1]); bn=mxGetN(prhs[1]); /* determine # of non-zeros */ acounts=mxCalloc(2*m,sizeof(mwSize)); bcounts=acounts+m; for (j=0; j<an; j++) for (i=0; i<m; i++) if (*a++!=0) acounts[i]++; a=a-m*an; for (k=*(bj+bn), i=0; i<k; i++) bcounts[bi[i]]++; for (k=0,i=0; i<m; i++) k+=acounts[i]*bcounts[i]; mxFree(acounts); /* create output array */ plhs[0]=mxCreateSparse(m,an*bn,k,mxREAL); ci=mxGetIr(plhs[0]); cj=mxGetJc(plhs[0]); c=mxGetPr(plhs[0]); /* main loop order: columns of a, columns of b, rows */ *cj++=0; ck=0; for (i=0; i<an; i++,a+=m){ bk=0; for (j=1; j<=bn; j++){ bjj=bj[j]; for (;bk<bjj;bk++){ ak=bi[bk]; if (a[ak]!=0){ *c++ = a[ak]*b[bk]; *ci++ = ak; ck++; } } *cj++=ck; } } } /* SPARSE/DOUBLE -> SPARSE */ else if (mxIsSparse(prhs[0]) && mxIsDouble(prhs[1])) { mwIndex *ai, *aj, *ci, *cj; double *bj; mwIndex ak, bk, ck, akk; mwSize *acounts, *bcounts; a=mxGetPr(prhs[0]); ai=mxGetIr(prhs[0]); aj=mxGetJc(prhs[0]); an=mxGetN(prhs[0]); b=mxGetPr(prhs[1]); bn=mxGetN(prhs[1]); /* determine # of non-zeros */ acounts=mxCalloc(2*m,sizeof(mwSize)); bcounts=acounts+m; for (k=*(aj+an), i=0; i<k; i++) acounts[ai[i]]++; for (j=0; j<bn; j++) for (i=0; i<m; i++) if (*b++!=0) bcounts[i]++; b=b-m*bn; for (k=0,i=0; i<m; i++) k+=acounts[i]*bcounts[i]; mxFree(acounts); /* create output array */ plhs[0]=mxCreateSparse(m,an*bn,k,mxREAL); ci=mxGetIr(plhs[0]); cj=mxGetJc(plhs[0]); c=mxGetPr(plhs[0]); /* main loop order: columns of a, columns of b, rows */ *cj++=0; ck=0; for (i=0; i<an; i++, aj++){ bj=b; for (j=1; j<=bn; j++, bj+=m){ for (ak=*aj,akk=*(aj+1);ak<akk;ak++){ bk=ai[ak]; if (bj[bk]!=0){ *c++ = a[ak]*bj[bk]; *ci++ = bk; ck++; } } *cj++=ck; } } } /* DOUBLE/DOUBLE -> DOUBLE */ else if (mxIsDouble(prhs[0]) && mxIsDouble(prhs[1])){ mwSize mbn; double *aend; an=mxGetN(prhs[0]); bn=mxGetN(prhs[1]); mbn=bn*m; a=mxGetPr(prhs[0]); b=mxGetPr(prhs[1]); plhs[0]=mxCreateDoubleMatrix(m,an*bn,mxREAL); c=mxGetPr(plhs[0]); for (i = 0; i < an; i++, b-=mbn, a=aend) for (aend=a+m, j=0; j<bn; j++, a-=m) while (a<aend) *c++ = *a++ * *b++; } else mexErrMsgTxt("Unsupported data type passed to dprod"); } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |