This file is indexed.

/usr/include/itpp/itmex.h is in libitpp-dev 4.3.1-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*!
 * \file
 * \brief Conversion routines between IT++ and Matlab
 * \author Tony Ottosson and Pal Frenger
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#ifndef ITMEX_H
#define ITMEX_H

#include <itpp/itbase.h>
#include <mex.h>


namespace itpp
{

//--------------------------------------------------------
// mex -> it++
//--------------------------------------------------------

/*!
  \addtogroup mexfiles
  \brief Conversions between IT++ and Matlab for writing mex-files.
  \author Tony Ottosson and Pal Frenger

  These routines are used to help writng mex-files for speeding up
  matlab simulations.
  A simple mex-file that performs QPSK modulation is given below.

  \code
  #include <itpp/itcomm.h>
  #include <itpp/itmex.h>

  using namespace itpp;

  void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[])
  {
  // Check the number of inputs and output arguments
  if(n_output!=1) mexErrMsgTxt("Wrong number of output variables!");
  if(n_input!=1) mexErrMsgTxt("Wrong number of input variables!");

  // Convert input variables to IT++ format
  bvec input_bits = mxArray2bvec(input[0]);

  // ------------------ Start of routine ---------------------------
  cvec output_symbols;
  QPSK qpsk;

  output_symbols = qpsk.modulate_bits(input_bits);
  // ------------------ End of routine -----------------------------

  // Create output vectors
  output[0] = mxCreateDoubleMatrix(1,output_symbols.size(), mxCOMPLEX);

  // Convert the IT++ format to Matlab format for output
  cvec2mxArray(output_symbols, output[0]);
  }
  \endcode
*/

/*!
 * \addtogroup mexfiles
 * @{
 */

// --------------------------------------------------------
// mex -> IT++
// --------------------------------------------------------

//! Convert the matlab-format mxArray to bin
bin mxArray2bin(const mxArray *in);
//! Convert the matlab-format mxArray to short
short mxArray2short(const mxArray *in);
//! Convert the matlab-format mxArray to int
int mxArray2int(const mxArray *in);
//! Convert the matlab-format mxArray to double
double mxArray2double(const mxArray *in);
//! Convert the matlab-format mxArray to complex<double>
std::complex<double> mxArray2double_complex(const mxArray *in);
//! Convert the matlab-format mxArray to string
std::string mxArray2string(const mxArray *in);

//! Convert the matlab-format mxArray to bvec
bvec mxArray2bvec(const mxArray *in);
//! Convert the matlab-format mxArray to svec
svec mxArray2svec(const mxArray *in);
//! Convert the matlab-format mxArray to ivec
ivec mxArray2ivec(const mxArray *in);
//! Convert the matlab-format mxArray to vec
vec mxArray2vec(const mxArray *in);
//! Convert the matlab-format mxArray to cvec
cvec mxArray2cvec(const mxArray *in);

//! Convert the matlab-format mxArray to bmat
bmat mxArray2bmat(const mxArray *in);
//! Convert the matlab-format mxArray to smat
smat mxArray2smat(const mxArray *in);
//! Convert the matlab-format mxArray to imat
imat mxArray2imat(const mxArray *in);
//! Convert the matlab-format mxArray to mat
mat mxArray2mat(const mxArray *in);
//! Convert the matlab-format mxArray to cmat
cmat mxArray2cmat(const mxArray *in);

// --------------------------------------------------------
// IT++ -> mex
// --------------------------------------------------------

//! Convert bin to the matlab-format mxArray
void bin2mxArray(const bin &in, mxArray *out);
//! Convert short to the matlab-format mxArray
void short2mxArray(const short &in, mxArray *out);
//! Convert int to the matlab-format mxArray
void int2mxArray(const int &in, mxArray *out);
//! Convert double to the matlab-format mxArray
void double2mxArray(const double &in, mxArray *out);
//! Convert complex<double> to the matlab-format mxArray
void double_complex2mxArray(const std::complex<double> &in, mxArray *out);
//! Convert string to the matlab-format mxArray
void string2mxArray(const std::string &in, mxArray* &out);

//! Convert bvec to the matlab-format mxArray
void bvec2mxArray(const bvec &in, mxArray *out);
//! Convert svec to the matlab-format mxArray
void svec2mxArray(const svec &in, mxArray *out);
//! Convert ivec to the matlab-format mxArray
void ivec2mxArray(const ivec &in, mxArray *out);
//! Convert vec to the matlab-format mxArray
void vec2mxArray(const vec &in, mxArray *out);
//! Convert cvec to the matlab-format mxArray
void cvec2mxArray(const cvec &in, mxArray *out);

//! Convert bmat to the matlab-format mxArray
void bmat2mxArray(const bmat &in, mxArray *out);
//! Convert smat to the matlab-format mxArray
void smat2mxArray(const smat &in, mxArray *out);
//! Convert imat to the matlab-format mxArray
void imat2mxArray(const imat &in, mxArray *out);
//! Convert mat to the matlab-format mxArray
void mat2mxArray(const mat &in, mxArray *out);
//! Convert cmat to the matlab-format mxArray
void cmat2mxArray(const cmat &in, mxArray *out);

// --------------------------------------------------------
// mex -> C
// --------------------------------------------------------

//! Convert the matlab-format mxArray to C-format pointer to short
void mxArray2Csvec(const mxArray *in, short *out);
//! Convert the matlab-format mxArray to C-format pointer to int
void mxArray2Civec(const mxArray *in, int *out);
//! Convert the matlab-format mxArray to C-format pointer to double
void mxArray2Cvec(const mxArray *in, double *out);
//! Convert the matlab-format mxArray to C-format pointers to double (real and imaginary parts)
void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag);

