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