Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

utils.cpp

Go to the documentation of this file.
00001 #include "utils.h"
00002 
00011 valarray<Real> 
00012 mergeunique(valarray<Real> a1, valarray<Real> a2) {
00013         valarray<Real> temp(a1.size() + a2.size());
00014         Real tempmin = -1 * TN_INFINITY;
00015 
00016         Natural a1idx = 0;
00017         Natural a2idx = 0;
00018         Natural tempidx = 0;
00019         bool a1hasmore = (a1idx < a1.size());
00020         bool a2hasmore = (a2idx < a2.size());
00021 
00022         while (a1hasmore || a2hasmore) {
00023                 if (a1hasmore && !a2hasmore) {
00024                         if (a1[a1idx] > tempmin) {
00025                                 tempmin = temp[tempidx++] = a1[a1idx];
00026                         }
00027                         a1idx++;
00028                 } else if (!a1hasmore && a2hasmore) {
00029                         if (a2[a2idx] > tempmin) {
00030                                 tempmin = temp[tempidx++] = a2[a2idx];
00031                         }
00032                         a2idx++;
00033                 } else if (a1[a1idx] < a2[a2idx]) {
00034                         // a1 is less than a2, take from a1
00035                         if (a1[a1idx] > tempmin) {
00036                                 tempmin = temp[tempidx++] = a1[a1idx];
00037                         }
00038                         a1idx++;
00039                 } else if (a1[a1idx] > a2[a2idx]) {
00040                         // a2 is less than a1, take from a2
00041                         if (a2[a2idx] > tempmin) {
00042                                 tempmin = temp[tempidx++] = a2[a2idx];
00043                         }
00044                         a2idx++;
00045                 } else {
00046                         // a1 = a2, take from a1 and advance both pointers
00047                         if (a1[a1idx] > tempmin) {
00048                                 tempmin = temp[tempidx++] = a1[a1idx];
00049                         }
00050                         a1idx++;
00051                         a2idx++;
00052                 }
00053                 a1hasmore = (a1idx < a1.size());
00054                 a2hasmore = (a2idx < a2.size());
00055         }
00056 
00057         temp.resize(tempidx);
00058         return temp;
00059 }
00060 
00061 
00062 short int sign(Real x)
00063 {
00064         if(x>0)
00065                 return 1;
00066         else if(x<0)
00067                 return -1;
00068         else
00069                 return 0;
00070 }
00071 
00072 
00073 //display error message, exit program if necessary
00074 void ErrorMsg(string str, bool mustexit)
00075 {
00076         cout << "Error: " << str << '\n';
00077 
00078         if(mustexit) {
00079                 cout << "Exiting program\n";
00080                 exit(1);
00081         }
00082         else {
00083                 char ch = 'Q';
00084 
00085                 do {
00086                         cout << "Continue program execution? (Y/N): ";
00087                         cin >> ch;
00088                         ch = toupper(ch);
00089                 } while((ch != 'Y') && (ch != 'N'));
00090 
00091                 if(ch == 'N') exit(1);
00092                 if(ch == 'Y') return;        //yes, superfluous, but makes it more undertandable
00093         }
00094 }
00095 
00096 Matrix transform1DvalarrayToColumnMatrix(valarray<Real> array)
00097 {
00098         Natural size=(Natural)array.size();
00099         Matrix res=Matrix(0.0,size,1);
00100         for(Natural i=0;i<size;i++)
00101                 res.SetValue(i,0,array[i]);
00102         return res;
00103 }
00104 
00105 // each valarray is here supposed to be of the same size
00106 // array[i] = the line (in the prupose of arrays of random numbers, [i] is the asset, and the line is the set of numbers
00107 Matrix transform2DvalarrayToMatrix(valarray<valarray<Real> > array)
00108 {
00109         Natural col=(Natural)array.size();
00110         Natural row=(Natural)array[0].size();
00111         Matrix res=Matrix(0.0,row,col);
00112         for(Natural i=0;i<row;i++)
00113                 for(Natural j=0;j<col;j++)
00114                         res.SetValue(i,j,array[i][j]);
00115         return res;
00116 }
00117 
00118 
00119 valarray<Real> transformColumnMatrixTo1Dvalarray(Matrix M)
00120 {
00121         Natural size=M.GetRows();
00122         valarray<Real> res(size);
00123         for(Natural i=0;i<size;i++)
00124                 res[i]=M(i,0);
00125         return res;
00126 }
00127 
00128 valarray<valarray<Real> > transformMatrixTo2Dvalarray(Matrix M)
00129 {
00130         Natural col=M.GetColumns();
00131         Natural row=M.GetRows();
00132         valarray<valarray<Real> > res(row);
00133         for(Natural i=0;i<row;i++){
00134                 res[i].resize(col);
00135                 for(Natural j=0;j<col;j++)
00136                         res[i][j]=M(i,j);
00137         }
00138         return res;
00139 }
00140 
00141 
00142 //get safe int input
00143 int getint(istream& istr)
00144 {
00145         int temp = 0;
00146         string str;
00147 
00148         while(!(istr >> temp)) {
00149                 istr.clear();
00150                 istr >> str;
00151         }
00152 
00153         return temp;
00154 }
00155 
00156 bool 
00157 realsEqual(Real realOne, Real realTwo, Real precision) {
00158         return (fabs(realOne - realTwo) < precision);
00159 }
00160 
00161 string
00162 valarrayRealToString(const valarray<Real> &theArray) {
00163         Natural size = theArray.size();
00164         char realString[10];
00165         string result = "[ ";
00166         for (Natural i = 0; i < size; i++) {
00167                 sprintf(realString, "%5.4f", theArray[i]);
00168                 result = result + realString + " ";
00169         }
00170         sprintf(realString, "%d", size);
00171         result = result + "] size: " + realString;
00172         return result;
00173 }

Note: Generated nightly - reload for latest version
Generated on Thu Dec 22 23:12:37 2005 for terreneuve by doxygen 1.3.6