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