[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
|
---|
| 18 | Class to generate a random sequence of values
|
---|
| 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 |
|
---|
[1103] | 50 | MuTyV & 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 | }
|
---|
[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
|
---|
| 69 | \param f : pointer to the sequence function
|
---|
| 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
|
---|
[894] | 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 | */
|
---|
[1103] | 97 |
|
---|
| 98 | MuTyV & RegularSequence::Value (uint_8 k) const
|
---|
[785] | 99 | {
|
---|
[1103] | 100 | double x = start_+(double)k*step_;
|
---|
| 101 | if (myf_) x = myf_(x);
|
---|
| 102 | retv_ = x;
|
---|
| 103 | return(retv_);
|
---|
[785] | 104 | }
|
---|
| 105 |
|
---|
[1103] | 106 | EnumeratedSequence::~EnumeratedSequence()
|
---|
| 107 | {
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | MuTyV & EnumeratedSequence::Value (uint_8 k) const
|
---|
| 111 | {
|
---|
| 112 | if (k >= vecv_.size()) retv_ = 0;
|
---|
| 113 | else retv_ = vecv_[k];
|
---|
| 114 | return(retv_);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | EnumeratedSequence & EnumeratedSequence::operator , (MuTyV const & v)
|
---|
| 118 | {
|
---|
| 119 | vecv_.push_back(v);
|
---|
| 120 | return(*this);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[894] | 123 | //////////////////////////////////////////////////////////
|
---|
[926] | 124 | /*!
|
---|
| 125 | \class SOPHYA::Range
|
---|
| 126 | \ingroup TArray
|
---|
| 127 | Class to define a range of indexes
|
---|
| 128 | */
|
---|
| 129 |
|
---|
[894] | 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 | */
|
---|
[813] | 141 | Range::Range(uint_4 start, uint_4 end, uint_4 size, uint_4 step)
|
---|
[785] | 142 | {
|
---|
| 143 | start_ = start;
|
---|
| 144 | step_ = (step > 0) ? step : 1;
|
---|
[813] | 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 | }
|
---|
[785] | 154 | }
|
---|
| 155 |
|
---|
[804] | 156 | /*
|
---|
[785] | 157 | Range & Range::operator = (uint_4 start)
|
---|
| 158 | {
|
---|
| 159 | start_ = start;
|
---|
| 160 | size_ = 1;
|
---|
| 161 | step_ = 1;
|
---|
| 162 | return (*this);
|
---|
| 163 | }
|
---|
[804] | 164 | */
|
---|
| 165 |
|
---|
| 166 |
|
---|
[894] | 167 | //////////////////////////////////////////////////////////
|
---|
[926] | 168 | /*!
|
---|
| 169 | \class SOPHYA::IdentityMatrix
|
---|
| 170 | \ingroup TArray
|
---|
| 171 | Class to define an identity matrix
|
---|
| 172 | */
|
---|
| 173 |
|
---|
[894] | 174 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
|
---|
[804] | 175 | IdentityMatrix::IdentityMatrix(double diag, uint_4 n)
|
---|
| 176 | {
|
---|
| 177 | size_ = n;
|
---|
| 178 | diag_ = diag;
|
---|
| 179 | }
|
---|