ConverterMatlabToNICE.cpp 6.7 KB

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