| [785] | 1 | //     Utility classes for template numerical arrays
 | 
|---|
 | 2 | //                     R. Ansari, C.Magneville   03/2000
 | 
|---|
 | 3 | 
 | 
|---|
| [2615] | 4 | #include "sopnamsp.h"
 | 
|---|
| [785] | 5 | #include "machdefs.h"
 | 
|---|
 | 6 | #include "utilarr.h"
 | 
|---|
| [850] | 7 | #include "srandgen.h"
 | 
|---|
| [785] | 8 | 
 | 
|---|
 | 9 | // Classe utilitaires
 | 
|---|
| [850] | 10 | 
 | 
|---|
| [1103] | 11 | Sequence::~Sequence()
 | 
|---|
 | 12 | {
 | 
|---|
 | 13 | }
 | 
|---|
 | 14 | 
 | 
|---|
| [894] | 15 | //////////////////////////////////////////////////////////
 | 
|---|
| [926] | 16 | /*!
 | 
|---|
 | 17 |   \class SOPHYA::RandomSequence
 | 
|---|
 | 18 |   \ingroup TArray
 | 
|---|
| [1404] | 19 |   Base class to generate a sequence of random values
 | 
|---|
| [926] | 20 | */
 | 
|---|
 | 21 | 
 | 
|---|
| [894] | 22 | //! Constructor
 | 
|---|
 | 23 | /*!
 | 
|---|
 | 24 |   \param typ : generator type
 | 
|---|
 | 25 |   \param m : mean parameter of the generator (if needed)
 | 
|---|
 | 26 |   \param s : sigma parameter of the generator (if needed)
 | 
|---|
 | 27 |  */
 | 
|---|
| [850] | 28 | RandomSequence::RandomSequence(int typ, double m, double s)
 | 
|---|
 | 29 | {
 | 
|---|
 | 30 |   typ_ = (typ == Flat) ? Flat : Gaussian;
 | 
|---|
 | 31 |   mean_ = m;
 | 
|---|
 | 32 |   sig_ = s;
 | 
|---|
 | 33 | }
 | 
|---|
| [1103] | 34 | RandomSequence::~RandomSequence()
 | 
|---|
 | 35 | {
 | 
|---|
 | 36 | }
 | 
|---|
| [850] | 37 | 
 | 
|---|
| [894] | 38 | //! Return random sequence values.
 | 
|---|
| [935] | 39 | /*!
 | 
|---|
 | 40 |   \return If typ = Flat : return [-1,+1]*sig + mean
 | 
|---|
 | 41 |   \return If typ = Gaussian : return gaussian distributed
 | 
|---|
 | 42 |                           with \b mean mean and sigma \b sig
 | 
|---|
 | 43 |  */
 | 
|---|
| [850] | 44 | double RandomSequence::Rand()
 | 