//! Convert the matlab-format mxArray to C-format pointer to pointer to short
void mxArray2Csmat(const mxArray *in, short **out);
//! Convert the matlab-format mxArray to C-format pointer to pointer to int
void mxArray2Cimat(const mxArray *in, int **out);
//! Convert the matlab-format mxArray to C-format pointer to pointer to double
void mxArray2Cmat(const mxArray *in, double **out);
//! Convert the matlab-format mxArray to C-format pointer to pointer to double (real and imaginary parts)
void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag);

// --------------------------------------------------------
// C -> mex
// --------------------------------------------------------

//! Convert C-format pointer to short to matlab-format mxArray
void Csvec2mxArray(short *in, mxArray *out);
//! Convert C-format pointer to int to matlab-format mxArray
void Civec2mxArray(int *in, mxArray *out);
//! Convert C-format pointer to double to matlab-format mxArray
void Cvec2mxArray(double *in, mxArray *out);
//! Convert C-format pointers to double (real and imaginary parts) to matlab-format mxArray
void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out);

//! Convert C-format pointer to pointer to short to matlab-format mxArray
void Csmat2mxArray(short **in, mxArray *out);
//! Convert C-format pointer to pointer to int to matlab-format mxArray
void Cimat2mxArray(int **in, mxArray *out);
//! Convert C-format pointer to pointer to double to matlab-format mxArray
void Cmat2mxArray(double **in, mxArray *out);
//! Convert C-format pointer to pointer to double (real and imaginary parts) to matlab-format mxArray
void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out);

/*!
 * @}
 */


bin mxArray2bin(const mxArray *in)
{
  int size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2bin: Pointer to data is NULL");
  size = mxGetNumberOfElements(in);
  if (size != 1) mexErrMsgTxt("mxArray2bin: Size of data is not equal to one");

  return (((*temp) > 0.0) ? bin(1) : bin(0));
}

short mxArray2short(const mxArray *in)
{
  int size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2short: Pointer to data is NULL");
  size = mxGetNumberOfElements(in);
  if (size != 1) mexErrMsgTxt("mxArray2short: Size of data is not equal to one");

  return (short)(*temp);
}

int mxArray2int(const mxArray *in)
{
  int size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2int: Pointer to data is NULL");
  size = mxGetNumberOfElements(in);
  if (size != 1) mexErrMsgTxt("mxArray2int: Size of data is not equal to one");

  return (int)(*temp);
}

