| 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 |  | 
|---|
| 10 | Sequence::~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 | */ | 
|---|
| 27 | RandomSequence::RandomSequence(int typ, double m, double s) | 
|---|
| 28 | { | 
|---|
| 29 | typ_ = (typ == Flat) ? Flat : Gaussian; | 
|---|
| 30 | mean_ = m; | 
|---|
| 31 | sig_ = s; | 
|---|
| 32 | } | 
|---|
| 33 | RandomSequence::~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 | */ | 
|---|
| 43 | double RandomSequence::Rand() | 
|---|
| 44 | { | 
|---|
| 45 | if (typ_ == Flat) | 
|---|
| 46 | return(drandpm1()*sig_ + mean_); | 
|---|
| 47 | else return(GauRnd(mean_, sig_)); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | MuTyV & 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 | */ | 
|---|
| 73 | RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f) | 
|---|
| 74 | { | 
|---|
| 75 | start_ = start; | 
|---|
| 76 | step_ = step; | 
|---|
| 77 | myf_ = f; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | RegularSequence::~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 |  | 
|---|
| 93 | MuTyV & 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 | \endcode | 
|---|
| 115 | */ | 
|---|
| 116 |  | 
|---|
| 117 | EnumeratedSequence::EnumeratedSequence() | 
|---|
| 118 | { | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | EnumeratedSequence::~EnumeratedSequence() | 
|---|
| 122 | { | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | //! Return the k th value in the sequence (default = 0) | 
|---|
| 126 | MuTyV & EnumeratedSequence::Value (sa_size_t k) const | 
|---|
| 127 | { | 
|---|
| 128 | if (k >= vecv_.size())  retv_ = 0; | 
|---|
| 129 | else retv_ = vecv_[k]; | 
|---|
| 130 | return(retv_); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v) | 
|---|
| 134 | { | 
|---|
| 135 | vecv_.push_back(v); | 
|---|
| 136 | return(*this); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v) | 
|---|
| 140 | { | 
|---|
| 141 | vecv_.clear(); | 
|---|
| 142 | vecv_.push_back(v); | 
|---|
| 143 | return(*this); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | ////////////////////////////////////////////////////////// | 
|---|
| 147 | /*! | 
|---|
| 148 | \class SOPHYA::Range | 
|---|
| 149 | \ingroup TArray | 
|---|
| 150 | Class to define a range of indexes | 
|---|
| 151 | */ | 
|---|
| 152 |  | 
|---|
| 153 | //! Constructor | 
|---|
| 154 | /*! | 
|---|
| 155 | Define a range of indexes | 
|---|
| 156 | \param start : start index (inclusive) | 
|---|
| 157 | \param end : end index (inclusive) | 
|---|
| 158 | \param size : size (number of elements, used if end \<= start) | 
|---|
| 159 | \param step : step (or stride) | 
|---|
| 160 |  | 
|---|
| 161 | \warning If \b end \> \b start, \b size is computed automatically | 
|---|
| 162 | \warning If not \b size is fixed and \b end recomputed | 
|---|
| 163 | */ | 
|---|
| 164 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step) | 
|---|
| 165 | { | 
|---|
| 166 | start_ = start; | 
|---|
| 167 | step_ = (step > 0) ? step : 1; | 
|---|
| 168 | if (end > start) {  // Taille calcule automatiquement | 
|---|
| 169 | end_ = end; | 
|---|
| 170 | if (step_ > ((end_-start_)+1))  size_ = 1; | 
|---|
| 171 | else size_ = ((end-start)+1)/step_; | 
|---|
| 172 | } | 
|---|
| 173 | else {     // Taille fixee | 
|---|
| 174 | size_ = size; | 
|---|
| 175 | end_ = start_+size_*step_; | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | /* | 
|---|
| 180 | Range & Range::operator = (sa_size_t start) | 
|---|
| 181 | { | 
|---|
| 182 | start_ = start; | 
|---|
| 183 | size_ = 1; | 
|---|
| 184 | step_ = 1; | 
|---|
| 185 | return (*this); | 
|---|
| 186 | } | 
|---|
| 187 | */ | 
|---|
| 188 |  | 
|---|
| 189 |  | 
|---|
| 190 | ////////////////////////////////////////////////////////// | 
|---|
| 191 | /*! | 
|---|
| 192 | \class SOPHYA::IdentityMatrix | 
|---|
| 193 | \ingroup TArray | 
|---|
| 194 | Class to define an identity matrix | 
|---|
| 195 | */ | 
|---|
| 196 |  | 
|---|
| 197 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal | 
|---|
| 198 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n) | 
|---|
| 199 | { | 
|---|
| 200 | size_ = n; | 
|---|
| 201 | diag_ = diag; | 
|---|
| 202 | } | 
|---|