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

Last change on this file since 1116 was 1103, checked in by ansari, 25 years ago

Suite operations entre matrices de <> MemOrg, Amelioration des Sequences - Reza 27/7/2000

File size: 3.7 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 Class to generate a random sequence of 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(uint_8 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
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 If the constructor was done with RandomSequence, return a RandomSequence
90 and \b k doesn't matter.
91
92 If the constructor has a NULL Arr_DoubleFunctionOfX, return start+k*step
93
94 If the constructor has a not NULL Arr_DoubleFunctionOfX, return f(start+k*step)
95 \return the \b k th value
96 */
97
98MuTyV & RegularSequence::Value (uint_8 k) const
99{
100 double x = start_+(double)k*step_;
101 if (myf_) x = myf_(x);
102 retv_ = x;
103 return(retv_);
104}
105
106EnumeratedSequence::~EnumeratedSequence()
107{
108}
109
110MuTyV & EnumeratedSequence::Value (uint_8 k) const
111{
112 if (k >= vecv_.size()) retv_ = 0;
113 else retv_ = vecv_[k];
114 return(retv_);
115}
116
117EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
118{
119 vecv_.push_back(v);
120 return(*this);
121}
122
123//////////////////////////////////////////////////////////
124/*!
125 \class SOPHYA::Range
126 \ingroup TArray
127 Class to define a range of indexes
128*/
129
130//! Constructor
131/*!
132 Define a range of indexes
133 \param start : start index
134 \param end : start end
135 \param size : size
136 \param step : step
137
138 \warning If \b end \> \b start, \b size is computed automatically
139 \warning If not \b size is fixed and \b end recomputed
140 */
141Range::Range(uint_4 start, uint_4 end, uint_4 size, uint_4 step)
142{
143 start_ = start;
144 step_ = (step > 0) ? step : 1;
145 if (end > start) { // Taille calcule automatiquement
146 end_ = end;
147 if (step_ > ((end_-start_)+1)) size_ = 1;
148 else size_ = ((end-start)+1)/step_;
149 }
150 else { // Taille fixee
151 size_ = size;
152 end_ = start_+size_*step_;
153 }
154}
155
156/*
157Range & Range::operator = (uint_4 start)
158{
159 start_ = start;
160 size_ = 1;
161 step_ = 1;
162 return (*this);
163}
164*/
165
166
167//////////////////////////////////////////////////////////
168/*!
169 \class SOPHYA::IdentityMatrix
170 \ingroup TArray
171 Class to define an identity matrix
172*/
173
174//! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
175IdentityMatrix::IdentityMatrix(double diag, uint_4 n)
176{
177 size_ = n;
178 diag_ = diag;
179}
Note: See TracBrowser for help on using the repository browser.