| 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 | 
 | 
|---|
| 11 | #include <stdlib.h>
 | 
|---|
| 12 | #include <vector>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | namespace SOPHYA {   
 | 
|---|
| 15 | 
 | 
|---|
| 16 | /* Quelques utilitaires pour les tableaux (Array) */             
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /*! \ingroup TArray
 | 
|---|
| 19 |   \typedef Arr_DoubleFunctionOfX
 | 
|---|
| 20 |   \brief define a function of double which returns a double
 | 
|---|
| 21 | */
 | 
|---|
| 22 | typedef double (* Arr_DoubleFunctionOfX) (double x);
 | 
|---|
| 23 | /*! \ingroup TArray
 | 
|---|
| 24 |   \typedef Arr_FloatFunctionOfX
 | 
|---|
| 25 |   \brief define a function of float which returns a float
 | 
|---|
| 26 | */
 | 
|---|
| 27 | typedef float  (* Arr_FloatFunctionOfX)  (float x);
 | 
|---|
| 28 | 
 | 
|---|
| 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 | 
 | 
|---|
| 40 | //////////////////////////////////////////////////////////
 | 
|---|
| 41 | //! Class to generate a sequence of values
 | 
|---|
| 42 | class Sequence {
 | 
|---|
| 43 | public:
 | 
|---|
| 44 |   virtual ~Sequence();
 | 
|---|
| 45 |   virtual MuTyV & Value(sa_size_t k) const = 0;
 | 
|---|
| 46 |   inline MuTyV & operator () (sa_size_t k) const { return(Value(k)) ; }
 | 
|---|
| 47 | };
 | 
|---|
| 48 | 
 | 
|---|
| 49 | class RandomSequence : public Sequence {
 | 
|---|
| 50 | public:
 | 
|---|
| 51 |   //! to define the generator type
 | 
|---|
| 52 |   enum {
 | 
|---|
| 53 |     Gaussian = 0, //!< gaussian generator
 | 
|---|
| 54 |     Flat = 1      //!< Flat generator
 | 
|---|
| 55 |     };
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   explicit RandomSequence(int typ = RandomSequence::Gaussian, double m=0., double s=1.);
 | 
|---|
| 58 |   virtual  ~RandomSequence();
 | 
|---|
| 59 |   virtual MuTyV & Value(sa_size_t k) const ;
 | 
|---|
| 60 |   double   Rand();
 | 
|---|
| 61 | 
 | 
|---|
| 62 | protected:
 | 
|---|
| 63 |   int typ_;            //!< random generation type
 | 
|---|
| 64 |   double mean_, sig_;  //!< generation parameters mean and sigma (if needed)
 | 
|---|
| 65 |   mutable MuTyV retv_;
 | 
|---|
| 66 | };
 | 
|---|
| 67 | 
 | 
|---|
| 68 | 
 | 
|---|
| 69 | //////////////////////////////////////////////////////////
 | 
|---|
| 70 | //! Class to generate a sequence of values
 | 
|---|
| 71 | class RegularSequence : public Sequence {
 | 
|---|
| 72 | public:
 | 
|---|
| 73 |   explicit RegularSequence (double start=0., double step=1., Arr_DoubleFunctionOfX f=NULL);
 | 
|---|
| 74 |   virtual  ~RegularSequence();
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   //! return start value of the sequence
 | 
|---|
| 77 |   inline double & Start() { return start_; }
 | 
|---|
| 78 |   //! return step value of the sequence
 | 
|---|
| 79 |   inline double & Step() { return step_; }
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   virtual MuTyV & Value(sa_size_t k) const ;
 | 
|---|
| 82 | 
 | 
|---|
| 83 | protected:
 | 
|---|
| 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
 | 
|---|
| 87 |   mutable MuTyV retv_;
 | 
|---|
| 88 | };
 | 
|---|
| 89 | 
 | 
|---|
| 90 | //////////////////////////////////////////////////////////
 | 
|---|
| 91 | //! Class for creation and handling of an explicitly defined list of values
 | 
|---|
| 92 | 
 | 