double mxArray2double(const mxArray *in)
{
  int size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2double: Pointer to data is NULL");
  size = mxGetNumberOfElements(in);
  if (size != 1) mexErrMsgTxt("mxArray2double: Size of data is not equal to one");

  return (*temp);
}

std::complex<double> mxArray2double_complex(const mxArray *in)
{
  int size;
  double* tempR = (double*) mxGetPr(in);
  double* tempI = (double*) mxGetPi(in);

  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2double_complex: Pointer to data is NULL");

  size = mxGetNumberOfElements(in);
  if (size != 1) mexErrMsgTxt("mxArray2double_complex: Size of data is not equal to one");

  if (tempR == 0) {
    return std::complex<double>(0.0 , (*tempI));
  }
  else if (tempI == 0) {
    return std::complex<double>((*tempR), 0.0);
  }
  else {
    return std::complex<double>((*tempR), (*tempI));
  }

}

std::string mxArray2string(const mxArray *in)
{
  if (in == 0)
    mexErrMsgTxt("mxArray2string: Pointer to data is NULL");
  std::string str = mxArrayToString(in);
  if (str.data() == 0)
    mexErrMsgTxt("mxArray2string: Could not convert mxArray to string");
  return str;
}

bvec mxArray2bvec(const mxArray *in)
{
  bvec out;
  int i, size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2bvec: Pointer to data is NULL");

  size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2bvec: Size of data is zero");

  out.set_size(size, false);

  for (i = 0; i < size; i++) {
    out(i) = (((*temp++) > 1e-5) ? bin(1) : bin(0));
  }

  return out;

}

svec mxArray2svec(const mxArray *in)
{
  svec out;
  int i, size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2svec: Pointer to data is NULL");

  size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2svec: Size of data is zero");

  out.set_size(size, false);

  for (i = 0; i < size; i++) {
    out(i) = (short)(*temp++);
  }

  return out;

}

ivec mxArray2ivec(const mxArray *in)
{
  ivec out;
  int i, size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2ivec: Pointer to data is NULL");

  size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2ivec: Size of data is zero");

  out.set_size(size, false);

  for (i = 0; i < size; i++) {
    out(i) = (int)(*temp++);
  }

  return out;

}

vec mxArray2vec(const mxArray *in)
{
  vec out;
  int i, size;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2vec: Pointer to data is NULL");

  size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2vec: Size of data is zero");

  out.set_size(size, false);

  for (i = 0; i < size; i++) {
    out(i) = (*temp++);
  }

  return out;

}

cvec mxArray2cvec(const mxArray *in)
{
  cvec out;
  int i, size;
  double* tempR = (double*) mxGetPr(in);
  double* tempI = (double*) mxGetPi(in);

  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cvec: Pointer data is NULL");

  size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2cvec: Size of data is zero");

  out.set_size(size, false);

  if (tempR == 0) {
    for (i = 0; i < size; i++) { out(i) = std::complex<double>(0.0, (*tempI++)); }
  }
  else if (tempI == 0) {
    for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), 0.0); }
  }
  else {
    for (i = 0; i < size; i++) { out(i) = std::complex<double>((*tempR++), (*tempI++)); }
  }

  return out;

}

bmat mxArray2bmat(const mxArray *in)
{
  bmat out;
  int r, c, rows, cols;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2bmat: Pointer to data is NULL");

  rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2bmat: Data has zero rows");
  cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2bmat: Data has zero columns");

  out.set_size(rows, cols, false);

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out(r, c) = (((*temp++) > 0.0) ? bin(1) : bin(0));
    }
  }

  return out;

}

smat mxArray2smat(const mxArray *in)
{
  smat out;
  int r, c, rows, cols;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2smat: Pointer to data is NULL");

  rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2smat: Data has zero rows");
  cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2smat: Data has zero columns");

  out.set_size(rows, cols, false);

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out(r, c) = (short)(*temp++);
    }
  }

  return out;

}

