| [772] | 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 |  | 
|---|
| [935] | 9 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 10 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 11 | //------------------------------------------------------------// | 
|---|
| [939] | 12 | //      La classe de calcul simple sur les TMatrix            // | 
|---|
| [935] | 13 | //------------------------------------------------------------// | 
|---|
|  | 14 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 15 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 16 |  | 
|---|
|  | 17 | namespace SOPHYA { | 
|---|
|  | 18 |  | 
|---|
| [926] | 19 | /*! | 
|---|
| [935] | 20 | \class SimpleMatrixOperation | 
|---|
| [926] | 21 | \ingroup TArray | 
|---|
| [2917] | 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 | 
|---|
| [947] | 29 | */ | 
|---|
| [926] | 30 | //! Class for simple operation on TMatrix | 
|---|
| [772] | 31 | template <class T> | 
|---|
|  | 32 | class SimpleMatrixOperation { | 
|---|
|  | 33 | public: | 
|---|
| [1007] | 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 |  | 
|---|
| [772] | 43 | static TMatrix<T> Inverse(TMatrix<T> const & A); | 
|---|
| [1007] | 44 | static T GausPiv(TMatrix<T>& A, TMatrix<T>& B, bool computedet=false); | 
|---|
|  | 45 |  | 
|---|
|  | 46 | protected: | 
|---|
|  | 47 | static int GausPivScaling; | 
|---|
| [772] | 48 | }; | 
|---|
|  | 49 |  | 
|---|
| [935] | 50 | } // Fin du namespace | 
|---|
|  | 51 |  | 
|---|
| [926] | 52 | //////////////////////////////////////////////////////////////// | 
|---|
| [935] | 53 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 54 | //------------------------------------------------------------// | 
|---|
|  | 55 | //              Resolution de systemes lineaires              // | 
|---|
|  | 56 | //------------------------------------------------------------// | 
|---|
|  | 57 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 58 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 59 |  | 
|---|
|  | 60 | namespace SOPHYA { | 
|---|
|  | 61 |  | 
|---|
|  | 62 | //------------------------------------------------------------ | 
|---|
| [772] | 63 | // Resolution du systeme A*C = B | 
|---|
| [935] | 64 | //------------------------------------------------------------ | 
|---|
| [999] | 65 | /*! \ingroup TArray \fn LinSolveInPlace(TMatrix<T>&,TVector<T>&) | 
|---|
| [1007] | 66 | \brief  Solve A*C = B for C in place and return 1 if OK, 0 if not. | 
|---|
| [947] | 67 | */ | 
|---|
| [999] | 68 | template <class T> | 
|---|
|  | 69 | inline T LinSolveInPlace(TMatrix<T>& a, TVector<T>& b) | 
|---|
| [926] | 70 | { | 
|---|
|  | 71 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| [999] | 72 | throw(SzMismatchError("LinSolveInPlace(TMatrix<T>,TVector<T>) size mismatch")); | 
|---|
|  | 73 | return SimpleMatrixOperation<T>::GausPiv(a,b); | 
|---|
| [926] | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [935] | 76 | //------------------------------------------------------------ | 
|---|
| [926] | 77 | // Resolution du systeme A*C = B, avec C retourne dans B | 
|---|
| [935] | 78 | //------------------------------------------------------------ | 
|---|
| [947] | 79 | /*! \ingroup TArray | 
|---|
| [999] | 80 | \fn LinSolve(const TMatrix<T>&, const TVector<T>&, TVector<T>&) | 
|---|
| [1007] | 81 | \brief Solve A*C = B and return C. Return 1 if OK, 0 if not. | 
|---|
| [947] | 82 | */ | 
|---|
| [999] | 83 | template <class T> | 
|---|
|  | 84 | inline T LinSolve(const TMatrix<T>& a, const TVector<T>& b, TVector<T>& c) { | 
|---|
| [926] | 85 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| [999] | 86 | throw(SzMismatchError("LinSolve(TMatrix<T>,TVector<T>) size mismatch")); | 
|---|
|  | 87 | c = b; TMatrix<T> a1(a); | 
|---|
|  | 88 | return SimpleMatrixOperation<T>::GausPiv(a1,c); | 
|---|
| [850] | 89 | } | 
|---|
|  | 90 |  | 
|---|
| [935] | 91 | } // Fin du namespace | 
|---|
|  | 92 |  | 
|---|
| [926] | 93 | //////////////////////////////////////////////////////////////// | 
|---|
| [935] | 94 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 95 | //------------------------------------------------------------// | 
|---|
|  | 96 | //                   Inverse d'une matrice                    // | 
|---|
|  | 97 | //------------------------------------------------------------// | 
|---|
|  | 98 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 99 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 100 |  | 
|---|
|  | 101 | namespace SOPHYA { | 
|---|
|  | 102 |  | 
|---|
| [947] | 103 | /*! \ingroup TArray | 
|---|
| [999] | 104 | \fn Inverse(TMatrix<T> const &) | 
|---|
| [1007] | 105 | \brief To inverse a TMatrix. | 
|---|
| [947] | 106 | */ | 
|---|
| [999] | 107 | template <class T> | 
|---|
|  | 108 | inline TMatrix<T> Inverse(TMatrix<T> const & A) | 
|---|
|  | 109 | {return SimpleMatrixOperation<T>::Inverse(A);} | 
|---|
|  | 110 |  | 
|---|
| [947] | 111 | /*! \ingroup TArray | 
|---|
| [999] | 112 | \fn Determinant(TMatrix<T> const &) | 
|---|
| [1007] | 113 | \brief Return the TMatrix determinant | 
|---|
| [947] | 114 | */ | 
|---|
| [999] | 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.); | 
|---|
| [1007] | 119 | return SimpleMatrixOperation<T>::GausPiv(a,b,true); | 
|---|
| [999] | 120 | } | 
|---|
|  | 121 |  | 
|---|
| [947] | 122 | /*! \ingroup TArray | 
|---|
| [1007] | 123 | \fn GausPiv(TMatrix<T> const &,TMatrix<T> &, bool) | 
|---|
| [999] | 124 | \brief Gauss pivoting on matrix \b A, doing the same operations | 
|---|
| [1007] | 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) | 
|---|
| [947] | 130 | */ | 
|---|
| [999] | 131 | template <class T> | 
|---|
| [1007] | 132 | inline T GausPiv(TMatrix<T> const & A,TMatrix<T> & B, bool computedet=false) { | 
|---|
| [999] | 133 | TMatrix<T> a(A,false); | 
|---|
| [1007] | 134 | return SimpleMatrixOperation<T>::GausPiv(a,B,computedet); | 
|---|
| [999] | 135 | } | 
|---|
| [926] | 136 |  | 
|---|
| [999] | 137 |  | 
|---|
| [935] | 138 | } // Fin du namespace | 
|---|
| [772] | 139 |  | 
|---|
| [935] | 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 | 
|---|
| [947] | 156 | */ | 
|---|
| [935] | 157 |  | 
|---|
| [926] | 158 | //!  Class for linear fitting | 
|---|
| [939] | 159 | template <class T> | 
|---|
| [804] | 160 | class LinFitter { | 
|---|
| [939] | 161 |  | 
|---|
| [804] | 162 | public : | 
|---|
|  | 163 |  | 
|---|
| [939] | 164 | LinFitter(); | 
|---|
|  | 165 | virtual ~LinFitter(); | 
|---|
| [804] | 166 |  | 
|---|
| [939] | 167 | //! Linear fitting | 
|---|
|  | 168 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, | 
|---|
| [3332] | 169 | sa_size_t nf, T (*f)(sa_size_t,T), TVector<T>& c); | 
|---|
| [939] | 170 |  | 
|---|
|  | 171 | //! Linear fitting | 
|---|
|  | 172 | r_8 LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c); | 
|---|
| [804] | 173 |  | 
|---|
| [939] | 174 | //! Linear fitting with errors | 
|---|
|  | 175 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, const TVector<T>& errY2, | 
|---|
| [3332] | 176 | sa_size_t nf,T (*f)(sa_size_t,T), TVector<T>& c, TVector<T>& errC); | 
|---|
| [804] | 177 |  | 
|---|
| [939] | 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); | 
|---|
| [804] | 181 | }; | 
|---|
|  | 182 |  | 
|---|
| [772] | 183 | } // Fin du namespace | 
|---|
|  | 184 |  | 
|---|
|  | 185 | #endif | 
|---|