| 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 | 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 | */
|
|---|
| 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
|
|---|
| 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 | 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 |
|
|---|
| 98 | MuTyV & RegularSequence::Value (sa_size_t k) const
|
|---|
| 99 | {
|
|---|
| 100 | double x = start_+(double)k*step_;
|
|---|
| 101 | if (myf_) x = myf_(x);
|
|---|
| 102 | retv_ = x;
|
|---|
| 103 | return(retv_);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | EnumeratedSequence::~EnumeratedSequence()
|
|---|
| 107 | {
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | MuTyV & EnumeratedSequence::Value (sa_size_t 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 |
|
|---|
| 123 | EnumeratedSequence & EnumeratedSequence::operator = (MuTyV const & v)
|
|---|
| 124 | {
|
|---|
| 125 | vecv_.clear();
|
|---|
| 126 | vecv_.push_back(v);
|
|---|
| 127 | return(*this);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | //////////////////////////////////////////////////////////
|
|---|
| 131 | /*!
|
|---|
| 132 | \class SOPHYA::Range
|
|---|
| 133 | \ingroup TArray
|
|---|
| 134 | Class to define a range of indexes
|
|---|
| 135 | */
|
|---|
| 136 |
|
|---|
| 137 | //! Constructor
|
|---|
| 138 | /*!
|
|---|
| 139 | Define a range of indexes
|
|---|
| 140 | \param start : start index
|
|---|
| 141 | \param end : start end
|
|---|
| 142 | \param size : size
|
|---|
| 143 | \param step : step
|
|---|
| 144 |
|
|---|
| 145 | \warning If \b end \> \b start, \b size is computed automatically
|
|---|
| 146 | \warning If not \b size is fixed and \b end recomputed
|
|---|
| 147 | */
|
|---|
| 148 | Range::Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step)
|
|---|
| 149 | {
|
|---|
| 150 | start_ = start;
|
|---|
| 151 | step_ = (step > 0) ? step : 1;
|
|---|
| 152 | if (end > start) { // Taille calcule automatiquement
|
|---|
| 153 | end_ = end;
|
|---|
| 154 | if (step_ > ((end_-start_)+1)) size_ = 1;
|
|---|
| 155 | else size_ = ((end-start)+1)/step_;
|
|---|
| 156 | }
|
|---|
| 157 | else { // Taille fixee
|
|---|
| 158 | size_ = size;
|
|---|
| 159 | end_ = start_+size_*step_;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /*
|
|---|
| 164 | Range & Range::operator = (sa_size_t start)
|
|---|
| 165 | {
|
|---|
| 166 | start_ = start;
|
|---|
| 167 | size_ = 1;
|
|---|
| 168 | step_ = 1;
|
|---|
| 169 | return (*this);
|
|---|
| 170 | }
|
|---|
| 171 | */
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | //////////////////////////////////////////////////////////
|
|---|
| 175 | /*!
|
|---|
| 176 | \class SOPHYA::IdentityMatrix
|
|---|
| 177 | \ingroup TArray
|
|---|
| 178 | Class to define an identity matrix
|
|---|
| 179 | */
|
|---|
| 180 |
|
|---|
| 181 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
|
|---|
| 182 | IdentityMatrix::IdentityMatrix(double diag, sa_size_t n)
|
|---|
| 183 | {
|
|---|
| 184 | size_ = n;
|
|---|
| 185 | diag_ = diag;
|
|---|
| 186 | }
|
|---|