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

Last change on this file since 12434 was 12434, checked in by Mathieu Morlighem, 13 years ago

changing xmalloc to xNew and xfree to xDelete

File size: 6.3 KB
Line 
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){{{*/
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 =xNew<int>(ndims);
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=xNewInit<double>(rows*cols,0.0);
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=xNewInit<double>(numel,0.0);
77
78 dims=xNew<int>(ndims);
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 xDelete<int>(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){{{*/
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 =xNew<int>(ndims);
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=xNewInit<bool>(rows*cols,false);
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=xNew<bool>(numel);
157 dims=xNew<int>(ndims);
158 for(i=0;i<numel;i++){
159 ColumnWiseDimsFromIndex(dims,i,size,ndims);
160 j=IndexFromRowWiseDims(dims,size,ndims);
161 *(matrix+j)=(bool)*(mxmatrix_ptr+i);
162 }
163 xDelete<int>(dims);
164 }
165
166 /*Assign output pointer: */
167 *pmatrix=matrix;
168 *pmatrix_numel=numel;
169 *pmatrix_ndims=ndims;
170 *pmatrix_size=size;
171
172 return 1;
173}
174/*}}}*/
175/*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
176int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
177
178 int i,j,rows,cols;
179 int numel,ndims;
180 int *size,*dims;
181 mxChar* mxmatrix_ptr=NULL;
182 const mwSize* ipt=NULL;
183
184 /*output: */
185 char* matrix=NULL;
186
187 /*matlab indices: */
188 mwIndex* ir=NULL;
189 mwIndex* jc=NULL;
190 char* pm=NULL;
191 int count;
192 int nnz;
193 int nz;
194
195 /*get Matlab matrix information: */
196 numel=mxGetNumberOfElements(mxmatrix);
197 ndims=mxGetNumberOfDimensions(mxmatrix);
198 ipt =mxGetDimensions(mxmatrix);
199 size =xNew<int>(ndims);
200 for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
201
202 /*Ok, first check if we are dealing with a sparse or full matrix: */
203 if (mxIsSparse(mxmatrix)){
204
205 /*Dealing with sparse matrix: recover size first: */
206 rows=mxGetM(mxmatrix);
207 cols=mxGetN(mxmatrix);
208 nnz=mxGetNzmax(mxmatrix);
209 nz=(int)((double)nnz/(double)rows);
210
211 matrix=xNew<char>(rows*cols);
212
213 /*Now, get ir,jc and pm: */
214 ir=mxGetIr(mxmatrix);
215 jc=mxGetJc(mxmatrix);
216 pm=(char*)mxGetData(mxmatrix);
217
218 /*Now, start inserting data into char* matrix: */
219 count=0;
220 for(i=0;i<cols;i++){
221 for(j=0;j<(jc[i+1]-jc[i]);j++){
222 *(matrix+rows*ir[count]+i)=(char)pm[count];
223 count++;
224 }
225 }
226
227 }
228 else{
229
230 /*Dealing with dense matrix: recover pointer and size: */
231 mxmatrix_ptr=mxGetChars(mxmatrix);
232
233 /*Create serial matrix: */
234 matrix=xNew<char>(numel+1);
235
236 /*looping code adapted from Matlab example explore.c: */
237 int elements_per_page = size[0] * size[1];
238 /* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
239 int total_number_of_pages = 1;
240 for (i=2; i<ndims; i++) {
241 total_number_of_pages *= size[i];
242 }
243
244 i=0;
245 for (int page=0; page < total_number_of_pages; page++) {
246 int row;
247 /* On each page, walk through each row. */
248 for (row=0; row<size[0]; row++) {
249 int column;
250 j = (page * elements_per_page) + row;
251
252 /* Walk along each column in the current row. */
253 for (column=0; column<size[1]; column++) {
254 *(matrix+i++)=(char)*(mxmatrix_ptr+j);
255 j += size[0];
256 }
257 }
258 }
259
260 }
261
262 /*Assign output pointer: */
263 *pmatrix=matrix;
264 *pmatrix_numel=numel;
265 *pmatrix_ndims=ndims;
266 *pmatrix_size=size;
267
268 return 1;
269}
270/*}}}*/
Note: See TracBrowser for help on using the repository browser.