/home/coin/SVN-release/OS-1.1.1/OS/src/OSUtils/OSMathUtil.cpp

Go to the documentation of this file.
00001 
00020 #include "OSDataStructures.h" 
00021 #include "OSMathUtil.h"
00022 
00023 
00024 #include <iostream>
00025 
00026 
00027 
00028 MathUtil::MathUtil(){
00029 }
00030 
00031 MathUtil::~MathUtil(){
00032 }
00033 
00034 
00035  
00036 SparseMatrix* MathUtil::convertLinearConstraintCoefficientMatrixToTheOtherMajor(
00037         bool isColumnMajor, int startSize, int valueSize, int* start, int* index, 
00038         double* value, int dimension){
00039         if(!start || startSize <= 1 ) return NULL;
00040         if(!value  || !index  ) return NULL;    
00041 
00042         int iStartSize = dimension + 1;
00043         SparseMatrix *matrix ;
00044         matrix = new SparseMatrix( !isColumnMajor, iStartSize, valueSize);              
00045         int i,j, iTemp;
00046         int iNumSource = startSize - 1;
00047         int* miStart = matrix->starts;
00048         int* miIndex = matrix->indexes;
00049         double* mdValue = matrix->values;
00050         
00051         for ( i = 0; i < iStartSize; i++){                      
00052                 miStart [ i ] = 0;
00053         }
00054         // for illustration assume we are converting from column to row major   
00055         // i is indexing columns (variables) and j is indexing row numbers 
00056         for (i = 0; i < iNumSource; i++){       
00057                 for (j = start[i]; j < start[ i + 1 ]; j++){
00058                         // index[ j] is a row index, we have just found an occurance of row index[j]
00059                         // therefore we increase by 1 (or push back) the start of the row indexed by index[j] + 1, 
00060                         // i.e. the start of the next row
00061                         miStart[ index[j] + 1] ++;                              
00062                 }
00063         }
00064         // at this point, miStart[ i] holds the number of columns with a nonzero in row i - 1
00065         // we are not done with the start indicies, if we are here, and we
00066         // knew the correct starting point of row i -1, the correct starting point
00067         // for row i is miStart[i] + miStart [i - 1]
00068         miStart[0] = 0;
00069         for (i = 1; i < iStartSize; i++ ){
00070                 miStart[i] += miStart [i - 1] ;         
00071         }
00072         
00073         // now get the correct values
00074         // again assume we are converting column major to row major
00075         // loop over bariables          
00076         for (i = 0; i < iNumSource; i++){
00077                 // get row indices and values of the A matrix
00078                 for (j = start[i]; j < start[ i + 1 ]; j++){
00079                         iTemp = miStart[ index[j]];
00080                         miIndex [ iTemp] = i;
00081                         mdValue [ iTemp] = value[j];
00082                         miStart[ index[j]] ++;                          
00083                 }                       
00084         } 
00085                 
00086         // miStart[ i] is now equal to miStart[ i + 1], so readjust
00087         for (i = iStartSize - 1; i >= 1; i-- ){
00088                 miStart[i] = miStart [i - 1] ;          
00089         }
00090                 
00091         miStart[0] = 0;
00092         return matrix;          
00093 }//convertLinearConstraintCoefficientMatrixToTheOtherMajor
00094 
00095 
00096 double os_strtod_wrap(const char *str,  char **strEnd){
00097 #ifndef USE_DTOA
00098         return strtod(str,  strEnd);    
00099 #else
00100         return os_strtod(str,  strEnd);;
00101 #endif
00102 }//end os_strtod_wrap
00103 
00104 
00105 std::string os_dtoa_format(double  x){
00106         ostringstream outStr;
00107 #ifndef USE_DTOA
00108         outStr << x;
00109         return outStr.str();
00110 #else
00111         outStr << "";
00112         char *charResult;
00113     int decimalPointPos;
00114     int sign;  
00115     int strLength = 0;
00116     int k = 0;
00117     charResult = os_dtoa(x, 0, 0, &decimalPointPos, &sign, NULL);
00118     // get the length
00119     // get the sign, 1 for negative
00120     if( sign == 1) outStr << "-";
00121     strLength = strlen( charResult);
00122     // return charResult if we have nan or infinity  -- if so, return orginal string
00123     if(decimalPointPos == 9999){
00124         for(k = 0; k < strLength; k++)outStr << charResult[ k];
00125         return outStr.str();            
00126     }
00127     if(decimalPointPos == strLength){ //don't we have an integer?
00128         for(k = 0; k < strLength; k++)outStr << charResult[ k];
00129         return outStr.str();
00130     } 
00131     if(decimalPointPos >= 0){
00132         if(decimalPointPos > strLength){
00133                 // put in all of the characters from charResult
00134                 outStr << charResult[ 0];
00135                 outStr <<  ".";
00136                 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00137                 //for(k = strLength; k < decimalPointPos; k++) outStr <<  "0";
00138                 outStr <<  "e";
00139                 outStr <<  decimalPointPos -  1;
00140         }else{
00141                 for(k = 0; k < decimalPointPos; k++) outStr << charResult[ k];
00142                 outStr <<  ".";
00143                 for(k = decimalPointPos; k < strLength; k++) outStr << charResult[ k];
00144         }
00145     }else{
00146                 outStr << charResult[ 0];
00147                 outStr <<  ".";
00148         //for(k = 0; k < -decimalPointPos; k++) outStr << "0";
00149         for(k = 1; k < strLength; k++)outStr <<  charResult[ k];
00150                 outStr <<  "e";
00151                 outStr <<  decimalPointPos -1 ;
00152     }
00153     //
00154     os_freedtoa( charResult);
00155         return outStr.str();
00156 #endif
00157 }// end os_dtoa_format
00158 
00159 
00160 
00161 

Generated on Tue Sep 30 03:01:25 2008 by  doxygen 1.4.7