00001
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
00055
00056 for (i = 0; i < iNumSource; i++){
00057 for (j = start[i]; j < start[ i + 1 ]; j++){
00058
00059
00060
00061 miStart[ index[j] + 1] ++;
00062 }
00063 }
00064
00065
00066
00067
00068 miStart[0] = 0;
00069 for (i = 1; i < iStartSize; i++ ){
00070 miStart[i] += miStart [i - 1] ;
00071 }
00072
00073
00074
00075
00076 for (i = 0; i < iNumSource; i++){
00077
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
00087 for (i = iStartSize - 1; i >= 1; i-- ){
00088 miStart[i] = miStart [i - 1] ;
00089 }
00090
00091 miStart[0] = 0;
00092 return matrix;
00093 }
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 }
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
00122
00123 if( sign == 1) outStr << "-";
00124 strLength = strlen( charResult);
00125
00126
00127
00128 if(decimalPointPos == 9999){
00129 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00130 return outStr.str();
00131 }
00132 if(decimalPointPos == strLength){
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
00140 outStr << charResult[ 0];
00141 if(decimalPointPos <= 5){
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
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 }
00174
00175