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

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

Documentation + debug Range et extraction sous-tableaux , Reza 23/02/2006

File size: 10.8 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 This class can be used to define a range of indices. Range objects are used to extract
316 sub-arrays and slices, from arrays and matrices.
317 \sa SOPHYA::TArray SOPHYA::TMatrix
318*/
319
320/*!
321 Constructor defining a range corresponding to the single index \b start
322 \param start : start=end index
323*/
324Range::Range(sa_size_t start)
325{
326 start_ = start;
327 end_ = start;
328 size_ = 1;
329 step_ = 1;
330}
331
332/*!
333 Constructor defining defining the range of indices, from \b start to \b end
334 \param start : start index (inclusive)
335 \param end : end index (inclusive)
336*/
337Range::Range(sa_size_t start, sa_size_t end)
338{
339 start_ = start;
340 end_ = end;
341 if (end >= start) size_ = end-start+1;
342 else size_ = 0;
343 step_ = 1;
344}
345
346/*!
347 Constructor defining defining the range of indices, from \b start to \b end
348 \param start : start index (inclusive)
349 \param end : end index (inclusive)
350 \param step : step (or stride) = index increment
351*/
352Range::Range(sa_size_t start, sa_size_t end, sa_size_t step)
353{
354 start_ = start;
355 end_ = end;
356 step_ = (step > 0) ? step : 1;
357 if (step < 1) step = 1;
358 if (end >= start)
359 size_ = (end-start)/step_+1;
360 else size_ = 0;
361}
362
363/*!
364 Define a range of indices
365 \param start : start index (inclusive)
366 \param end : end index (inclusive, used if size \<= 0 and end \>= start)
367 \param size : size (number of elements, used if \>= 1 )
368 \param step : step (or stride) = index increment
369
370 \warning If \b size \>= 1 , \b end index computed automatically.
371 If \b size \< 1 and \b end < \b start , equivalent to \b end = Range()::lastIndex()
372 */
373Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
374{
375 start_ = start;
376 step_ = (step > 0) ? step : 1;
377 if (size > 0) { // Nb d'elements fixe
378 size_ = size;
379 if ( (start == end) && (end == Range::lastIndex()) ) start_ = end_ = end;
380 else end_ = start_+(size_-1)*step_;
381 }
382 else {
383 if (end >= start) { // Indice de fin fixe
384 end_ = end;
385 size_ = (end-start)/step_+1;
386 }
387 else { // rien fixe
388 size_ = 0;
389 end_ = Range::lastIndex();
390 }
391 }
392}
393
394/*
395Range::Range(Range const& a)
396{
397 start_ = a.start_;
398 end_ = a.end_;
399 size_ = a.size_;
400 step_ = a.step_;
401}
402*/
403
404/*!
405 This method is called to recompute index ranges, specifying the original array size
406 by the TArray<T> (or derived classes) sub array extraction methods
407*/
408void Range::Update(sa_size_t osz)
409{
410 if (end_ >= 0) return;
411 if (osz == 0) {
412 start_ = end_ = 0;
413 size_ = step_ = 1;
414 return;
415 }
416 if (end_ == start_) {
417 end_ = osz-1;
418 if ((size_ > 0) && (size_ <= osz/step_))
419 start_ = end_ - (size_-1)*step_;
420 else {
421 start_ = end_;
422 size_ = 1;
423 }
424 }
425 else {
426 end_ = osz-1;
427 size_ = (end_-start_)/step_+1;
428 }
429 //DBG cout << ">>> DBG/Update start=" << start_ << " end=" << end_
430 //DBG << " size=" << size_ << " step=" << step_ << endl;
431 return;
432}
433
434/*
435Range & Range::operator = (sa_size_t start)
436{
437 start_ = start;
438 size_ = 1;
439 step_ = 1;
440 return (*this);
441}
442*/
443
444
445//////////////////////////////////////////////////////////
446/*!
447 \class SOPHYA::IdentityMatrix
448 \ingroup TArray
449 Class to define an identity matrix. This class is mainly intented for
450 initializing TMatrix<T> objects, proportional to the identity matrix.
451*/
452
453//! Constructor representing a square matrix (\b n x \b n) with value \b diag on the diagonal
454IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
455{
456 size_ = n;
457 diag_ = diag;
458}
Note: See TracBrowser for help on using the repository browser.