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

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

Generated on Sun Jan 2 03:04:48 2011 by  doxygen 1.4.7