annotate examples/private/collincomb.c @ 5:f44689e95ea4

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