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

Last change on this file since 2147 was 1560, checked in by ansari, 24 years ago

Suite (et fin ?) de lecture/ecriture ASCII pour TArray, Correction petit bug ds tvector.cc - Reza 3/7/2001

File size: 8.2 KB
RevLine 
[785]1// Utility classes for template numerical arrays
2// R. Ansari, C.Magneville 03/2000
3
4#include "machdefs.h"
5#include "utilarr.h"
[850]6#include "srandgen.h"
[785]7
8// Classe utilitaires
[850]9
[1103]10Sequence::~Sequence()
11{
12}
13
[894]14//////////////////////////////////////////////////////////
[926]15/*!
16 \class SOPHYA::RandomSequence
17 \ingroup TArray
[1404]18 Base class to generate a sequence of random values
[926]19*/
20
[894]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 */
[850]27RandomSequence::RandomSequence(int typ, double m, double s)
28{
29 typ_ = (typ == Flat) ? Flat : Gaussian;
30 mean_ = m;
31 sig_ = s;
32}
[1103]33RandomSequence::~RandomSequence()
34{
35}
[850]36
[894]37//! Return random sequence values.
[935]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 */
[850]43double RandomSequence::Rand()
44{
45 if (typ_ == Flat)
46 return(drandpm1()*sig_ + mean_);
47 else return(GauRnd(mean_, sig_));
48}
49
[1156]50MuTyV & RandomSequence::Value(sa_size_t k) const
[1103]51{
52 if (typ_ == Flat) retv_ = drandpm1()*sig_ + mean_;
53 else retv_ = GauRnd(mean_, sig_);
54 return retv_;
55}
[850]56
[1103]57
[894]58//////////////////////////////////////////////////////////
[926]59/*!
[1103]60 \class SOPHYA::RegularSequence
[926]61 \ingroup TArray
62 Class to generate a sequence of values
63*/
64
[894]65//! Constructor
66/*!
67 \param start : start value
68 \param step : step value
[1404]69 \param f : pointer to the sequence function (default = NULL, f(x)=x )
[894]70
[1103]71 See \ref RegularSequenceOperat "operator()"
[894]72 */
[1103]73RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
[785]74{
75 start_ = start;
76 step_ = step;
77 myf_ = f;
78}
79
[1103]80RegularSequence::~RegularSequence()
[850]81{
82}
83
[894]84//! Get the \b k th value
85/*!
86 \param k : index of the value
[1103]87 \anchor RegularSequenceOperat
[1404]88
89 \return f(start+k*step)
[894]90
91 */
[1103]92
[1156]93MuTyV & RegularSequence::Value (sa_size_t k) const
[785]94{
[1103]95 double x = start_+(double)k*step_;
96 if (myf_) x = myf_(x);
97 retv_ = x;
98 return(retv_);
[785]99}
100
[1404]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;
[1558]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;
[1404]120 \endcode
121*/
122
[1558]123//! Default constructor
[1404]124EnumeratedSequence::EnumeratedSequence()
125{
126}
127
[1558]128//! Copy constructor
129EnumeratedSequence::EnumeratedSequence(EnumeratedSequence const & es)
130{
131 Append(es);
132}
133
[1103]134EnumeratedSequence::~EnumeratedSequence()
135{
136}
137
[1404]138//! Return the k th value in the sequence (default = 0)
[1156]139MuTyV & EnumeratedSequence::Value (sa_size_t k) const
[1103]140{
141 if (k >= vecv_.size()) retv_ = 0;
142 else retv_ = vecv_[k];
143 return(retv_);
144}
145
[1558]146//! Appends a new value to the sequence
[1103]147EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
148{
149 vecv_.push_back(v);
150 return(*this);
151}
152
[1558]153//! Initialize the sequence with a single value \b v
[1156]154EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
155{
156 vecv_.clear();
157 vecv_.push_back(v);
158 return(*this);
159}
160
[1558]161//! Copy operator
162EnumeratedSequence & EnumeratedSequence::operator = (EnumeratedSequence const & seq)
[1550]163{
[1558]164 Clear();
165 Append(seq);
166 return(*this);
[1550]167}
168
[1558]169
170//! Prints the list to the output stream \b os
[1550]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 }
[1558]179 os << endl;
[1550]180 return;
181}
182
[1558]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)
[1550]188{
[1558]189 for(int k=0; k<seq.vecv_.size(); k++)
190 vecv_.push_back(seq.vecv_[k]);
191 return(seq.vecv_.size());
[1550]192}
193
[1558]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<double> z = mtv;
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*/
[1550]264sa_size_t EnumeratedSequence::FillFromFile(istream& is,
265 sa_size_t& nr, sa_size_t& nc)
266{
[1558]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);
[1560]277 // cout << " DBG : buff=" << buff << " :state=" << is.rdstate() << endl;
[1558]278 line += buff;
279 if (is.good()) {
280 nel = Append(line, nbad);
[1560]281 // cout << " Decoding line = " << line << " Nel= " << nel << endl;
[1558]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);
[1560]291 // cout << " Decoding Eline = " << line << " Nel= " << nel << endl;
[1558]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);
[1550]303}
304
[894]305//////////////////////////////////////////////////////////
[926]306/*!
307 \class SOPHYA::Range
308 \ingroup TArray
309 Class to define a range of indexes
310*/
311
[894]312//! Constructor
313/*!
314 Define a range of indexes
[1412]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)
[894]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 */
[1156]323Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
[785]324{
325 start_ = start;
326 step_ = (step > 0) ? step : 1;
[813]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 }
[785]336}
337
[804]338/*
[1156]339Range & Range::operator = (sa_size_t start)
[785]340{
341 start_ = start;
342 size_ = 1;
343 step_ = 1;
344 return (*this);
345}
[804]346*/
347
348
[894]349//////////////////////////////////////////////////////////
[926]350/*!
351 \class SOPHYA::IdentityMatrix
352 \ingroup TArray
353 Class to define an identity matrix
354*/
355
[894]356//! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
[1156]357IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
[804]358{
359 size_ = n;
360 diag_ = diag;
361}
Note: See TracBrowser for help on using the repository browser.