source: issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp@ 12013

Last change on this file since 12013 was 12013, checked in by Eric.Larour, 13 years ago

Some debugging of compilation

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