imat mxArray2imat(const mxArray *in)
{
  imat out;
  int r, c, rows, cols;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2imat: Pointer to data is NULL");

  rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2imat: Data has zero rows");
  cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2imat: Data has zero columns");
  out.set_size(rows, cols, false);

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out(r, c) = (int)(*temp++);
    }
  }

  return out;

}

mat mxArray2mat(const mxArray *in)
{
  mat out;
  int r, c, rows, cols;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2mat: Pointer to data is NULL");

  rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2mat: Data has zero rows");
  cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2mat: Data has zero columns");
  out.set_size(rows, cols, false);

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out(r, c) = (*temp++);
    }
  }

  return out;

}

cmat mxArray2cmat(const mxArray *in)
{
  cmat out;
  int r, c, rows, cols;
  double* tempR = (double*) mxGetPr(in);
  double* tempI = (double*) mxGetPi(in);

  if ((tempR == 0) && (tempI == 0)) mexErrMsgTxt("mxArray2cmat: Pointer to data is NULL");

  rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2cmat: Data has zero rows");
  cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2cmat: Data has zero columns");
  out.set_size(rows, cols, false);

  if (tempR == 0) {
    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>(0.0 , (*tempI++)); } }
  }
  else if (tempI == 0) {
    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), 0.0); } }
  }
  else {
    for (c = 0; c < cols; c++) { for (r = 0; r < rows; r++) { out(r, c) = std::complex<double>((*tempR++), (*tempI++)); } }
  }

  return out;

}

void double2mxArray(const double &in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("double2mxArray: Pointer to data is NULL");

  *temp = (double) in;
}

void double_complex2mxArray(const std::complex<double> &in, mxArray *out)
{
  double* tempR = (double *) mxGetPr(out);
  double* tempI = (double *) mxGetPi(out);
  if (tempR == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("double_complex2mxArray: Pointer to imaginary valued part is NULL");

  *tempR = (double) in.real();
  *tempI = (double) in.imag();
}

void string2mxArray(const std::string &in, mxArray* &out)
{
  if (in.data() == 0)
    mexErrMsgTxt("string2mxArray: Pointer to string is NULL");
  out = mxCreateString(in.data());
  if (out == 0)
    mexErrMsgTxt("string2mxArray: Could not convert string to mxArray");
}

void bvec2mxArray(const bvec &in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("bvec2mxArray: Pointer to data is NULL");
  if (in.size() == 0) mexErrMsgTxt("bvec2mxArray: Size of data is zero");
  for (int i = 0; i < in.size(); i++) {
    if (in(i))
      *temp++ = 1.0;
    else
      *temp++ = 0.0;
  }
}

void ivec2mxArray(const ivec &in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("ivec2mxArray: Pointer to data is NULL");
  if (in.size() == 0) mexErrMsgTxt("ivec2mxArray: Size of data is zero");

  for (int i = 0; i < in.size(); i++) {
    *temp++ = (double) in(i);
  }
}

void vec2mxArray(const vec &in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("vec2mxArray: Pointer to data is NULL");
  if (in.size() == 0) mexErrMsgTxt("vec2mxArray: Size of data is zero");

  for (int i = 0; i < in.size(); i++) {
    *temp++ = (double) in(i);
  }
}

void cvec2mxArray(const cvec &in, mxArray *out)
{
  double* tempR = (double *) mxGetPr(out);
  double* tempI = (double *) mxGetPi(out);
  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");
  if (in.size() == 0) mexErrMsgTxt("cvec2mxArray: Size of data is zero");

  for (int i = 0; i < in.size(); i++) {
    *tempR++ = (double) in(i).real();
    *tempI++ = (double) in(i).imag();
  }
}

void bmat2mxArray(const bmat &in, mxArray *out)
{
  int rows, cols, r, c;

  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("bmat2mxArray: Pointer to data is NULL");

  rows = in.rows();
  cols = in.cols();
  if (rows == 0) mexErrMsgTxt("bmat2mxArray: Data has zero rows");
  if (cols == 0) mexErrMsgTxt("bmat2mxArray: Data has zero columns");

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      if (in(r, c))
        *temp++ = 1.0;
      else
        *temp++ = 0.0;
    }
  }

}

