source: Sophya/trunk/SophyaLib/TArray/utilarr.cc@ 3871

Last change on this file since 3871 was 3613, checked in by ansari, 16 years ago

Adaptation a l'introduction de la suite des classes RandomGeneratorInterface et Cie , Reza 30/04/2009

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