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