| 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 |  | 
|---|
| 26 | //! Class for simple operation on TMatrix | 
|---|
| 27 | template <class T> | 
|---|
| 28 | class SimpleMatrixOperation { | 
|---|
| 29 | public: | 
|---|
| 30 | static TMatrix<T> Inverse(TMatrix<T> const & A); | 
|---|
| 31 | static T GausPiv(TMatrix<T>& A, TMatrix<T>& B); | 
|---|
| 32 | }; | 
|---|
| 33 |  | 
|---|
| 34 | } // Fin du namespace | 
|---|
| 35 |  | 
|---|
| 36 | //////////////////////////////////////////////////////////////// | 
|---|
| 37 | //////////////////////////////////////////////////////////////// | 
|---|
| 38 | //------------------------------------------------------------// | 
|---|
| 39 | //              Resolution de systemes lineaires              // | 
|---|
| 40 | //------------------------------------------------------------// | 
|---|
| 41 | //////////////////////////////////////////////////////////////// | 
|---|
| 42 | //////////////////////////////////////////////////////////////// | 
|---|
| 43 |  | 
|---|
| 44 | namespace SOPHYA { | 
|---|
| 45 |  | 
|---|
| 46 | //------------------------------------------------------------ | 
|---|
| 47 | // Resolution du systeme A*C = B | 
|---|
| 48 | //------------------------------------------------------------ | 
|---|
| 49 |  | 
|---|
| 50 | /*! \ingroup TArray \fn LinSolveInPlace(TMatrix<r_4>&,TVector<r_4>&) | 
|---|
| 51 | \brief  Solve A*C = B for C in place and return determinant | 
|---|
| 52 | */ | 
|---|
| 53 | inline r_4 LinSolveInPlace(TMatrix<r_4>& a, TVector<r_4>& b) | 
|---|
| 54 | { | 
|---|
| 55 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| 56 | throw(SzMismatchError("LinSolveInPlace(TMatrix<r_4>,TVector<r_4>) size mismatch")); | 
|---|
| 57 | return SimpleMatrixOperation<r_4>::GausPiv(a,b); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /*! \ingroup TArray \fn LinSolveInPlace(TMatrix<r_8>&,TVector<r_8>&) | 
|---|
| 61 | \brief  Solve A*X = B in place and return determinant | 
|---|
| 62 | */ | 
|---|
| 63 | inline r_8 LinSolveInPlace(TMatrix<r_8>& a, TVector<r_8>& b) | 
|---|
| 64 | { | 
|---|
| 65 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| 66 | throw(SzMismatchError("LinSolveInPlace(TMatrix<r_8>,TVector<r_8>) size mismatch")); | 
|---|
| 67 | return SimpleMatrixOperation<r_8>::GausPiv(a,b); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | /*! \ingroup TArray | 
|---|
| 71 | \fn LinSolveInPlace(TMatrix< complex<r_4> >&, TVector< complex<r_4> >&) | 
|---|
| 72 | \brief Solve A*X = B in place and return determinant */ | 
|---|
| 73 | inline complex<r_4> LinSolveInPlace(TMatrix< complex<r_4> >& a, TVector< complex<r_4> >& b) | 
|---|
| 74 | { | 
|---|
| 75 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| 76 | throw(SzMismatchError("LinSolveInPlace(TMatrix< complex<r_4> >,TVector< complex<r_4> >) size mismatch")); | 
|---|
| 77 | return SimpleMatrixOperation< complex<r_4> >::GausPiv(a,b); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /*! \ingroup TArray | 
|---|
| 81 | \fn LinSolveInPlace(TMatrix< complex<r_8> >&, TVector< complex<r_8> >&) | 
|---|
| 82 | \brief  Solve A*X = B in place and return determinant | 
|---|
| 83 | */ | 
|---|
| 84 | inline complex<r_8> LinSolveInPlace(TMatrix< complex<r_8> >& a, TVector< complex<r_8> >& b) | 
|---|
| 85 | { | 
|---|
| 86 | if(a.NCols() != b.NRows() || a.NCols() != a.NRows()) | 
|---|
| 87 | throw(SzMismatchError("LinSolveInPlace(TMatrix< complex<r_8> >,TVector< complex<r_8> >) size mismatch")); | 
|---|
| 88 | return SimpleMatrixOperation< complex<r_8> >::GausPiv(a,b); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | //------------------------------------------------------------ | 
|---|
| 92 | // Resolution du systeme A*C = B, avec C retourne dans B | 
|---|
| 93 | //------------------------------------------------------------ | 
|---|
| 94 |  | 
|---|
| 95 | /*! \ingroup TArray | 
|---|
| 96 | \fn LinSolve(const TMatrix<r_4>&, const TVector<r_4>&, TVector<r_4>&) | 
|---|
| 97 | \brief Solve A*C = B and return C and determinant | 
|---|
| 98 | */ | 
|---|
| 99 |  | 
|---|
| 100 | inline r_4 LinSolve(const TMatrix<r_4>& a, const TVector<r_4>& b, TVector<r_4>& c) { | 
|---|
| 101 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| 102 | throw(SzMismatchError("LinSolve(TMatrix<r_4>,TVector<r_4>) size mismatch")); | 
|---|
| 103 | c = b; TMatrix<r_4> a1(a); | 
|---|
| 104 | return SimpleMatrixOperation<r_4>::GausPiv(a1,c); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /*! \ingroup TArray | 
|---|
| 108 | \fn LinSolve(const TMatrix<r_8>&, const TVector<r_8>&, TVector<r_8>&) | 
|---|
| 109 | \brief Solve A*C = B and return C and determinant | 
|---|
| 110 | */ | 
|---|
| 111 | inline r_8 LinSolve(const TMatrix<r_8>& a, const TVector<r_8>& b, TVector<r_8>& c) { | 
|---|
| 112 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| 113 | throw(SzMismatchError("LinSolve(TMatrix<r_8>,TVector<r_8>) size mismatch")); | 
|---|
| 114 | c = b; TMatrix<r_8> a1(a); | 
|---|
| 115 | return SimpleMatrixOperation<r_8>::GausPiv(a1,c); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /*! \ingroup TArray | 
|---|
| 119 | \fn LinSolve(const TMatrix< complex<r_4> >&, const TVector< complex<r_4> >&, TVector< complex<r_4> >&) | 
|---|
| 120 | \brief Solve A*C = B and return C and determinant | 
|---|
| 121 | */ | 
|---|
| 122 | inline complex<r_4> LinSolve(const TMatrix< complex<r_4> >& a, const TVector< complex<r_4> >& b, TVector< complex<r_4> >& c) { | 
|---|
| 123 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| 124 | throw(SzMismatchError("LinSolve(TMatrix< complex<r_4> >,TVector< complex<r_4> >) size mismatch")); | 
|---|
| 125 | c = b; TMatrix< complex<r_4> > a1(a); | 
|---|
| 126 | return SimpleMatrixOperation< complex<r_4> >::GausPiv(a1,c); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | /*! \ingroup TArray | 
|---|
| 130 | \fn LinSolve(const TMatrix< complex<r_8> >&, const TVector< complex<r_8> >&, TVector< complex<r_8> >&) | 
|---|
| 131 | \brief  Solve A*C = B and return C and determinant | 
|---|
| 132 | */ | 
|---|
| 133 | inline complex<r_8> LinSolve(const TMatrix< complex<r_8> >& a, const TVector< complex<r_8> >& b, TVector< complex<r_8> >& c) { | 
|---|
| 134 | if(a.NCols()!=b.NRows() || a.NCols()!=a.NRows()) | 
|---|
| 135 | throw(SzMismatchError("LinSolve(TMatrix< complex<r_8> >,TVector< complex<r_8> >) size mismatch")); | 
|---|
| 136 | c = b; TMatrix< complex<r_8> > a1(a); | 
|---|
| 137 | return SimpleMatrixOperation< complex<r_8> >::GausPiv(a1,c); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | } // Fin du namespace | 
|---|
| 141 |  | 
|---|
| 142 | //////////////////////////////////////////////////////////////// | 
|---|
| 143 | //////////////////////////////////////////////////////////////// | 
|---|
| 144 | //------------------------------------------------------------// | 
|---|
| 145 | //                   Inverse d'une matrice                    // | 
|---|
| 146 | //------------------------------------------------------------// | 
|---|
| 147 | //////////////////////////////////////////////////////////////// | 
|---|
| 148 | //////////////////////////////////////////////////////////////// | 
|---|
| 149 |  | 
|---|
| 150 | namespace SOPHYA { | 
|---|
| 151 |  | 
|---|
| 152 | /*! \ingroup TArray | 
|---|
| 153 | \fn Inverse(TMatrix<r_4> const &) | 
|---|
| 154 | \brief To inverse a TMatrix | 
|---|
| 155 | */ | 
|---|
| 156 | inline TMatrix<r_4> Inverse(TMatrix<r_4> const & A) | 
|---|
| 157 | {return SimpleMatrixOperation<r_4>::Inverse(A);} | 
|---|
| 158 | /*! \ingroup TArray | 
|---|
| 159 | \fn Inverse(TMatrix<r_8> const &) | 
|---|
| 160 | \brief To inverse a TMatrix | 
|---|
| 161 | */ | 
|---|
| 162 | inline TMatrix<r_8> Inverse(TMatrix<r_8> const & A) | 
|---|
| 163 | {return SimpleMatrixOperation<r_8>::Inverse(A);} | 
|---|
| 164 | /*! \ingroup TArray | 
|---|
| 165 | \fn Inverse(TMatrix< complex<r_4> > const &) | 
|---|
| 166 | \brief To inverse a TMatrix (complex numbers) | 
|---|
| 167 | */ | 
|---|
| 168 | inline TMatrix< complex<r_4> > Inverse(TMatrix< complex<r_4> > const & A) | 
|---|
| 169 | {return SimpleMatrixOperation< complex<r_4> >::Inverse(A);} | 
|---|
| 170 | /*! \ingroup TArray | 
|---|
| 171 | \fn Inverse(TMatrix< complex<r_8> > const &) | 
|---|
| 172 | \brief To inverse a TMatrix (complex numbers) | 
|---|
| 173 | */ | 
|---|
| 174 | inline TMatrix< complex<r_8> > Inverse(TMatrix< complex<r_8> > const & A) | 
|---|
| 175 | {return SimpleMatrixOperation< complex<r_8> >::Inverse(A);} | 
|---|
| 176 |  | 
|---|
| 177 | } // Fin du namespace | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 | //////////////////////////////////////////////////////////////// | 
|---|
| 181 | //////////////////////////////////////////////////////////////// | 
|---|
| 182 | //------------------------------------------------------------// | 
|---|
| 183 | //                   Linear fitting                           // | 
|---|
| 184 | //------------------------------------------------------------// | 
|---|
| 185 | //////////////////////////////////////////////////////////////// | 
|---|
| 186 | //////////////////////////////////////////////////////////////// | 
|---|
| 187 |  | 
|---|
| 188 | namespace SOPHYA { | 
|---|
| 189 |  | 
|---|
| 190 | /*! | 
|---|
| 191 | \class LinFitter | 
|---|
| 192 | \ingroup TArray | 
|---|
| 193 | Class for linear fitting | 
|---|
| 194 | \sa TMatrix TArray | 
|---|
| 195 | */ | 
|---|
| 196 |  | 
|---|
| 197 | //!  Class for linear fitting | 
|---|
| 198 | template <class T> | 
|---|
| 199 | class LinFitter { | 
|---|
| 200 |  | 
|---|
| 201 | public : | 
|---|
| 202 |  | 
|---|
| 203 | LinFitter(); | 
|---|
| 204 | virtual ~LinFitter(); | 
|---|
| 205 |  | 
|---|
| 206 | //! Linear fitting | 
|---|
| 207 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, | 
|---|
| 208 | uint_4 nf, T (*f)(uint_4,T), TVector<T>& c); | 
|---|
| 209 |  | 
|---|
| 210 | //! Linear fitting | 
|---|
| 211 | r_8 LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c); | 
|---|
| 212 |  | 
|---|
| 213 | //! Linear fitting with errors | 
|---|
| 214 | r_8 LinFit(const TVector<T>& x, const TVector<T>& y, const TVector<T>& errY2, | 
|---|
| 215 | uint_4 nf,T (*f)(uint_4,T), TVector<T>& c, TVector<T>& errC); | 
|---|
| 216 |  | 
|---|
| 217 | //! Linear fitting with errors | 
|---|
| 218 | r_8 LinFit(const TMatrix<T>& fx, const TVector<T>& y, | 
|---|
| 219 | const TVector<T>& errY2, TVector<T>& c, TVector<T>& errC); | 
|---|
| 220 | }; | 
|---|
| 221 |  | 
|---|
| 222 | } // Fin du namespace | 
|---|
| 223 |  | 
|---|
| 224 | #endif | 
|---|