[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] | 10 | Sequence::~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] | 27 | RandomSequence::RandomSequence(int typ, double m, double s)
|
---|
| 28 | {
|
---|
| 29 | typ_ = (typ == Flat) ? Flat : Gaussian;
|
---|
| 30 | mean_ = m;
|
---|
| 31 | sig_ = s;
|
---|
| 32 | }
|
---|
[1103] | 33 | RandomSequence::~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] | 43 | double RandomSequence::Rand()
|
---|
| 44 | {
|
---|
| 45 | if (typ_ == Flat)
|
---|
| 46 | return(drandpm1()*sig_ + mean_);
|
---|
| 47 | else return(GauRnd(mean_, sig_));
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[1156] | 50 | MuTyV & 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] | 73 | RegularSequence::RegularSequence(double start, double step, Arr_DoubleFunctionOfX f)
|
---|
[785] | 74 | {
|
---|
| 75 | start_ = start;
|
---|
| 76 | step_ = step;
|
---|
| 77 | myf_ = f;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1103] | 80 | RegularSequence::~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] | 93 | MuTyV & 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;
|
---|
| 114 | \endcode
|
---|
| 115 | */
|
---|
| 116 |
|
---|
| 117 | EnumeratedSequence::EnumeratedSequence()
|
---|
| 118 | {
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[1103] | 121 | EnumeratedSequence::~EnumeratedSequence()
|
---|
| 122 | {
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[1404] | 125 | //! Return the k th value in the sequence (default = 0)
|
---|
[1156] | 126 | MuTyV & EnumeratedSequence::Value (sa_size_t k) const
|
---|
[1103] | 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 |
|
---|
[1156] | 139 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
|
---|
| 140 | {
|
---|
| 141 | vecv_.clear();
|
---|
| 142 | vecv_.push_back(v);
|
---|
| 143 | return(*this);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[894] | 146 | //////////////////////////////////////////////////////////
|
---|
[926] | 147 | /*!
|
---|
| 148 | \class SOPHYA::Range
|
---|
| 149 | \ingroup TArray
|
---|
| 150 | Class to define a range of indexes
|
---|
| 151 | */
|
---|
| 152 |
|
---|
[894] | 153 | //! Constructor
|
---|
| 154 | /*!
|
---|
| 155 | Define a range of indexes
|
---|
[1412] | 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)
|
---|
[894] | 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 | */
|
---|
[1156] | 164 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
|
---|
[785] | 165 | {
|
---|
| 166 | start_ = start;
|
---|
| 167 | step_ = (step > 0) ? step : 1;
|
---|
[813] | 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 | }
|
---|
[785] | 177 | }
|
---|
| 178 |
|
---|
[804] | 179 | /*
|
---|
[1156] | 180 | Range & Range::operator = (sa_size_t start)
|
---|
[785] | 181 | {
|
---|
| 182 | start_ = start;
|
---|
| 183 | size_ = 1;
|
---|
| 184 | step_ = 1;
|
---|
| 185 | return (*this);
|
---|
| 186 | }
|
---|
[804] | 187 | */
|
---|
| 188 |
|
---|
| 189 |
|
---|
[894] | 190 | //////////////////////////////////////////////////////////
|
---|
[926] | 191 | /*!
|
---|
| 192 | \class SOPHYA::IdentityMatrix
|
---|
| 193 | \ingroup TArray
|
---|
| 194 | Class to define an identity matrix
|
---|
| 195 | */
|
---|
| 196 |
|
---|
[894] | 197 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
|
---|
[1156] | 198 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
|
---|
[804] | 199 | {
|
---|
| 200 | size_ = n;
|
---|
| 201 | diag_ = diag;
|
---|
| 202 | }
|
---|