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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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