/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/src/OSUtils/OSMathUtil.cpp

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

Generated on Sat Mar 29 22:38:03 2008 by  doxygen 1.5.3