ConverterMatlabToNICE.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "ConverterMatlabToNICE.h"
  2. using namespace NICE;
  3. ConverterMatlabToNICE::ConverterMatlabToNICE()
  4. {
  5. }
  6. ConverterMatlabToNICE::~ConverterMatlabToNICE()
  7. {
  8. }
  9. /* Pass analyze_sparse a pointer to a sparse mxArray. A sparse mxArray
  10. only stores its nonzero elements. The values of the nonzero elements
  11. are stored in the pr and pi arrays. The tricky part of analyzing
  12. sparse mxArray's is figuring out the indices where the nonzero
  13. elements are stored. (See the mxSetIr and mxSetJc reference pages
  14. for details. */
  15. std::vector< const NICE::SparseVector * > ConverterMatlabToNICE::convertSparseMatrixToNice( const mxArray *array_ptr ) const
  16. {
  17. double *pr;//, *pi;
  18. mwIndex *ir, *jc;
  19. mwSize col, total=0;
  20. mwIndex starting_row_index, stopping_row_index, current_row_index;
  21. mwSize i_numExamples, i_numDim;
  22. /* Get the starting positions of all four data arrays. */
  23. pr = mxGetPr( array_ptr );
  24. // no complex data supported here
  25. // pi = mxGetPi(array_ptr);
  26. ir = mxGetIr( array_ptr );
  27. jc = mxGetJc( array_ptr );
  28. // dimenions of the matrix -> feature dimension and number of examples
  29. i_numExamples = mxGetM( array_ptr );
  30. i_numDim = mxGetN( array_ptr );
  31. // initialize output variable -- don't use const pointers here since the content of the vectors will change
  32. // in the following loop. We reinterprete the vector lateron into a const version
  33. std::vector< NICE::SparseVector * > sparseMatrix;
  34. sparseMatrix.resize ( i_numExamples );
  35. for ( std::vector< NICE::SparseVector * >::iterator matIt = sparseMatrix.begin();
  36. matIt != sparseMatrix.end(); matIt++)
  37. {
  38. *matIt = new NICE::SparseVector( i_numDim );
  39. }
  40. // now copy the data
  41. for ( col = 0; col < i_numDim; col++ )
  42. {
  43. starting_row_index = jc[col];
  44. stopping_row_index = jc[col+1];
  45. // empty column?
  46. if (starting_row_index == stopping_row_index)
  47. continue;
  48. else
  49. {
  50. for ( current_row_index = starting_row_index;
  51. current_row_index < stopping_row_index;
  52. current_row_index++
  53. )
  54. {
  55. //note: no complex data supported her
  56. sparseMatrix[ ir[current_row_index] ]->insert( std::pair<int, double>( col, pr[total++] ) );
  57. } // for-loop
  58. }
  59. } // for-loop over columns
  60. //NOTE
  61. // Compiler doesn't know how to automatically convert
  62. // std::vector<T*> to std::vector<T const*> because the way
  63. // the template system works means that in theory the two may
  64. // be specialised differently. This is an explicit conversion.
  65. return reinterpret_cast< std::vector< const NICE::SparseVector *> &>( sparseMatrix );
  66. }
  67. // b_adaptIndexMtoC: if true, dim k will be inserted as k, not as k-1 (which would be the default for M->C)
  68. NICE::SparseVector ConverterMatlabToNICE::convertSparseVectorToNice(
  69. const mxArray* array_ptr,
  70. const bool & b_adaptIndexMtoC
  71. ) const
  72. {
  73. double *pr, *pi;
  74. mwIndex *ir, *jc;
  75. mwSize col, total=0;
  76. mwIndex starting_row_index, stopping_row_index, current_row_index;
  77. mwSize dimy, dimx;
  78. /* Get the starting positions of all four data arrays. */
  79. pr = mxGetPr( array_ptr );
  80. pi = mxGetPi( array_ptr );
  81. ir = mxGetIr( array_ptr );
  82. jc = mxGetJc( array_ptr );
  83. // dimenions of the matrix -> feature dimension and number of examples
  84. dimy = mxGetM( array_ptr );
  85. dimx = mxGetN( array_ptr );
  86. double* ptr = mxGetPr( array_ptr );
  87. if( (dimx != 1) && (dimy != 1) )
  88. mexErrMsgIdAndTxt("mexnice:error","Vector expected");
  89. NICE::SparseVector svec( std::max(dimx, dimy) );
  90. if ( dimx > 1)
  91. {
  92. for ( mwSize row=0; row < dimx; row++)
  93. {
  94. // empty column?
  95. if (jc[row] == jc[row+1])
  96. {
  97. continue;
  98. }
  99. else
  100. {
  101. //note: no complex data supported her
  102. double value ( pr[total++] );
  103. if ( b_adaptIndexMtoC )
  104. svec.insert( std::pair<int, double>( row+1, value ) );
  105. else
  106. svec.insert( std::pair<int, double>( row, value ) );
  107. }
  108. } // for loop over cols
  109. }
  110. else
  111. {
  112. mwSize numNonZero = jc[1]-jc[0];
  113. for ( mwSize colNonZero=0; colNonZero < numNonZero; colNonZero++)
  114. {
  115. //note: no complex data supported her
  116. double value ( pr[total++] );
  117. if ( b_adaptIndexMtoC )
  118. svec.insert( std::pair<int, double>( ir[colNonZero]+1, value ) );
  119. else
  120. svec.insert( std::pair<int, double>( ir[colNonZero], value ) );
  121. }
  122. }
  123. return svec;
  124. }
  125. NICE::Matrix ConverterMatlabToNICE::convertDoubleMatrixToNice( const mxArray* matlabMatrix ) const
  126. {
  127. if( !mxIsDouble( matlabMatrix ) )
  128. mexErrMsgIdAndTxt( "mexnice:error","Expected double in convertDoubleMatrixToNice" );
  129. const mwSize *dims;
  130. int dimx, dimy, numdims;
  131. //figure out dimensions
  132. dims = mxGetDimensions( matlabMatrix );
  133. numdims = mxGetNumberOfDimensions( matlabMatrix );
  134. dimy = (int)dims[0];
  135. dimx = (int)dims[1];
  136. double* ptr = mxGetPr( matlabMatrix );
  137. NICE::Matrix niceMatrix(ptr, dimy, dimx, NICE::Matrix::external);
  138. return niceMatrix;
  139. }
  140. NICE::Vector ConverterMatlabToNICE::convertDoubleVectorToNice( const mxArray* matlabMatrix ) const
  141. {
  142. if( !mxIsDouble( matlabMatrix ) )
  143. mexErrMsgIdAndTxt( "mexnice:error","Expected double in convertDoubleVectorToNice" );
  144. const mwSize *dims;
  145. int dimx, dimy, numdims;
  146. //figure out dimensions
  147. dims = mxGetDimensions( matlabMatrix );
  148. numdims = mxGetNumberOfDimensions( matlabMatrix );
  149. dimy = (int)dims[0];
  150. dimx = (int)dims[1];
  151. double* ptr = mxGetPr( matlabMatrix );
  152. if( (dimx != 1) && (dimy != 1) )
  153. mexErrMsgIdAndTxt("mexnice:error","Vector expected");
  154. int dim = std::max(dimx, dimy);
  155. NICE::Vector niceVector( dim, 0.0 );
  156. for( int i = 0; i < dim; i++ )
  157. {
  158. niceVector(i) = ptr[i];
  159. }
  160. return niceVector;
  161. }
  162. std::string ConverterMatlabToNICE::convertMatlabToString( const mxArray *matlabString ) const
  163. {
  164. if( !mxIsChar( matlabString ) )
  165. mexErrMsgIdAndTxt("mexnice:error","Expected string");
  166. char *cstring = mxArrayToString( matlabString );
  167. std::string s( cstring );
  168. mxFree(cstring);
  169. return s;
  170. }
  171. int ConverterMatlabToNICE::convertMatlabToInt32( const mxArray *matlabInt32 ) const
  172. {
  173. if( !mxIsInt32( matlabInt32 ) )
  174. mexErrMsgIdAndTxt("mexnice:error","Expected int32");
  175. int* ptr = (int*) mxGetData( matlabInt32 );
  176. return ptr[0];
  177. }
  178. double ConverterMatlabToNICE::convertMatlabToDouble( const mxArray *matlabDouble ) const
  179. {
  180. if( !mxIsDouble(matlabDouble) )
  181. mexErrMsgIdAndTxt("mexnice:error","Expected double");
  182. double* ptr = (double*) mxGetData( matlabDouble );
  183. return ptr[0];
  184. }
  185. bool ConverterMatlabToNICE::convertMatlabToBool( const mxArray *matlabBool ) const
  186. {
  187. if( !mxIsLogical( matlabBool ) )
  188. mexErrMsgIdAndTxt("mexnice:error","Expected bool");
  189. bool* ptr = (bool*) mxGetData( matlabBool );
  190. return ptr[0];
  191. }