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

Go to the documentation of this file.
00001 /* $Id: OSMathUtil.cpp 4292 2011-09-21 05:47:18Z kmartin $ */
00019 #include "OSConfig.h"
00020 #include "OSMathUtil.h"
00021 #include "OSGeneral.h"
00022 
00023 #ifdef HAVE_CSTDLIB
00024 # include <cstdlib>
00025 #else
00026 # ifdef HAVE_STDLIB_H
00027 #  include <stdlib.h>
00028 # endif
00029 #endif
00030 
00031 #ifdef HAVE_CSTRING
00032 # include <cstring>
00033 #else
00034 # ifdef HAVE_STRING_H
00035 #  include <string.h>
00036 # else
00037 #  error "don't have header file for string"
00038 # endif
00039 #endif
00040 
00041 #include <iostream>
00042 #include <sstream>
00043 
00044 using std::ostringstream;
00045 
00046 
00047 MathUtil::MathUtil()
00048 {
00049 }
00050 
00051 MathUtil::~MathUtil()
00052 {
00053 }
00054 
00055 
00056 
00057 SparseMatrix* MathUtil::convertLinearConstraintCoefficientMatrixToTheOtherMajor(
00058     bool isColumnMajor, int startSize, int valueSize, int* start, int* index,
00059     double* value, int dimension)
00060 {
00061     if(!start || startSize <= 1 ) return NULL;
00062     if(!value  || !index  ) return NULL;
00063 
00064     int iStartSize = dimension + 1;
00065     SparseMatrix *matrix ;
00066     matrix = new SparseMatrix( !isColumnMajor, iStartSize, valueSize);
00067     int i,j, iTemp;
00068     int iNumSource = startSize - 1;
00069     int* miStart = matrix->starts;
00070     int* miIndex = matrix->indexes;
00071     double* mdValue = matrix->values;
00072 
00073     for ( i = 0; i < iStartSize; i++)
00074     {
00075         miStart [ i ] = 0;
00076     }
00077     // for illustration assume we are converting from column to row major
00078     // i is indexing columns (variables) and j is indexing row numbers
00079     for (i = 0; i < iNumSource; i++)
00080     {
00081         for (j = start[i]; j < start[ i + 1 ]; j++)
00082         {
00083             // index[ j] is a row index, we have just found an occurance of row index[j]
00084             // therefore we increase by 1 (or push back) the start of the row indexed by index[j] + 1,
00085             // i.e. the start of the next row
00086             miStart[ index[j] + 1] ++;
00087         }
00088     }
00089     // at this point, miStart[ i] holds the number of columns with a nonzero in row i - 1
00090     // we are not done with the start indices, if we are here, and we
00091     // knew the correct starting point of row i -1, the correct starting point
00092     // for row i is miStart[i] + miStart [i - 1]
00093     miStart[0] = 0;
00094     for (i = 1; i < iStartSize; i++ )
00095     {
00096         miStart[i] += miStart [i - 1] ;
00097     }
00098 
00099     // now get the correct values
00100     // again assume we are converting column major to row major
00101     // loop over columns
00102     for (i = 0; i < iNumSource; i++)
00103     {
00104         // get row indices and values of the A matrix
00105         for (j = start[i]; j < start[ i + 1 ]; j++)
00106         {
00107             iTemp = miStart[ index[j]];
00108             miIndex [ iTemp] = i;
00109             mdValue [ iTemp] = value[j];
00110             miStart[ index[j]] ++;
00111         }
00112     }
00113 
00114     // miStart[ i] is now equal to miStart[ i + 1], so readjust
00115     for (i = iStartSize - 1; i >= 1; i-- )
00116     {
00117         miStart[i] = miStart [i - 1] ;
00118     }
00119 
00120     miStart[0] = 0;
00121     return matrix;
00122 }//convertLinearConstraintCoefficientMatrixToTheOtherMajor
00123 
00124 
00125 double os_strtod_wrap(const char *str,  char **strEnd)
00126 {
00127 #ifndef USE_DTOA
00128     return    strtod(str, strEnd);
00129 #else
00130     return os_strtod(str, strEnd);
00131 #endif
00132 }//end os_strtod_wrap
00133 
00134 
00135 std::string os_dtoa_format(double  x)
00136 {
00137     ostringstream outStr;
00138     if (x ==  OSDBL_MAX)
00139     {
00140         outStr <<  "INF";
00141         return outStr.str();
00142     }
00143     if (x == -OSDBL_MAX)
00144     {
00145         outStr << "-INF";
00146         return outStr.str();
00147     }
00148     if ( OSIsnan(x) )
00149     {
00150         outStr <<  "NaN";
00151         return outStr.str();
00152     }
00153 #ifndef USE_DTOA
00154     outStr << x;
00155     return outStr.str();
00156 #else
00157     outStr << "";
00158     char *charResult;
00159     int decimalPointPos;
00160     int sign;
00161     int strLength = 0;
00162     int k = 0;
00163     charResult = os_dtoa(x, 0, 0, &decimalPointPos, &sign, NULL);
00164     // get the length
00165     // get the sign, 1 for negative
00166     if( sign == 1) outStr << "-";
00167     strLength = strlen( charResult);
00168 
00169 
00170     // return charResult if we have nan or infinity  -- if so, return orginal string
00171     if(decimalPointPos == 9999)
00172     {
00173         for(k = 0; k < strLength; k++)outStr << charResult[ k];
00174         return outStr.str();
00175     }
00176     if(decimalPointPos == strLength)  //don't we have an integer?
00177     {
00178         for(k = 0; k < strLength; k++)outStr << charResult[ k];
00179         return outStr.str();
00180     }
00181     if(decimalPointPos >= 0)
00182     {
00183         if(decimalPointPos > strLength)
00184         {
00185             if(strLength == 1)
00186             {
00187                 // put in all of the characters from charResult
00188                 outStr << charResult[ 0];
00189                 if(decimalPointPos <= 5)  //hey for than 5 zeros go for e notataion
00190                 {
00191                     for(k = strLength; k < decimalPointPos; k++) outStr <<  "0";
00192                 }
00193                 else
00194                 {
00195                     outStr <<  ".";
00196                     for(k = 1; k < strLength; k++) outStr << charResult[ k];
00197                     outStr <<  "e";
00198                     outStr <<  decimalPointPos -  1;
00199                 }
00200             }
00201             else
00202             {
00203                 outStr << charResult[ 0];
00204                 outStr <<  ".";
00205                 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00206                 outStr <<  "e";
00207                 outStr <<  decimalPointPos -  1;
00208             }
00209         }
00210         else
00211         {
00212             for(k = 0; k < decimalPointPos; k++) outStr << charResult[ k];
00213             outStr <<  ".";
00214             for(k = decimalPointPos; k < strLength; k++) outStr << charResult[ k];
00215         }
00216     }
00217     else
00218     {
00219         outStr << charResult[ 0];
00220         outStr <<  ".";
00221         //for(k = 0; k < -decimalPointPos; k++) outStr << "0";
00222         for(k = 1; k < strLength; k++)outStr <<  charResult[ k];
00223         outStr <<  "e";
00224         outStr <<  decimalPointPos -1 ;
00225     }
00226     //
00227     os_freedtoa( charResult);
00228     return outStr.str();
00229 #endif
00230 }// end os_dtoa_format
00231 
00232 
00240 double OSRand()
00241 {
00242     int i;
00243 
00244     i = rand();
00245 
00246     return (double) i/RAND_MAX;
00247 }
00248 
00249 
00257 double OSiRand(int iMin, int iMax)
00258 {
00259     return iMin + rand()%(iMax - iMin + 1);
00260 }

Generated on Thu Sep 22 03:06:04 2011 by  doxygen 1.4.7