00001
00019 #include "OSConfig.h"
00020 #include "OSMathUtil.h"
00021 #include "OSParameters.h"
00022 #include "OSGeneral.h"
00023
00024 #ifdef HAVE_CSTDLIB
00025 # include <cstdlib>
00026 #else
00027 # ifdef HAVE_STDLIB_H
00028 # include <stdlib.h>
00029 # endif
00030 #endif
00031
00032 #ifdef HAVE_CSTRING
00033 # include <cstring>
00034 #else
00035 # ifdef HAVE_STRING_H
00036 # include <string.h>
00037 # else
00038 # error "don't have header file for string"
00039 # endif
00040 #endif
00041
00042 #include <iostream>
00043 #include <sstream>
00044
00045 using std::ostringstream;
00046
00047
00048 MathUtil::MathUtil()
00049 {
00050 }
00051
00052 MathUtil::~MathUtil()
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
00078
00079 for (i = 0; i < iNumSource; i++)
00080 {
00081 for (j = start[i]; j < start[ i + 1 ]; j++)
00082 {
00083
00084
00085
00086 miStart[ index[j] + 1] ++;
00087 }
00088 }
00089
00090
00091
00092
00093 miStart[0] = 0;
00094 for (i = 1; i < iStartSize; i++ )
00095 {
00096 miStart[i] += miStart [i - 1] ;
00097 }
00098
00099
00100
00101
00102 for (i = 0; i < iNumSource; i++)
00103 {
00104
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
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 }
00123
00124
00125 double os_strtod_wrap(const char *str, char **strEnd)
00126 {
00127
00128
00129
00130 if (strcmp(str, "INF") == 0)
00131 {
00132 *strEnd = (char *)str + 3;
00133 return OSDBL_MAX;
00134 }
00135 else if (strcmp(str,"-INF") == 0)
00136 {
00137 *strEnd = (char *)str + 4;
00138 return -OSDBL_MAX;
00139 }
00140 else if (strcmp(str, "NaN") == 0)
00141 {
00142 *strEnd = (char *)str + 3;
00143 return OSNaN();
00144 }
00145 else
00146 #ifndef USE_DTOA
00147 return strtod(str, strEnd);
00148 #else
00149 return os_strtod(str, strEnd);
00150 #endif
00151 }
00152
00153
00154 std::string os_dtoa_format(double x)
00155 {
00156
00157
00158
00159 ostringstream outStr;
00160 if (x == OSDBL_MAX)
00161 {
00162 outStr << "INF";
00163 return outStr.str();
00164 }
00165 if (x == -OSDBL_MAX)
00166 {
00167 outStr << "-INF";
00168 return outStr.str();
00169 }
00170 if ( OSIsnan(x) )
00171 {
00172 outStr << "NaN";
00173 return outStr.str();
00174 }
00175 #ifndef USE_DTOA
00176 outStr << x;
00177 return outStr.str();
00178 #else
00179 outStr << "";
00180 char *charResult;
00181 int decimalPointPos;
00182 int sign;
00183 int strLength = 0;
00184 int k = 0;
00185 charResult = os_dtoa(x, 0, 0, &decimalPointPos, &sign, NULL);
00186
00187
00188 if( sign == 1) outStr << "-";
00189 strLength = strlen( charResult);
00190
00191
00192
00193 if(decimalPointPos == 9999)
00194 {
00195 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00196 return outStr.str();
00197 }
00198 if(decimalPointPos == strLength)
00199 {
00200 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00201 return outStr.str();
00202 }
00203 if(decimalPointPos >= 0)
00204 {
00205 if(decimalPointPos > strLength)
00206 {
00207 if(strLength == 1)
00208 {
00209
00210 outStr << charResult[ 0];
00211 if(decimalPointPos <= 5)
00212 {
00213 for(k = strLength; k < decimalPointPos; k++) outStr << "0";
00214 }
00215 else
00216 {
00217 outStr << ".";
00218 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00219 outStr << "e";
00220 outStr << decimalPointPos - 1;
00221 }
00222 }
00223 else
00224 {
00225 outStr << charResult[ 0];
00226 outStr << ".";
00227 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00228 outStr << "e";
00229 outStr << decimalPointPos - 1;
00230 }
00231 }
00232 else
00233 {
00234 for(k = 0; k < decimalPointPos; k++) outStr << charResult[ k];
00235 outStr << ".";
00236 for(k = decimalPointPos; k < strLength; k++) outStr << charResult[ k];
00237 }
00238 }
00239 else
00240 {
00241 outStr << charResult[ 0];
00242 outStr << ".";
00243
00244 for(k = 1; k < strLength; k++)outStr << charResult[ k];
00245 outStr << "e";
00246 outStr << decimalPointPos -1 ;
00247 }
00248
00249 os_freedtoa( charResult);
00250 return outStr.str();
00251 #endif
00252 }
00253
00254
00262 double OSRand()
00263 {
00264 int i;
00265
00266 i = rand();
00267
00268 return (double) i/RAND_MAX;
00269 }
00270
00271
00279 double OSiRand(int iMin, int iMax)
00280 {
00281 return iMin + rand()%(iMax - iMin + 1);
00282 }
00283
00284