ConverterMatlabToNICE.cpp 6.6 KB

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