[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()) {
|
---|
[2795] | 283 | /* Reza, Juin 2005 : Remplace par getline(istream, string) - plus sur
|
---|
| 284 | is.clear();
|
---|
| 285 | is.getline(buff, 256); line += buff; nel = 0; */
|
---|
| 286 | line = "";
|
---|
| 287 | getline(is, line);
|
---|
| 288 | if (is.good() || is.eof()) {
|
---|
[2286] | 289 | if ((line.length() > 0) && (line[0]!=clm)) {
|
---|
[2752] | 290 | nel = Append(line, nbad, sep);
|
---|
[2286] | 291 | if (nel > 0) {
|
---|
| 292 | nr++; n += nel;
|
---|
| 293 | }
|
---|
| 294 | nbadtot += nbad;
|
---|
[1558] | 295 | }
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
[2795] | 298 | /* Reza, Juin 2005 : plus necessaire
|
---|
[2286] | 299 | if ((line.length() > 0) && (line[0]!=clm)) {
|
---|
[2752] | 300 | nel = Append(line, nbad, sep);
|
---|
[2795] | 301 | if (nel > 0) { nr++; n += nel; }
|
---|
| 302 | nbadtot += nbad; line = ""; }
|
---|
| 303 | */
|
---|
[1558] | 304 | if (nbadtot > 0)
|
---|
| 305 | cout << "EnumeratedSequence::FillFromFile()/Warning " << nbadtot
|
---|
| 306 | << " bad match (quotes or parenthesis) in stream " << endl;
|
---|
| 307 | nc = n/nr;
|
---|
| 308 | return (n);
|
---|
[1550] | 309 | }
|
---|
| 310 |
|
---|
[894] | 311 | //////////////////////////////////////////////////////////
|
---|
[926] | 312 | /*!
|
---|
| 313 | \class SOPHYA::Range
|
---|
| 314 | \ingroup TArray
|
---|
| 315 | Class to define a range of indexes
|
---|
| 316 | */
|
---|
| 317 |
|
---|
[894] | 318 | //! Constructor
|
---|
| 319 | /*!
|
---|
| 320 | Define a range of indexes
|
---|
[1412] | 321 | \param start : start index (inclusive)
|
---|
| 322 | \param end : end index (inclusive)
|
---|
| 323 | \param size : size (number of elements, used if end \<= start)
|
---|
| 324 | \param step : step (or stride)
|
---|
[894] | 325 |
|
---|
| 326 | \warning If \b end \> \b start, \b size is computed automatically
|
---|
| 327 | \warning If not \b size is fixed and \b end recomputed
|
---|
| 328 | */
|
---|
[1156] | 329 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
|
---|
[785] | 330 | {
|
---|
| 331 | start_ = start;
|
---|
| 332 | step_ = (step > 0) ? step : 1;
|
---|
[813] | 333 | if (end > start) { // Taille calcule automatiquement
|
---|
| 334 | end_ = end;
|
---|
| 335 | if (step_ > ((end_-start_)+1)) size_ = 1;
|
---|
| 336 | else size_ = ((end-start)+1)/step_;
|
---|
| 337 | }
|
---|
| 338 | else { // Taille fixee
|
---|
| 339 | size_ = size;
|
---|
| 340 | end_ = start_+size_*step_;
|
---|
| 341 | }
|
---|
[785] | 342 | }
|
---|
| 343 |
|
---|
[804] | 344 | /*
|
---|
[1156] | 345 | Range & Range::operator = (sa_size_t start)
|
---|
[785] | 346 | {
|
---|
| 347 | start_ = start;
|
---|
| 348 | size_ = 1;
|
---|
| 349 | step_ = 1;
|
---|
| 350 | return (*this);
|
---|
| 351 | }
|
---|
[804] | 352 | */
|
---|
| 353 |
|
---|
| 354 |
|
---|
[894] | 355 | //////////////////////////////////////////////////////////
|
---|
[926] | 356 | /*!
|
---|
| 357 | \class SOPHYA::IdentityMatrix
|
---|
| 358 | \ingroup TArray
|
---|
| 359 | Class to define an identity matrix
|
---|
| 360 | */
|
---|
| 361 |
|
---|
[894] | 362 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
|
---|
[1156] | 363 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
|
---|
[804] | 364 | {
|
---|
| 365 | size_ = n;
|
---|
| 366 | diag_ = diag;
|
---|
| 367 | }
|
---|