[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"
|
---|
[3394] | 7 | #include "stsrand.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 | */
|
---|
[3394] | 28 | static RandomGenerator* uarg_ = NULL;
|
---|
[850] | 29 | RandomSequence::RandomSequence(int typ, double m, double s)
|
---|
| 30 | {
|
---|
| 31 | typ_ = (typ == Flat) ? Flat : Gaussian;
|
---|
| 32 | mean_ = m;
|
---|
| 33 | sig_ = s;
|
---|
[3394] | 34 | if (uarg_ == NULL) uarg_ = new RandomGenerator(1024, true);
|
---|
[850] | 35 | }
|
---|
[1103] | 36 | RandomSequence::~RandomSequence()
|
---|
| 37 | {
|
---|
| 38 | }
|
---|
[850] | 39 |
|
---|
[894] | 40 | //! Return random sequence values.
|
---|
[935] | 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 | */
|
---|
[850] | 46 | double RandomSequence::Rand()
|
---|
| 47 | {
|
---|
| 48 | if (typ_ == Flat)
|
---|
[3394] | 49 | return(uarg_->Flatpm1()*sig_ + mean_);
|
---|
| 50 | else return(uarg_->Gaussian(sig_, mean_));
|
---|
[850] | 51 | }
|
---|
| 52 |
|
---|
[1156] | 53 | MuTyV & RandomSequence::Value(sa_size_t k) const
|
---|
[1103] | 54 | {
|
---|
[3394] | 55 | if (typ_ == Flat) retv_ = uarg_->Flatpm1()*sig_ + mean_;
|
---|
| 56 | else retv_ = uarg_->Gaussian(sig_, mean_);
|
---|
[1103] | 57 | return retv_;
|
---|
| 58 | }
|
---|
[850] | 59 |
|
---|
[1103] | 60 |
|
---|
[894] | 61 | //////////////////////////////////////////////////////////
|
---|
[926] | 62 | /*!
|
---|
[1103] | 63 | \class SOPHYA::RegularSequence
|
---|
[926] | 64 | \ingroup TArray
|
---|
| 65 | Class to generate a sequence of values
|
---|
| 66 | */
|
---|
| 67 |
|
---|
[894] | 68 | //! Constructor
|
---|
| 69 | /*!
|
---|
| 70 | \param start : start value
|
---|
| 71 | \param step : step value
|
---|
[1404] | 72 | \param f : pointer to the sequence function (default = NULL, f(x)=x )
|
---|
[894] | 73 |
|
---|
[1103] | 74 | See \ref RegularSequenceOperat "operator()"
|
---|
[894] | 75 | */
|
---|
[1103] | 76 | RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
|
---|
[785] | 77 | {
|
---|
| 78 | start_ = start;
|
---|
| 79 | step_ = step;
|
---|
| 80 | myf_ = f;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1103] | 83 | RegularSequence::~RegularSequence()
|
---|
[850] | 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[894] | 87 | //! Get the \b k th value
|
---|
| 88 | /*!
|
---|
| 89 | \param k : index of the value
|
---|
[1103] | 90 | \anchor RegularSequenceOperat
|
---|
[1404] | 91 |
|
---|
| 92 | \return f(start+k*step)
|
---|
[894] | 93 |
|
---|
| 94 | */
|
---|
[1103] | 95 |
|
---|
[1156] | 96 | MuTyV & RegularSequence::Value (sa_size_t k) const
|
---|
[785] | 97 | {
|
---|
[1103] | 98 | double x = start_+(double)k*step_;
|
---|
| 99 | if (myf_) x = myf_(x);
|
---|
| 100 | retv_ = x;
|
---|
| 101 | return(retv_);
|
---|
[785] | 102 | }
|
---|
| 103 |
|
---|
[1404] | 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;
|
---|
[1558] | 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;
|
---|
[1404] | 123 | \endcode
|
---|
| 124 | */
|
---|
| 125 |
|
---|
[1558] | 126 | //! Default constructor
|
---|
[1404] | 127 | EnumeratedSequence::EnumeratedSequence()
|
---|
| 128 | {
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[1558] | 131 | //! Copy constructor
|
---|
| 132 | EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es)
|
---|
| 133 | {
|
---|
| 134 | Append(es);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[1103] | 137 | EnumeratedSequence::~EnumeratedSequence()
|
---|
| 138 | {
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[1404] | 141 | //! Return the k th value in the sequence (default = 0)
|
---|
[1156] | 142 | MuTyV & EnumeratedSequence::Value (sa_size_t k) const
|
---|
[1103] | 143 | {
|
---|
[3572] | 144 | if (k >= (sa_size_t)vecv_.size()) retv_ = 0;
|
---|
[1103] | 145 | else retv_ = vecv_[k];
|
---|
| 146 | return(retv_);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[1558] | 149 | //! Appends a new value to the sequence
|
---|
[1103] | 150 | EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
|
---|
| 151 | {
|
---|
| 152 | vecv_.push_back(v);
|
---|
| 153 | return(*this);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[1558] | 156 | //! Initialize the sequence with a single value \b v
|
---|
[1156] | 157 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
|
---|
| 158 | {
|
---|
| 159 | vecv_.clear();
|
---|
| 160 | vecv_.push_back(v);
|
---|
| 161 | return(*this);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[1558] | 164 | //! Copy operator
|
---|
| 165 | EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq)
|
---|
[1550] | 166 | {
|
---|
[1558] | 167 | Clear();
|
---|
| 168 | Append(seq);
|
---|
| 169 | return(*this);
|
---|
[1550] | 170 | }
|
---|
| 171 |
|
---|
[1558] | 172 |
|
---|
| 173 | //! Prints the list to the output stream \b os
|
---|
[1550] | 174 | void EnumeratedSequence::Print(ostream& os) const
|
---|
| 175 | {
|
---|
| 176 | os << " EnumeratedSequence::Print() - Size()= " << Size() << endl;
|
---|
[3572] | 177 | for(size_t k=0; k<vecv_.size(); k++) {
|
---|
[1550] | 178 | os << vecv_[k];
|
---|
| 179 | if ((k > 0) && (k%10 == 0)) os << endl;
|
---|
| 180 | else os << " " ;
|
---|
| 181 | }
|
---|
[1558] | 182 | os << endl;
|
---|
[1550] | 183 | return;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[1558] | 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)
|
---|
[1550] | 191 | {
|
---|
[3572] | 192 | for(size_t k=0; k<seq.vecv_.size(); k++)
|
---|
[1558] | 193 | vecv_.push_back(seq.vecv_[k]);
|
---|
| 194 | return(seq.vecv_.size());
|
---|
[1550] | 195 | }
|
---|
| 196 |
|
---|
[1558] | 197 | //! Decodes the string, appending values to the end of the sequence.
|
---|
| 198 | /*!
|
---|
| 199 | \param str : string to be decoded
|
---|
[2286] | 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.
|
---|
[1558] | 202 | \return the number of added elements.
|
---|
| 203 | */
|
---|
[2286] | 204 | sa_size_t EnumeratedSequence::Append(string const & str, int& nbad, const char* sep)
|
---|
[1558] | 205 | {
|
---|
| 206 | nbad = 0;
|
---|
| 207 | sa_size_t n = 0;
|
---|
| 208 | size_t l = str.length();
|
---|
| 209 | if (l < 1) return(0);
|
---|
[2286] | 210 | // if ((str[0] == '#') || (str[0] == '*')) return(0);
|
---|
[1558] | 211 | size_t q = 0;
|
---|
[2286] | 212 | size_t p = 0;
|
---|
| 213 | /*
|
---|
| 214 | size_t p = str.find_first_not_of(sep);
|
---|
[1558] | 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);
|
---|
[2286] | 220 | */
|
---|
[1558] | 221 | while(q < l) {
|
---|
[2286] | 222 | p = str.find_first_not_of(sep,q);
|
---|
[1558] | 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));
|
---|
[2149] | 237 | complex<r_8> z = mtv.operator complex<r_8>();
|
---|
[1558] | 238 | vecv_.push_back(MuTyV(z));
|
---|
| 239 | n++;
|
---|
| 240 | }
|
---|
| 241 | else nbad++;
|
---|
| 242 | }
|
---|
| 243 | else {
|
---|
[2286] | 244 | q = str.find_first_of(sep,p);
|
---|
[2288] | 245 | if ( !isdigit(str[p]) && !(str[p] == '+')
|
---|
| 246 | && !(str[p] == '-') && !(str[p] == '.') ) { // une chaine
|
---|
[2286] | 247 | vecv_.push_back(MuTyV(str.substr(p,q-p)));
|
---|
[2288] | 248 | n++;
|
---|
[1558] | 249 | }
|
---|
| 250 | else { // C'est un nombre
|
---|
[2288] | 251 | if (str.find_first_of(".eE",p) < q) { // c'est un flottant
|
---|
[1558] | 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)
|
---|
[2286] | 270 | \param clm : Lines starting with clm character are treated as comment lines
|
---|
| 271 | \param sep : word separator in lines
|
---|
[1558] | 272 | \return Number of decoded elements
|
---|
| 273 | */
|
---|
[2286] | 274 | sa_size_t EnumeratedSequence::FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc,
|
---|
| 275 | char clm, const char* sep)
|
---|
[1550] | 276 | {
|
---|
[1558] | 277 | nr = 0;
|
---|
| 278 | nc = 0;
|
---|
| 279 | sa_size_t n = 0;
|
---|
[3572] | 280 | //char buff[256];
|
---|
[1558] | 281 | string line;
|
---|
| 282 | int nbad, nbadtot, nel;
|
---|
| 283 | nbadtot = nbad = 0;
|
---|
| 284 | while (!is.eof()) {
|
---|
[2795] | 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()) {
|
---|
[2286] | 291 | if ((line.length() > 0) && (line[0]!=clm)) {
|
---|
[2752] | 292 | nel = Append(line, nbad, sep);
|
---|
[2286] | 293 | if (nel > 0) {
|
---|
| 294 | nr++; n += nel;
|
---|
| 295 | }
|
---|
| 296 | nbadtot += nbad;
|
---|
[1558] | 297 | }
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
[2795] | 300 | /* Reza, Juin 2005 : plus necessaire
|
---|
[2286] | 301 | if ((line.length() > 0) && (line[0]!=clm)) {
|
---|
[2752] | 302 | nel = Append(line, nbad, sep);
|
---|
[2795] | 303 | if (nel > 0) { nr++; n += nel; }
|
---|
| 304 | nbadtot += nbad; line = ""; }
|
---|
| 305 | */
|
---|
[1558] | 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);
|
---|
[1550] | 311 | }
|
---|
| 312 |
|
---|
[894] | 313 | //////////////////////////////////////////////////////////
|
---|
[926] | 314 | /*!
|
---|
| 315 | \class SOPHYA::Range
|
---|
| 316 | \ingroup TArray
|
---|
[2917] | 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
|
---|
[926] | 320 | */
|
---|
| 321 |
|
---|
[894] | 322 | /*!
|
---|
[2917] | 323 | Constructor defining a range corresponding to the single index \b start
|
---|
| 324 | \param start : start=end index
|
---|
[2915] | 325 | */
|
---|
| 326 | Range::Range(sa_size_t start)
|
---|
| 327 | {
|
---|
| 328 | start_ = start;
|
---|
[2917] | 329 | end_ = start;
|
---|
| 330 | size_ = 1;
|
---|
[2915] | 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)
|
---|
[1412] | 337 | \param end : end index (inclusive)
|
---|
[2915] | 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 | }
|
---|
[894] | 347 |
|
---|
[2915] | 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()
|
---|
[894] | 374 | */
|
---|
[1156] | 375 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
|
---|
[785] | 376 | {
|
---|
| 377 | start_ = start;
|
---|
| 378 | step_ = (step > 0) ? step : 1;
|
---|
[2915] | 379 | if (size > 0) { // Nb d'elements fixe
|
---|
[813] | 380 | size_ = size;
|
---|
[2917] | 381 | if ( (start == end) && (end == Range::lastIndex()) ) start_ = end_ = end;
|
---|
| 382 | else end_ = start_+(size_-1)*step_;
|
---|
[813] | 383 | }
|
---|
[2915] | 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 | }
|
---|
[785] | 394 | }
|
---|
| 395 |
|
---|
[804] | 396 | /*
|
---|
[2915] | 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 | {
|
---|
[2917] | 412 | if (end_ >= 0) return;
|
---|
[2915] | 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_))
|
---|
[2917] | 421 | start_ = end_ - (size_-1)*step_;
|
---|
[2915] | 422 | else {
|
---|
| 423 | start_ = end_;
|
---|
| 424 | size_ = 1;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 | else {
|
---|
| 428 | end_ = osz-1;
|
---|
| 429 | size_ = (end_-start_)/step_+1;
|
---|
| 430 | }
|
---|
[2917] | 431 | //DBG cout << ">>> DBG/Update start=" << start_ << " end=" << end_
|
---|
| 432 | //DBG << " size=" << size_ << " step=" << step_ << endl;
|
---|
[2915] | 433 | return;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | /*
|
---|
[1156] | 437 | Range & Range::operator = (sa_size_t start)
|
---|
[785] | 438 | {
|
---|
| 439 | start_ = start;
|
---|
| 440 | size_ = 1;
|
---|
| 441 | step_ = 1;
|
---|
| 442 | return (*this);
|
---|
| 443 | }
|
---|
[804] | 444 | */
|
---|
| 445 |
|
---|
| 446 |
|
---|
[894] | 447 | //////////////////////////////////////////////////////////
|
---|
[926] | 448 | /*!
|
---|
| 449 | \class SOPHYA::IdentityMatrix
|
---|
| 450 | \ingroup TArray
|
---|
[2917] | 451 | Class to define an identity matrix. This class is mainly intented for
|
---|
| 452 | initializing TMatrix<T> objects, proportional to the identity matrix.
|
---|
[926] | 453 | */
|
---|
| 454 |
|
---|
[2917] | 455 | //! Constructor representing a square matrix (\b n x \b n) with value \b diag on the diagonal
|
---|
[1156] | 456 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
|
---|
[804] | 457 | {
|
---|
| 458 | size_ = n;
|
---|
| 459 | diag_ = diag;
|
---|
| 460 | }
|
---|