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