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

Last change on this file since 2213 was 2149, checked in by ansari, 23 years ago

Appel explicite de l'operateur de conversion sur objet MuTyV ds utilarr.cc , Reza 31/7/2002

File size: 8.2 KB
Line 
1// Utility classes for template numerical arrays
2// R. Ansari, C.Magneville 03/2000
3
4#include "machdefs.h"
5#include "utilarr.h"
6#include "srandgen.h"
7
8// Classe utilitaires
9
10Sequence::~Sequence()
11{
12}
13
14//////////////////////////////////////////////////////////
15/*!
16 \class SOPHYA::RandomSequence
17 \ingroup TArray
18 Base class to generate a sequence of random values
19*/
20
21//! Constructor
22/*!
23 \param typ : generator type
24 \param m : mean parameter of the generator (if needed)
25 \param s : sigma parameter of the generator (if needed)
26 */
27RandomSequence::RandomSequence(int typ, double m, double s)
28{
29 typ_ = (typ == Flat) ? Flat : Gaussian;
30 mean_ = m;
31 sig_ = s;
32}
33RandomSequence::~RandomSequence()
34{
35}
36
37//! Return random sequence values.
38/*!
39 \return If typ = Flat : return [-1,+1]*sig + mean
40 \return If typ = Gaussian : return gaussian distributed
41 with \b mean mean and sigma \b sig
42 */
43double RandomSequence::Rand()
44{
45 if (typ_ == Flat)
46 return(drandpm1()*sig_ + mean_);
47 else return(GauRnd(mean_, sig_));
48}
49
50MuTyV & RandomSequence::Value(sa_size_t k) const
51{
52 if (typ_ == Flat) retv_ = drandpm1()*sig_ + mean_;
53 else retv_ = GauRnd(mean_, sig_);
54 return retv_;
55}
56
57
58//////////////////////////////////////////////////////////
59/*!
60 \class SOPHYA::RegularSequence
61 \ingroup TArray
62 Class to generate a sequence of values
63*/
64
65//! Constructor
66/*!
67 \param start : start value
68 \param step : step value
69 \param f : pointer to the sequence function (default = NULL, f(x)=x )
70
71 See \ref RegularSequenceOperat "operator()"
72 */
73RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
74{
75 start_ = start;
76 step_ = step;
77 myf_ = f;
78}
79
80RegularSequence::~RegularSequence()
81{
82}
83
84//! Get the \b k th value
85/*!
86 \param k : index of the value
87 \anchor RegularSequenceOperat
88
89 \return f(start+k*step)
90
91 */
92
93MuTyV & RegularSequence::Value (sa_size_t k) const
94{
95 double x = start_+(double)k*step_;
96 if (myf_) x = myf_(x);
97 retv_ = x;
98 return(retv_);
99}
100
101/*!
102 \class SOPHYA::EnumeratedSequence
103 \ingroup TArray
104 Explicitly defined sequence of values. The comma operator has
105 been redefined to let an easy definition of sequences.
106
107 \code
108 // Initializing a sequence
109 EnumeratedSequence es;
110 es = 11, 22, 33, 44, 55, 66;
111
112 for(int k=0; k<8; k++)
113 cout << " k= " << k << " es(k)= " << es(k) << endl;
114
115 // Decoding a sequence from a string
116 EnumeratedSequence ess;
117 int nbad;
118 ess.Append("56.5 (1.,-1.) 4 8 16", nbad);
119 cout << ess;
120 \endcode
121*/
122
123//! Default constructor
124EnumeratedSequence::EnumeratedSequence()
125{
126}
127
128//! Copy constructor
129EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es)
130{
131 Append(es);
132}
133
134EnumeratedSequence::~EnumeratedSequence()
135{
136}
137
138//! Return the k th value in the sequence (default = 0)
139MuTyV & EnumeratedSequence::Value (sa_size_t k) const
140{
141 if (k >= vecv_.size()) retv_ = 0;
142 else retv_ = vecv_[k];
143 return(retv_);
144}
145
146//! Appends a new value to the sequence
147EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
148{
149 vecv_.push_back(v);
150 return(*this);
151}
152
153//! Initialize the sequence with a single value \b v
154EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
155{
156 vecv_.clear();
157 vecv_.push_back(v);
158 return(*this);
159}
160
161//! Copy operator
162EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq)
163{
164 Clear();
165 Append(seq);
166 return(*this);
167}
168
169
170//! Prints the list to the output stream \b os
171void EnumeratedSequence::Print(ostream& os) const
172{
173 os << " EnumeratedSequence::Print() - Size()= " << Size() << endl;
174 for(int k=0; k<vecv_.size(); k++) {
175 os << vecv_[k];
176 if ((k > 0) && (k%10 == 0)) os << endl;
177 else os << " " ;
178 }
179 os << endl;
180 return;
181}
182
183//! Append the \b seq to the end of the sequence.
184/*!
185 \return the number of added elements.
186*/
187sa_size_t EnumeratedSequence::Append(EnumeratedSequence const & seq)
188{
189 for(int k=0; k<seq.vecv_.size(); k++)
190 vecv_.push_back(seq.vecv_[k]);
191 return(seq.vecv_.size());
192}
193
194//! Decodes the string, appending values to the end of the sequence.
195/*!
196 \param str : string to be decoded
197 \param nbad : number of unmatched quotes or parenthesis
198 \return the number of added elements.
199*/
200sa_size_t EnumeratedSequence::Append(string const & str, int& nbad)
201{
202 nbad = 0;
203 sa_size_t n = 0;
204 size_t l = str.length();
205 if (l < 1) return(0);
206 if ((str[0] == '#') || (str[0] == '*')) return(0);
207 size_t q = 0;
208 size_t p = str.find_first_not_of(" \t");
209 if ((str[p] == '+') || (str[p] == '-')) {
210 if (p == l-1) return(0);
211 if (!isdigit(str[p+1])) return(0);
212 }
213 else if (!isdigit(str[p]) && (str[p] != '\'') && (str[p] != '(') ) return(0);
214
215 while(q < l) {
216 p = str.find_first_not_of(" \t",q);
217 if (p >= l) break;
218 if (str[p] == '\'') { // Decodage d'un string
219 q = str.find('\'',p+1);
220 if (q < l) {
221 vecv_.push_back(MuTyV(str.substr(p+1,q-p-1)));
222 n++; q++;
223 }
224 else nbad++;
225 }
226 else if (str[p] == '(') { // Decodage d'un complex
227 q = str.find(')',p);
228 if (q < l) {
229 q++;
230 MuTyV mtv(str.substr(p,q-p));
231 complex<r_8> z = mtv.operator complex<r_8>();
232 vecv_.push_back(MuTyV(z));
233 n++;
234 }
235 else nbad++;
236 }
237 else {
238 q = str.find_first_of(" \t",p);
239 if (!isdigit(str[p]) && !(str[p] == '+') && !(str[p] == '-') ) { // une chaine
240 continue;
241 }
242 else { // C'est un nombre
243 if (str.find('.',p) < q) { // c'est un flottant
244 r_8 x = atof(str.substr(p,q-p).c_str());
245 vecv_.push_back(MuTyV(x));
246 }
247 else { // un entier
248 int_8 l = atol(str.substr(p,q-p).c_str());
249 vecv_.push_back(MuTyV(l));
250 }
251 n++;
252 }
253 }
254 }
255 return (n);
256}
257
258//! Decodes the input ASCII stream, creating a sequence of values
259/*! \param is : Input ASCII stream
260 \param nr : Number of non empty (or comment) lines in stream (return value)
261 \param nc : Number of columns (= ntot/nlines) (return value)
262 \return Number of decoded elements
263*/
264sa_size_t EnumeratedSequence::FillFromFile(istream& is,
265 sa_size_t& nr, sa_size_t& nc)
266{
267 nr = 0;
268 nc = 0;
269 sa_size_t n = 0;
270 char buff[256];
271 string line;
272 int nbad, nbadtot, nel;
273 nbadtot = nbad = 0;
274 while (!is.eof()) {
275 is.clear();
276 is.getline(buff, 256);
277 // cout << " DBG : buff=" << buff << " :state=" << is.rdstate() << endl;
278 line += buff;
279 if (is.good()) {
280 nel = Append(line, nbad);
281 // cout << " Decoding line = " << line << " Nel= " << nel << endl;
282 if (nel > 0) {
283 nr++; n += nel;
284 }
285 nbadtot += nbad;
286 line = "";
287 }
288 }
289 if (line.length() > 0) {
290 nel = Append(line, nbad);
291 // cout << " Decoding Eline = " << line << " Nel= " << nel << endl;
292 if (nel > 0) {
293 nr++; n += nel;
294 }
295 nbadtot += nbad;
296 line = "";
297 }
298 if (nbadtot > 0)
299 cout << "EnumeratedSequence::FillFromFile()/Warning " << nbadtot
300 << " bad match (quotes or parenthesis) in stream " << endl;
301 nc = n/nr;
302 return (n);
303}
304
305//////////////////////////////////////////////////////////
306/*!
307 \class SOPHYA::Range
308 \ingroup TArray
309 Class to define a range of indexes
310*/
311
312//! Constructor
313/*!
314 Define a range of indexes
315 \param start : start index (inclusive)
316 \param end : end index (inclusive)
317 \param size : size (number of elements, used if end \<= start)
318 \param step : step (or stride)
319
320 \warning If \b end \> \b start, \b size is computed automatically
321 \warning If not \b size is fixed and \b end recomputed
322 */
323Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
324{
325 start_ = start;
326 step_ = (step > 0) ? step : 1;
327 if (end > start) { // Taille calcule automatiquement
328 end_ = end;
329 if (step_ > ((end_-start_)+1)) size_ = 1;
330 else size_ = ((end-start)+1)/step_;
331 }
332 else { // Taille fixee
333 size_ = size;
334 end_ = start_+size_*step_;
335 }
336}
337
338/*
339Range & Range::operator = (sa_size_t start)
340{
341 start_ = start;
342 size_ = 1;
343 step_ = 1;
344 return (*this);
345}
346*/
347
348
349//////////////////////////////////////////////////////////
350/*!
351 \class SOPHYA::IdentityMatrix
352 \ingroup TArray
353 Class to define an identity matrix
354*/
355
356//! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
357IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
358{
359 size_ = n;
360 diag_ = diag;
361}
Note: See TracBrowser for help on using the repository browser.