IT++ Logo
itmex.h
Go to the documentation of this file.
1 
29 #ifndef ITMEX_H
30 #define ITMEX_H
31 
32 #include <itpp/itbase.h>
33 #include <mex.h>
34 
35 
36 namespace itpp
37 {
38 
39 //--------------------------------------------------------
40 // mex -> it++
41 //--------------------------------------------------------
42 
88 // --------------------------------------------------------
89 // mex -> IT++
90 // --------------------------------------------------------
91 
93 bin mxArray2bin(const mxArray *in);
95 short mxArray2short(const mxArray *in);
97 int mxArray2int(const mxArray *in);
99 double mxArray2double(const mxArray *in);
101 std::complex<double> mxArray2double_complex(const mxArray *in);
103 std::string mxArray2string(const mxArray *in);
104 
106 bvec mxArray2bvec(const mxArray *in);
108 svec mxArray2svec(const mxArray *in);
110 ivec mxArray2ivec(const mxArray *in);
112 vec mxArray2vec(const mxArray *in);
114 cvec mxArray2cvec(const mxArray *in);
115 
117 bmat mxArray2bmat(const mxArray *in);
119 smat mxArray2smat(const mxArray *in);
121 imat mxArray2imat(const mxArray *in);
123 mat mxArray2mat(const mxArray *in);
125 cmat mxArray2cmat(const mxArray *in);
126 
127 // --------------------------------------------------------
128 // IT++ -> mex
129 // --------------------------------------------------------
130 
132 void bin2mxArray(const bin &in, mxArray *out);
134 void short2mxArray(const short &in, mxArray *out);
136 void int2mxArray(const int &in, mxArray *out);
138 void double2mxArray(const double &in, mxArray *out);
140 void double_complex2mxArray(const std::complex<double> &in, mxArray *out);
142 void string2mxArray(const std::string &in, mxArray* &out);
143 
145 void bvec2mxArray(const bvec &in, mxArray *out);
147 void svec2mxArray(const svec &in, mxArray *out);
149 void ivec2mxArray(const ivec &in, mxArray *out);
151 void vec2mxArray(const vec &in, mxArray *out);
153 void cvec2mxArray(const cvec &in, mxArray *out);
154 
156 void bmat2mxArray(const bmat &in, mxArray *out);
158 void smat2mxArray(const smat &in, mxArray *out);
160 void imat2mxArray(const imat &in, mxArray *out);
162 void mat2mxArray(const mat &in, mxArray *out);
164 void cmat2mxArray(const cmat &in, mxArray *out);
165 
166 // --------------------------------------------------------
167 // mex -> C
168 // --------------------------------------------------------
169 
171 void mxArray2Csvec(const mxArray *in, short *out);
173 void mxArray2Civec(const mxArray *in, int *out);
175 void mxArray2Cvec(const mxArray *in, double *out);
177 void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag);
178 
180 void mxArray2Csmat(const mxArray *in, short **out);
182 void mxArray2Cimat(const mxArray *in, int **out);
184 void mxArray2Cmat(const mxArray *in, double **out);
186 void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag);
187 
188 // --------------------------------------------------------
189 // C -> mex
190 // --------------------------------------------------------
191 
193 void Csvec2mxArray(short *in, mxArray *out);
195 void Civec2mxArray(int *in, mxArray *out);
197 void Cvec2mxArray(double *in, mxArray *out);
199 void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out);
200 
202 void Csmat2mxArray(short **in, mxArray *out);
204 void Cimat2mxArray(int **in, mxArray *out);
206 void Cmat2mxArray(double **in, mxArray *out);
208 void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out);
209 
215 bin mxArray2bin(const mxArray *in)
216 {
217  int size;
218  double* temp = (double*) mxGetPr(in);
219  if (temp == 0) mexErrMsgTxt("mxArray2bin: Pointer to data is NULL");
220  size = mxGetNumberOfElements(in);
221  if (size != 1) mexErrMsgTxt("mxArray2bin: Size of data is not equal to one");
222 
223  return (((*temp) > 0.0) ? bin(1) : bin(0));
224 }
225 
226 short mxArray2short(const mxArray *in)
227 {
228  int size;
229  double* temp = (double*) mxGetPr(in);
230  if (temp == 0) mexErrMsgTxt("mxArray2short: Pointer to data is NULL");
231  size = mxGetNumberOfElements(in);
232  if (size != 1) mexErrMsgTxt("mxArray2short: Size of data is not equal to one");
233 
234  return (short)(*temp);
235 }
236 
237 int mxArray2int(const mxArray *in)
238 {
239  int size;
240  double* temp = (double*) mxGetPr(in);
241  if (temp == 0) mexErrMsgTxt("mxArray2int: Pointer to data is NULL");
242  size = mxGetNumberOfElements(in);
243  if (size != 1) mexErrMsgTxt("mxArray2int: Size of data is not equal to one");
244 
245  return (int)(*temp);
246 }
247 
248 double mxArray2double(const mxArray *in)
249 {
250  int size;
251  double* temp = (double*) mxGetPr(in);
252  if (temp == 0) mexErrMsgTxt("mxArray2double: Pointer to data is NULL");
253  size = mxGetNumberOfElements(in);
254  if (size != 1) mexErrMsgTxt("mxArray2double: Size of data is not equal to one");
255 
256  return (*temp);
257 }
258 
259 std::complex<double> mxArray2double_complex(const mxArray *in)
260 {
261  int size;
262  double* tempR = (double*) mxGetPr(in);
263  double* tempI = (double*) mxGetPi(in);
264 
265  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2double_complex: Pointer to data is NULL");
266 
267  size = mxGetNumberOfElements(in);
268  if (size != 1) mexErrMsgTxt("mxArray2double_complex: Size of data is not equal to one");
269 
270  if (tempR == 0) {
271  return std::complex<double>(0.0 , (*tempI));
272  }
273  else if (tempI == 0) {
274  return std::complex<double>((*tempR), 0.0);
275  }
276  else {
277  return std::complex<double>((*tempR), (*tempI));
278  }
279 
280 }
281 
282 std::string mxArray2string(const mxArray *in)
283 {
284  if (in == 0)
285  mexErrMsgTxt("mxArray2string: Pointer to data is NULL");
286  std::string str = mxArrayToString(in);
287  if (str.data() == 0)
288  mexErrMsgTxt("mxArray2string: Could not convert mxArray to string");
289  return str;
290 }
291 
292 bvec mxArray2bvec(const mxArray *in)
293 {
294  bvec out;
295  int i, size;
296  double* temp = (double*) mxGetPr(in);
297  if (temp == 0) mexErrMsgTxt("mxArray2bvec: Pointer to data is NULL");
298 
299  size = mxGetNumberOfElements(in);
300  if (size == 0) mexErrMsgTxt("mxArray2bvec: Size of data is zero");
301 
302  out.set_size(size, false);
303 
304  for (i = 0; i < size; i++) {
305  out(i) = (((*temp++) > 1e-5) ? bin(1) : bin(0));
306  }
307 
308  return out;
309 
310 }
311 
312 svec mxArray2svec(const mxArray *in)
313 {
314  svec out;
315  int i, size;
316  double* temp = (double*) mxGetPr(in);
317  if (temp == 0) mexErrMsgTxt("mxArray2svec: Pointer to data is NULL");
318 
319  size = mxGetNumberOfElements(in);
320  if (size == 0) mexErrMsgTxt("mxArray2svec: Size of data is zero");
321 
322  out.set_size(size, false);
323 
324  for (i = 0; i < size; i++) {
325  out(i) = (short)(*temp++);
326  }
327 
328  return out;
329 
330 }
331 
332 ivec mxArray2ivec(const mxArray *in)
333 {
334  ivec out;
335  int i, size;
336  double* temp = (double*) mxGetPr(in);
337  if (temp == 0) mexErrMsgTxt("mxArray2ivec: Pointer to data is NULL");
338 
339  size = mxGetNumberOfElements(in);
340  if (size == 0) mexErrMsgTxt("mxArray2ivec: Size of data is zero");
341 
342  out.set_size(size, false);
343 
344  for (i = 0; i < size; i++) {
345  out(i) = (int)(*temp++);
346  }
347 
348  return out;
349 
350 }
351 
352 vec mxArray2vec(const mxArray *in)
353 {
354  vec out;
355  int i, size;
356  double* temp = (double*) mxGetPr(in);
357  if (temp == 0) mexErrMsgTxt("mxArray2vec: Pointer to data is NULL");
358 
359  size = mxGetNumberOfElements(in);
360  if (size == 0) mexErrMsgTxt("mxArray2vec: Size of data is zero");
361 
362  out.set_size(size, false);
363 
364  for (i = 0; i < size; i++) {
365  out(i) = (*temp++);
366  }
367 
368  return out;
369 
370 }
371 
372 cvec mxArray2cvec(const mxArray *in)
373 {
374  cvec out;
375  int i, size;
376  double* tempR = (double*) mxGetPr(in);
377  double* tempI = (double*) mxGetPi(in);
378 
379  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cvec: Pointer data is NULL");
380 
381  size = mxGetNumberOfElements(in);
382  if (size == 0) mexErrMsgTxt("mxArray2cvec: Size of data is zero");
383 
384  out.set_size(size, false);
385 
386  if (tempR == 0) {
387  for (i = 0; i < size; i++) { out(i) = std::complex<double>(0.0, (*tempI++)); }
388  }
389  else if (tempI == 0) {
390  for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), 0.0); }
391  }
392  else {
393  for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), (*tempI++)); }
394  }
395 
396  return out;
397 
398 }
399 
400 bmat mxArray2bmat(const mxArray *in)
401 {
402  bmat out;
403  int r, c, rows, cols;
404  double* temp = (double*) mxGetPr(in);
405  if (temp == 0) mexErrMsgTxt("mxArray2bmat: Pointer to data is NULL");
406 
407  rows = mxGetM(in);
408  if (rows == 0) mexErrMsgTxt("mxArray2bmat: Data has zero rows");
409  cols = mxGetN(in);
410  if (cols == 0) mexErrMsgTxt("mxArray2bmat: Data has zero columns");
411 
412  out.set_size(rows, cols, false);
413 
414  for (c = 0; c < cols; c++) {
415  for (r = 0; r < rows; r++) {
416  out(r, c) = (((*temp++) > 0.0) ? bin(1) : bin(0));
417  }
418  }
419 
420  return out;
421 
422 }
423 
424 smat mxArray2smat(const mxArray *in)
425 {
426  smat out;
427  int r, c, rows, cols;
428  double* temp = (double*) mxGetPr(in);
429  if (temp == 0) mexErrMsgTxt("mxArray2smat: Pointer to data is NULL");
430 
431  rows = mxGetM(in);
432  if (rows == 0) mexErrMsgTxt("mxArray2smat: Data has zero rows");
433  cols = mxGetN(in);
434  if (cols == 0) mexErrMsgTxt("mxArray2smat: Data has zero columns");
435 
436  out.set_size(rows, cols, false);
437 
438  for (c = 0; c < cols; c++) {
439  for (r = 0; r < rows; r++) {
440  out(r, c) = (short)(*temp++);
441  }
442  }
443 
444  return out;
445 
446 }
447 
448 imat mxArray2imat(const mxArray *in)
449 {
450  imat out;
451  int r, c, rows, cols;
452  double* temp = (double*) mxGetPr(in);
453  if (temp == 0) mexErrMsgTxt("mxArray2imat: Pointer to data is NULL");
454 
455  rows = mxGetM(in);
456  if (rows == 0) mexErrMsgTxt("mxArray2imat: Data has zero rows");
457  cols = mxGetN(in);
458  if (cols == 0) mexErrMsgTxt("mxArray2imat: Data has zero columns");
459  out.set_size(rows, cols, false);
460 
461  for (c = 0; c < cols; c++) {
462  for (r = 0; r < rows; r++) {
463  out(r, c) = (int)(*temp++);
464  }
465  }
466 
467  return out;
468 
469 }
470 
471 mat mxArray2mat(const mxArray *in)
472 {
473  mat out;
474  int r, c, rows, cols;
475  double* temp = (double*) mxGetPr(in);
476  if (temp == 0) mexErrMsgTxt("mxArray2mat: Pointer to data is NULL");
477 
478  rows = mxGetM(in);
479  if (rows == 0) mexErrMsgTxt("mxArray2mat: Data has zero rows");
480  cols = mxGetN(in);
481  if (cols == 0) mexErrMsgTxt("mxArray2mat: Data has zero columns");
482  out.set_size(rows, cols, false);
483 
484  for (c = 0; c < cols; c++) {
485  for (r = 0; r < rows; r++) {
486  out(r, c) = (*temp++);
487  }
488  }
489 
490  return out;
491 
492 }
493 
494 cmat mxArray2cmat(const mxArray *in)
495 {
496  cmat out;
497  int r, c, rows, cols;
498  double* tempR = (double*) mxGetPr(in);
499  double* tempI = (double*) mxGetPi(in);
500 
501  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cmat: Pointer to data is NULL");
502 
503  rows = mxGetM(in);
504  if (rows == 0) mexErrMsgTxt("mxArray2cmat: Data has zero rows");
505  cols = mxGetN(in);
506  if (cols == 0) mexErrMsgTxt("mxArray2cmat: Data has zero columns");
507  out.set_size(rows, cols, false);
508 
509  if (tempR == 0) {
510  for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>(0.0 , (*tempI++)); } }
511  }
512  else if (tempI == 0) {
513  for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), 0.0); } }
514  }
515  else {
516  for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), (*tempI++)); } }
517  }
518 
519  return out;
520 
521 }
522 
523 void double2mxArray(const double &in, mxArray *out)
524 {
525  double* temp = (double *) mxGetPr(out);
526  if (temp == 0) mexErrMsgTxt("double2mxArray: Pointer to data is NULL");
527 
528  *temp = (double) in;
529 }
530 
531 void double_complex2mxArray(const std::complex<double> &in, mxArray *out)
532 {
533  double* tempR = (double *) mxGetPr(out);
534  double* tempI = (double *) mxGetPi(out);
535  if (tempR == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to real valued part is NULL");
536  if (tempI == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to imaginary valued part is NULL");
537 
538  *tempR = (double) in.real();
539  *tempI = (double) in.imag();
540 }
541 
542 void string2mxArray(const std::string &in, mxArray* &out)
543 {
544  if (in.data() == 0)
545  mexErrMsgTxt("string2mxArray: Pointer to string is NULL");
546  out = mxCreateString(in.data());
547  if (out == 0)
548  mexErrMsgTxt("string2mxArray: Could not convert string to mxArray");
549 }
550 
551 void bvec2mxArray(const bvec &in, mxArray *out)
552 {
553  double* temp = (double *) mxGetPr(out);
554  if (temp == 0) mexErrMsgTxt("bvec2mxArray: Pointer to data is NULL");
555  if (in.size() == 0) mexErrMsgTxt("bvec2mxArray: Size of data is zero");
556  for (int i = 0; i < in.size(); i++) {
557  if (in(i))
558  *temp++ = 1.0;
559  else
560  *temp++ = 0.0;
561  }
562 }
563 
564 void ivec2mxArray(const ivec &in, mxArray *out)
565 {
566  double* temp = (double *) mxGetPr(out);
567  if (temp == 0) mexErrMsgTxt("ivec2mxArray: Pointer to data is NULL");
568  if (in.size() == 0) mexErrMsgTxt("ivec2mxArray: Size of data is zero");
569 
570  for (int i = 0; i < in.size(); i++) {
571  *temp++ = (double) in(i);
572  }
573 }
574 
575 void vec2mxArray(const vec &in, mxArray *out)
576 {
577  double* temp = (double *) mxGetPr(out);
578  if (temp == 0) mexErrMsgTxt("vec2mxArray: Pointer to data is NULL");
579  if (in.size() == 0) mexErrMsgTxt("vec2mxArray: Size of data is zero");
580 
581  for (int i = 0; i < in.size(); i++) {
582  *temp++ = (double) in(i);
583  }
584 }
585 
586 void cvec2mxArray(const cvec &in, mxArray *out)
587 {
588  double* tempR = (double *) mxGetPr(out);
589  double* tempI = (double *) mxGetPi(out);
590  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
591  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");
592  if (in.size() == 0) mexErrMsgTxt("cvec2mxArray: Size of data is zero");
593 
594  for (int i = 0; i < in.size(); i++) {
595  *tempR++ = (double) in(i).real();
596  *tempI++ = (double) in(i).imag();
597  }
598 }
599 
600 void bmat2mxArray(const bmat &in, mxArray *out)
601 {
602  int rows, cols, r, c;
603 
604  double* temp = (double *) mxGetPr(out);
605  if (temp == 0) mexErrMsgTxt("bmat2mxArray: Pointer to data is NULL");
606 
607  rows = in.rows();
608  cols = in.cols();
609  if (rows == 0) mexErrMsgTxt("bmat2mxArray: Data has zero rows");
610  if (cols == 0) mexErrMsgTxt("bmat2mxArray: Data has zero columns");
611 
612  for (c = 0; c < cols; c++) {
613  for (r = 0; r < rows; r++) {
614  if (in(r, c))
615  *temp++ = 1.0;
616  else
617  *temp++ = 0.0;
618  }
619  }
620 
621 }
622 
623 void smat2mxArray(const smat &in, mxArray *out)
624 {
625  int rows, cols, r, c;
626 
627  double* temp = (double *) mxGetPr(out);
628  if (temp == 0) mexErrMsgTxt("smat2mxArray: Pointer to data is NULL");
629 
630  rows = in.rows();
631  cols = in.cols();
632  if (rows == 0) mexErrMsgTxt("smat2mxArray: Data has zero rows");
633  if (cols == 0) mexErrMsgTxt("smat2mxArray: Data has zero columns");
634 
635  for (c = 0; c < cols; c++) {
636  for (r = 0; r < rows; r++) {
637  *temp++ = (double) in(r, c);
638  }
639  }
640 
641 }
642 
643 void imat2mxArray(const imat &in, mxArray *out)
644 {
645  int rows, cols, r, c;
646 
647  double* temp = (double *) mxGetPr(out);
648  if (temp == 0) mexErrMsgTxt("imat2mxArray: Pointer to data is NULL");
649 
650  rows = in.rows();
651  cols = in.cols();
652  if (rows == 0) mexErrMsgTxt("imat2mxArray: Data has zero rows");
653  if (cols == 0) mexErrMsgTxt("imat2mxArray: Data has zero columns");
654 
655  for (c = 0; c < cols; c++) {
656  for (r = 0; r < rows; r++) {
657  *temp++ = (double) in(r, c);
658  }
659  }
660 
661 }
662 
663 void mat2mxArray(const mat &in, mxArray *out)
664 {
665  int rows, cols, r, c;
666 
667  double* temp = (double *) mxGetPr(out);
668  if (temp == 0) mexErrMsgTxt("mat2mxArray: Pointer to data is NULL");
669 
670  rows = in.rows();
671  cols = in.cols();
672  if (rows == 0) mexErrMsgTxt("mat2mxArray: Data has zero rows");
673  if (cols == 0) mexErrMsgTxt("mat2mxArray: Data has zero columns");
674 
675  for (c = 0; c < cols; c++) {
676  for (r = 0; r < rows; r++) {
677  *temp++ = in(r, c);
678  }
679  }
680 
681 }
682 
683 void cmat2mxArray(const cmat &in, mxArray *out)
684 {
685  int rows, cols, r, c;
686 
687  double* tempR = (double *) mxGetPr(out);
688  double* tempI = (double *) mxGetPi(out);
689  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
690  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");
691 
692  rows = in.rows();
693  cols = in.cols();
694  if (rows == 0) mexErrMsgTxt("cvec2mxArray: Data has zero rows");
695  if (cols == 0) mexErrMsgTxt("cvec2mxArray: Data has zero columns");
696 
697  for (c = 0; c < cols; c++) {
698  for (r = 0; r < rows; r++) {
699  *tempR++ = (double) in(r, c).real();
700  *tempI++ = (double) in(r, c).imag();
701  }
702  }
703 
704 }
705 
706 void mxArray2Csvec(const mxArray *in, short *out)
707 {
708  double* temp = (double*) mxGetPr(in);
709  if (temp == 0) mexErrMsgTxt("mxArray2Csvec: Pointer to data is NULL");
710  int size = mxGetNumberOfElements(in);
711  if (size == 0) mexErrMsgTxt("mxArray2Csvec: Size of data is zero");
712  for (int i = 0; i < size; i++) { out[i] = (short)(*temp++); }
713 }
714 
715 void mxArray2Civec(const mxArray *in, int *out)
716 {
717  double* temp = (double*) mxGetPr(in);
718  if (temp == 0) mexErrMsgTxt("mxArray2Civec: Pointer to data is NULL");
719  int size = mxGetNumberOfElements(in);
720  if (size == 0) mexErrMsgTxt("mxArray2Civec: Size of data is zero");
721  for (int i = 0; i < size; i++) { out[i] = (int)(*temp++); }
722 }
723 
724 void mxArray2Cvec(const mxArray *in, double *out)
725 {
726  double* temp = (double*) mxGetPr(in);
727  if (temp == 0) mexErrMsgTxt("mxArray2Cvec: Pointer to data is NULL");
728  int size = mxGetNumberOfElements(in);
729  if (size == 0) mexErrMsgTxt("mxArray2Cvec: Size of data is zero");
730  for (int i = 0; i < size; i++) { out[i] = (*temp++); }
731 }
732 
733 void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag)
734 {
735  double* tempR = (double*) mxGetPr(in);
736  double* tempI = (double*) mxGetPi(in);
737  if (tempR == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to real valued part is NULL");
738  if (tempI == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to imaginary valued part is NULL");
739  int size = mxGetNumberOfElements(in);
740  if (size == 0) mexErrMsgTxt("mxArray2Ccvec: Size of data is zero");
741  for (int i = 0; i < size; i++) { out_real[i] = (*tempR++); out_imag[i] = (*tempI++); }
742 }
743 
744 void mxArray2Csmat(const mxArray *in, short **out)
745 {
746  int r, c;
747  double* temp = (double*) mxGetPr(in);
748  if (temp == 0) mexErrMsgTxt("mxArray2Csmat: Pointer to data is NULL");
749  int rows = mxGetM(in);
750  if (rows == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero rows");
751  int cols = mxGetN(in);
752  if (cols == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero columns");
753  for (c = 0; c < cols; c++) {
754  for (r = 0; r < rows; r++) {
755  out[r][c] = (short)(*temp++);
756  }
757  }
758 }
759 
760 void mxArray2Cimat(const mxArray *in, int **out)
761 {
762  int r, c;
763  double* temp = (double*) mxGetPr(in);
764  if (temp == 0) mexErrMsgTxt("mxArray2Cimat: Pointer to data is NULL");
765  int rows = mxGetM(in);
766  if (rows == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero rows");
767  int cols = mxGetN(in);
768  if (cols == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero columns");
769  for (c = 0; c < cols; c++) {
770  for (r = 0; r < rows; r++) {
771  out[r][c] = (int)(*temp++);
772  }
773  }
774 }
775 
776 void mxArray2Cmat(const mxArray *in, double **out)
777 {
778  int r, c;
779  double* temp = (double*) mxGetPr(in);
780  if (temp == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to data is NULL");
781  int rows = mxGetM(in);
782  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
783  int cols = mxGetN(in);
784  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
785  for (c = 0; c < cols; c++) {
786  for (r = 0; r < rows; r++) {
787  out[r][c] = (*temp++);
788  }
789  }
790 }
791 
792 void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag)
793 {
794  int r, c;
795  double* tempR = (double*) mxGetPr(in);
796  double* tempI = (double*) mxGetPi(in);
797  if (tempR == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to real valued part is NULL");
798  if (tempI == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to imaginary valued part is NULL");
799  int rows = mxGetM(in);
800  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
801  int cols = mxGetN(in);
802  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
803  for (c = 0; c < cols; c++) {
804  for (r = 0; r < rows; r++) {
805  out_real[r][c] = (*tempR++);
806  out_imag[r][c] = (*tempI++);
807  }
808  }
809 }
810 
811 void Csvec2mxArray(short *in, mxArray *out)
812 {
813  double* temp = (double *) mxGetPr(out);
814  if (temp == 0) mexErrMsgTxt("Csvec2mxArray: Pointer to data is NULL");
815  int size = mxGetNumberOfElements(out);
816  if (size == 0) mexErrMsgTxt("Csvec2mxArray: Size of data is zero");
817  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
818 }
819 
820 void Civec2mxArray(int *in, mxArray *out)
821 {
822  double* temp = (double *) mxGetPr(out);
823  if (temp == 0) mexErrMsgTxt("Civec2mxArray: Pointer to data is NULL");
824  int size = mxGetNumberOfElements(out);
825  if (size == 0) mexErrMsgTxt("Civec2mxArray: Size of data is zero");
826  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
827 }
828 
829 void Cvec2mxArray(double *in, mxArray *out)
830 {
831  double* temp = (double *) mxGetPr(out);
832  if (temp == 0) mexErrMsgTxt("Cvec2mxArray: Pointer to data is NULL");
833  int size = mxGetNumberOfElements(out);
834  if (size == 0) mexErrMsgTxt("Cvec2mxArray: Size of data is zero");
835  for (int i = 0; i < size; i++) { *temp++ = in[i]; }
836 }
837 
838 void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out)
839 {
840  double* tempR = (double *) mxGetPr(out);
841  double* tempI = (double *) mxGetPi(out);
842  if (tempR == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to real valued part is NULL");
843  if (tempI == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to imaginary valued part is NULL");
844  int size = mxGetNumberOfElements(out);
845  if (size == 0) mexErrMsgTxt("Ccvec2mxArray: Size of data is zero");
846  for (int i = 0; i < size; i++) { *tempR++ = in_real[i]; *tempI++ = in_imag[i]; }
847 }
848 
849 void Csmat2mxArray(short **in, mxArray *out)
850 {
851  int r, c;
852  double* temp = (double *) mxGetPr(out);
853  if (temp == 0) mexErrMsgTxt("Csmat2mxArray: Pointer to data is NULL");
854  int rows = mxGetM(out);
855  if (rows == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero rows");
856  int cols = mxGetN(out);
857  if (cols == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero columns");
858  for (c = 0; c < cols; c++) {
859  for (r = 0; r < rows; r++) {
860  *temp++ = (short) in[r][c];
861  }
862  }
863 }
864 
865 void Cimat2mxArray(int **in, mxArray *out)
866 {
867  int r, c;
868  double* temp = (double *) mxGetPr(out);
869  if (temp == 0) mexErrMsgTxt("Cimat2mxArray: Pointer to data is NULL");
870  int rows = mxGetM(out);
871  if (rows == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero rows");
872  int cols = mxGetN(out);
873  if (cols == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero columns");
874  for (c = 0; c < cols; c++) {
875  for (r = 0; r < rows; r++) {
876  *temp++ = (int) in[r][c];
877  }
878  }
879 }
880 
881 void Cmat2mxArray(double **in, mxArray *out)
882 {
883  int r, c;
884  double* temp = (double *) mxGetPr(out);
885  if (temp == 0) mexErrMsgTxt("Cmat2mxArray: Pointer to data is NULL");
886  int rows = mxGetM(out);
887  if (rows == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero rows");
888  int cols = mxGetN(out);
889  if (cols == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero columns");
890  for (c = 0; c < cols; c++) {
891  for (r = 0; r < rows; r++) {
892  *temp++ = in[r][c];
893  }
894  }
895 }
896 
897 void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out)
898 {
899  int r, c;
900  double* tempR = (double *) mxGetPr(out);
901  double* tempI = (double *) mxGetPi(out);
902  if (tempR == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to real valued part is NULL");
903  if (tempI == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to imaginary valued part is NULL");
904  int rows = mxGetM(out);
905  if (rows == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero rows");
906  int cols = mxGetN(out);
907  if (cols == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero columns");
908  for (c = 0; c < cols; c++) {
909  for (r = 0; r < rows; r++) {
910  *tempR++ = in_real[r][c];
911  *tempI++ = in_imag[r][c];
912  }
913  }
914 }
915 
916 } // namespace itpp
917 
918 #endif // #ifndef ITMEX_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:24 for IT++ by Doxygen 1.8.2