|---|
 | 45 | {
 | 
|---|
 | 46 |   if (typ_ == Flat) 
 | 
|---|
 | 47 |     return(drandpm1()*sig_ + mean_);
 | 
|---|
 | 48 |   else return(GauRnd(mean_, sig_));
 | 
|---|
 | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
| [1156] | 51 | MuTyV & RandomSequence::Value(sa_size_t k) const 
 | 
|---|
| [1103] | 52 | {
 | 
|---|
 | 53 |   if (typ_ == Flat) retv_ = drandpm1()*sig_ + mean_;
 | 
|---|
 | 54 |   else retv_ = GauRnd(mean_, sig_);
 | 
|---|
 | 55 |   return retv_;
 | 
|---|
 | 56 | }
 | 
|---|
| [850] | 57 | 
 | 
|---|
| [1103] | 58 | 
 | 
|---|
| [894] | 59 | //////////////////////////////////////////////////////////
 | 
|---|
| [926] | 60 | /*!
 | 
|---|
| [1103] | 61 |   \class SOPHYA::RegularSequence
 | 
|---|
| [926] | 62 |   \ingroup TArray
 | 
|---|
 | 63 |   Class to generate a sequence of values
 | 
|---|
 | 64 | */
 | 
|---|
 | 65 | 
 | 
|---|
| [894] | 66 | //! Constructor
 | 
|---|
 | 67 | /*!
 | 
|---|
 | 68 |   \param start : start value
 | 
|---|
 | 69 |   \param step : step value
 | 
|---|
| [1404] | 70 |   \param f : pointer to the sequence function (default = NULL, f(x)=x )
 | 
|---|
| [894] | 71 | 
 | 
|---|
| [1103] | 72 |   See \ref RegularSequenceOperat "operator()"
 | 
|---|
| [894] | 73 |  */
 | 
|---|
| [1103] | 74 | RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
 | 
|---|
| [785] | 75 | {
 | 
|---|
 | 76 |   start_ = start;
 | 
|---|
 | 77 |   step_ = step;
 | 
|---|
 | 78 |   myf_ = f;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
| [1103] | 81 | RegularSequence::~RegularSequence()
 | 
|---|
| [850] | 82 | {
 | 
|---|
 | 83 | }
 | 
|---|
 | 84 | 
 | 
|---|
| [894] | 85 | //! Get the \b k th value
 | 
|---|
 | 86 | /*!
 | 
|---|
 | 87 |   \param k : index of the value
 | 
|---|
| [1103] | 88 |   \anchor RegularSequenceOperat
 | 
|---|
| [1404] | 89 |   
 | 
|---|
 | 90 |   \return f(start+k*step)
 | 
|---|
| [894] | 91 | 
 | 
|---|
 | 92 |  */
 | 
|---|
| [1103] | 93 | 
 | 
|---|
| [1156] | 94 | MuTyV & RegularSequence::Value (sa_size_t k) const 
 | 
|---|
| [785] | 95 | {
 | 
|---|
| [1103] | 96 |   double x = start_+(double)k*step_;
 | 
|---|
 | 97 |   if (myf_)  x = myf_(x);
 | 
|---|
 | 98 |   retv_ = x;
 | 
|---|
 | 99 |   return(retv_);
 | 
|---|
| [785] | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [1404] | 102 | /*!
 | 
|---|
 | 103 |   \class SOPHYA::EnumeratedSequence
 | 
|---|
 | 104 |   \ingroup TArray
 | 
|---|
 | 105 |   Explicitly defined sequence of values. The comma operator has
 | 
|---|
 | 106 |   been redefined to let an easy definition of sequences.
 | 
|---|
 | 107 |   
 | 
|---|
 | 108 |   \code
 | 
|---|
 | 109 |   // Initializing a sequence 
 | 
|---|
 | 110 |   EnumeratedSequence es;
 | 
|---|
 | 111 |   es = 11, 22, 33, 44, 55, 66;
 | 
|---|
 | 112 |   
 | 
|---|
 | 113 |   for(int k=0; k<8; k++) 
 | 
|---|
 | 114 |     cout << " k= " << k << " es(k)= " << es(k) << endl; 
 | 
|---|
| [1558] | 115 | 
 | 
|---|
 | 116 |   // Decoding a sequence from a string
 | 
|---|
 | 117 |   EnumeratedSequence ess;
 | 
|---|
 | 118 |   int nbad;
 | 
|---|
 | 119 |   ess.Append("56.5 (1.,-1.) 4 8 16", nbad);
 | 
|---|
 | 120 |   cout << ess;
 | 
|---|
| [1404] | 121 |   \endcode
 | 
|---|
 | 122 | */
 | 
|---|
 | 123 | 
 | 
|---|
| [1558] | 124 | //! Default constructor
 | 
|---|
| [1404] | 125 | EnumeratedSequence::EnumeratedSequence()
 | 
|---|
 | 126 | {
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [1558] | 129 | //! Copy constructor
 | 
|---|
 | 130 | EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es)
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |   Append(es);
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
| [1103] | 135 | EnumeratedSequence::~EnumeratedSequence()
 | 
|---|
 | 136 | {
 | 
|---|
 | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
| [1404] | 139 | //! Return the k th value in the sequence (default = 0)
 | 
|---|
| [1156] | 140 | MuTyV & EnumeratedSequence::Value (sa_size_t k) const 
 | 
|---|
| [1103] | 141 | {
 | 
|---|
 | 142 |   if (k >= vecv_.size())  retv_ = 0;
 | 
|---|
 | 143 |   else retv_ = vecv_[k];
 | 
|---|
 | 144 |   return(retv_);
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
| [1558] | 147 | //! Appends a new value to the sequence
 | 
|---|
| [1103] | 148 | EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
 | 
|---|
 | 149 | {
 | 
|---|
 | 150 |   vecv_.push_back(v);
 | 
|---|
 | 151 |   return(*this);
 | 
|---|
 | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [1558] | 154 | //! Initialize the sequence with a single value \b v
 | 
|---|
| [1156] | 155 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
 | 
|---|
 | 156 | {
 | 
|---|
 | 157 |   vecv_.clear();
 | 
|---|
 | 158 |   vecv_.push_back(v);
 | 
|---|
 | 159 |   return(*this);
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
| [1558] | 162 | //! Copy operator
 | 
|---|
 | 163 | EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq)
 | 
|---|
| [1550] | 164 | {
 | 
|---|
| [1558] | 165 |   Clear();
 | 
|---|
 | 166 |   Append(seq);
 | 
|---|
 | 167 |   return(*this);  
 | 
|---|
| [1550] | 168 | }
 | 
|---|
 | 169 | 
 | 
|---|
| [1558] | 170 | 
 | 
|---|
 | 171 | //! Prints the list to the output stream \b os
 | 
|---|
| [1550] | 172 | void EnumeratedSequence::Print(ostream& os) const 
 | 
|---|
 | 173 | {
 | 
|---|
 | 174 |   os << " EnumeratedSequence::Print() - Size()= " << Size() << endl;
 | 
|---|
 | 175 |   for(int k=0; k<vecv_.size(); k++) {
 | 
|---|
 | 176 |     os << vecv_[k];
 | 
|---|
 | 177 |     if ((k > 0) && (k%10 == 0))  os << endl;
 | 
|---|
 | 178 |     else os << " " ;
 | 
|---|
 | 179 |   }
 | 
|---|
| [1558] | 180 |   os << endl;
 | 
|---|
| [1550] | 181 |   return;
 | 
|---|
 | 182 | }
 | 
|---|
 | 183 | 
 | 
|---|
| [1558] | 184 | //! Append the \b seq to the end of the sequence.
 | 
|---|
 | 185 | /*!
 | 
|---|
 | 186 |   \return the number of added elements.
 | 
|---|
 | 187 | */ 
 | 
|---|
 | 188 | sa_size_t EnumeratedSequence::Append(EnumeratedSequence const & seq)
 | 
|---|
| [1550] | 189 | {
 | 
|---|
| [1558] | 190 |   for(int k=0; k<seq.vecv_.size(); k++) 
 | 
|---|
 | 191 |     vecv_.push_back(seq.vecv_[k]);
 | 
|---|
 | 192 |   return(seq.vecv_.size());
 | 
|---|
| [1550] | 193 | }
 | 
|---|
 | 194 | 
 | 
|---|
| [1558] | 195 | //! Decodes the string, appending values to the end of the sequence.
 | 
|---|
 | 196 | /*!
 | 
|---|
 | 197 |   \param str : string to be decoded
 | 
|---|
| [2286] | 198 |   \param nbad : number of unmatched quotes or parenthesis (returned value)
 | 
|---|
 | 199 |   \param sep : word separator in string. Each word is decoded as a MuTyV value.
 | 
|---|
| [1558] | 200 |   \return the number of added elements.
 | 
|---|
 | 201 | */ 
 | 
|---|
| [2286] | 202 | sa_size_t EnumeratedSequence::Append(string const & str, int& nbad, const char* sep)
 | 
|---|
| [1558] | 203 | {
 | 
|---|
 | 204 |   nbad = 0;
 | 
|---|
 | 205 |   sa_size_t n = 0;
 | 
|---|
 | 206 |   size_t l = str.length();
 | 
|---|
 | 207 |   if (l < 1) return(0);
 | 
|---|
| [2286] | 208 |   //  if ((str[0] == '#') || (str[0] == '*')) return(0);
 | 
|---|
| [1558] | 209 |   size_t q = 0;
 | 
|---|
| [2286] | 210 |   size_t p = 0;
 | 
|---|
 | 211 |   /*
 | 
|---|
 | 212 |   size_t p = str.find_first_not_of(sep);
 | 
|---|
| [1558] | 213 |   if ((str[p] == '+') || (str[p] == '-')) {
 | 
|---|
 | 214 |     if (p == l-1)  return(0);
 | 
|---|
 | 215 |     if (!isdigit(str[p+1])) return(0);
 | 
|---|
 | 216 |   }
 | 
|---|
 | 217 |   else if (!isdigit(str[p]) && (str[p] != '\'') && (str[p] != '(') ) return(0);
 | 
|---|
| [2286] | 218 |   */
 | 
|---|
| [1558] | 219 |   while(q < l) {
 | 
|---|
| [2286] | 220 |     p = str.find_first_not_of(sep,q);
 | 
|---|
| [1558] | 221 |     if (p >= l)  break;
 | 
|---|
 | 222 |     if (str[p] == '\'')    {  // Decodage d'un string
 | 
|---|
 | 223 |       q = str.find('\'',p+1);
 | 
|---|
 | 224 |       if (q < l) {
 | 
|---|
 | 225 |         vecv_.push_back(MuTyV(str.substr(p+1,q-p-1)));
 | 
|---|
 | 226 |         n++;    q++;
 | 
|---|
 | 227 |       }
 | 
|---|
 | 228 |       else nbad++;
 | 
|---|
 | 229 |     }
 | 
|---|
 | 230 |     else if (str[p] == '(')    {  // Decodage d'un complex
 | 
|---|
 | 231 |       q = str.find(')',p);
 | 
|---|
 | 232 |       if (q < l) {
 | 
|---|
 | 233 |         q++;
 | 
|---|
 | 234 |         MuTyV mtv(str.substr(p,q-p));
 | 
|---|
| [2149] | 235 |         complex<r_8> z = mtv.operator complex<r_8>();
 | 
|---|
| [1558] | 236 |         vecv_.push_back(MuTyV(z));
 | 
|---|
 | 237 |         n++;
 | 
|---|
 | 238 |       }
 | 
|---|
 | 239 |       else nbad++;      
 | 
|---|
 | 240 |     }
 | 
|---|
 | 241 |     else { 
 | 
|---|
| [2286] | 242 |       q = str.find_first_of(sep,p);
 | 
|---|
| [2288] | 243 |       if ( !isdigit(str[p]) && !(str[p] == '+')
 | 
|---|
 | 244 |         && !(str[p] == '-') && !(str[p] == '.') ) { // une chaine
 | 
|---|
| [2286] | 245 |         vecv_.push_back(MuTyV(str.substr(p,q-p)));
 | 
|---|
| [2288] | 246 |         n++;
 | 
|---|
| [1558] | 247 |       }
 | 
|---|
 | 248 |       else {  // C'est un nombre 
 | 
|---|
| [2288] | 249 |         if (str.find_first_of(".eE",p) < q)   { // c'est un flottant
 | 
|---|
| [1558] | 250 |           r_8 x = atof(str.substr(p,q-p).c_str());
 | 
|---|
 | 251 |           vecv_.push_back(MuTyV(x));
 | 
|---|
 | 252 |         }
 | 
|---|
 | 253 |         else {   // un entier
 | 
|---|
 | 254 |           int_8 l = atol(str.substr(p,q-p).c_str());
 | 
|---|
 | 255 |           vecv_.push_back(MuTyV(l));
 | 
|---|
 | 256 |         }
 | 
|---|
 | 257 |         n++;
 | 
|---|
 | 258 |       }
 | 
|---|
 | 259 |     }
 | 
|---|
 | 260 |   }
 | 
|---|
 | 261 |   return (n);
 | 
|---|
 | 262 | }
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 | //! Decodes the input ASCII stream, creating a sequence of values
 | 
|---|
 | 265 | /*! \param is : Input ASCII stream
 | 
|---|
 | 266 |     \param nr : Number of non empty (or comment) lines in stream (return value)
 | 
|---|
 | 267 |     \param nc : Number of columns (= ntot/nlines) (return value)
 | 
|---|
| [2286] | 268 |     \param clm : Lines starting with clm character are treated as comment lines
 | 
|---|
 | 269 |     \param sep : word separator in lines
 | 
|---|
| [1558] | 270 |     \return Number of decoded elements
 | 
|---|
 | 271 | */
 | 
|---|
| [2286] | 272 | sa_size_t EnumeratedSequence::FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc,
 | 
|---|
 | 273 |                                            char clm, const char* sep)
 | 
|---|
| [1550] | 274 | {
 | 
|---|
| [1558] | 275 |   nr = 0;
 | 
|---|
 | 276 |   nc = 0;
 | 
|---|
 | 277 |   sa_size_t n = 0;
 | 
|---|
 | 278 |   char buff[256];
 | 
|---|
 | 279 |   string line;
 | 
|---|
 | 280 |   int nbad, nbadtot, nel;
 | 
|---|
 | 281 |   nbadtot = nbad = 0;
 | 
|---|
 | 282 |   while (!is.eof()) {
 | 
|---|
 | 283 |     is.clear(); 
 | 
|---|
 | 284 |     is.getline(buff, 256);
 | 
|---|
| [1560] | 285 |     //    cout << " DBG : buff=" << buff << " :state=" << is.rdstate() << endl;
 | 
|---|
| [2288] | 286 |     line += buff;  nel = 0;
 | 
|---|
| [1558] | 287 |     if (is.good()) {
 | 
|---|
| [2286] | 288 |       if ((line.length() > 0) && (line[0]!=clm)) {
 | 
|---|
 | 289 |         nel = Append(line, nbad);
 | 
|---|
 | 290 |         if (nel > 0)  { 
 | 
|---|
 | 291 |           nr++;  n += nel;
 | 
|---|
 | 292 |         }
 | 
|---|
 | 293 |         nbadtot += nbad;
 | 
|---|
| [1558] | 294 |       }
 | 
|---|
| [2288] | 295 |       //              cout << " Decoding line = " << line << " Nel= " << nel << endl;
 | 
|---|
| [1558] | 296 |       line = "";
 | 
|---|
 | 297 |     }
 | 
|---|
 | 298 |   }
 | 
|---|
| [2286] | 299 |   if ((line.length() > 0) && (line[0]!=clm)) {
 | 
|---|
| [1558] | 300 |     nel = Append(line, nbad);
 | 
|---|
| [2288] | 301 |     //        cout << " Decoding Eline = " << line << " Nel= " << nel << endl;
 | 
|---|
| [1558] | 302 |     if (nel > 0)  { 
 | 
|---|
 | 303 |       nr++;  n += nel;
 | 
|---|
 | 304 |     }
 | 
|---|
 | 305 |     nbadtot += nbad;
 | 
|---|
 | 306 |     line = ""; 
 | 
|---|
 | 307 |   }
 | 
|---|
 | 308 |   if (nbadtot > 0) 
 | 
|---|
 | 309 |     cout << "EnumeratedSequence::FillFromFile()/Warning " << nbadtot 
 | 
|---|
 | 310 |          << " bad match (quotes or parenthesis) in stream " << endl;
 | 
|---|
 | 311 |   nc = n/nr;
 | 
|---|
 | 312 |   return (n);
 | 
|---|
| [1550] | 313 | }
 | 
|---|
 | 314 | 
 | 
|---|
| [894] | 315 | //////////////////////////////////////////////////////////
 | 
|---|
| [926] | 316 | /*!
 | 
|---|
 | 317 |   \class SOPHYA::Range
 | 
|---|
 | 318 |   \ingroup TArray
 | 
|---|
 | 319 |   Class to define a range of indexes
 | 
|---|
 | 320 | */
 | 
|---|
 | 321 | 
 | 
|---|
| [894] | 322 | //! Constructor
 | 
|---|
 | 323 | /*!
 | 
|---|
 | 324 |   Define a range of indexes
 | 
|---|
| [1412] | 325 |   \param start : start index (inclusive)
 | 
|---|
 | 326 |   \param end : end index (inclusive)
 | 
|---|
 | 327 |   \param size : size (number of elements, used if end \<= start)
 | 
|---|
 | 328 |   \param step : step (or stride)
 | 
|---|
| [894] | 329 | 
 | 
|---|
 | 330 |   \warning If \b end \> \b start, \b size is computed automatically
 | 
|---|
 | 331 |   \warning If not \b size is fixed and \b end recomputed
 | 
|---|
 | 332 |  */
 | 
|---|
| [1156] | 333 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
 | 
|---|
| [785] | 334 | {
 | 
|---|
 | 335 |   start_ = start;
 | 
|---|
 | 336 |   step_ = (step > 0) ? step : 1;
 | 
|---|
| [813] | 337 |   if (end > start) {  // Taille calcule automatiquement 
 | 
|---|
 | 338 |     end_ = end;
 | 
|---|
 | 339 |     if (step_ > ((end_-start_)+1))  size_ = 1;
 | 
|---|
 | 340 |     else size_ = ((end-start)+1)/step_;
 | 
|---|
 | 341 |   }
 | 
|---|
 | 342 |   else {     // Taille fixee
 | 
|---|
 | 343 |     size_ = size;
 | 
|---|
 | 344 |     end_ = start_+size_*step_;
 | 
|---|
 | 345 |   }
 | 
|---|
| [785] | 346 | }
 | 
|---|
 | 347 | 
 | 
|---|
| [804] | 348 | /*
 | 
|---|
| [1156] | 349 | Range & Range::operator = (sa_size_t start)
 | 
|---|
| [785] | 350 | {
 | 
|---|
 | 351 |   start_ = start;
 | 
|---|
 | 352 |   size_ = 1;
 | 
|---|
 | 353 |   step_ = 1;
 | 
|---|
 | 354 |   return (*this);
 | 
|---|
 | 355 | }
 | 
|---|
| [804] | 356 | */
 | 
|---|
 | 357 | 
 | 
|---|
 | 358 | 
 | 
|---|
| [894] | 359 | //////////////////////////////////////////////////////////
 | 
|---|
| [926] | 360 | /*!
 | 
|---|
 | 361 |   \class SOPHYA::IdentityMatrix
 | 
|---|
 | 362 |   \ingroup TArray
 | 
|---|
 | 363 |   Class to define an identity matrix
 | 
|---|
 | 364 | */
 | 
|---|
 | 365 | 
 | 
|---|
| [894] | 366 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
 | 
|---|
| [1156] | 367 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
 | 
|---|
| [804] | 368 | {
 | 
|---|
 | 369 |   size_ = n;
 | 
|---|
 | 370 |   diag_ = diag;
 | 
|---|
 | 371 | }
 | 
|---|