| 1 | //     Utility classes for template numerical arrays | 
|---|
| 2 | //                     R. Ansari, C.Magneville   03/2000 | 
|---|
| 3 |  | 
|---|
| 4 | #include "sopnamsp.h" | 
|---|
| 5 | #include "machdefs.h" | 
|---|
| 6 | #include "utilarr.h" | 
|---|
| 7 | #include "stsrand.h" | 
|---|
| 8 |  | 
|---|
| 9 | // Classe utilitaires | 
|---|
| 10 |  | 
|---|
| 11 | Sequence::~Sequence() | 
|---|
| 12 | { | 
|---|
| 13 | } | 
|---|
| 14 |  | 
|---|
| 15 | ////////////////////////////////////////////////////////// | 
|---|
| 16 | /*! | 
|---|
| 17 | \class SOPHYA::RandomSequence | 
|---|
| 18 | \ingroup TArray | 
|---|
| 19 | Base class to generate a sequence of random values | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 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 | */ | 
|---|
| 28 | static RandomGenerator* uarg_ = NULL; | 
|---|
| 29 | RandomSequence::RandomSequence(int typ, double m, double s) | 
|---|
| 30 | { | 
|---|
| 31 | typ_ = (typ == Flat) ? Flat : Gaussian; | 
|---|
| 32 | mean_ = m; | 
|---|
| 33 | sig_ = s; | 
|---|
| 34 | if (uarg_ == NULL) uarg_ = new RandomGenerator(1024, true); | 
|---|
| 35 | } | 
|---|
| 36 | RandomSequence::~RandomSequence() | 
|---|
| 37 | { | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | //! Return random sequence values. | 
|---|
| 41 | /*! | 
|---|
| 42 | \return If typ = Flat : return [-1,+1]*sig + mean | 
|---|
| 43 | \return If typ = Gaussian : return gaussian distributed | 
|---|
| 44 | with \b mean mean and sigma \b sig | 
|---|
| 45 | */ | 
|---|
| 46 | double RandomSequence::Rand() | 
|---|
| 47 | { | 
|---|
| 48 | if (typ_ == Flat) | 
|---|
| 49 | return(uarg_->Flatpm1()*sig_ + mean_); | 
|---|
| 50 | else return(uarg_->Gaussian(sig_, mean_)); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | MuTyV & RandomSequence::Value(sa_size_t k) const | 
|---|
| 54 | { | 
|---|
| 55 | if (typ_ == Flat) retv_ = uarg_->Flatpm1()*sig_ + mean_; | 
|---|
| 56 | else retv_ = uarg_->Gaussian(sig_, mean_); | 
|---|
| 57 | return retv_; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 |  | 
|---|
| 61 | ////////////////////////////////////////////////////////// | 
|---|
| 62 | /*! | 
|---|
| 63 | \class SOPHYA::RegularSequence | 
|---|
| 64 | \ingroup TArray | 
|---|
| 65 | Class to generate a sequence of values | 
|---|
| 66 | */ | 
|---|
| 67 |  | 
|---|
| 68 | //! Constructor | 
|---|
| 69 | /*! | 
|---|
| 70 | \param start : start value | 
|---|
| 71 | \param step : step value | 
|---|
| 72 | \param f : pointer to the sequence function (default = NULL, f(x)=x ) | 
|---|
| 73 |  | 
|---|
| 74 | See \ref RegularSequenceOperat "operator()" | 
|---|
| 75 | */ | 
|---|
| 76 | RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f) | 
|---|
| 77 | { | 
|---|
| 78 | start_ = start; | 
|---|
| 79 | step_ = step; | 
|---|
| 80 | myf_ = f; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | RegularSequence::~RegularSequence() | 
|---|
| 84 | { | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | //! Get the \b k th value | 
|---|
| 88 | /*! | 
|---|
| 89 | \param k : index of the value | 
|---|
| 90 | \anchor RegularSequenceOperat | 
|---|
| 91 |  | 
|---|
| 92 | \return f(start+k*step) | 
|---|
| 93 |  | 
|---|
| 94 | */ | 
|---|
| 95 |  | 
|---|
| 96 | MuTyV & RegularSequence::Value (sa_size_t k) const | 
|---|
| 97 | { | 
|---|
| 98 | double x = start_+(double)k*step_; | 
|---|
| 99 | if (myf_)  x = myf_(x); | 
|---|
| 100 | retv_ = x; | 
|---|
| 101 | return(retv_); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | /*! | 
|---|
| 105 | \class SOPHYA::EnumeratedSequence | 
|---|
| 106 | \ingroup TArray | 
|---|
| 107 | Explicitly defined sequence of values. The comma operator has | 
|---|
| 108 | been redefined to let an easy definition of sequences. | 
|---|
| 109 |  | 
|---|
| 110 | \code | 
|---|
| 111 | // Initializing a sequence | 
|---|
| 112 | EnumeratedSequence es; | 
|---|
| 113 | es = 11, 22, 33, 44, 55, 66; | 
|---|
| 114 |  | 
|---|
| 115 | for(int k=0; k<8; k++) | 
|---|
| 116 | cout << " k= " << k << " es(k)= " << es(k) << endl; | 
|---|
| 117 |  | 
|---|
| 118 | // Decoding a sequence from a string | 
|---|
| 119 | EnumeratedSequence ess; | 
|---|
| 120 | int nbad; | 
|---|
| 121 | ess.Append("56.5 (1.,-1.) 4 8 16", nbad); | 
|---|
| 122 | cout << ess; | 
|---|
| 123 | \endcode | 
|---|
| 124 | */ | 
|---|
| 125 |  | 
|---|
| 126 | //! Default constructor | 
|---|
| 127 | EnumeratedSequence::EnumeratedSequence() | 
|---|
| 128 | { | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | //! Copy constructor | 
|---|
| 132 | EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es) | 
|---|
| 133 | { | 
|---|
| 134 | Append(es); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | EnumeratedSequence::~EnumeratedSequence() | 
|---|
| 138 | { | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | //! Return the k th value in the sequence (default = 0) | 
|---|
| 142 | MuTyV & EnumeratedSequence::Value (sa_size_t k) const | 
|---|
| 143 | { | 
|---|
| 144 | if (k >= vecv_.size())  retv_ = 0; | 
|---|
| 145 | else retv_ = vecv_[k]; | 
|---|
| 146 | return(retv_); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | //! Appends a new value to the sequence | 
|---|
| 150 | EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v) | 
|---|
| 151 | { | 
|---|
| 152 | vecv_.push_back(v); | 
|---|
| 153 | return(*this); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | //! Initialize the sequence with a single value \b v | 
|---|
| 157 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v) | 
|---|
| 158 | { | 
|---|
| 159 | vecv_.clear(); | 
|---|
| 160 | vecv_.push_back(v); | 
|---|
| 161 | return(*this); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | //! Copy operator | 
|---|
| 165 | EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq) | 
|---|
| 166 | { | 
|---|
| 167 | Clear(); | 
|---|
| 168 | Append(seq); | 
|---|
| 169 | return(*this); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | //! Prints the list to the output stream \b os | 
|---|
| 174 | void EnumeratedSequence::Print(ostream& os) const | 
|---|
| 175 | { | 
|---|
| 176 | os << " EnumeratedSequence::Print() - Size()= " << Size() << endl; | 
|---|
| 177 | for(int k=0; k<vecv_.size(); k++) { | 
|---|
| 178 | os << vecv_[k]; | 
|---|
| 179 | if ((k > 0) && (k%10 == 0))  os << endl; | 
|---|
| 180 | else os << " " ; | 
|---|
| 181 | } | 
|---|
| 182 | os << endl; | 
|---|
| 183 | return; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | //! Append the \b seq to the end of the sequence. | 
|---|
| 187 | /*! | 
|---|
| 188 | \return the number of added elements. | 
|---|
| 189 | */ | 
|---|
| 190 | sa_size_t EnumeratedSequence::Append(EnumeratedSequence const & seq) | 
|---|
| 191 | { | 
|---|
| 192 | for(int k=0; k<seq.vecv_.size(); k++) | 
|---|
| 193 | vecv_.push_back(seq.vecv_[k]); | 
|---|
| 194 | return(seq.vecv_.size()); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | //! Decodes the string, appending values to the end of the sequence. | 
|---|
| 198 | /*! | 
|---|
| 199 | \param str : string to be decoded | 
|---|
| 200 | \param nbad : number of unmatched quotes or parenthesis (returned value) | 
|---|
| 201 | \param sep : word separator in string. Each word is decoded as a MuTyV value. | 
|---|
| 202 | \return the number of added elements. | 
|---|
| 203 | */ | 
|---|
| 204 | sa_size_t EnumeratedSequence::Append(string const & str, int& nbad, const char* sep) | 
|---|
| 205 | { | 
|---|
| 206 | nbad = 0; | 
|---|
| 207 | sa_size_t n = 0; | 
|---|
| 208 | size_t l = str.length(); | 
|---|
| 209 | if (l < 1) return(0); | 
|---|
| 210 | //  if ((str[0] == '#') || (str[0] == '*')) return(0); | 
|---|
| 211 | size_t q = 0; | 
|---|
| 212 | size_t p = 0; | 
|---|
| 213 | /* | 
|---|
| 214 | size_t p = str.find_first_not_of(sep); | 
|---|
| 215 | if ((str[p] == '+') || (str[p] == '-')) { | 
|---|
| 216 | if (p == l-1)  return(0); | 
|---|
| 217 | if (!isdigit(str[p+1])) return(0); | 
|---|
| 218 | } | 
|---|
| 219 | else if (!isdigit(str[p]) && (str[p] != '\'') && (str[p] != '(') ) return(0); | 
|---|
| 220 | */ | 
|---|
| 221 | while(q < l) { | 
|---|
| 222 | p = str.find_first_not_of(sep,q); | 
|---|
| 223 | if (p >= l)  break; | 
|---|
| 224 | if (str[p] == '\'')    {  // Decodage d'un string | 
|---|
| 225 | q = str.find('\'',p+1); | 
|---|
| 226 | if (q < l) { | 
|---|
| 227 | vecv_.push_back(MuTyV(str.substr(p+1,q-p-1))); | 
|---|
| 228 | n++;    q++; | 
|---|
| 229 | } | 
|---|
| 230 | else nbad++; | 
|---|
| 231 | } | 
|---|
| 232 | else if (str[p] == '(')    {  // Decodage d'un complex | 
|---|
| 233 | q = str.find(')',p); | 
|---|
| 234 | if (q < l) { | 
|---|
| 235 | q++; | 
|---|
| 236 | MuTyV mtv(str.substr(p,q-p)); | 
|---|
| 237 | complex<r_8> z = mtv.operator complex<r_8>(); | 
|---|
| 238 | vecv_.push_back(MuTyV(z)); | 
|---|
| 239 | n++; | 
|---|
| 240 | } | 
|---|
| 241 | else nbad++; | 
|---|
| 242 | } | 
|---|
| 243 | else { | 
|---|
| 244 | q = str.find_first_of(sep,p); | 
|---|
| 245 | if ( !isdigit(str[p]) && !(str[p] == '+') | 
|---|
| 246 | && !(str[p] == '-') && !(str[p] == '.') ) { // une chaine | 
|---|
| 247 | vecv_.push_back(MuTyV(str.substr(p,q-p))); | 
|---|
| 248 | n++; | 
|---|
| 249 | } | 
|---|
| 250 | else {  // C'est un nombre | 
|---|
| 251 | if (str.find_first_of(".eE",p) < q)   { // c'est un flottant | 
|---|
| 252 | r_8 x = atof(str.substr(p,q-p).c_str()); | 
|---|
| 253 | vecv_.push_back(MuTyV(x)); | 
|---|
| 254 | } | 
|---|
| 255 | else {   // un entier | 
|---|
| 256 | int_8 l = atol(str.substr(p,q-p).c_str()); | 
|---|
| 257 | vecv_.push_back(MuTyV(l)); | 
|---|
| 258 | } | 
|---|
| 259 | n++; | 
|---|
| 260 | } | 
|---|
| 261 | } | 
|---|
| 262 | } | 
|---|
| 263 | return (n); | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | //! Decodes the input ASCII stream, creating a sequence of values | 
|---|
| 267 | /*! \param is : Input ASCII stream | 
|---|
| 268 | \param nr : Number of non empty (or comment) lines in stream (return value) | 
|---|
| 269 | \param nc : Number of columns (= ntot/nlines) (return value) | 
|---|
| 270 | \param clm : Lines starting with clm character are treated as comment lines | 
|---|
| 271 | \param sep : word separator in lines | 
|---|
| 272 | \return Number of decoded elements | 
|---|
| 273 | */ | 
|---|
| 274 | sa_size_t EnumeratedSequence::FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc, | 
|---|
| 275 | char clm, const char* sep) | 
|---|
| 276 | { | 
|---|
| 277 | nr = 0; | 
|---|
| 278 | nc = 0; | 
|---|
| 279 | sa_size_t n = 0; | 
|---|
| 280 | char buff[256]; | 
|---|
| 281 | string line; | 
|---|
| 282 | int nbad, nbadtot, nel; | 
|---|
| 283 | nbadtot = nbad = 0; | 
|---|
| 284 | while (!is.eof()) { | 
|---|
| 285 | /* Reza, Juin 2005 : Remplace par getline(istream, string) - plus sur | 
|---|
| 286 | is.clear(); | 
|---|
| 287 | is.getline(buff, 256);    line += buff;  nel = 0; */ | 
|---|
| 288 | line = ""; | 
|---|
| 289 | getline(is, line); | 
|---|
| 290 | if (is.good() || is.eof()) { | 
|---|
| 291 | if ((line.length() > 0) && (line[0]!=clm)) { | 
|---|
| 292 | nel = Append(line, nbad, sep); | 
|---|
| 293 | if (nel > 0)  { | 
|---|
| 294 | nr++;  n += nel; | 
|---|
| 295 | } | 
|---|
| 296 | nbadtot += nbad; | 
|---|
| 297 | } | 
|---|
| 298 | } | 
|---|
| 299 | } | 
|---|
| 300 | /* Reza, Juin 2005 : plus necessaire | 
|---|
| 301 | if ((line.length() > 0) && (line[0]!=clm)) { | 
|---|
| 302 | nel = Append(line, nbad, sep); | 
|---|
| 303 | if (nel > 0)  {       nr++;  n += nel; } | 
|---|
| 304 | nbadtot += nbad;    line = ""; } | 
|---|
| 305 | */ | 
|---|
| 306 | if (nbadtot > 0) | 
|---|
| 307 | cout << "EnumeratedSequence::FillFromFile()/Warning " << nbadtot | 
|---|
| 308 | << " bad match (quotes or parenthesis) in stream " << endl; | 
|---|
| 309 | nc = n/nr; | 
|---|
| 310 | return (n); | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | ////////////////////////////////////////////////////////// | 
|---|
| 314 | /*! | 
|---|
| 315 | \class SOPHYA::Range | 
|---|
| 316 | \ingroup TArray | 
|---|
| 317 | This class can be used to define a range of indices. Range objects are used to extract | 
|---|
| 318 | sub-arrays and slices, from arrays and matrices. | 
|---|
| 319 | \sa SOPHYA::TArray SOPHYA::TMatrix | 
|---|
| 320 | */ | 
|---|
| 321 |  | 
|---|
| 322 | /*! | 
|---|
| 323 | Constructor defining a range corresponding to the single index \b start | 
|---|
| 324 | \param start : start=end index | 
|---|
| 325 | */ | 
|---|
| 326 | Range::Range(sa_size_t start) | 
|---|
| 327 | { | 
|---|
| 328 | start_ = start; | 
|---|
| 329 | end_ = start; | 
|---|
| 330 | size_ = 1; | 
|---|
| 331 | step_ = 1; | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | /*! | 
|---|
| 335 | Constructor defining defining the range of indices, from \b start to \b end | 
|---|
| 336 | \param start : start index (inclusive) | 
|---|
| 337 | \param end : end index (inclusive) | 
|---|
| 338 | */ | 
|---|
| 339 | Range::Range(sa_size_t start, sa_size_t end) | 
|---|
| 340 | { | 
|---|
| 341 | start_ = start; | 
|---|
| 342 | end_ = end; | 
|---|
| 343 | if (end >= start)   size_ = end-start+1; | 
|---|
| 344 | else size_ = 0; | 
|---|
| 345 | step_ = 1; | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | /*! | 
|---|
| 349 | Constructor defining defining the range of indices, from \b start to \b end | 
|---|
| 350 | \param start : start index (inclusive) | 
|---|
| 351 | \param end : end index (inclusive) | 
|---|
| 352 | \param step : step (or stride) = index increment | 
|---|
| 353 | */ | 
|---|
| 354 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t step) | 
|---|
| 355 | { | 
|---|
| 356 | start_ = start; | 
|---|
| 357 | end_ = end; | 
|---|
| 358 | step_ = (step > 0) ? step : 1; | 
|---|
| 359 | if (step < 1) step = 1; | 
|---|
| 360 | if (end >= start) | 
|---|
| 361 | size_ = (end-start)/step_+1; | 
|---|
| 362 | else size_ = 0; | 
|---|
| 363 | } | 
|---|
| 364 |  | 
|---|
| 365 | /*! | 
|---|
| 366 | Define a range of indices | 
|---|
| 367 | \param start : start index (inclusive) | 
|---|
| 368 | \param end : end index (inclusive, used if size \<= 0 and end \>= start) | 
|---|
| 369 | \param size : size (number of elements, used if \>= 1 ) | 
|---|
| 370 | \param step : step (or stride) = index increment | 
|---|
| 371 |  | 
|---|
| 372 | \warning If \b size \>= 1 , \b end index computed automatically. | 
|---|
| 373 | If \b size \< 1 and \b end < \b start , equivalent to \b end = Range()::lastIndex() | 
|---|
| 374 | */ | 
|---|
| 375 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step) | 
|---|
| 376 | { | 
|---|
| 377 | start_ = start; | 
|---|
| 378 | step_ = (step > 0) ? step : 1; | 
|---|
| 379 | if (size > 0) {  // Nb d'elements fixe | 
|---|
| 380 | size_ = size; | 
|---|
| 381 | if ( (start == end) && (end == Range::lastIndex()) ) start_ = end_ = end; | 
|---|
| 382 | else end_ = start_+(size_-1)*step_; | 
|---|
| 383 | } | 
|---|
| 384 | else { | 
|---|
| 385 | if (end >= start) {  // Indice de fin fixe | 
|---|
| 386 | end_ = end; | 
|---|
| 387 | size_ = (end-start)/step_+1; | 
|---|
| 388 | } | 
|---|
| 389 | else {  // rien fixe | 
|---|
| 390 | size_ = 0; | 
|---|
| 391 | end_ = Range::lastIndex(); | 
|---|
| 392 | } | 
|---|
| 393 | } | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | /* | 
|---|
| 397 | Range::Range(Range const& a) | 
|---|
| 398 | { | 
|---|
| 399 | start_ = a.start_; | 
|---|
| 400 | end_ = a.end_; | 
|---|
| 401 | size_ = a.size_; | 
|---|
| 402 | step_ = a.step_; | 
|---|
| 403 | } | 
|---|
| 404 | */ | 
|---|
| 405 |  | 
|---|
| 406 | /*! | 
|---|
| 407 | This method is called to recompute index ranges, specifying the original array size | 
|---|
| 408 | by the TArray<T> (or derived classes) sub array extraction methods | 
|---|
| 409 | */ | 
|---|
| 410 | void Range::Update(sa_size_t osz) | 
|---|
| 411 | { | 
|---|
| 412 | if (end_ >= 0)  return; | 
|---|
| 413 | if (osz == 0) { | 
|---|
| 414 | start_ = end_ = 0; | 
|---|
| 415 | size_ = step_ = 1; | 
|---|
| 416 | return; | 
|---|
| 417 | } | 
|---|
| 418 | if (end_ == start_) { | 
|---|
| 419 | end_ = osz-1; | 
|---|
| 420 | if ((size_ > 0) && (size_ <= osz/step_)) | 
|---|
| 421 | start_ = end_ - (size_-1)*step_; | 
|---|
| 422 | else { | 
|---|
| 423 | start_ = end_; | 
|---|
| 424 | size_ = 1; | 
|---|
| 425 | } | 
|---|
| 426 | } | 
|---|
| 427 | else { | 
|---|
| 428 | end_ = osz-1; | 
|---|
| 429 | size_ = (end_-start_)/step_+1; | 
|---|
| 430 | } | 
|---|
| 431 | //DBG  cout << ">>> DBG/Update start=" << start_ << " end=" << end_ | 
|---|
| 432 | //DBG     << " size=" << size_ << " step=" << step_ << endl; | 
|---|
| 433 | return; | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | /* | 
|---|
| 437 | Range & Range::operator = (sa_size_t start) | 
|---|
| 438 | { | 
|---|
| 439 | start_ = start; | 
|---|
| 440 | size_ = 1; | 
|---|
| 441 | step_ = 1; | 
|---|
| 442 | return (*this); | 
|---|
| 443 | } | 
|---|
| 444 | */ | 
|---|
| 445 |  | 
|---|
| 446 |  | 
|---|
| 447 | ////////////////////////////////////////////////////////// | 
|---|
| 448 | /*! | 
|---|
| 449 | \class SOPHYA::IdentityMatrix | 
|---|
| 450 | \ingroup TArray | 
|---|
| 451 | Class to define an identity matrix. This class is mainly intented for | 
|---|
| 452 | initializing TMatrix<T> objects, proportional to the identity matrix. | 
|---|
| 453 | */ | 
|---|
| 454 |  | 
|---|
| 455 | //! Constructor representing a square matrix (\b n x \b n) with value \b diag on the diagonal | 
|---|
| 456 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n) | 
|---|
| 457 | { | 
|---|
| 458 | size_ = n; | 
|---|
| 459 | diag_ = diag; | 
|---|
| 460 | } | 
|---|