| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | //     Utility classes for template numerical arrays | 
|---|
| 3 | //                     R. Ansari, C.Magneville   03/2000 | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef UtilArray_SEEN | 
|---|
| 6 | #define UtilArray_SEEN | 
|---|
| 7 |  | 
|---|
| 8 | #include "machdefs.h" | 
|---|
| 9 | #include "mutyv.h" | 
|---|
| 10 | #include "randinterf.h" | 
|---|
| 11 |  | 
|---|
| 12 | #include <stdlib.h> | 
|---|
| 13 | #include <vector> | 
|---|
| 14 |  | 
|---|
| 15 | namespace SOPHYA { | 
|---|
| 16 |  | 
|---|
| 17 | /* Quelques utilitaires pour les tableaux (Array) */ | 
|---|
| 18 |  | 
|---|
| 19 | /*! \ingroup TArray | 
|---|
| 20 | \typedef Arr_DoubleFunctionOfX | 
|---|
| 21 | \brief define a function of double which returns a double | 
|---|
| 22 | */ | 
|---|
| 23 | typedef double (* Arr_DoubleFunctionOfX) (double x); | 
|---|
| 24 | /*! \ingroup TArray | 
|---|
| 25 | \typedef Arr_FloatFunctionOfX | 
|---|
| 26 | \brief define a function of float which returns a float | 
|---|
| 27 | */ | 
|---|
| 28 | typedef float  (* Arr_FloatFunctionOfX)  (float x); | 
|---|
| 29 |  | 
|---|
| 30 | /*! \ingroup TArray | 
|---|
| 31 | \typedef Arr_ComplexDoubleFunctionOfX | 
|---|
| 32 | \brief define a function of a complex<double> which returns a complex<double> | 
|---|
| 33 | */ | 
|---|
| 34 | typedef complex<double> (* Arr_ComplexDoubleFunctionOfX) (complex<double> x); | 
|---|
| 35 | /*! \ingroup TArray | 
|---|
| 36 | \typedef Arr_ComplexFloatFunctionOfX | 
|---|
| 37 | \brief define a function of complex<float> which returns a complex<float> | 
|---|
| 38 | */ | 
|---|
| 39 | typedef float  (* Arr_ComplexFloatFunctionOfX)  (complex<float> x); | 
|---|
| 40 |  | 
|---|
| 41 | ////////////////////////////////////////////////////////// | 
|---|
| 42 | //! Class to generate a sequence of values | 
|---|
| 43 | class Sequence { | 
|---|
| 44 | public: | 
|---|
| 45 | virtual ~Sequence(); | 
|---|
| 46 | virtual MuTyV & Value(sa_size_t k) const = 0; | 
|---|
| 47 | inline MuTyV & operator () (sa_size_t k) const { return(Value(k)) ; } | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | class RandomSequence : public Sequence { | 
|---|
| 51 | public: | 
|---|
| 52 | //! to define the generator type | 
|---|
| 53 | enum { | 
|---|
| 54 | Gaussian = 0, //!< gaussian generator | 
|---|
| 55 | Flat = 1      //!< Flat generator | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | explicit RandomSequence(int typ = RandomSequence::Gaussian, double m=0., double s=1.); | 
|---|
| 59 | explicit RandomSequence(RandomGeneratorInterface& rgen, RNDTypes rtyp=C_RND_Gaussian, double mean=0., double sigma=1.); | 
|---|
| 60 | virtual  ~RandomSequence(); | 
|---|
| 61 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| 62 | double   Next() const ; | 
|---|
| 63 |  | 
|---|
| 64 | protected: | 
|---|
| 65 | RNDTypes typ_;            //!< random generation type | 
|---|
| 66 | double mean_, sig_;  //!< generation parameters mean and sigma (if needed) | 
|---|
| 67 | mutable MuTyV retv_; | 
|---|
| 68 | mutable RandomGeneratorInterface* rgp_; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | ////////////////////////////////////////////////////////// | 
|---|
| 73 | //! Class to generate a sequence of values | 
|---|
| 74 | class RegularSequence : public Sequence { | 
|---|
| 75 | public: | 
|---|
| 76 | explicit RegularSequence (double start=0., double step=1., Arr_DoubleFunctionOfX f=NULL); | 
|---|
| 77 | virtual  ~RegularSequence(); | 
|---|
| 78 |  | 
|---|
| 79 | //! return start value of the sequence | 
|---|
| 80 | inline double & Start() { return start_; } | 
|---|
| 81 | //! return step value of the sequence | 
|---|
| 82 | inline double & Step() { return step_; } | 
|---|
| 83 |  | 
|---|
| 84 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| 85 |  | 
|---|
| 86 | protected: | 
|---|
| 87 | double start_;              //!< start value of the sequence | 
|---|
| 88 | double step_;               //!< step value of the sequence | 
|---|
| 89 | Arr_DoubleFunctionOfX myf_; //!< pointer to the sequence function | 
|---|
| 90 | mutable MuTyV retv_; | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | ////////////////////////////////////////////////////////// | 
|---|
| 94 | //! Class for creation and handling of an explicitly defined list of values | 
|---|
| 95 |  | 
|---|
| 96 | class EnumeratedSequence : public Sequence  { | 
|---|
| 97 | public: | 
|---|
| 98 | explicit EnumeratedSequence(); | 
|---|
| 99 | EnumeratedSequence(EnumeratedSequence const & es); | 
|---|
| 100 | virtual ~EnumeratedSequence(); | 
|---|
| 101 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| 102 |  | 
|---|
| 103 | inline sa_size_t Size() const { return vecv_.size(); } | 
|---|
| 104 |  | 
|---|
| 105 | EnumeratedSequence & operator , (MuTyV const & v); | 
|---|
| 106 | EnumeratedSequence & operator = (MuTyV const & v); | 
|---|
| 107 |  | 
|---|
| 108 | EnumeratedSequence & operator = (EnumeratedSequence const & es); | 
|---|
| 109 |  | 
|---|
| 110 | inline  void         Clear()  { vecv_.clear(); } | 
|---|
| 111 | void                 Print(ostream& os) const; | 
|---|
| 112 |  | 
|---|
| 113 | sa_size_t            Append(EnumeratedSequence const & seq); | 
|---|
| 114 | sa_size_t            Append(string const & str, int & nbad, const char* sep=" \t"); | 
|---|
| 115 | sa_size_t            FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc, | 
|---|
| 116 | char clm='#', const char* sep=" \t"); | 
|---|
| 117 |  | 
|---|
| 118 | private: | 
|---|
| 119 | vector<MuTyV> vecv_; | 
|---|
| 120 | mutable MuTyV retv_; | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | inline ostream& operator << (ostream& os, const EnumeratedSequence& a) | 
|---|
| 124 | { a.Print(os);    return(os);    } | 
|---|
| 125 |  | 
|---|
| 126 | //inline EnumeratedSequence operator , (MuTyV const & a, MuTyV const & b) | 
|---|
| 127 | //{ EnumeratedSequence seq;   return ((seq,a),b) ; } | 
|---|
| 128 |  | 
|---|
| 129 | ////////////////////////////////////////////////////////// | 
|---|
| 130 | //! Class to define a range of indexes | 
|---|
| 131 | class Range { | 
|---|
| 132 | public: | 
|---|
| 133 | //! Index range containing a single index = \b start | 
|---|
| 134 | Range(sa_size_t start); | 
|---|
| 135 | //! Index range start \<= index \<= end | 
|---|
| 136 | Range(sa_size_t start, sa_size_t end); | 
|---|
| 137 | //! Index range start \<= index \<= end with increment step | 
|---|
| 138 | Range(sa_size_t start, sa_size_t end, sa_size_t step); | 
|---|
| 139 | //! General Index range specification, using size or start/end and increment | 
|---|
| 140 | Range(sa_size_t start, sa_size_t end, sa_size_t size, sa_size_t step); | 
|---|
| 141 | //! Return the start index | 
|---|
| 142 | inline sa_size_t & Start()  {  return start_; } | 
|---|
| 143 | //! Return the last index | 
|---|
| 144 | inline sa_size_t & End()    {  return end_; } | 
|---|
| 145 | //! Return the size | 
|---|
| 146 | inline sa_size_t & Size()   {  return size_; } | 
|---|
| 147 | //! Return the step | 
|---|
| 148 | inline sa_size_t & Step()   {  return step_; } | 
|---|
| 149 | //! return a constant value defining the first element | 
|---|
| 150 | inline static sa_size_t firstIndex() { return 0; } | 
|---|
| 151 | //! return a constant value as a placeholder for the last valid index | 
|---|
| 152 | inline static sa_size_t lastIndex() { return -1; } | 
|---|
| 153 | //! return a Range object specifing all indices, from first to last | 
|---|
| 154 | inline static Range all() | 
|---|
| 155 | { return Range(Range::firstIndex() , Range::lastIndex()); } | 
|---|
| 156 | //! return a Range object specifing the first index | 
|---|
| 157 | inline static Range first() | 
|---|
| 158 | { return Range(Range::firstIndex() , Range::firstIndex(), 1, 1); } | 
|---|
| 159 | //! return a Range object specifing the last valid index | 
|---|
| 160 | inline static Range last() | 
|---|
| 161 | { return Range(Range::lastIndex() , Range::lastIndex(), 1, 1); } | 
|---|
| 162 |  | 
|---|
| 163 | //! For internal TArray module use. | 
|---|
| 164 | void Update(sa_size_t osz); | 
|---|
| 165 |  | 
|---|
| 166 | protected: | 
|---|
| 167 | sa_size_t start_; //!< start index | 
|---|
| 168 | sa_size_t end_;   //!< end index | 
|---|
| 169 | sa_size_t size_;  //!< size | 
|---|
| 170 | sa_size_t step_;  //!< step | 
|---|
| 171 | }; | 
|---|
| 172 |  | 
|---|
| 173 | ////////////////////////////////////////////////////////// | 
|---|
| 174 | //! Class to define an identity matrix | 
|---|
| 175 | class IdentityMatrix { | 
|---|
| 176 | public: | 
|---|
| 177 | explicit IdentityMatrix(double diag=1., sa_size_t n=0); | 
|---|
| 178 | //! return the size of the identity matrix | 
|---|
| 179 | inline sa_size_t Size() { return size_; } | 
|---|
| 180 | //! return the value of the diagonal elements | 
|---|
| 181 | inline double Diag() { return diag_; } | 
|---|
| 182 | protected: | 
|---|
| 183 | sa_size_t size_; //!< size of the matrix | 
|---|
| 184 | double diag_; //!< value of the diagonal elements | 
|---|
| 185 | }; | 
|---|
| 186 |  | 
|---|
| 187 | } // Fin du namespace | 
|---|
| 188 |  | 
|---|
| 189 | #endif | 
|---|