|---|
| 93 | class EnumeratedSequence : public Sequence  {
 | 
|---|
| 94 | public:
 | 
|---|
| 95 |   explicit EnumeratedSequence();
 | 
|---|
| 96 |            EnumeratedSequence(EnumeratedSequence const & es);
 | 
|---|
| 97 |   virtual ~EnumeratedSequence();
 | 
|---|
| 98 |   virtual MuTyV & Value(sa_size_t k) const ;
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   inline sa_size_t Size() const { return vecv_.size(); }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   EnumeratedSequence & operator , (MuTyV const & v);
 | 
|---|
| 103 |   EnumeratedSequence & operator = (MuTyV const & v);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   EnumeratedSequence & operator = (EnumeratedSequence const & es);
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   inline  void         Clear()  { vecv_.clear(); }      
 | 
|---|
| 108 |   void                 Print(ostream& os) const;
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   sa_size_t            Append(EnumeratedSequence const & seq);
 | 
|---|
| 111 |   sa_size_t            Append(string const & str, int & nbad);
 | 
|---|
| 112 |   sa_size_t            FillFromFile(istream& is, sa_size_t& nr, sa_size_t& nc);
 | 
|---|
| 113 | 
 | 
|---|
| 114 | private:
 | 
|---|
| 115 |   vector<MuTyV> vecv_;
 | 
|---|
| 116 |   mutable MuTyV retv_;
 | 
|---|
| 117 | };
 | 
|---|
| 118 | 
 | 
|---|
| 119 | inline ostream& operator << (ostream& os, const EnumeratedSequence& a)
 | 
|---|
| 120 |                             { a.Print(os);    return(os);    }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   //inline EnumeratedSequence operator , (MuTyV const & a, MuTyV const & b)
 | 
|---|
| 123 |   //{ EnumeratedSequence seq;   return ((seq,a),b) ; }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | //////////////////////////////////////////////////////////
 | 
|---|
| 126 | //! Class to define a range of indexes
 | 
|---|
| 127 | class Range {
 | 
|---|
| 128 | public:
 | 
|---|
| 129 |   explicit Range(sa_size_t start=0, sa_size_t end=0, sa_size_t size=1, sa_size_t step=1);
 | 
|---|
| 130 |   //! Return the start index
 | 
|---|
| 131 |   inline sa_size_t & Start()  {  return start_; }
 | 
|---|
| 132 |   //! Return the last index
 | 
|---|
| 133 |   inline sa_size_t & End()    {  return end_; }
 | 
|---|
| 134 |   //! Return the size
 | 
|---|
| 135 |   inline sa_size_t & Size()   {  return size_; }
 | 
|---|
| 136 |   //! Return the step
 | 
|---|
| 137 |   inline sa_size_t & Step()   {  return step_; }
 | 
|---|
| 138 | protected:
 | 
|---|
| 139 |   sa_size_t start_; //!< start index
 | 
|---|
| 140 |   sa_size_t end_;   //!< end index
 | 
|---|
| 141 |   sa_size_t size_;  //!< size
 | 
|---|
| 142 |   sa_size_t step_;  //!< step
 | 
|---|
| 143 | };
 | 
|---|
| 144 | 
 | 
|---|
| 145 | //////////////////////////////////////////////////////////
 | 
|---|
| 146 | //! Class to define an identity matrix
 | 
|---|
| 147 | class IdentityMatrix {
 | 
|---|
| 148 | public:
 | 
|---|
| 149 |   explicit IdentityMatrix(double diag=1., sa_size_t n=0);
 | 
|---|
| 150 |   //! return the size of the identity matrix
 | 
|---|
| 151 |   inline sa_size_t Size() { return size_; }
 | 
|---|
| 152 |   //! return the value of the diagonal elements
 | 
|---|
| 153 |   inline double Diag() { return diag_; }
 | 
|---|
| 154 | protected:
 | 
|---|
| 155 |   sa_size_t size_; //!< size of the matrix
 | 
|---|
| 156 |   double diag_; //!< value of the diagonal elements
 | 
|---|
| 157 | };
 | 
|---|
| 158 | 
 | 
|---|
| 159 | } // Fin du namespace
 | 
|---|
| 160 | 
 | 
|---|
| 161 | #endif
 | 
|---|