void smat2mxArray(const smat &in, mxArray *out)
{
  int rows, cols, r, c;

  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("smat2mxArray: Pointer to data is NULL");

  rows = in.rows();
  cols = in.cols();
  if (rows == 0) mexErrMsgTxt("smat2mxArray: Data has zero rows");
  if (cols == 0) mexErrMsgTxt("smat2mxArray: Data has zero columns");

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = (double) in(r, c);
    }
  }

}

void imat2mxArray(const imat &in, mxArray *out)
{
  int rows, cols, r, c;

  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("imat2mxArray: Pointer to data is NULL");

  rows = in.rows();
  cols = in.cols();
  if (rows == 0) mexErrMsgTxt("imat2mxArray: Data has zero rows");
  if (cols == 0) mexErrMsgTxt("imat2mxArray: Data has zero columns");

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = (double) in(r, c);
    }
  }

}

void mat2mxArray(const mat &in, mxArray *out)
{
  int rows, cols, r, c;

  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("mat2mxArray: Pointer to data is NULL");

  rows = in.rows();
  cols = in.cols();
  if (rows == 0) mexErrMsgTxt("mat2mxArray: Data has zero rows");
  if (cols == 0) mexErrMsgTxt("mat2mxArray: Data has zero columns");

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = in(r, c);
    }
  }

}

void cmat2mxArray(const cmat &in, mxArray *out)
{
  int rows, cols, r, c;

  double* tempR = (double *) mxGetPr(out);
  double* tempI = (double *) mxGetPi(out);
  if (tempR == 0) mexErrMsgTxt("cvec2mxArray: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("cvec2mxArray: Pointer to imaginary valued part is NULL");

  rows = in.rows();
  cols = in.cols();
  if (rows == 0) mexErrMsgTxt("cvec2mxArray: Data has zero rows");
  if (cols == 0) mexErrMsgTxt("cvec2mxArray: Data has zero columns");

  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *tempR++ = (double) in(r, c).real();
      *tempI++ = (double) in(r, c).imag();
    }
  }

}

void mxArray2Csvec(const mxArray *in, short *out)
{
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Csvec: Pointer to data is NULL");
  int size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2Csvec: Size of data is zero");
  for (int i = 0; i < size; i++) { out[i] = (short)(*temp++); }
}

void mxArray2Civec(const mxArray *in, int *out)
{
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Civec: Pointer to data is NULL");
  int size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2Civec: Size of data is zero");
  for (int i = 0; i < size; i++) { out[i] = (int)(*temp++); }
}

void mxArray2Cvec(const mxArray *in, double *out)
{
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Cvec: Pointer to data is NULL");
  int size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2Cvec: Size of data is zero");
  for (int i = 0; i < size; i++) { out[i] = (*temp++); }
}

void mxArray2Ccvec(const mxArray *in, double *out_real, double *out_imag)
{
  double* tempR = (double*) mxGetPr(in);
  double* tempI = (double*) mxGetPi(in);
  if (tempR == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("mxArray2Ccvec: Pointer to imaginary valued part is NULL");
  int size = mxGetNumberOfElements(in);
  if (size == 0) mexErrMsgTxt("mxArray2Ccvec: Size of data is zero");
  for (int i = 0; i < size; i++) { out_real[i] = (*tempR++); out_imag[i] = (*tempI++); }
}

void mxArray2Csmat(const mxArray *in, short **out)
{
  int r, c;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Csmat: Pointer to data is NULL");
  int rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero rows");
  int cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2Csmat: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out[r][c] = (short)(*temp++);
    }
  }
}

void mxArray2Cimat(const mxArray *in, int **out)
{
  int r, c;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Cimat: Pointer to data is NULL");
  int rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero rows");
  int cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2Cimat: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out[r][c] = (int)(*temp++);
    }
  }
}

void mxArray2Cmat(const mxArray *in, double **out)
{
  int r, c;
  double* temp = (double*) mxGetPr(in);
  if (temp == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to data is NULL");
  int rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
  int cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out[r][c] = (*temp++);
    }
  }
}

