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

Last change on this file since 13622 was 13622, checked in by Mathieu Morlighem, 12 years ago

CHG: cosmetics, removing all deboule blank lines and indent single white lines correctly

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