[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 |
|
---|
[894] | 10 | //////////////////////////////////////////////////////////
|
---|
[926] | 11 | /*!
|
---|
| 12 | \class SOPHYA::RandomSequence
|
---|
| 13 | \ingroup TArray
|
---|
| 14 | Class to generate a random sequence of values
|
---|
| 15 | */
|
---|
| 16 |
|
---|
[894] | 17 | //! Constructor
|
---|
| 18 | /*!
|
---|
| 19 | \param typ : generator type
|
---|
| 20 | \param m : mean parameter of the generator (if needed)
|
---|
| 21 | \param s : sigma parameter of the generator (if needed)
|
---|
| 22 | */
|
---|
[850] | 23 | RandomSequence::RandomSequence(int typ, double m, double s)
|
---|
| 24 | {
|
---|
| 25 | typ_ = (typ == Flat) ? Flat : Gaussian;
|
---|
| 26 | mean_ = m;
|
---|
| 27 | sig_ = s;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[894] | 30 | //! Return random sequence values.
|
---|
[850] | 31 | double RandomSequence::Rand()
|
---|
| 32 | {
|
---|
| 33 | if (typ_ == Flat)
|
---|
| 34 | return(drandpm1()*sig_ + mean_);
|
---|
| 35 | else return(GauRnd(mean_, sig_));
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
[894] | 39 | //////////////////////////////////////////////////////////
|
---|
[926] | 40 | /*!
|
---|
| 41 | \class SOPHYA::Sequence
|
---|
| 42 | \ingroup TArray
|
---|
| 43 | Class to generate a sequence of values
|
---|
| 44 | */
|
---|
| 45 |
|
---|
[894] | 46 | //! Constructor
|
---|
| 47 | /*!
|
---|
| 48 | \param start : start value
|
---|
| 49 | \param step : step value
|
---|
| 50 | \param f : pointer to the sequence function
|
---|
| 51 |
|
---|
| 52 | See \ref SequenceOperat "operator()"
|
---|
| 53 | */
|
---|
[785] | 54 | Sequence::Sequence(double start, double step, Arr_DoubleFunctionOfX f)
|
---|
[850] | 55 | : rseq_(RandomSequence::Gaussian)
|
---|
[785] | 56 | {
|
---|
| 57 | start_ = start;
|
---|
| 58 | step_ = step;
|
---|
| 59 | myf_ = f;
|
---|
[850] | 60 | fgrseq_ = false;
|
---|
[785] | 61 | }
|
---|
| 62 |
|
---|
[894] | 63 | //! Constructor
|
---|
| 64 | /*!
|
---|
| 65 | \param rseq : RandomSequence
|
---|
| 66 |
|
---|
| 67 | See \ref SequenceOperat "operator()"
|
---|
| 68 | */
|
---|
[850] | 69 | Sequence::Sequence(RandomSequence rseq)
|
---|
| 70 | {
|
---|
| 71 | start_ = 0.;
|
---|
| 72 | step_ = 1.;
|
---|
| 73 | myf_ = NULL;
|
---|
| 74 | rseq_ = rseq;
|
---|
| 75 | fgrseq_ = true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[894] | 78 | //! Get the \b k th value
|
---|
| 79 | /*!
|
---|
| 80 | \param k : index of the value
|
---|
| 81 | \anchor SequenceOperat
|
---|
| 82 |
|
---|
| 83 | If the constructor was done with RandomSequence, return a RandomSequence
|
---|
| 84 | and \b k doesn't matter.
|
---|
| 85 |
|
---|
| 86 | If the constructor has a NULL Arr_DoubleFunctionOfX, return start+k*step
|
---|
| 87 |
|
---|
| 88 | If the constructor has a not NULL Arr_DoubleFunctionOfX, return f(start+k*step)
|
---|
| 89 | \return the \b k th value
|
---|
| 90 | */
|
---|
[785] | 91 | double Sequence::operator () (uint_4 k)
|
---|
| 92 | {
|
---|
[850] | 93 | if (fgrseq_) return(rseq_.Rand());
|
---|
| 94 | else {
|
---|
| 95 | double x = start_+(double)k*step_;
|
---|
| 96 | if (myf_) return(myf_(x));
|
---|
| 97 | else return x;
|
---|
| 98 | }
|
---|
[785] | 99 | }
|
---|
| 100 |
|
---|
[894] | 101 | //////////////////////////////////////////////////////////
|
---|
[926] | 102 | /*!
|
---|
| 103 | \class SOPHYA::Range
|
---|
| 104 | \ingroup TArray
|
---|
| 105 | Class to define a range of indexes
|
---|
| 106 | */
|
---|
| 107 |
|
---|
[894] | 108 | //! Constructor
|
---|
| 109 | /*!
|
---|
| 110 | Define a range of indexes
|
---|
| 111 | \param start : start index
|
---|
| 112 | \param end : start end
|
---|
| 113 | \param size : size
|
---|
| 114 | \param step : step
|
---|
| 115 |
|
---|
| 116 | \warning If \b end \> \b start, \b size is computed automatically
|
---|
| 117 | \warning If not \b size is fixed and \b end recomputed
|
---|
| 118 | */
|
---|
[813] | 119 | Range::Range(uint_4 start, uint_4 end, uint_4 size, uint_4 step)
|
---|
[785] | 120 | {
|
---|
| 121 | start_ = start;
|
---|
| 122 | step_ = (step > 0) ? step : 1;
|
---|
[813] | 123 | if (end > start) { // Taille calcule automatiquement
|
---|
| 124 | end_ = end;
|
---|
| 125 | if (step_ > ((end_-start_)+1)) size_ = 1;
|
---|
| 126 | else size_ = ((end-start)+1)/step_;
|
---|
| 127 | }
|
---|
| 128 | else { // Taille fixee
|
---|
| 129 | size_ = size;
|
---|
| 130 | end_ = start_+size_*step_;
|
---|
| 131 | }
|
---|
[785] | 132 | }
|
---|
| 133 |
|
---|
[804] | 134 | /*
|
---|
[785] | 135 | Range & Range::operator = (uint_4 start)
|
---|
| 136 | {
|
---|
| 137 | start_ = start;
|
---|
| 138 | size_ = 1;
|
---|
| 139 | step_ = 1;
|
---|
| 140 | return (*this);
|
---|
| 141 | }
|
---|
[804] | 142 | */
|
---|
| 143 |
|
---|
| 144 |
|
---|
[894] | 145 | //////////////////////////////////////////////////////////
|
---|
[926] | 146 | /*!
|
---|
| 147 | \class SOPHYA::IdentityMatrix
|
---|
| 148 | \ingroup TArray
|
---|
| 149 | Class to define an identity matrix
|
---|
| 150 | */
|
---|
| 151 |
|
---|
[894] | 152 | //! Constructor of a (n,n) diagonal matrix with value diag on the diagonal
|
---|
[804] | 153 | IdentityMatrix::IdentityMatrix(double diag, uint_4 n)
|
---|
| 154 | {
|
---|
| 155 | size_ = n;
|
---|
| 156 | diag_ = diag;
|
---|
| 157 | }
|
---|