utilities.h

Go to the documentation of this file.
00001 /***************************************************************************/
00020 #ifndef AURESERVOIR_UTILITIES_H__
00021 #define AURESERVOIR_UTILITIES_H__
00022 
00023 // external includes
00024 #include <flens/flens.h>
00025 #include <string>
00026 #include <ctime>
00027 #include <iostream>
00028 #include <iomanip>
00029 #include <sstream>
00030 #include <cstdlib>
00031 // #include <complex>
00032 
00033 #include "auexcept.h"
00034 #include "denormal.h"
00035 
00036 namespace aureservoir
00037 {
00038 
00039 using std::string;
00040 using std::complex;
00041 using flens::_;
00042 
00044 template<typename T = float>
00045 struct SPMatrix
00046 {
00047   typedef flens::SparseGeMatrix<flens::CRS<T> > Type;
00048 };
00049 
00051 template<typename T = float>
00052 struct DEMatrix
00053 {
00054   typedef flens::GeMatrix<flens::FullStorage<T, flens::ColMajor> > Type;
00055 };
00056 
00058 template<typename T = float>
00059 struct DEVector
00060 {
00061   typedef flens::DenseVector<flens::Array<T> > Type;
00062 };
00063 
00065 template<typename T = float>
00066 struct CDEVector
00067 {
00068   typedef flens::DenseVector<flens::Array< complex<T> > > Type;
00069 };
00070 
00071 
00082 template <typename T=float>
00083 class Rand
00084 {
00085  public:
00086 
00088   static void initSeed()
00089   { srand(time(0)); }
00090 
00097   static T uniform(float min=-1, float max=1)
00098   {
00099    T tmp = std::rand() / (T(RAND_MAX)+1); // between [0|1)
00100    return tmp*(max-min) + min;
00101   }
00102 
00109   static void uniform(typename DEVector<T>::Type &vec, float min=-1, float max=1)
00110   {
00111     for(int i=0; i<vec.length(); ++i)
00112       vec.data()[i] = std::rand() / (T(RAND_MAX)+1); // between [0|1)
00113 
00114     vec *= (max-min);
00115     vec += min;
00116   }
00117 };
00118 
00120 // template<>
00121 // static dcplx Rand<dcplx>::uniform(float min, float max)
00122 // {
00123 //   double real = std::rand() / (double(RAND_MAX)+1);
00124 //   double imag = std::rand() / (double(RAND_MAX)+1);
00125 //   dcplx tmp(real,imag);
00126 //   return tmp*(dcplx(max)-dcplx(min)) + dcplx(min);
00127 // }
00128 
00129 
00134 inline double stringToDouble(const string& s)
00135   throw(AUExcept)
00136 {
00137   std::istringstream is(s);
00138   double val;
00139   char c;
00140 
00141   // throw error if conversion is wrong or if we still have values
00142   if( !(is >> val) || is.get(c) )
00143     throw AUExcept("stringToDouble: could not convert parameter: " + s);
00144 
00145   return val;
00146 }
00147 
00152 inline int stringToInt(const string& s)
00153   throw(AUExcept)
00154 {
00155   std::istringstream is(s);
00156   int val;
00157   char c;
00158 
00159   // throw error if conversion is wrong or if we still have values
00160   if( !(is >> val) || is.get(c) )
00161     throw AUExcept("stringToInt: could not convert parameter: " + s);
00162 
00163   return val;
00164 }
00165 
00166 } // end of namespace aureservoir
00167 
00168 #endif // AURESERVOIR_UTILITIES_H__

Generated on Wed Mar 12 21:16:05 2008 for aureservoir by  doxygen 1.5.3