annotate Problems/private/rowlincomb.c @ 40:6416fc12f2b8

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:35:24 +0000
parents 207a6ae9a76f
children
rev   line source
idamnjanovic@10 1 /**************************************************************************
idamnjanovic@10 2 *
idamnjanovic@10 3 * File name: rowlincomb.c
idamnjanovic@10 4 *
idamnjanovic@10 5 * Ron Rubinstein
idamnjanovic@10 6 * Computer Science Department
idamnjanovic@10 7 * Technion, Haifa 32000 Israel
idamnjanovic@10 8 * ronrubin@cs
idamnjanovic@10 9 *
idamnjanovic@10 10 * Last Updated: 21.5.2009
idamnjanovic@10 11 *
idamnjanovic@10 12 *************************************************************************/
idamnjanovic@10 13
idamnjanovic@10 14 #include "mex.h"
idamnjanovic@10 15
idamnjanovic@10 16
idamnjanovic@10 17 /* Input Arguments */
idamnjanovic@10 18
idamnjanovic@10 19 #define X_IN prhs[0]
idamnjanovic@10 20 #define A_IN prhs[1]
idamnjanovic@10 21 #define ROWS_IN prhs[2]
idamnjanovic@10 22 #define COLS_IN prhs[3]
idamnjanovic@10 23
idamnjanovic@10 24
idamnjanovic@10 25 /* Output Arguments */
idamnjanovic@10 26
idamnjanovic@10 27 #define Y_OUT plhs[0]
idamnjanovic@10 28
idamnjanovic@10 29
idamnjanovic@10 30 void mexFunction(int nlhs, mxArray *plhs[],
idamnjanovic@10 31 int nrhs, const mxArray*prhs[])
idamnjanovic@10 32
idamnjanovic@10 33 {
idamnjanovic@10 34 double *A, *x, *y, *rows, *cols;
idamnjanovic@10 35 mwSize m,n,m1,n1,m2,n2,rownum,colnum;
idamnjanovic@10 36 mwIndex *row_ids,*col_ids,i,j;
idamnjanovic@10 37 int colnumspecified=0;
idamnjanovic@10 38
idamnjanovic@10 39
idamnjanovic@10 40 /* Check for proper number of arguments */
idamnjanovic@10 41
idamnjanovic@10 42 if (nrhs!=3 && nrhs!=4) {
idamnjanovic@10 43 mexErrMsgTxt("Invalid number of input arguments.");
idamnjanovic@10 44 } else if (nlhs > 1) {
idamnjanovic@10 45 mexErrMsgTxt("Too many output arguments.");
idamnjanovic@10 46 }
idamnjanovic@10 47
idamnjanovic@10 48
idamnjanovic@10 49 /* Check the input dimensions */
idamnjanovic@10 50
idamnjanovic@10 51 m = mxGetM(A_IN);
idamnjanovic@10 52 n = mxGetN(A_IN);
idamnjanovic@10 53 if (!mxIsDouble(A_IN) || mxIsComplex(A_IN) || mxGetNumberOfDimensions(A_IN)>2) {
idamnjanovic@10 54 mexErrMsgTxt("ROWLINCOMB requires that A be a double matrix.");
idamnjanovic@10 55 }
idamnjanovic@10 56
idamnjanovic@10 57 m1 = mxGetM(ROWS_IN);
idamnjanovic@10 58 n1 = mxGetN(ROWS_IN);
idamnjanovic@10 59 if (!mxIsDouble(ROWS_IN) || mxIsComplex(ROWS_IN) || (m1!=1 && n1!=1)) {
idamnjanovic@10 60 mexErrMsgTxt("ROWLINCOMB requires that ROWS be an index vector of type double.");
idamnjanovic@10 61 }
idamnjanovic@10 62 rownum = (m1 > n1) ? m1 : n1; /* the number of rows in the linear combination */
idamnjanovic@10 63
idamnjanovic@10 64 m2 = mxGetM(X_IN);
idamnjanovic@10 65 n2 = mxGetN(X_IN);
idamnjanovic@10 66 if (!mxIsDouble(X_IN) || mxIsComplex(X_IN) || ((m2!=1) && (n2!=1))) {
idamnjanovic@10 67 mexErrMsgTxt("ROWLINCOMB requires that X be a double vector.");
idamnjanovic@10 68 }
idamnjanovic@10 69
idamnjanovic@10 70 if (m2 != rownum && n2 != rownum) {
idamnjanovic@10 71 mexErrMsgTxt("The length of X does not match the number of rows in ROWS.");
idamnjanovic@10 72 }
idamnjanovic@10 73
idamnjanovic@10 74 if (nrhs==4) {
idamnjanovic@10 75 m1 = mxGetM(COLS_IN);
idamnjanovic@10 76 n1 = mxGetN(COLS_IN);
idamnjanovic@10 77 if (!mxIsDouble(COLS_IN) || mxIsComplex(COLS_IN) || (m1!=1 && n1!=1)) {
idamnjanovic@10 78 mexErrMsgTxt("ROWLINCOMB requires that COLS be an index vector of type double.");
idamnjanovic@10 79 }
idamnjanovic@10 80 colnum = (m1 > n1) ? m1 : n1; /* the number of columns */
idamnjanovic@10 81 colnumspecified = 1;
idamnjanovic@10 82 cols = mxGetPr(COLS_IN);
idamnjanovic@10 83
idamnjanovic@10 84 Y_OUT = mxCreateDoubleMatrix(1, colnum, mxREAL);
idamnjanovic@10 85 }
idamnjanovic@10 86 else {
idamnjanovic@10 87 cols = 0;
idamnjanovic@10 88 Y_OUT = mxCreateDoubleMatrix(1, n, mxREAL);
idamnjanovic@10 89 }
idamnjanovic@10 90
idamnjanovic@10 91
idamnjanovic@10 92 /* Assign pointers to the various parameters */
idamnjanovic@10 93 A = mxGetPr(A_IN);
idamnjanovic@10 94 rows = mxGetPr(ROWS_IN);
idamnjanovic@10 95 x = mxGetPr(X_IN);
idamnjanovic@10 96 y = mxGetPr(Y_OUT);
idamnjanovic@10 97
idamnjanovic@10 98
idamnjanovic@10 99 /* check row indices */
idamnjanovic@10 100
idamnjanovic@10 101 row_ids = (mwIndex*)mxMalloc(rownum*sizeof(mwIndex));
idamnjanovic@10 102
idamnjanovic@10 103 for (i=0; i<rownum; ++i) {
idamnjanovic@10 104 row_ids[i] = (mwIndex)(rows[i]+0.1)-1;
idamnjanovic@10 105 if (row_ids[i]<0 || row_ids[i]>=m) {
idamnjanovic@10 106 mexErrMsgTxt("Row index in ROWS is out of range.");
idamnjanovic@10 107 }
idamnjanovic@10 108 }
idamnjanovic@10 109
idamnjanovic@10 110
idamnjanovic@10 111
idamnjanovic@10 112 if (colnumspecified) {
idamnjanovic@10 113
idamnjanovic@10 114 /* check column indices */
idamnjanovic@10 115 col_ids = (mwIndex*)mxMalloc(colnum*sizeof(mwIndex));
idamnjanovic@10 116
idamnjanovic@10 117 for (i=0; i<colnum; ++i) {
idamnjanovic@10 118 col_ids[i] = (mwIndex)(cols[i]+0.1)-1;
idamnjanovic@10 119 if (col_ids[i]<0 || col_ids[i]>=n) {
idamnjanovic@10 120 mexErrMsgTxt("Column index in COLS is out of range.");
idamnjanovic@10 121 }
idamnjanovic@10 122 }
idamnjanovic@10 123
idamnjanovic@10 124 /* Do the actual computation */
idamnjanovic@10 125 for (j=0; j<colnum; ++j) {
idamnjanovic@10 126 for (i=0; i<rownum; ++i) {
idamnjanovic@10 127 y[j] += A[m*col_ids[j]+row_ids[i]]*x[i];
idamnjanovic@10 128 }
idamnjanovic@10 129 }
idamnjanovic@10 130
idamnjanovic@10 131 mxFree(col_ids);
idamnjanovic@10 132 }
idamnjanovic@10 133
idamnjanovic@10 134 else {
idamnjanovic@10 135
idamnjanovic@10 136 /* Do the actual computation */
idamnjanovic@10 137 for (j=0; j<n; ++j) {
idamnjanovic@10 138 for (i=0; i<rownum; ++i) {
idamnjanovic@10 139 y[j] += A[m*j+row_ids[i]]*x[i];
idamnjanovic@10 140 }
idamnjanovic@10 141 }
idamnjanovic@10 142 }
idamnjanovic@10 143
idamnjanovic@10 144
idamnjanovic@10 145 mxFree(row_ids);
idamnjanovic@10 146
idamnjanovic@10 147 return;
idamnjanovic@10 148 }