00001
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
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 #ifndef USE_DTOA
00128 return strtod(str, strEnd);
00129 #else
00130 return os_strtod(str, strEnd);
00131 #endif
00132 }
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
00165
00166 if( sign == 1) outStr << "-";
00167 strLength = strlen( charResult);
00168
00169
00170
00171 if(decimalPointPos == 9999)
00172 {
00173 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00174 return outStr.str();
00175 }
00176 if(decimalPointPos == strLength)
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
00188 outStr << charResult[ 0];
00189 if(decimalPointPos <= 5)
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
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 }
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 }