1 | /* \file MatlabNArrayToNArray.cpp
|
---|
2 | * \brief: convert a sparse or dense matlab n-dimensional array to cpp n-dimensional array
|
---|
3 | */
|
---|
4 |
|
---|
5 |
|
---|
6 | #ifdef HAVE_CONFIG_H
|
---|
7 | #include <config.h>
|
---|
8 | #else
|
---|
9 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | #include "../../shared/shared.h"
|
---|
13 | #include "../../include/include.h"
|
---|
14 |
|
---|
15 | #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
|
---|
16 | #include <mex.h>
|
---|
17 |
|
---|
18 | /*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
|
---|
19 | int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
20 |
|
---|
21 | int i,j,rows,cols;
|
---|
22 | int numel,ndims;
|
---|
23 | int *size,*dims;
|
---|
24 | double* mxmatrix_ptr=NULL;
|
---|
25 | const mwSize* ipt=NULL;
|
---|
26 |
|
---|
27 | /*output: */
|
---|
28 | double* matrix=NULL;
|
---|
29 |
|
---|
30 | /*matlab indices: */
|
---|
31 | mwIndex* ir=NULL;
|
---|
32 | mwIndex* jc=NULL;
|
---|
33 | double* pr=NULL;
|
---|
34 | int count;
|
---|
35 | int nnz;
|
---|
36 | int nz;
|
---|
37 |
|
---|
38 | /*get Matlab matrix information: */
|
---|
39 | numel=mxGetNumberOfElements(mxmatrix);
|
---|
40 | ndims=mxGetNumberOfDimensions(mxmatrix);
|
---|
41 | ipt =mxGetDimensions(mxmatrix);
|
---|
42 | size =(int *) xcalloc(ndims,sizeof(int));
|
---|
43 | for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
|
---|
44 |
|
---|
45 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
46 | if (mxIsSparse(mxmatrix)){
|
---|
47 |
|
---|
48 | /*Dealing with sparse matrix: recover size first: */
|
---|
49 | rows=mxGetM(mxmatrix);
|
---|
50 | cols=mxGetN(mxmatrix);
|
---|
51 | nnz=mxGetNzmax(mxmatrix);
|
---|
52 | nz=(int)((double)nnz/(double)rows);
|
---|
53 |
|
---|
54 | matrix=(double*)xcalloc(rows*cols,sizeof(double));
|
---|
55 |
|
---|
56 | /*Now, get ir,jc and pr: */
|
---|
57 | ir=mxGetIr(mxmatrix);
|
---|
58 | jc=mxGetJc(mxmatrix);
|
---|
59 | pr=mxGetPr(mxmatrix);
|
---|
60 |
|
---|
61 | /*Now, start inserting data into double* matrix: */
|
---|
62 | count=0;
|
---|
63 | for(i=0;i<cols;i++){
|
---|
64 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
65 | *(matrix+rows*ir[count]+i)=pr[count];
|
---|
66 | count++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 | else{
|
---|
72 |
|
---|
73 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
74 | mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
|
---|
75 |
|
---|
76 | /*Create serial matrix: */
|
---|
77 | matrix=(double*)xcalloc(numel,sizeof(double));
|
---|
78 |
|
---|
79 | dims=(int *) xcalloc(ndims,sizeof(int));
|
---|
80 | for(i=0;i<numel;i++){
|
---|
81 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
82 | j=IndexFromRowWiseDims(dims,size,ndims);
|
---|
83 | *(matrix+j)=*(mxmatrix_ptr+i);
|
---|
84 | }
|
---|
85 | xfree((void**)&dims);
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | /*Assign output pointer: */
|
---|
90 | *pmatrix=matrix;
|
---|
91 | *pmatrix_numel=numel;
|
---|
92 | *pmatrix_ndims=ndims;
|
---|
93 | *pmatrix_size=size;
|
---|
94 |
|
---|
95 | return 1;
|
---|
96 | }
|
---|
97 | /*}}}*/
|
---|
98 | /*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
|
---|
99 | int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
100 |
|
---|
101 | int i,j,rows,cols;
|
---|
102 | int numel,ndims;
|
---|
103 | int *size,*dims;
|
---|
104 | bool* mxmatrix_ptr=NULL;
|
---|
105 | const mwSize* ipt=NULL;
|
---|
106 |
|
---|
107 | /*output: */
|
---|
108 | bool* matrix=NULL;
|
---|
109 |
|
---|
110 | /*matlab indices: */
|
---|
111 | mwIndex* ir=NULL;
|
---|
112 | mwIndex* jc=NULL;
|
---|
113 | bool* pm=NULL;
|
---|
114 | int count;
|
---|
115 | int nnz;
|
---|
116 | int nz;
|
---|
117 |
|
---|
118 | /*get Matlab matrix information: */
|
---|
119 | numel=mxGetNumberOfElements(mxmatrix);
|
---|
120 | ndims=mxGetNumberOfDimensions(mxmatrix);
|
---|
121 | ipt =mxGetDimensions(mxmatrix);
|
---|
122 | size =(int *) xcalloc(ndims,sizeof(int));
|
---|
123 | for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
|
---|
124 |
|
---|
125 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
126 | if (mxIsSparse(mxmatrix)){
|
---|
127 |
|
---|
128 | /*Dealing with sparse matrix: recover size first: */
|
---|
129 | rows=mxGetM(mxmatrix);
|
---|
130 | cols=mxGetN(mxmatrix);
|
---|
131 | nnz=mxGetNzmax(mxmatrix);
|
---|
132 | nz=(int)((double)nnz/(double)rows);
|
---|
133 |
|
---|
134 | matrix=(bool*)xcalloc(rows*cols,sizeof(bool));
|
---|
135 |
|
---|
136 | /*Now, get ir,jc and pm: */
|
---|
137 | ir=mxGetIr(mxmatrix);
|
---|
138 | jc=mxGetJc(mxmatrix);
|
---|
139 | pm=(bool*)mxGetData(mxmatrix);
|
---|
140 |
|
---|
141 | /*Now, start inserting data into bool* matrix: */
|
---|
142 | count=0;
|
---|
143 | for(i=0;i<cols;i++){
|
---|
144 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
145 | *(matrix+rows*ir[count]+i)=pm[count];
|
---|
146 | count++;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 | else{
|
---|
152 |
|
---|
153 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
154 | mxmatrix_ptr=(bool*)mxGetData(mxmatrix);
|
---|
155 |
|
---|
156 | /*Create serial matrix: */
|
---|
157 | matrix=(bool*)xcalloc(numel,sizeof(bool));
|
---|
158 |
|
---|
159 | dims=(int *) xcalloc(ndims,sizeof(int));
|
---|
160 | for(i=0;i<numel;i++){
|
---|
161 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
162 | j=IndexFromRowWiseDims(dims,size,ndims);
|
---|
163 | *(matrix+j)=(bool)*(mxmatrix_ptr+i);
|
---|
164 | }
|
---|
165 | xfree((void**)&dims);
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*Assign output pointer: */
|
---|
170 | *pmatrix=matrix;
|
---|
171 | *pmatrix_numel=numel;
|
---|
172 | *pmatrix_ndims=ndims;
|
---|
173 | *pmatrix_size=size;
|
---|
174 |
|
---|
175 | return 1;
|
---|
176 | }
|
---|
177 | /*}}}*/
|
---|
178 | /*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{1*/
|
---|
179 | int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
180 |
|
---|
181 | int i,j,rows,cols;
|
---|
182 | int numel,ndims;
|
---|
183 | int *size,*dims;
|
---|
184 | mxChar* mxmatrix_ptr=NULL;
|
---|
185 | const mwSize* ipt=NULL;
|
---|
186 |
|
---|
187 | /*output: */
|
---|
188 | char* matrix=NULL;
|
---|
189 |
|
---|
190 | /*matlab indices: */
|
---|
191 | mwIndex* ir=NULL;
|
---|
192 | mwIndex* jc=NULL;
|
---|
193 | char* pm=NULL;
|
---|
194 | int count;
|
---|
195 | int nnz;
|
---|
196 | int nz;
|
---|
197 |
|
---|
198 | /*get Matlab matrix information: */
|
---|
199 | numel=mxGetNumberOfElements(mxmatrix);
|
---|
200 | ndims=mxGetNumberOfDimensions(mxmatrix);
|
---|
201 | ipt =mxGetDimensions(mxmatrix);
|
---|
202 | size =(int *) xcalloc(ndims,sizeof(int));
|
---|
203 | for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
|
---|
204 |
|
---|
205 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
206 | if (mxIsSparse(mxmatrix)){
|
---|
207 |
|
---|
208 | /*Dealing with sparse matrix: recover size first: */
|
---|
209 | rows=mxGetM(mxmatrix);
|
---|
210 | cols=mxGetN(mxmatrix);
|
---|
211 | nnz=mxGetNzmax(mxmatrix);
|
---|
212 | nz=(int)((double)nnz/(double)rows);
|
---|
213 |
|
---|
214 | matrix=(char*)xcalloc(rows*cols,sizeof(double));
|
---|
215 |
|
---|
216 | /*Now, get ir,jc and pm: */
|
---|
217 | ir=mxGetIr(mxmatrix);
|
---|
218 | jc=mxGetJc(mxmatrix);
|
---|
219 | pm=(char*)mxGetData(mxmatrix);
|
---|
220 |
|
---|
221 | /*Now, start inserting data into char* matrix: */
|
---|
222 | count=0;
|
---|
223 | for(i=0;i<cols;i++){
|
---|
224 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
225 | *(matrix+rows*ir[count]+i)=(char)pm[count];
|
---|
226 | count++;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | }
|
---|
231 | else{
|
---|
232 |
|
---|
233 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
234 | mxmatrix_ptr=mxGetChars(mxmatrix);
|
---|
235 |
|
---|
236 | /*Create serial matrix: */
|
---|
237 | matrix=(char*)xcalloc(numel+1,sizeof(mxChar));
|
---|
238 |
|
---|
239 | /*looping code adapted from Matlab example explore.c: */
|
---|
240 | int elements_per_page = size[0] * size[1];
|
---|
241 | /* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
|
---|
242 | int total_number_of_pages = 1;
|
---|
243 | for (i=2; i<ndims; i++) {
|
---|
244 | total_number_of_pages *= size[i];
|
---|
245 | }
|
---|
246 |
|
---|
247 | i=0;
|
---|
248 | for (int page=0; page < total_number_of_pages; page++) {
|
---|
249 | int row;
|
---|
250 | /* On each page, walk through each row. */
|
---|
251 | for (row=0; row<size[0]; row++) {
|
---|
252 | int column;
|
---|
253 | j = (page * elements_per_page) + row;
|
---|
254 |
|
---|
255 | /* Walk along each column in the current row. */
|
---|
256 | for (column=0; column<size[1]; column++) {
|
---|
257 | *(matrix+i++)=(char)*(mxmatrix_ptr+j);
|
---|
258 | j += size[0];
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*Assign output pointer: */
|
---|
266 | *pmatrix=matrix;
|
---|
267 | *pmatrix_numel=numel;
|
---|
268 | *pmatrix_ndims=ndims;
|
---|
269 | *pmatrix_size=size;
|
---|
270 |
|
---|
271 | return 1;
|
---|
272 | }
|
---|
273 | /*}}}*/
|
---|
274 | #endif
|
---|