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