| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | #ifndef SOpeMatrix_SEEN | 
|---|
| 3 | #define SOpeMatrix_SEEN | 
|---|
| 4 |  | 
|---|
| 5 | #include "machdefs.h" | 
|---|
| 6 | #include "tmatrix.h" | 
|---|
| 7 | #include "tvector.h" | 
|---|
| 8 |  | 
|---|
| 9 | //////////////////////////////////////////////////////////////// | 
|---|
| 10 | //////////////////////////////////////////////////////////////// | 
|---|
| 11 | //------------------------------------------------------------// | 
|---|
| 12 | //      La classe de calcul simple sur les TMatrix            // | 
|---|
| 13 | //------------------------------------------------------------// | 
|---|
| 14 | //////////////////////////////////////////////////////////////// | 
|---|
| 15 | //////////////////////////////////////////////////////////////// | 
|---|
| 16 |  | 
|---|
| 17 | namespace SOPHYA { | 
|---|
| 18 |  | 
|---|
| 19 | /*! | 
|---|
| 20 | \class SimpleMatrixOperation | 
|---|
| 21 | \ingroup TArray | 
|---|
| 22 | \brief Class for simple operation on TMatrix | 
|---|
| 23 | This class provides presently a basic implementation of the Gauss | 
|---|
| 24 | pivoting for linear system solving and matrix inversion. | 
|---|
| 25 |  | 
|---|
| 26 | \warning For large or ill conditioned matrices, use the methods | 
|---|
| 27 | from LinAlg module. | 
|---|
| 28 | \sa SOPHYA::LapackServer  SOPHYA::TMatrix | 
|---|
| 29 | */ | 
|---|
| 30 | //! Class for simple operation on TMatrix | 
|---|
| 31 | template <class T> | 
|---|
| 32 | class SimpleMatrixOperation { | 
|---|
| 33 | public: | 
|---|
| 34 | //! To define the type of data scaling for Gauss pivoting method (type T) | 
|---|
| 35 | enum GausPivScal { | 
|---|
| 36 | PIV_NO_SCALE = 0,   //!< do not scale datas for gauss pivoting | 
|---|
| 37 | PIV_GLOB_SCALE = 1, //!< do a global scaling of datas for gauss pivoting (default) | 
|---|
| 38 | PIV_ROW_SCALE = 2   //!< do a scaling by row of datas for gauss pivoting | 
|---|
| 39 | }; | 
|---|
| 40 | static inline int SetGausPivScal(int gps = PIV_GLOB_SCALE) | 
|---|
| 41 | { GausPivScaling = gps; return GausPivScaling; } | 
|---|
| 42 |  | 
|---|
| 43 | static TMatrix<T> Inverse(TMatrix<T> const & A); | 
|---|
| 44 | static T GausPiv(TMatrix<T>& A, TMatrix<T>& B, bool computedet=false); | 
|---|
| 45 |  | 
|---|
| 46 | protected: | 
|---|
| 47 | static int GausPivScaling; | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | } // Fin du namespace | 
|---|
| 51 |  | 
|---|
| 52 | //////////////////////////////////////////////////////////////// | 
|---|
| 53 | //////////////////////////////////////////////////////////////// | 
|---|
| 54 | //------------------------------------------------------------// | 
|---|
| 55 | //              Resolution de systemes lineaires              // | 
|---|
| 56 | //------------------------------------------------------------// | 
|---|
| 57 | //////////////////////////////////////////////////////////////// | 
|---|
| 58 | //////////////////////////////////////////////////////////////// | 
|---|
| 59 |  | 
|---|
| 60 | namespace SOPHYA { | 
|---|
| 61 |  | 
|---|
| 62 | //------------------------------------------------------------ | 
|---|
| 63 | // Resolution du systeme A*C = B | 
|---|
| 64 | //------------------------------------------------------------ | 
|---|
| 65 | /*! \ingroup TArray \fn LinSolveInPlace(TMatrix<T>&,TVector<T>&) | 
|---|
| 66 | \brief  Solve A*C = B for C in place and return 1 if OK, 0 if not. | 
|---|
| 67 | */ | 
|---|
| 68 | template <class T> | 
|---|
| 69 | inline T LinSolveInPlace(TMatrix<T>& a, TVector<T>& b) | 
|---|
| 70 | { | 
|---|
| 71 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| 72 | throw(SzMismatchError("LinSolveInPlace(TMatrix<T>,TVector<T>) size mismatch")); | 
|---|
| 73 | return SimpleMatrixOperation<T>::GausPiv(a,b); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | //------------------------------------------------------------ | 
|---|
| 77 | // Resolution du systeme A*C = B, avec C retourne dans B | 
|---|
| 78 | //------------------------------------------------------------ | 
|---|
| 79 | /*! \ingroup TArray | 
|---|
| 80 | \fn LinSolve(const TMatrix<T>&, const TVector<T>&, TVector<T>&) | 
|---|
| 81 | \brief Solve A*C = B and return C. Return 1 if OK, 0 if not. | 
|---|
| 82 | */ | 
|---|
| 83 | template <class T> | 
|---|
| 84 | inline T LinSolve(const TMatrix<T>& a, const TVector<T>& b, TVector<T>& c) { | 
|---|
| 85 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| 86 | throw(SzMismatchError("LinSolve(TMatrix<T>,TVector<T>) size mismatch")); | 
|---|
| 87 | c = b; TMatrix<T> a1(a); | 
|---|
| 88 | return SimpleMatrixOperation<T>::GausPiv(a1,c); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | } // Fin du namespace | 
|---|
| 92 |  | 
|---|
| 93 | //////////////////////////////////////////////////////////////// | 
|---|
| 94 | //////////////////////////////////////////////////////////////// | 
|---|
| 95 | //------------------------------------------------------------// | 
|---|
| 96 | //                   Inverse d'une matrice                    // | 
|---|
| 97 | //------------------------------------------------------------// | 
|---|
| 98 | //////////////////////////////////////////////////////////////// | 
|---|
| 99 | //////////////////////////////////////////////////////////////// | 
|---|
| 100 |  | 
|---|
| 101 | namespace SOPHYA { | 
|---|
| 102 |  | 
|---|
| 103 | /*! \ingroup TArray | 
|---|
| 104 | \fn Inverse(TMatrix<T> const &) | 
|---|
| 105 | \brief To inverse a TMatrix. | 
|---|
| 106 | */ | 
|---|
| 107 | template <class T> | 
|---|
| 108 | inline TMatrix<T> Inverse(TMatrix<T> const & A) | 
|---|
| 109 | {return SimpleMatrixOperation<T>::Inverse(A);} | 
|---|
| 110 |  | 
|---|
| 111 | /*! \ingroup TArray | 
|---|
| 112 | \fn Determinant(TMatrix<T> const &) | 
|---|
| 113 | \brief Return the TMatrix determinant | 
|---|
| 114 | */ | 
|---|
| 115 | template <class T> | 
|---|
| 116 | inline T Determinant(TMatrix<T> const & A) { | 
|---|
| 117 | TMatrix<T> a(A,false); | 
|---|
| 118 | TMatrix<T> b(a.NCols(),a.NRows());  b = IdentityMatrix(1.); | 
|---|
| 119 | return SimpleMatrixOperation<T>::GausPiv(a,b,true); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /*! \ingroup TArray | 
|---|
| 123 | \fn GausPiv(TMatrix<T> const &,TMatrix<T> &, bool) | 
|---|
| 124 | \brief Gauss pivoting on matrix \b A, doing the same operations | 
|---|
| 125 | on matrix \b B. | 
|---|
| 126 | \param computedet = true : return the determinant of \b A | 
|---|
| 127 | (beware of over/underfloat), default is false. | 
|---|
| 128 | \return determinant if requested, or 1 if OK. | 
|---|
| 129 | \sa SOPHYA::SimpleMatrixOperation::GausPiv(TMatrix<T>&,TMatrix<T>&,bool) | 
|---|
| 130 | */ | 
|---|
| 131 | template <class T> | 
|---|
| 132 | inline T GausPiv(TMatrix<T> const & A,TMatrix<T> & B, bool computedet=false) { | 
|---|
| 133 | TMatrix<T> a(A,false); | 
|---|
| 134 | return SimpleMatrixOperation<T>::GausPiv(a,B,computedet); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 | } // Fin du namespace | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | //////////////////////////////////////////////////////////////// | 
|---|
| 142 | //////////////////////////////////////////////////////////////// | 
|---|
| 143 | //------------------------------------------------------------// | 
|---|
| 144 | //                   Linear fitting                           // | 
|---|
| 145 | //------------------------------------------------------------// | 
|---|
| 146 | //////////////////////////////////////////////////////////////// | 
|---|
| 147 | //////////////////////////////////////////////////////////////// | 
|---|
| 148 |  | 
|---|
| 149 | namespace SOPHYA { | 
|---|
| 150 |  | 
|---|
| 151 | /*! | 
|---|
| 152 | \class LinFitter | 
|---|
| 153 | \ingroup TArray | 
|---|
| 154 | Class for linear fitting | 
|---|
| 155 | \sa TMatrix TArray | 
|---|
| 156 | */ | 
|---|
| 157 |  | 
|---|
| 158 | //!  Class for linear fitting | 
|---|
| 159 | template <class T> | 
|---|
| 160 | class LinFitter { | 
|---|
| 161 |  | 
|---|
| 162 | public : | 
|---|
| 163 |  | 
|---|
| 164 | LinFitter(); | 
|---|
| 165 | virtual ~LinFitter(); | 
|---|
| 166 |  | 
|---|
| 167 | //! Linear fitting | 
|---|
| 168 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, | 
|---|
| 169 | sa_size_t nf, T (*f)(sa_size_t,T), TVector<T>& c); | 
|---|
| 170 |  | 
|---|
| 171 | //! Linear fitting | 
|---|
| 172 | r_8 LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c); | 
|---|
| 173 |  | 
|---|
| 174 | //! Linear fitting with errors | 
|---|
| 175 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, const TVector<T>& errY2, | 
|---|
| 176 | sa_size_t nf,T (*f)(sa_size_t,T), TVector<T>& c, TVector<T>& errC); | 
|---|
| 177 |  | 
|---|
| 178 | //! Linear fitting with errors | 
|---|
| 179 | r_8 LinFit(const TMatrix<T>& fx, const TVector<T>& y, | 
|---|
| 180 | const TVector<T>& errY2, TVector<T>& c, TVector<T>& errC); | 
|---|
| 181 | }; | 
|---|
| 182 |  | 
|---|
| 183 | } // Fin du namespace | 
|---|
| 184 |  | 
|---|
| 185 | #endif | 
|---|