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

Last change on this file since 2915 was 2915, checked in by ansari, 20 years ago

Amelioration de la classe Range - permettant une valeur symbolique pour specifier le dernier index (last()) - Reza 22/02/2006

File size: 10.4 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 "srandgen.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//! 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 */
28RandomSequence::RandomSequence(int typ, double m, double s)
29{
30 typ_ = (typ == Flat) ? Flat : Gaussian;
31 mean_ = m;
32 sig_ = s;
33}
34RandomSequence::~RandomSequence()
35{
36}
37
38//! Return random sequence values.
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 */
44double RandomSequence::Rand()
45{
46 if (typ_ == Flat)
47 return(drandpm1()*sig_ + mean_);
48 else return(GauRnd(mean_, sig_));
49}
50
51MuTyV & RandomSequence::Value(sa_size_t k) const
52{
53 if (typ_ == Flat) retv_ = drandpm1()*sig_ + mean_;
54 else retv_ = GauRnd(mean_, sig_);
55 return retv_;
56}
57
58
59//////////////////////////////////////////////////////////
60/*!
61 \class SOPHYA::RegularSequence
62 \ingroup TArray
63 Class to generate a sequence of values
64*/
65
66//! Constructor
67/*!
68 \param start : start value
69 \param step : step value
70 \param f : pointer to the sequence function (default = NULL, f(x)=x )
71
72 See \ref RegularSequenceOperat "operator()"
73 */
74RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
75{
76 start_ = start;
77 step_ = step;
78 myf_ = f;
79}
80
81RegularSequence::~RegularSequence()
82{
83}
84
85//! Get the \b k th value
86/*!
87 \param k : index of the value
88 \anchor RegularSequenceOperat
89
90 \return f(start+k*step)
91
92 */
93
94MuTyV & RegularSequence::Value (sa_size_t k) const
95{
96 double x = start_+(double)k*step_;
97 if (myf_) x = myf_(x);
98 retv_ = x;
99 return(retv_);
100}
101
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;
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;
121 \endcode
122*/
123
124//! Default constructor
125EnumeratedSequence::EnumeratedSequence()
126{
127}
128
129//! Copy constructor
130EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es)
131{
132 Append(es);
133}
134
135EnumeratedSequence::~EnumeratedSequence()
136{
137}
138
139//! Return the k th value in the sequence (default = 0)
140MuTyV & EnumeratedSequence::Value (sa_size_t k) const
141{
142 if (k >= vecv_.size()) retv_ = 0;
143 else retv_ = vecv_[k];
144 return(retv_);
145}
146
147//! Appends a new value to the sequence
148EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
149{
150 vecv_.push_back(v);
151 return(*this);
152}
153
154//! Initialize the sequence with a single value \b v
155EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
156{
157 vecv_.clear();
158 vecv_.push_back(v);
159 return(*this);
160}
161
162//! Copy operator
163EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq)
164{
165 Clear();
166 Append(seq);
167 return(*this);
168}
169
170
171//! Prints the list to the output stream \b os
172void 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 }
180 os << endl;
181 return;
182}
183
184//! Append the \b seq to the end of the sequence.
185/*!
186 \return the number of added elements.
187*/
188sa_size_t EnumeratedSequence::Append(EnumeratedSequence const & seq)
189{
190 for(int k=0; k<seq.vecv_.size(); k++)
191 vecv_.push_back(seq.vecv_[k]);
192 return(seq.vecv_.size());
193}
194
195//! Decodes the string, appending values to the end of the sequence.
196/*!
197 \param str : string to be decoded
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.
200 \return the number of added elements.
201*/
202sa_size_t EnumeratedSequence::Append(string const & str, int& nbad, const char* sep)
203{
204 nbad = 0;
205 sa_size_t n = 0;
206 size_t l = str.length();
207 if (l < 1) return(0);
208 // if ((str[0] == '#') || (str[0] == '*')) return(0);
209 size_t q = 0;
210 size_t p = 0;
211 /*
212 size_t p = str.find_first_not_of(sep);
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);
218 */
219 while(q < l) {
220 p = str.find_first_not_of(sep,q);
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));
235 complex<r_8> z = mtv.operator complex<r_8>();
236 vecv_.push_back(MuTyV(z));
237 n++;
238 }
239 else nbad++;
240 }
241 else {
242 q = str.find_first_of(sep,p);
243 if ( !isdigit(str[p]) && !(str[p] == '+')
244 && !(str[p] == '-') && !(str[p] == '.') ) { // une chaine
245 vecv_.push_back(MuTyV(str.substr(p,q-p)));
246 n++;
247 }
248 else { // C'est un nombre
249 if (str.find_first_of(".eE",p) < q) { // c'est un flottant
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)
268 \param clm : Lines starting with clm character are treated as comment lines
269 \param sep : word separator in lines
270 \return Number of decoded elements
271*/
272sa_size_t EnumeratedSequence::FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc,
273 char clm, const char* sep)
274{
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 /* 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()) {
289 if ((line.length() > 0) && (line[0]!=clm)) {
290 nel = Append(line, nbad, sep);
291 if (nel > 0) {
292 nr++; n += nel;
293 }
294 nbadtot += nbad;
295 }
296 }
297 }
298/* Reza, Juin 2005 : plus necessaire
299 if ((line.length() > 0) && (line[0]!=clm)) {
300 nel = Append(line, nbad, sep);
301 if (nel > 0) { nr++; n += nel; }
302 nbadtot += nbad; line = ""; }
303*/
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);
309}
310
311//////////////////////////////////////////////////////////
312/*!
313 \class SOPHYA::Range
314 \ingroup TArray
315 Class to define a range of indices, to be used with TArra<T> TMatrix<T> TVector<T> ...
316*/
317
318/*!
319 Constructor defining defining the range of indices, starting from \b start to the last.
320 \param start : start index (inclusive)
321*/
322Range::Range(sa_size_t start)
323{
324 start_ = start;
325 end_ = Range::lastIndex();
326 size_ = 0;
327 step_ = 1;
328}
329
330/*!
331 Constructor defining defining the range of indices, from \b start to \b end
332 \param start : start index (inclusive)
333 \param end : end index (inclusive)
334*/
335Range::Range(sa_size_t start, sa_size_t end)
336{
337 start_ = start;
338 end_ = end;
339 if (end >= start) size_ = end-start+1;
340 else size_ = 0;
341 step_ = 1;
342}
343
344/*!
345 Constructor defining defining the range of indices, from \b start to \b end
346 \param start : start index (inclusive)
347 \param end : end index (inclusive)
348 \param step : step (or stride) = index increment
349*/
350Range::Range(sa_size_t start, sa_size_t end, sa_size_t step)
351{
352 start_ = start;
353 end_ = end;
354 step_ = (step > 0) ? step : 1;
355 if (step < 1) step = 1;
356 if (end >= start)
357 size_ = (end-start)/step_+1;
358 else size_ = 0;
359}
360
361/*!
362 Define a range of indices
363 \param start : start index (inclusive)
364 \param end : end index (inclusive, used if size \<= 0 and end \>= start)
365 \param size : size (number of elements, used if \>= 1 )
366 \param step : step (or stride) = index increment
367
368 \warning If \b size \>= 1 , \b end index computed automatically.
369 If \b size \< 1 and \b end < \b start , equivalent to \b end = Range()::lastIndex()
370 */
371Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
372{
373 start_ = start;
374 step_ = (step > 0) ? step : 1;
375 if (size > 0) { // Nb d'elements fixe
376 size_ = size;
377 if (end == Range::lastIndex()) start_ = end_ = end;
378 else end_ = start_+size_*step_;
379 }
380 else {
381 if (end >= start) { // Indice de fin fixe
382 end_ = end;
383 size_ = (end-start)/step_+1;
384 }
385 else { // rien fixe
386 size_ = 0;
387 end_ = Range::lastIndex();
388 }
389 }
390}
391
392/*
393Range::Range(Range const& a)
394{
395 start_ = a.start_;
396 end_ = a.end_;
397 size_ = a.size_;
398 step_ = a.step_;
399}
400*/
401
402/*!
403 This method is called to recompute index ranges, specifying the original array size
404 by the TArray<T> (or derived classes) sub array extraction methods
405*/
406void Range::Update(sa_size_t osz)
407{
408 if (end_ >= 0) return;
409 if (osz == 0) {
410 start_ = end_ = 0;
411 size_ = step_ = 1;
412 return;
413 }
414 if (end_ == start_) {
415 end_ = osz-1;
416 if ((size_ > 0) && (size_ <= osz/step_))
417 start_ = end_ - size_*step_;
418 else {
419 start_ = end_;
420 size_ = 1;
421 }
422 }
423 else {
424 end_ = osz-1;
425 size_ = (end_-start_)/step_+1;
426 }
427 return;
428}
429
430/*
431Range & Range::operator = (sa_size_t start)
432{
433 start_ = start;
434 size_ = 1;
435 step_ = 1;
436 return (*this);
437}
438*/
439
440
441//////////////////////////////////////////////////////////
442/*!
443 \class SOPHYA::IdentityMatrix
444 \ingroup TArray
445 Class to define an identity matrix
446*/
447
448//! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
449IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
450{
451 size_ = n;
452 diag_ = diag;
453}
Note: See TracBrowser for help on using the repository browser.