[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"
|
---|
[3613] | 10 | #include "randinterf.h"
|
---|
[1103] | 11 |
|
---|
[785] | 12 | #include <stdlib.h>
|
---|
[1103] | 13 | #include <vector>
|
---|
[785] | 14 |
|
---|
| 15 | namespace SOPHYA {
|
---|
| 16 |
|
---|
| 17 | /* Quelques utilitaires pour les tableaux (Array) */
|
---|
| 18 |
|
---|
[956] | 19 | /*! \ingroup TArray
|
---|
| 20 | \typedef Arr_DoubleFunctionOfX
|
---|
[1517] | 21 | \brief define a function of double which returns a double
|
---|
[956] | 22 | */
|
---|
[785] | 23 | typedef double (* Arr_DoubleFunctionOfX) (double x);
|
---|
[956] | 24 | /*! \ingroup TArray
|
---|
| 25 | \typedef Arr_FloatFunctionOfX
|
---|
[1517] | 26 | \brief define a function of float which returns a float
|
---|
[956] | 27 | */
|
---|
[785] | 28 | typedef float (* Arr_FloatFunctionOfX) (float x);
|
---|
| 29 |
|
---|
[1517] | 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 |
|
---|
[894] | 41 | //////////////////////////////////////////////////////////
|
---|
[1404] | 42 | //! Class to generate a sequence of values
|
---|
[1103] | 43 | class Sequence {
|
---|
[850] | 44 | public:
|
---|
[1103] | 45 | virtual ~Sequence();
|
---|
[1156] | 46 | virtual MuTyV & Value(sa_size_t k) const = 0;
|
---|
| 47 | inline MuTyV & operator () (sa_size_t k) const { return(Value(k)) ; }
|
---|
[1103] | 48 | };
|
---|
| 49 |
|
---|
| 50 | class RandomSequence : public Sequence {
|
---|
| 51 | public:
|
---|
[894] | 52 | //! to define the generator type
|
---|
| 53 | enum {
|
---|
| 54 | Gaussian = 0, //!< gaussian generator
|
---|
| 55 | Flat = 1 //!< Flat generator
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[1103] | 58 | explicit RandomSequence(int typ = RandomSequence::Gaussian, double m=0., double s=1.);
|
---|
[3613] | 59 | explicit RandomSequence(RandomGeneratorInterface& rgen, RNDTypes rtyp=C_RND_Gaussian, double mean=0., double sigma=1.);
|
---|
[1103] | 60 | virtual ~RandomSequence();
|
---|
[1156] | 61 | virtual MuTyV & Value(sa_size_t k) const ;
|
---|
[3613] | 62 | double Next() const ;
|
---|
[1103] | 63 |
|
---|
[850] | 64 | protected:
|
---|
[3613] | 65 | RNDTypes typ_; //!< random generation type
|
---|
[894] | 66 | double mean_, sig_; //!< generation parameters mean and sigma (if needed)
|
---|
[1103] | 67 | mutable MuTyV retv_;
|
---|
[3613] | 68 | mutable RandomGeneratorInterface* rgp_;
|
---|
[850] | 69 | };
|
---|
| 70 |
|
---|
| 71 |
|
---|
[894] | 72 | //////////////////////////////////////////////////////////
|
---|
| 73 | //! Class to generate a sequence of values
|
---|
[1103] | 74 | class RegularSequence : public Sequence {
|
---|
[785] | 75 | public:
|
---|
[1103] | 76 | explicit RegularSequence (double start=0., double step=1., Arr_DoubleFunctionOfX f=NULL);
|
---|
| 77 | virtual ~RegularSequence();
|
---|
[894] | 78 |
|
---|
| 79 | //! return start value of the sequence
|
---|
[785] | 80 | inline double & Start() { return start_; }
|
---|
[894] | 81 | //! return step value of the sequence
|
---|
[785] | 82 | inline double & Step() { return step_; }
|
---|
[1103] | 83 |
|
---|
[1156] | 84 | virtual MuTyV & Value(sa_size_t k) const ;
|
---|
[1103] | 85 |
|
---|
[785] | 86 | protected:
|
---|
[894] | 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
|
---|
[1103] | 90 | mutable MuTyV retv_;
|
---|
[785] | 91 | };
|
---|
| 92 |
|
---|
[1550] | 93 | //////////////////////////////////////////////////////////
|
---|
| 94 | //! Class for creation and handling of an explicitly defined list of values
|
---|
| 95 |
|
---|
[1103] | 96 | class EnumeratedSequence : public Sequence {
|
---|
| 97 | public:
|
---|
[1404] | 98 | explicit EnumeratedSequence();
|
---|
[1558] | 99 | EnumeratedSequence(EnumeratedSequence const & es);
|
---|
[1103] | 100 | virtual ~EnumeratedSequence();
|
---|
[1156] | 101 | virtual MuTyV & Value(sa_size_t k) const ;
|
---|
[1550] | 102 |
|
---|
| 103 | inline sa_size_t Size() const { return vecv_.size(); }
|
---|
| 104 |
|
---|
[1103] | 105 | EnumeratedSequence & operator , (MuTyV const & v);
|
---|
[1156] | 106 | EnumeratedSequence & operator = (MuTyV const & v);
|
---|
[1550] | 107 |
|
---|
[1558] | 108 | EnumeratedSequence & operator = (EnumeratedSequence const & es);
|
---|
| 109 |
|
---|
[1550] | 110 | inline void Clear() { vecv_.clear(); }
|
---|
| 111 | void Print(ostream& os) const;
|
---|
| 112 |
|
---|
[1558] | 113 | sa_size_t Append(EnumeratedSequence const & seq);
|
---|
[2286] | 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");
|
---|
[1550] | 117 |
|
---|
[1103] | 118 | private:
|
---|
| 119 | vector<MuTyV> vecv_;
|
---|
| 120 | mutable MuTyV retv_;
|
---|
| 121 | };
|
---|
| 122 |
|
---|
[1550] | 123 | inline ostream& operator << (ostream& os, const EnumeratedSequence& a)
|
---|
| 124 | { a.Print(os); return(os); }
|
---|
| 125 |
|
---|
[1156] | 126 | //inline EnumeratedSequence operator , (MuTyV const & a, MuTyV const & b)
|
---|
| 127 | //{ EnumeratedSequence seq; return ((seq,a),b) ; }
|
---|
[1103] | 128 |
|
---|
[894] | 129 | //////////////////////////////////////////////////////////
|
---|
| 130 | //! Class to define a range of indexes
|
---|
[785] | 131 | class Range {
|
---|
| 132 | public:
|
---|
[2917] | 133 | //! Index range containing a single index = \b start
|
---|
[2915] | 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);
|
---|
[894] | 141 | //! Return the start index
|
---|
[1156] | 142 | inline sa_size_t & Start() { return start_; }
|
---|
[894] | 143 | //! Return the last index
|
---|
[1156] | 144 | inline sa_size_t & End() { return end_; }
|
---|
[894] | 145 | //! Return the size
|
---|
[1156] | 146 | inline sa_size_t & Size() { return size_; }
|
---|
[894] | 147 | //! Return the step
|
---|
[1156] | 148 | inline sa_size_t & Step() { return step_; }
|
---|
[2915] | 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()
|
---|
[2917] | 158 | { return Range(Range::firstIndex() , Range::firstIndex(), 1, 1); }
|
---|
[2915] | 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 |
|
---|
[785] | 166 | protected:
|
---|
[1156] | 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
|
---|
[785] | 171 | };
|
---|
| 172 |
|
---|
[894] | 173 | //////////////////////////////////////////////////////////
|
---|
| 174 | //! Class to define an identity matrix
|
---|
[804] | 175 | class IdentityMatrix {
|
---|
| 176 | public:
|
---|
[1156] | 177 | explicit IdentityMatrix(double diag=1., sa_size_t n=0);
|
---|
[894] | 178 | //! return the size of the identity matrix
|
---|
[1156] | 179 | inline sa_size_t Size() { return size_; }
|
---|
[894] | 180 | //! return the value of the diagonal elements
|
---|
[804] | 181 | inline double Diag() { return diag_; }
|
---|
| 182 | protected:
|
---|
[1156] | 183 | sa_size_t size_; //!< size of the matrix
|
---|
[894] | 184 | double diag_; //!< value of the diagonal elements
|
---|
[804] | 185 | };
|
---|
| 186 |
|
---|
[785] | 187 | } // Fin du namespace
|
---|
| 188 |
|
---|
| 189 | #endif
|
---|