void mxArray2Ccmat(const mxArray *in, double **out_real, double **out_imag)
{
  int r, c;
  double* tempR = (double*) mxGetPr(in);
  double* tempI = (double*) mxGetPi(in);
  if (tempR == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("mxArray2Cmat: Pointer to imaginary valued part is NULL");
  int rows = mxGetM(in);
  if (rows == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero rows");
  int cols = mxGetN(in);
  if (cols == 0) mexErrMsgTxt("mxArray2Cmat: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      out_real[r][c] = (*tempR++);
      out_imag[r][c] = (*tempI++);
    }
  }
}

void Csvec2mxArray(short *in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Csvec2mxArray: Pointer to data is NULL");
  int size = mxGetNumberOfElements(out);
  if (size == 0) mexErrMsgTxt("Csvec2mxArray: Size of data is zero");
  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
}

void Civec2mxArray(int *in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Civec2mxArray: Pointer to data is NULL");
  int size = mxGetNumberOfElements(out);
  if (size == 0) mexErrMsgTxt("Civec2mxArray: Size of data is zero");
  for (int i = 0; i < size; i++) { *temp++ = (double) in[i]; }
}

void Cvec2mxArray(double *in, mxArray *out)
{
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Cvec2mxArray: Pointer to data is NULL");
  int size = mxGetNumberOfElements(out);
  if (size == 0) mexErrMsgTxt("Cvec2mxArray: Size of data is zero");
  for (int i = 0; i < size; i++) { *temp++ = in[i]; }
}

void Ccvec2mxArray(double *in_real, double *in_imag, mxArray *out)
{
  double* tempR = (double *) mxGetPr(out);
  double* tempI = (double *) mxGetPi(out);
  if (tempR == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("Ccvec2mxArray: Pointer to imaginary valued part is NULL");
  int size = mxGetNumberOfElements(out);
  if (size == 0) mexErrMsgTxt("Ccvec2mxArray: Size of data is zero");
  for (int i = 0; i < size; i++) { *tempR++ = in_real[i]; *tempI++ = in_imag[i]; }
}

void Csmat2mxArray(short **in, mxArray *out)
{
  int r, c;
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Csmat2mxArray: Pointer to data is NULL");
  int rows = mxGetM(out);
  if (rows == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero rows");
  int cols = mxGetN(out);
  if (cols == 0) mexErrMsgTxt("Csmat2mxArray: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = (short) in[r][c];
    }
  }
}

void Cimat2mxArray(int **in, mxArray *out)
{
  int r, c;
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Cimat2mxArray: Pointer to data is NULL");
  int rows = mxGetM(out);
  if (rows == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero rows");
  int cols = mxGetN(out);
  if (cols == 0) mexErrMsgTxt("Cimat2mxArray: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = (int) in[r][c];
    }
  }
}

void Cmat2mxArray(double **in, mxArray *out)
{
  int r, c;
  double* temp = (double *) mxGetPr(out);
  if (temp == 0) mexErrMsgTxt("Cmat2mxArray: Pointer to data is NULL");
  int rows = mxGetM(out);
  if (rows == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero rows");
  int cols = mxGetN(out);
  if (cols == 0) mexErrMsgTxt("Cmat2mxArray: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *temp++ = in[r][c];
    }
  }
}

void Ccmat2mxArray(double **in_real, double **in_imag, mxArray *out)
{
  int r, c;
  double* tempR = (double *) mxGetPr(out);
  double* tempI = (double *) mxGetPi(out);
  if (tempR == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to real valued part is NULL");
  if (tempI == 0) mexErrMsgTxt("Ccmat2mxArray: Pointer to imaginary valued part is NULL");
  int rows = mxGetM(out);
  if (rows == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero rows");
  int cols = mxGetN(out);
  if (cols == 0) mexErrMsgTxt("Ccmat2mxArray: Data has zero columns");
  for (c = 0; c < cols; c++) {
    for (r = 0; r < rows; r++) {
      *tempR++ = in_real[r][c];
      *tempI++ = in_imag[r][c];
    }
  }
}

} // namespace itpp

#endif // #ifndef ITMEX_H