00001
00021 #include "OSMathUtil.h"
00022 #include "OSGeneral.h"
00023
00024
00025 #include <iostream>
00026
00027
00028
00029 MathUtil::MathUtil(){
00030 }
00031
00032 MathUtil::~MathUtil(){
00033 }
00034
00035
00036
00037 SparseMatrix* MathUtil::convertLinearConstraintCoefficientMatrixToTheOtherMajor(
00038 bool isColumnMajor, int startSize, int valueSize, int* start, int* index,
00039 double* value, int dimension){
00040 if(!start || startSize <= 1 ) return NULL;
00041 if(!value || !index ) return NULL;
00042
00043 int iStartSize = dimension + 1;
00044 SparseMatrix *matrix ;
00045 matrix = new SparseMatrix( !isColumnMajor, iStartSize, valueSize);
00046 int i,j, iTemp;
00047 int iNumSource = startSize - 1;
00048 int* miStart = matrix->starts;
00049 int* miIndex = matrix->indexes;
00050 double* mdValue = matrix->values;
00051
00052 for ( i = 0; i < iStartSize; i++){
00053 miStart [ i ] = 0;
00054 }
00055
00056
00057 for (i = 0; i < iNumSource; i++){
00058 for (j = start[i]; j < start[ i + 1 ]; j++){
00059
00060
00061
00062 miStart[ index[j] + 1] ++;
00063 }
00064 }
00065
00066
00067
00068
00069 miStart[0] = 0;
00070 for (i = 1; i < iStartSize; i++ ){
00071 miStart[i] += miStart [i - 1] ;
00072 }
00073
00074
00075
00076
00077 for (i = 0; i < iNumSource; i++){
00078
00079 for (j = start[i]; j < start[ i + 1 ]; j++){
00080 iTemp = miStart[ index[j]];
00081 miIndex [ iTemp] = i;
00082 mdValue [ iTemp] = value[j];
00083 miStart[ index[j]] ++;
00084 }
00085 }
00086
00087
00088 for (i = iStartSize - 1; i >= 1; i-- ){
00089 miStart[i] = miStart [i - 1] ;
00090 }
00091
00092 miStart[0] = 0;
00093 return matrix;
00094 }
00095
00096
00097 double os_strtod_wrap(const char *str, char **strEnd){
00098 #ifndef USE_DTOA
00099 return strtod(str, strEnd);
00100 #else
00101 return os_strtod(str, strEnd);;
00102 #endif
00103 }
00104
00105
00106 std::string os_dtoa_format(double x){
00107 ostringstream outStr;
00108 #ifndef USE_DTOA
00109 outStr << x;
00110 return outStr.str();
00111 #else
00112 outStr << "";
00113 char *charResult;
00114 int decimalPointPos;
00115 int sign;
00116 int strLength = 0;
00117 int k = 0;
00118 charResult = os_dtoa(x, 0, 0, &decimalPointPos, &sign, NULL);
00119
00120
00121 if( sign == 1) outStr << "-";
00122 strLength = strlen( charResult);
00123
00124
00125
00126 if(decimalPointPos == 9999){
00127 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00128 return outStr.str();
00129 }
00130 if(decimalPointPos == strLength){
00131 for(k = 0; k < strLength; k++)outStr << charResult[ k];
00132 return outStr.str();
00133 }
00134 if(decimalPointPos >= 0){
00135 if(decimalPointPos > strLength){
00136 if(strLength == 1){
00137
00138 outStr << charResult[ 0];
00139 if(decimalPointPos <= 5){
00140 for(k = strLength; k < decimalPointPos; k++) outStr << "0";
00141 }else{
00142 outStr << ".";
00143 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00144 outStr << "e";
00145 outStr << decimalPointPos - 1;
00146 }
00147 }else{
00148 outStr << charResult[ 0];
00149 outStr << ".";
00150 for(k = 1; k < strLength; k++) outStr << charResult[ k];
00151 outStr << "e";
00152 outStr << decimalPointPos - 1;
00153 }
00154 }else{
00155 for(k = 0; k < decimalPointPos; k++) outStr << charResult[ k];
00156 outStr << ".";
00157 for(k = decimalPointPos; k < strLength; k++) outStr << charResult[ k];
00158 }
00159 }else{
00160 outStr << charResult[ 0];
00161 outStr << ".";
00162
00163 for(k = 1; k < strLength; k++)outStr << charResult[ k];
00164 outStr << "e";
00165 outStr << decimalPointPos -1 ;
00166 }
00167
00168 os_freedtoa( charResult);
00169 return outStr.str();
00170 #endif
00171 }
00172
00173