| [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 | 
|---|
|  | 20 | \brief define a function of float which returns a double | 
|---|
|  | 21 | */ | 
|---|
| [785] | 22 | typedef double (* Arr_DoubleFunctionOfX) (double x); | 
|---|
| [956] | 23 | /*! \ingroup TArray | 
|---|
|  | 24 | \typedef Arr_FloatFunctionOfX | 
|---|
|  | 25 | \brief define a function of float which returns a double | 
|---|
|  | 26 | */ | 
|---|
| [785] | 27 | typedef float  (* Arr_FloatFunctionOfX)  (float x); | 
|---|
|  | 28 |  | 
|---|
| [894] | 29 | ////////////////////////////////////////////////////////// | 
|---|
| [1404] | 30 | //! Class to generate a sequence of values | 
|---|
| [1103] | 31 | class Sequence { | 
|---|
| [850] | 32 | public: | 
|---|
| [1103] | 33 | virtual ~Sequence(); | 
|---|
| [1156] | 34 | virtual MuTyV & Value(sa_size_t k) const = 0; | 
|---|
|  | 35 | inline MuTyV & operator () (sa_size_t k) const { return(Value(k)) ; } | 
|---|
| [1103] | 36 | }; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | class RandomSequence : public Sequence { | 
|---|
|  | 39 | public: | 
|---|
| [894] | 40 | //! to define the generator type | 
|---|
|  | 41 | enum { | 
|---|
|  | 42 | Gaussian = 0, //!< gaussian generator | 
|---|
|  | 43 | Flat = 1      //!< Flat generator | 
|---|
|  | 44 | }; | 
|---|
|  | 45 |  | 
|---|
| [1103] | 46 | explicit RandomSequence(int typ = RandomSequence::Gaussian, double m=0., double s=1.); | 
|---|
|  | 47 | virtual  ~RandomSequence(); | 
|---|
| [1156] | 48 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| [1103] | 49 | double   Rand(); | 
|---|
|  | 50 |  | 
|---|
| [850] | 51 | protected: | 
|---|
| [894] | 52 | int typ_;            //!< random generation type | 
|---|
|  | 53 | double mean_, sig_;  //!< generation parameters mean and sigma (if needed) | 
|---|
| [1103] | 54 | mutable MuTyV retv_; | 
|---|
| [850] | 55 | }; | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
| [894] | 58 | ////////////////////////////////////////////////////////// | 
|---|
|  | 59 | //! Class to generate a sequence of values | 
|---|
| [1103] | 60 | class RegularSequence : public Sequence { | 
|---|
| [785] | 61 | public: | 
|---|
| [1103] | 62 | explicit RegularSequence (double start=0., double step=1., Arr_DoubleFunctionOfX f=NULL); | 
|---|
|  | 63 | virtual  ~RegularSequence(); | 
|---|
| [894] | 64 |  | 
|---|
|  | 65 | //! return start value of the sequence | 
|---|
| [785] | 66 | inline double & Start() { return start_; } | 
|---|
| [894] | 67 | //! return step value of the sequence | 
|---|
| [785] | 68 | inline double & Step() { return step_; } | 
|---|
| [1103] | 69 |  | 
|---|
| [1156] | 70 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| [1103] | 71 |  | 
|---|
| [785] | 72 | protected: | 
|---|
| [894] | 73 | double start_;              //!< start value of the sequence | 
|---|
|  | 74 | double step_;               //!< step value of the sequence | 
|---|
|  | 75 | Arr_DoubleFunctionOfX myf_; //!< pointer to the sequence function | 
|---|
| [1103] | 76 | mutable MuTyV retv_; | 
|---|
| [785] | 77 | }; | 
|---|
|  | 78 |  | 
|---|
| [1103] | 79 | class EnumeratedSequence : public Sequence  { | 
|---|
|  | 80 | public: | 
|---|
| [1404] | 81 | explicit EnumeratedSequence(); | 
|---|
| [1103] | 82 | virtual ~EnumeratedSequence(); | 
|---|
| [1156] | 83 | virtual MuTyV & Value(sa_size_t k) const ; | 
|---|
| [1103] | 84 | EnumeratedSequence & operator , (MuTyV const & v); | 
|---|
| [1156] | 85 | EnumeratedSequence & operator = (MuTyV const & v); | 
|---|
| [1103] | 86 | private: | 
|---|
|  | 87 | vector<MuTyV> vecv_; | 
|---|
|  | 88 | mutable MuTyV retv_; | 
|---|
|  | 89 | }; | 
|---|
|  | 90 |  | 
|---|
| [1156] | 91 | //inline EnumeratedSequence operator , (MuTyV const & a, MuTyV const & b) | 
|---|
|  | 92 | //{ EnumeratedSequence seq;   return ((seq,a),b) ; } | 
|---|
| [1103] | 93 |  | 
|---|
| [894] | 94 | ////////////////////////////////////////////////////////// | 
|---|
|  | 95 | //! Class to define a range of indexes | 
|---|
| [785] | 96 | class Range { | 
|---|
|  | 97 | public: | 
|---|
| [1156] | 98 | explicit Range(sa_size_t start=0, sa_size_t end=0, sa_size_t size=1, sa_size_t step=1); | 
|---|
| [894] | 99 | //! Return the start index | 
|---|
| [1156] | 100 | inline sa_size_t & Start()  {  return start_; } | 
|---|
| [894] | 101 | //! Return the last index | 
|---|
| [1156] | 102 | inline sa_size_t & End()    {  return end_; } | 
|---|
| [894] | 103 | //! Return the size | 
|---|
| [1156] | 104 | inline sa_size_t & Size()   {  return size_; } | 
|---|
| [894] | 105 | //! Return the step | 
|---|
| [1156] | 106 | inline sa_size_t & Step()   {  return step_; } | 
|---|
| [785] | 107 | protected: | 
|---|
| [1156] | 108 | sa_size_t start_; //!< start index | 
|---|
|  | 109 | sa_size_t end_;   //!< end index | 
|---|
|  | 110 | sa_size_t size_;  //!< size | 
|---|
|  | 111 | sa_size_t step_;  //!< step | 
|---|
| [785] | 112 | }; | 
|---|
|  | 113 |  | 
|---|
| [894] | 114 | ////////////////////////////////////////////////////////// | 
|---|
|  | 115 | //! Class to define an identity matrix | 
|---|
| [804] | 116 | class IdentityMatrix { | 
|---|
|  | 117 | public: | 
|---|
| [1156] | 118 | explicit IdentityMatrix(double diag=1., sa_size_t n=0); | 
|---|
| [894] | 119 | //! return the size of the identity matrix | 
|---|
| [1156] | 120 | inline sa_size_t Size() { return size_; } | 
|---|
| [894] | 121 | //! return the value of the diagonal elements | 
|---|
| [804] | 122 | inline double Diag() { return diag_; } | 
|---|
|  | 123 | protected: | 
|---|
| [1156] | 124 | sa_size_t size_; //!< size of the matrix | 
|---|
| [894] | 125 | double diag_; //!< value of the diagonal elements | 
|---|
| [804] | 126 | }; | 
|---|
|  | 127 |  | 
|---|
| [785] | 128 | } // Fin du namespace | 
|---|
|  | 129 |  | 
|---|
|  | 130 | #endif | 
|---|