- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

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

Go to the documentation of this file.

    00001 /* $Id: OSMathUtil.cpp 4120 2011-03-30 06:28:16Z kmartin $ */
    00019 #include "OSMathUtil.h"
    00020 #include "OSGeneral.h"
    00021
    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 indices, 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 columns
    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         if (x ==  OSDBL_MAX) {outStr <<  "INF"; return outStr.str();}
    00108         if (x == -OSDBL_MAX) {outStr << "-INF"; return outStr.str();}
    00109         if ( OSIsnan(x) )    {outStr <<  "NaN"; return outStr.str();}
    00110 #ifndef USE_DTOA
    00111         outStr << x;
    00112         return outStr.str();
    00113 #else
    00114         outStr << "";
    00115         char *charResult;
    00116     int decimalPointPos;
    00117     int sign;
    00118     int strLength = 0;
    00119     int k = 0;
    00120     charResult = os_dtoa(x, 0, 0, &decimalPointPos, &sign, NULL);
    00121     // get the length
    00122     // get the sign, 1 for negative
    00123     if( sign == 1) outStr << "-";
    00124     strLength = strlen( charResult);
    00125
    00126
    00127     // return charResult if we have nan or infinity  -- if so, return orginal string
    00128     if(decimalPointPos == 9999){
    00129         for(k = 0; k < strLength; k++)outStr << charResult[ k];
    00130         return outStr.str();
    00131     }
    00132     if(decimalPointPos == strLength){ //don't we have an integer?
    00133         for(k = 0; k < strLength; k++)outStr << charResult[ k];
    00134         return outStr.str();
    00135     }
    00136     if(decimalPointPos >= 0){
    00137         if(decimalPointPos > strLength){
    00138                         if(strLength == 1){
    00139                                 // put in all of the characters from charResult
    00140                                 outStr << charResult[ 0];
    00141                                 if(decimalPointPos <= 5){ //hey for than 5 zeros go for e notataion
    00142                                         for(k = strLength; k < decimalPointPos; k++) outStr <<  "0";
    00143                                 }else{
    00144                                         outStr <<  ".";
    00145                                         for(k = 1; k < strLength; k++) outStr << charResult[ k];
    00146                                         outStr <<  "e";
    00147                                         outStr <<  decimalPointPos -  1;
    00148                                 }
    00149                         }else{
    00150                                 outStr << charResult[ 0];
    00151                                 outStr <<  ".";
    00152                                 for(k = 1; k < strLength; k++) outStr << charResult[ k];
    00153                                 outStr <<  "e";
    00154                                 outStr <<  decimalPointPos -  1;
    00155                         }
    00156         }else{
    00157                 for(k = 0; k < decimalPointPos; k++) outStr << charResult[ k];
    00158                 outStr <<  ".";
    00159                 for(k = decimalPointPos; k < strLength; k++) outStr << charResult[ k];
    00160         }
    00161     }else{
    00162                 outStr << charResult[ 0];
    00163                 outStr <<  ".";
    00164         //for(k = 0; k < -decimalPointPos; k++) outStr << "0";
    00165         for(k = 1; k < strLength; k++)outStr <<  charResult[ k];
    00166                 outStr <<  "e";
    00167                 outStr <<  decimalPointPos -1 ;
    00168     }
    00169     //
    00170     os_freedtoa( charResult);
    00171         return outStr.str();
    00172 #endif
    00173 }// end os_dtoa_format
    00174
    00175