| [772] | 1 | //                         C.Magneville, R.Ansari        03/2000 | 
|---|
|  | 2 |  | 
|---|
|  | 3 | #include "machdefs.h" | 
|---|
|  | 4 | #include <stdio.h> | 
|---|
|  | 5 | #include <iostream.h> | 
|---|
|  | 6 | #include <complex> | 
|---|
|  | 7 | #include <math.h> | 
|---|
|  | 8 | #include "sopemtx.h" | 
|---|
|  | 9 | #include "smathconst.h" | 
|---|
|  | 10 |  | 
|---|
|  | 11 | //////////////////////////////////////////////////////////////// | 
|---|
| [939] | 12 | // ---------------------------------------------------------- // | 
|---|
|  | 13 | //  La classe de gestion des lignes et colonnes d'une matrice // | 
|---|
|  | 14 | // ---------------------------------------------------------- // | 
|---|
| [772] | 15 | //////////////////////////////////////////////////////////////// | 
|---|
| [926] | 16 |  | 
|---|
| [939] | 17 | /*! | 
|---|
|  | 18 | \class SOPHYA::TMatrixRC | 
|---|
|  | 19 | \ingroup TArray | 
|---|
|  | 20 | Class for representing rows, columns and diagonal of a matrix. | 
|---|
|  | 21 | \sa TMatrix TArray | 
|---|
|  | 22 | */ | 
|---|
|  | 23 |  | 
|---|
| [926] | 24 | //! Class of line, column or diagonal of a TMatrix | 
|---|
|  | 25 | /*! | 
|---|
|  | 26 | A TMatrixRC represents a line, a column or the diagonal of a TMatrix | 
|---|
|  | 27 | */ | 
|---|
| [804] | 28 | template <class T> | 
|---|
|  | 29 | class TMatrixRC { | 
|---|
|  | 30 | public: | 
|---|
| [926] | 31 | //! Define type of TMatrixRC | 
|---|
|  | 32 | enum TRCKind { | 
|---|
|  | 33 | TmatrixRow=0,  //!< TMatrixRC ligne | 
|---|
|  | 34 | TmatrixCol=1,  //!< TMatrixRC column | 
|---|
|  | 35 | TmatrixDiag=2  //!< TMatrixRC diagonal | 
|---|
|  | 36 | }; | 
|---|
| [804] | 37 | TMatrixRC(); | 
|---|
|  | 38 | TMatrixRC(TMatrix<T>& m, TRCKind kind, uint_4 index=0); | 
|---|
|  | 39 | virtual ~TMatrixRC() {} | 
|---|
| [772] | 40 |  | 
|---|
| [804] | 41 | // Acces aux rangees et colonnes de matrices | 
|---|
|  | 42 | static TMatrixRC<T> Row(TMatrix<T> & m, uint_4 r); | 
|---|
|  | 43 | static TMatrixRC<T> Col(TMatrix<T> & m, uint_4 c); | 
|---|
|  | 44 | static TMatrixRC<T> Diag(TMatrix<T> & m); | 
|---|
|  | 45 |  | 
|---|
|  | 46 | // ---- A virer   $CHECK$ Reza 03/2000 | 
|---|
|  | 47 | //  int_4 Next(); | 
|---|
|  | 48 | //  int_4 Prev(); | 
|---|
|  | 49 | int_4 SetCol(int_4 c); | 
|---|
|  | 50 | int_4 SetRow(int_4 r); | 
|---|
|  | 51 | int_4 SetDiag(); | 
|---|
|  | 52 |  | 
|---|
|  | 53 | static uint_4 Step(const TMatrix<T>& m, TRCKind rckind); | 
|---|
|  | 54 | static T* Org(const TMatrix<T>&, TRCKind rckind, uint_4 ind=0); | 
|---|
|  | 55 |  | 
|---|
| [926] | 56 | //! Return the kind of TMatrix (line,column,diagonal) | 
|---|
| [804] | 57 | TRCKind Kind() const { return kind; } | 
|---|
|  | 58 | uint_4 NElts() const; | 
|---|
|  | 59 | T& operator()(uint_4 i); | 
|---|
|  | 60 | T  operator()(uint_4 i) const; | 
|---|
|  | 61 |  | 
|---|
|  | 62 |  | 
|---|
|  | 63 | TMatrixRC<T>& operator = (const TMatrixRC<T>& rc); | 
|---|
|  | 64 |  | 
|---|
|  | 65 | // ---- A virer   $CHECK$ Reza 03/2000 | 
|---|
|  | 66 | //   TVector<T> GetVect() const; | 
|---|
|  | 67 | //   TMatrixRC<T>& operator += (const TMatrixRC<T>& rc); | 
|---|
|  | 68 | //   TMatrixRC<T>& operator -= (const TMatrixRC<T>& rc); | 
|---|
|  | 69 |  | 
|---|
|  | 70 | TMatrixRC<T>& operator *= (T x); | 
|---|
|  | 71 | TMatrixRC<T>& operator /= (T x); | 
|---|
|  | 72 |  | 
|---|
|  | 73 | //   TMatrixRC<T>& operator -= (T x); | 
|---|
|  | 74 | //   TMatrixRC<T>& operator += (T x); | 
|---|
|  | 75 |  | 
|---|
|  | 76 |  | 
|---|
|  | 77 | TMatrixRC<T>& LinComb(T a, T b, const TMatrixRC& rc, uint_4 first=0); | 
|---|
|  | 78 | TMatrixRC<T>& LinComb(T b, const TMatrixRC<T>& rc, uint_4 first=0); | 
|---|
|  | 79 |  | 
|---|
|  | 80 | uint_4 IMaxAbs(uint_4 first=0); | 
|---|
| [813] | 81 | void Print(ostream & os) const ; | 
|---|
| [804] | 82 |  | 
|---|
|  | 83 | static void Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2); | 
|---|
|  | 84 |  | 
|---|
| [926] | 85 | //! Define Absolute value for uint_1 | 
|---|
| [804] | 86 | inline static double Abs_Value(uint_1 v) {return (double) v;} | 
|---|
| [926] | 87 | //! Define Absolute value for uint_2 | 
|---|
| [804] | 88 | inline static double Abs_Value(uint_2 v) {return (double) v;} | 
|---|
| [926] | 89 | //! Define Absolute value for int_2 | 
|---|
| [804] | 90 | inline static double Abs_Value(int_2 v)  {return (v>0)? (double) v: (double) -v;} | 
|---|
| [926] | 91 | //! Define Absolute value for int_4 | 
|---|
| [804] | 92 | inline static double Abs_Value(int_4 v)  {return (v>0)? (double) v: (double) -v;} | 
|---|
| [926] | 93 | //! Define Absolute value for int_8 | 
|---|
| [804] | 94 | inline static double Abs_Value(int_8 v)  {return (v>0)? (double) v: (double) -v;} | 
|---|
| [926] | 95 | //! Define Absolute value for uint_4 | 
|---|
| [804] | 96 | inline static double Abs_Value(uint_4 v) {return (double) v;} | 
|---|
| [926] | 97 | //! Define Absolute value for uint_8 | 
|---|
| [804] | 98 | inline static double Abs_Value(uint_8 v) {return (double) v;} | 
|---|
| [926] | 99 | //! Define Absolute value for r_4 | 
|---|
| [804] | 100 | inline static double Abs_Value(r_4 v)    {return (double) fabsf(v);} | 
|---|
| [926] | 101 | //! Define Absolute value for r_8 | 
|---|
| [804] | 102 | inline static double Abs_Value(r_8 v)    {return fabs(v);} | 
|---|
| [926] | 103 | //! Define Absolute value for complex r_4 | 
|---|
|  | 104 | inline static double Abs_Value(complex<r_4> v) | 
|---|
| [804] | 105 | {return sqrt(v.real()*v.real()+v.imag()*v.imag());} | 
|---|
| [926] | 106 | //! Define Absolute value for complex r_8 | 
|---|
|  | 107 | inline static double Abs_Value(complex<r_8> v) | 
|---|
| [804] | 108 | {return sqrt(v.real()*v.real()+v.imag()*v.imag());} | 
|---|
|  | 109 |  | 
|---|
|  | 110 | protected: | 
|---|
| [926] | 111 | TMatrix<T>* matrix;  //!< pointer to the TMatrix | 
|---|
|  | 112 | T*          data;    //!< pointer to the beginnig of interesting datas | 
|---|
|  | 113 | int_4       index;   //!< index of the line/column | 
|---|
|  | 114 | uint_4      step;    //!< step of the line/column | 
|---|
|  | 115 | TRCKind     kind;    //!< type: line, column or diagonal | 
|---|
| [804] | 116 | }; | 
|---|
|  | 117 |  | 
|---|
| [935] | 118 | //! Scalar product of two TMatrixRC | 
|---|
|  | 119 | /*! | 
|---|
|  | 120 | \return sum[ a(i) * b(i) ] | 
|---|
|  | 121 | */ | 
|---|
| [804] | 122 | template <class T> | 
|---|
|  | 123 | inline T operator * (const TMatrixRC<T>& a, const TMatrixRC<T>& b) | 
|---|
|  | 124 | { | 
|---|
|  | 125 | if ( a.NElts() != b.NElts() ) | 
|---|
|  | 126 | throw(SzMismatchError("TMatrixRC::operator * size mismatch\n")); | 
|---|
|  | 127 | if ( a.Kind() != b.Kind() ) | 
|---|
|  | 128 | throw(SzMismatchError("TMatrixRC::operator * type mismatch\n")); | 
|---|
|  | 129 | T sum = 0; | 
|---|
|  | 130 | for(uint_4 i=0; i<a.NElts(); i++) sum += a(i)*b(i); | 
|---|
|  | 131 | return sum; | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
| [935] | 134 | //! Get the step in datas for a TMatrix for type rckind | 
|---|
|  | 135 | /*! | 
|---|
|  | 136 | \param rckind : line, column or diagonal | 
|---|
|  | 137 | \return step in TMatrix along TMatrixRC | 
|---|
|  | 138 | */ | 
|---|
| [804] | 139 | template <class T> | 
|---|
|  | 140 | inline uint_4 TMatrixRC<T>::Step(const TMatrix<T>& m, TRCKind rckind) | 
|---|
| [813] | 141 | { switch (rckind) { case TmatrixRow  : return m.Step(m.ColsKA()); | 
|---|
|  | 142 | case TmatrixCol  : return m.Step(m.RowsKA()); | 
|---|
| [804] | 143 | case TmatrixDiag : return (m.Step(m.RowsKA())+m.Step(m.ColsKA())); } | 
|---|
|  | 144 | return 0; } | 
|---|
|  | 145 |  | 
|---|
| [935] | 146 | //! Get the origin of datas. | 
|---|
|  | 147 | /*! | 
|---|
|  | 148 | Get the origin of datas in the TMatrix for a TMatrixRC for type | 
|---|
|  | 149 | \b rckind and index \b index . | 
|---|
|  | 150 | \param rckind : line, column or diagonal | 
|---|
|  | 151 | \param index : index of the line or column. | 
|---|
|  | 152 | \return adress of the first element in datas. | 
|---|
|  | 153 | */ | 
|---|
| [804] | 154 | template <class T> | 
|---|
|  | 155 | inline T* TMatrixRC<T>::Org(const TMatrix<T>& m, TRCKind rckind, uint_4 index) | 
|---|
|  | 156 | { switch (rckind) { case TmatrixRow  : return const_cast<T *>(&(m(index,0))); | 
|---|
|  | 157 | case TmatrixCol  : return const_cast<T *>(&(m(0,index))); | 
|---|
|  | 158 | case TmatrixDiag : return const_cast<T *>(m.Data()); } | 
|---|
|  | 159 | return NULL; } | 
|---|
|  | 160 |  | 
|---|
| [926] | 161 | //! return number of elements for a TMatrixRC | 
|---|
| [804] | 162 | template <class T> inline uint_4 TMatrixRC<T>::NElts() const | 
|---|
|  | 163 | { if (!matrix) return 0; | 
|---|
|  | 164 | switch (kind) { case TmatrixRow  : return matrix->NCols(); | 
|---|
|  | 165 | case TmatrixCol  : return matrix->NRows(); | 
|---|
|  | 166 | case TmatrixDiag : return matrix->NCols(); } | 
|---|
|  | 167 | return 0; } | 
|---|
|  | 168 |  | 
|---|
| [926] | 169 | //! access of element \b i | 
|---|
| [804] | 170 | template <class T> | 
|---|
|  | 171 | inline T& TMatrixRC<T>::operator()(uint_4 i) {return data[i*step];} | 
|---|
| [926] | 172 | //! access of element \b i | 
|---|
| [804] | 173 | template <class T> | 
|---|
|  | 174 | inline T  TMatrixRC<T>::operator()(uint_4 i) const {return data[i*step];} | 
|---|
|  | 175 |  | 
|---|
|  | 176 | //////////////////////////////////////////////////////////////// | 
|---|
| [935] | 177 | //! Typedef to simplify TMatrixRC<r_8> writing | 
|---|
| [804] | 178 | typedef TMatrixRC<r_8> MatrixRC; | 
|---|
|  | 179 |  | 
|---|
|  | 180 |  | 
|---|
| [926] | 181 | //! Default constructor | 
|---|
| [772] | 182 | template <class T> TMatrixRC<T>::TMatrixRC() | 
|---|
|  | 183 | : matrix(NULL), data(NULL), index(0), step(0) | 
|---|
|  | 184 | {} | 
|---|
|  | 185 |  | 
|---|
| [926] | 186 | //! Constructor | 
|---|
|  | 187 | /*! | 
|---|
|  | 188 | \param m : matrix | 
|---|
|  | 189 | \param rckind : select line, column or diagonal | 
|---|
|  | 190 | \param ind : number of the line or column | 
|---|
|  | 191 | */ | 
|---|
| [772] | 192 | template <class T> TMatrixRC<T>::TMatrixRC(TMatrix<T>& m,TRCKind rckind,uint_4 ind) | 
|---|
|  | 193 | : matrix(&m), data(Org(m,rckind,ind)), | 
|---|
|  | 194 | index(ind), step(Step(m,rckind)), kind(rckind) | 
|---|
|  | 195 | { | 
|---|
|  | 196 | if (kind == TmatrixDiag && m.NCols() != m.NRows()) | 
|---|
|  | 197 | throw(SzMismatchError("TMatrixRC::TMatrixRC(...,TmatrixDiag,...) size mismatch\n")); | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 201 | // Acces aux rangees et colonnes de matrices | 
|---|
|  | 202 |  | 
|---|
| [926] | 203 | //! Return TMatrixRC for line \b r of matrix \b m | 
|---|
| [772] | 204 | template <class T> | 
|---|
|  | 205 | TMatrixRC<T> TMatrixRC<T>::Row(TMatrix<T> & m, uint_4 r) | 
|---|
|  | 206 | { | 
|---|
|  | 207 | TMatrixRC<T> rc(m, TmatrixRow, r); | 
|---|
|  | 208 | return rc; | 
|---|
|  | 209 | } | 
|---|
|  | 210 |  | 
|---|
| [926] | 211 | //! Return TMatrixRC for column \b r of matrix \b m | 
|---|
| [772] | 212 | template <class T> | 
|---|
|  | 213 | TMatrixRC<T> TMatrixRC<T>::Col(TMatrix<T> & m, uint_4 c) | 
|---|
|  | 214 | { | 
|---|
|  | 215 | TMatrixRC<T> rc(m, TmatrixCol, c); | 
|---|
|  | 216 | return rc; | 
|---|
|  | 217 | } | 
|---|
|  | 218 |  | 
|---|
| [926] | 219 | //! Return TMatrixRC for diagonal of matrix \b m | 
|---|
| [772] | 220 | template <class T> | 
|---|
|  | 221 | TMatrixRC<T> TMatrixRC<T>::Diag(TMatrix<T> & m) | 
|---|
|  | 222 | { | 
|---|
|  | 223 | TMatrixRC<T> rc(m, TmatrixDiag); | 
|---|
|  | 224 | return rc; | 
|---|
|  | 225 | } | 
|---|
|  | 226 |  | 
|---|
|  | 227 |  | 
|---|
| [804] | 228 | // ---- A virer   $CHECK$ Reza 03/2000 | 
|---|
|  | 229 | // template <class T> int_4 TMatrixRC<T>::Next() | 
|---|
|  | 230 | // { | 
|---|
|  | 231 | // if (!matrix || kind==TmatrixDiag) return -1; | 
|---|
|  | 232 | // index++; | 
|---|
|  | 233 | // if(kind == TmatrixRow) { | 
|---|
|  | 234 | //   if(index > (int_4)matrix->NRows()) {index = (int_4)matrix->NRows(); return -1;} | 
|---|
|  | 235 | //   data += matrix->NCols(); | 
|---|
|  | 236 | // } else { | 
|---|
|  | 237 | //   if (index > (int_4)matrix->NCols()) {index = (int_4)matrix->NCols(); return -1;} | 
|---|
|  | 238 | //   data++; | 
|---|
|  | 239 | // } | 
|---|
|  | 240 | // return index; | 
|---|
|  | 241 | // } | 
|---|
| [772] | 242 |  | 
|---|
| [804] | 243 | // template <class T> int_4 TMatrixRC<T>::Prev() | 
|---|
|  | 244 | // { | 
|---|
|  | 245 | // if (!matrix || kind == TmatrixDiag) return -1; | 
|---|
|  | 246 | // index--; | 
|---|
|  | 247 | // if(index < 0) {index = 0; return -1;} | 
|---|
|  | 248 | // if(kind == TmatrixRow) data -= matrix->NCols(); | 
|---|
|  | 249 | // else data--; | 
|---|
|  | 250 | // return index; | 
|---|
|  | 251 | // } | 
|---|
| [772] | 252 |  | 
|---|
| [926] | 253 | //! Set column \b c for this TMatrixRC | 
|---|
| [772] | 254 | template <class T> int_4 TMatrixRC<T>::SetCol(int_4 c) | 
|---|
|  | 255 | { | 
|---|
|  | 256 | if(!matrix) return -1; | 
|---|
|  | 257 | if(c<0 || c>(int_4)matrix->NCols()) return -1; | 
|---|
|  | 258 | kind = TmatrixCol; | 
|---|
|  | 259 | index = c; | 
|---|
|  | 260 | step = Step(*matrix, TmatrixCol); | 
|---|
|  | 261 | data = Org(*matrix, TmatrixCol, c); | 
|---|
|  | 262 | return c; | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
| [926] | 265 | //! Set line \b r for this TMatrixRC | 
|---|
| [772] | 266 | template <class T> int_4 TMatrixRC<T>::SetRow(int_4 r) | 
|---|
|  | 267 | { | 
|---|
|  | 268 | if(!matrix) return -1; | 
|---|
|  | 269 | if(r<0 && r>(int_4)matrix->NRows()) return -1; | 
|---|
|  | 270 | kind = TmatrixRow; | 
|---|
|  | 271 | index = r; | 
|---|
|  | 272 | step = Step(*matrix, TmatrixRow); | 
|---|
|  | 273 | data = Org(*matrix, TmatrixRow, r); | 
|---|
|  | 274 | return r; | 
|---|
|  | 275 | } | 
|---|
|  | 276 |  | 
|---|
| [926] | 277 | //! Set line diaginal for this TMatrixRC | 
|---|
| [772] | 278 | template <class T> int_4 TMatrixRC<T>::SetDiag() | 
|---|
|  | 279 | { | 
|---|
|  | 280 | if (!matrix) return -1; | 
|---|
|  | 281 | if (matrix->NCols() != matrix->NRows()) | 
|---|
|  | 282 | throw(SzMismatchError("TMatrixRC::SetDiag size mismatch\n")); | 
|---|
|  | 283 | kind = TmatrixDiag; | 
|---|
|  | 284 | index = 0; | 
|---|
|  | 285 | step = Step(*matrix, TmatrixDiag); | 
|---|
|  | 286 | data = Org(*matrix, TmatrixDiag); | 
|---|
|  | 287 | return 0; | 
|---|
|  | 288 | } | 
|---|
|  | 289 |  | 
|---|
| [926] | 290 | //! Operator = | 
|---|
| [772] | 291 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator = (const TMatrixRC<T>& rc) | 
|---|
|  | 292 | { | 
|---|
|  | 293 | matrix = rc.matrix; | 
|---|
|  | 294 | data   = rc.data; | 
|---|
|  | 295 | index  = rc.index; | 
|---|
|  | 296 | step   = rc.step; | 
|---|
|  | 297 | kind   = rc.kind; | 
|---|
|  | 298 | return *this; | 
|---|
|  | 299 | } | 
|---|
|  | 300 |  | 
|---|
| [804] | 301 | // ---- A virer   $CHECK$ Reza 03/2000 | 
|---|
|  | 302 | // template <class T> TVector<T> TMatrixRC<T>::GetVect() const | 
|---|
|  | 303 | // { | 
|---|
|  | 304 | // TVector<T> v(NElts()); | 
|---|
|  | 305 | // for (uint_4 i=0; i<NElts(); i++) v(i) = (*this)(i); | 
|---|
|  | 306 | // return v; | 
|---|
|  | 307 | // } | 
|---|
| [772] | 308 |  | 
|---|
| [804] | 309 | // template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (const TMatrixRC<T>& rc) | 
|---|
|  | 310 | // { | 
|---|
|  | 311 | // if ( NElts() != rc.NElts() ) | 
|---|
|  | 312 | //   throw(SzMismatchError("TMatrixRC::operator+= size mismatch\n")); | 
|---|
|  | 313 | // if ( kind != rc.kind ) | 
|---|
|  | 314 | //   throw(SzMismatchError("TMatrixRC::operator+= type mismatch\n")); | 
|---|
|  | 315 | // for (uint_4 i=0; i<NElts(); i++) (*this)(i) += rc(i); | 
|---|
|  | 316 | // return *this; | 
|---|
|  | 317 | // } | 
|---|
| [772] | 318 |  | 
|---|
| [804] | 319 | // template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (const TMatrixRC<T>& rc) | 
|---|
|  | 320 | // { | 
|---|
|  | 321 | // if( NElts() != rc.NElts() ) | 
|---|
|  | 322 | //   throw(SzMismatchError("TMatrixRC::operator-= size mismatch\n")); | 
|---|
|  | 323 | // if( kind != rc.kind ) | 
|---|
|  | 324 | //   throw(SzMismatchError("TMatrixRC::operator-= type mismatch\n")); | 
|---|
|  | 325 | // for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= rc(i); | 
|---|
|  | 326 | // return *this; | 
|---|
|  | 327 | // } | 
|---|
| [772] | 328 |  | 
|---|
| [926] | 329 | //! Operator to multiply by constant \b x | 
|---|
| [772] | 330 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator *= (T x) | 
|---|
|  | 331 | { | 
|---|
|  | 332 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) *= x; | 
|---|
|  | 333 | return *this; | 
|---|
|  | 334 | } | 
|---|
|  | 335 |  | 
|---|
| [926] | 336 | //! Operator to divide by constant \b x | 
|---|
| [772] | 337 | template <class T> TMatrixRC<T>& TMatrixRC<T>::operator /= (T x) | 
|---|
|  | 338 | { | 
|---|
|  | 339 | for(uint_4 i=0; i<NElts(); i++) (*this)(i) /= x; | 
|---|
|  | 340 | return *this; | 
|---|
|  | 341 | } | 
|---|
|  | 342 |  | 
|---|
|  | 343 |  | 
|---|
| [804] | 344 | // ---- A virer   $CHECK$ Reza 03/2000 | 
|---|
|  | 345 | // template <class T> TMatrixRC<T>& TMatrixRC<T>::operator -= (T x) | 
|---|
|  | 346 | // { | 
|---|
|  | 347 | // for(uint_4 i=0; i<NElts(); i++) (*this)(i) -= x; | 
|---|
|  | 348 | // return *this; | 
|---|
|  | 349 | // } | 
|---|
| [772] | 350 |  | 
|---|
| [804] | 351 | // template <class T> TMatrixRC<T>& TMatrixRC<T>::operator += (T x) | 
|---|
|  | 352 | // { | 
|---|
|  | 353 | // for(uint_4 i=0; i<NElts(); i++) (*this)(i) += x; | 
|---|
|  | 354 | // return *this; | 
|---|
|  | 355 | // } | 
|---|
| [772] | 356 |  | 
|---|
| [926] | 357 | //! Linear combination | 
|---|
|  | 358 | /*! | 
|---|
|  | 359 | Do : \f$ MRC(i) = MRC(i)*a + rc(i)*b \f$ | 
|---|
|  | 360 | \return *this | 
|---|
|  | 361 | */ | 
|---|
| [772] | 362 | template <class T> | 
|---|
|  | 363 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T a, T b, const TMatrixRC<T>& rc, uint_4 first) | 
|---|
|  | 364 | { | 
|---|
|  | 365 | if ( NElts() != rc.NElts() ) | 
|---|
|  | 366 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n")); | 
|---|
|  | 367 | if ( kind != rc.kind ) | 
|---|
|  | 368 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n")); | 
|---|
|  | 369 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) = (*this)(i)*a + rc(i)*b; | 
|---|
|  | 370 | return *this; | 
|---|
|  | 371 | } | 
|---|
|  | 372 |  | 
|---|
| [926] | 373 | //! Linear combination | 
|---|
|  | 374 | /*! | 
|---|
|  | 375 | Do : \f$ MRC(i) = MRC(i) + rc(i)*b \f$ | 
|---|
|  | 376 | */ | 
|---|
| [772] | 377 | template <class T> | 
|---|
|  | 378 | TMatrixRC<T>& TMatrixRC<T>::LinComb(T b, const TMatrixRC<T>& rc, uint_4 first) | 
|---|
|  | 379 | { | 
|---|
|  | 380 | if ( NElts() != rc.NElts() ) | 
|---|
|  | 381 | throw(SzMismatchError("TMatrixRC::LinComb size mismatch\n")); | 
|---|
|  | 382 | if ( kind != rc.kind ) | 
|---|
|  | 383 | throw(SzMismatchError("TMatrixRC::LinComb type mismatch\n")); | 
|---|
|  | 384 | for(uint_4 i=first; i<NElts(); i++) (*this)(i) += rc(i)*b; | 
|---|
|  | 385 | return *this; | 
|---|
|  | 386 | } | 
|---|
|  | 387 |  | 
|---|
| [926] | 388 | //! Find maximum absolute value in TMatrixRC, search begin at \b first | 
|---|
| [772] | 389 | template <class T> uint_4 TMatrixRC<T>::IMaxAbs(uint_4 first) | 
|---|
|  | 390 | { | 
|---|
|  | 391 | if (first>NElts()) | 
|---|
|  | 392 | throw(SzMismatchError("TMatrixRC::IMaxAbs size mismatch\n")); | 
|---|
|  | 393 | uint_4 imax = first; | 
|---|
|  | 394 | double vmax = Abs_Value((*this)(first)); | 
|---|
|  | 395 | for(uint_4 i=first+1; i<NElts(); i++) { | 
|---|
|  | 396 | double v = Abs_Value((*this)(i)); | 
|---|
|  | 397 | if(v > vmax) {vmax = v; imax = i;} | 
|---|
|  | 398 | } | 
|---|
|  | 399 | return imax; | 
|---|
|  | 400 | } | 
|---|
|  | 401 |  | 
|---|
| [926] | 402 | //! Print on stream \b os | 
|---|
| [772] | 403 | template <class T> | 
|---|
| [813] | 404 | void TMatrixRC<T>::Print(ostream & os) const | 
|---|
|  | 405 | { | 
|---|
|  | 406 | os << " TMatrixRC<T>::Print(ostream & os) " << NElts() << " Kind=" | 
|---|
|  | 407 | << kind << " Index=" << index << " Step= " << step << endl; | 
|---|
|  | 408 | for(uint_4 i=0; i<NElts(); i++) { | 
|---|
|  | 409 | os << (*this)(i); | 
|---|
|  | 410 | if (kind == TmatrixCol)  os << endl; | 
|---|
|  | 411 | else os << ", "; | 
|---|
|  | 412 | } | 
|---|
|  | 413 | os << endl; | 
|---|
|  | 414 | } | 
|---|
|  | 415 |  | 
|---|
| [926] | 416 | //! Swap two TMatrixRC of the same kind | 
|---|
| [813] | 417 | template <class T> | 
|---|
| [772] | 418 | void TMatrixRC<T>::Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2) | 
|---|
|  | 419 | { | 
|---|
|  | 420 | if(rc1.NElts() != rc2.NElts()) | 
|---|
|  | 421 | throw(SzMismatchError("TMatrixRC::Swap size mismatch\n")); | 
|---|
|  | 422 | if(rc1.kind != rc2.kind) | 
|---|
|  | 423 | throw(SzMismatchError("TMatrixRC::Swap type mismatch\n")); | 
|---|
|  | 424 | if(rc1.data == rc2.data) return; | 
|---|
|  | 425 | for(uint_4 i=0; i<rc1.NElts(); i++) | 
|---|
|  | 426 | {T tmp = rc1(i); rc1(i) = rc2(i); rc2(i) = tmp;} | 
|---|
|  | 427 | } | 
|---|
|  | 428 |  | 
|---|
|  | 429 |  | 
|---|
| [926] | 430 | //////////////////////////////////////////////////////////////// | 
|---|
| [939] | 431 | // ---------------------------------------------------------- // | 
|---|
|  | 432 | //      La classe de calcul simple sur les TMatrix            // | 
|---|
|  | 433 | // ---------------------------------------------------------- // | 
|---|
| [926] | 434 | //////////////////////////////////////////////////////////////// | 
|---|
| [772] | 435 |  | 
|---|
|  | 436 | //**** Pour inversion | 
|---|
|  | 437 | #ifndef M_LN2 | 
|---|
|  | 438 | #define M_LN2 0.69314718055994530942 | 
|---|
|  | 439 | #endif | 
|---|
| [1007] | 440 | //// CMV BUG BUG : sur OSF 5.0 DMINEXP est deconnant (~1.e+19 !!!) | 
|---|
| [772] | 441 | #ifndef LN_MINDOUBLE | 
|---|
|  | 442 | #define LN_MINDOUBLE  (M_LN2 * (DMINEXP - 1)) | 
|---|
|  | 443 | #endif | 
|---|
|  | 444 | #ifndef LN_MAXDOUBLE | 
|---|
|  | 445 | #define LN_MAXDOUBLE  (M_LN2 * DMAXEXP) | 
|---|
|  | 446 | #endif | 
|---|
|  | 447 |  | 
|---|
| [1007] | 448 | template <class T> | 
|---|
|  | 449 | int SimpleMatrixOperation<T>::GausPivScaling = PIV_GLOB_SCALE; | 
|---|
|  | 450 |  | 
|---|
| [926] | 451 | //! Gaussian pivoting | 
|---|
|  | 452 | /*! | 
|---|
| [999] | 453 | Do Gauss pivoting of \b a, doing the same operations on matrix \b b | 
|---|
| [1007] | 454 | \param computedet = true : return determimant of \b a (beware of over/underfloat) | 
|---|
|  | 455 | \param computedet = false : return 1 if OK, 0 if not. | 
|---|
| [999] | 456 | \verbatim | 
|---|
|  | 457 | Solve linear system A(n,n) * X(n,m) = B(n,m) | 
|---|
|  | 458 | and put solution X in B for return. | 
|---|
|  | 459 | \endverbatim | 
|---|
|  | 460 | \warning If \b b is identity matrix, return inverse of \b a | 
|---|
|  | 461 | \warning matrix \b a and \b b are modified. | 
|---|
| [926] | 462 | */ | 
|---|
| [772] | 463 | template <class T> | 
|---|
| [1007] | 464 | T SimpleMatrixOperation<T>::GausPiv(TMatrix<T>& a, TMatrix<T>& b,bool computedet) | 
|---|
| [772] | 465 | // Pivot de Gauss | 
|---|
|  | 466 | // * Attention: egcs impose que cette fonction soit mise dans le .cc | 
|---|
|  | 467 | //              avant ::Inverse() (car Inverse() l'utilise) | 
|---|
|  | 468 | // {TMatrix A(a); TMatrix B(b); return (T) TMatrix::GausPiv(A,B);} | 
|---|
|  | 469 | { | 
|---|
|  | 470 | uint_4 n = a.NRows(); | 
|---|
|  | 471 | if(n!=b.NRows()) | 
|---|
|  | 472 | throw(SzMismatchError("TMatrix::GausPiv size mismatch\n")); | 
|---|
|  | 473 |  | 
|---|
| [926] | 474 | T det = 1; | 
|---|
| [1007] | 475 |  | 
|---|
|  | 476 | ////////////////// | 
|---|
|  | 477 | // Data scaling // | 
|---|
|  | 478 | ////////////////// | 
|---|
|  | 479 |  | 
|---|
|  | 480 | // Pas de normalisation des donnees | 
|---|
|  | 481 | if(GausPivScaling==PIV_NO_SCALE) { | 
|---|
|  | 482 | if(computedet) det = (T) 1; | 
|---|
|  | 483 | // normalisation des donnees ligne par ligne | 
|---|
|  | 484 | } else if(GausPivScaling==PIV_ROW_SCALE) { | 
|---|
|  | 485 | double nrm = 0.; | 
|---|
|  | 486 | for(uint_4 iii=0; iii<a.NRows(); iii++) { | 
|---|
|  | 487 | uint_4 jjj; double vmax = -1.; | 
|---|
|  | 488 | for(jjj=0; jjj<a.NCols(); jjj++) { | 
|---|
|  | 489 | double v = TMatrixRC<T>::Abs_Value(a(iii,jjj)); | 
|---|
|  | 490 | if(v>vmax) vmax = v; | 
|---|
|  | 491 | } | 
|---|
|  | 492 | if(vmax>0.) { | 
|---|
|  | 493 | for(jjj=0; jjj<a.NCols(); jjj++) a(iii,jjj) /= (T) vmax; | 
|---|
|  | 494 | for(jjj=0; jjj<b.NCols(); jjj++) b(iii,jjj) /= (T) vmax; | 
|---|
|  | 495 | nrm += log(vmax); | 
|---|
|  | 496 | } else return (T) 0; | 
|---|
| [772] | 497 | } | 
|---|
| [1007] | 498 | if(computedet) { | 
|---|
|  | 499 | if(nrm <= LN_MINDOUBLE || nrm >= LN_MAXDOUBLE) { | 
|---|
|  | 500 | cerr<<"GausPiv_Row: normalisation failure, " | 
|---|
|  | 501 | <<"determinant has to be multiplied by exp("<<nrm<<")"<<endl; | 
|---|
|  | 502 | } else det = (T) exp(nrm); | 
|---|
|  | 503 | } | 
|---|
|  | 504 | // On fait une normalisation un peu brutale globale | 
|---|
|  | 505 | } else { | 
|---|
|  | 506 | double vmin=MAXDOUBLE, vmax=0; | 
|---|
|  | 507 | for(uint_4 iii=0; iii<a.NRows(); iii++) | 
|---|
|  | 508 | for(uint_4 jjj=0; jjj<a.NCols(); jjj++) { | 
|---|
|  | 509 | double v = TMatrixRC<T>::Abs_Value(a(iii,jjj)); | 
|---|
|  | 510 | if(v>vmax) vmax = v; | 
|---|
|  | 511 | if(v<vmin && v>0.) vmin = v; | 
|---|
|  | 512 | } | 
|---|
|  | 513 | double nrm = sqrt(vmin*vmax); | 
|---|
|  | 514 | if(nrm>0.) { a /= (T) nrm; b /= (T) nrm;} else return (T) 0; | 
|---|
|  | 515 | if(computedet) { | 
|---|
|  | 516 | nrm = a.NRows() * log(nrm); | 
|---|
|  | 517 | if (nrm <= LN_MINDOUBLE || nrm >= LN_MAXDOUBLE) { | 
|---|
|  | 518 | cerr<<"GausPiv_Glob: normalisation failure, " | 
|---|
|  | 519 | <<"determinant has to be multiplied by exp("<<nrm<<")"<<endl; | 
|---|
|  | 520 | } else det = (T) exp(nrm); | 
|---|
|  | 521 | } | 
|---|
| [772] | 522 | } | 
|---|
|  | 523 |  | 
|---|
| [1007] | 524 | //////////////////////////////////////// | 
|---|
|  | 525 | // Gaussian elimination with pivoting // | 
|---|
|  | 526 | //////////////////////////////////////// | 
|---|
|  | 527 |  | 
|---|
| [926] | 528 | TMatrixRC<T> pivRowa(a,TMatrixRC<T>::TmatrixRow); | 
|---|
|  | 529 | TMatrixRC<T> pivRowb(b,TMatrixRC<T>::TmatrixRow); | 
|---|
| [772] | 530 |  | 
|---|
|  | 531 | for(uint_4 k=0; k<n-1; k++) { | 
|---|
|  | 532 | uint_4 iPiv = TMatrixRC<T>::Col(a, k).IMaxAbs(k); | 
|---|
|  | 533 | if(iPiv != k) { | 
|---|
|  | 534 | TMatrixRC<T> aIPiv(TMatrixRC<T>::Row(a,iPiv)); | 
|---|
|  | 535 | TMatrixRC<T> aK(TMatrixRC<T>::Row(a, k)); | 
|---|
|  | 536 | TMatrixRC<T>::Swap(aIPiv,aK); | 
|---|
|  | 537 | TMatrixRC<T> bIPiv(TMatrixRC<T>::Row(b, iPiv)); | 
|---|
|  | 538 | TMatrixRC<T> bK(TMatrixRC<T>::Row(b, k)); | 
|---|
|  | 539 | TMatrixRC<T>::Swap(bIPiv,bK); | 
|---|
|  | 540 | } | 
|---|
| [926] | 541 | T pivot = a(k,k); | 
|---|
|  | 542 | if( TMatrixRC<T>::Abs_Value(pivot) < 1.e-50 ) return (T) 0; | 
|---|
| [1007] | 543 | if(computedet) det *= pivot; | 
|---|
| [772] | 544 | pivRowa.SetRow(k); // to avoid constructors | 
|---|
|  | 545 | pivRowb.SetRow(k); | 
|---|
|  | 546 | for (uint_4 i=k+1; i<n; i++) { | 
|---|
| [926] | 547 | T r = -a(i,k)/pivot; | 
|---|
| [772] | 548 | TMatrixRC<T>::Row(a, i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa | 
|---|
|  | 549 | TMatrixRC<T>::Row(b, i).LinComb(r, pivRowb); | 
|---|
|  | 550 | } | 
|---|
|  | 551 | } | 
|---|
| [1007] | 552 | if(computedet) det *= a(n-1, n-1); | 
|---|
| [772] | 553 |  | 
|---|
|  | 554 | // on remonte | 
|---|
|  | 555 | for(uint_4 kk=n-1; kk>0; kk--) { | 
|---|
| [926] | 556 | T pivot = a(kk,kk); | 
|---|
|  | 557 | if( TMatrixRC<T>::Abs_Value(pivot) <= 1.e-50 ) return (T) 0; | 
|---|
| [772] | 558 | pivRowa.SetRow(kk); // to avoid constructors | 
|---|
|  | 559 | pivRowb.SetRow(kk); | 
|---|
|  | 560 | for(uint_4 jj=0; jj<kk; jj++) { | 
|---|
| [926] | 561 | T r = -a(jj,kk)/pivot; | 
|---|
| [772] | 562 | TMatrixRC<T>::Row(a, jj).LinComb(r, pivRowa); | 
|---|
|  | 563 | TMatrixRC<T>::Row(b, jj).LinComb(r, pivRowb); | 
|---|
|  | 564 | } | 
|---|
|  | 565 | } | 
|---|
|  | 566 |  | 
|---|
|  | 567 | for(uint_4 l=0; l<n; l++) { | 
|---|
| [926] | 568 | if( TMatrixRC<T>::Abs_Value(a(l,l)) <= 1.e-50 ) return (T) 0; | 
|---|
| [772] | 569 | TMatrixRC<T>::Row(b, l) /= a(l,l); | 
|---|
|  | 570 | } | 
|---|
|  | 571 |  | 
|---|
|  | 572 | return det; | 
|---|
|  | 573 | } | 
|---|
|  | 574 |  | 
|---|
| [926] | 575 | //! Return the inverse matrix of \b A | 
|---|
| [772] | 576 | template <class T> | 
|---|
|  | 577 | TMatrix<T> SimpleMatrixOperation<T>::Inverse(TMatrix<T> const & A) | 
|---|
|  | 578 | { | 
|---|
| [976] | 579 | TMatrix<T> a(A,false); | 
|---|
| [813] | 580 | TMatrix<T> b(a.NCols(),a.NRows());  b = IdentityMatrix(1.); | 
|---|
| [1007] | 581 | if(GausPiv(a,b)==(T) 0) | 
|---|
| [926] | 582 | throw(MathExc("TMatrix Inverse() Singular Matrix")); | 
|---|
| [772] | 583 | return b; | 
|---|
|  | 584 | } | 
|---|
|  | 585 |  | 
|---|
|  | 586 |  | 
|---|
| [926] | 587 | //////////////////////////////////////////////////////////////// | 
|---|
| [939] | 588 | // ---------------------------------------------------------- // | 
|---|
|  | 589 | //               La classe fit lineaire                       // | 
|---|
|  | 590 | // ---------------------------------------------------------- // | 
|---|
| [926] | 591 | //////////////////////////////////////////////////////////////// | 
|---|
|  | 592 |  | 
|---|
| [939] | 593 | //! Creator | 
|---|
|  | 594 | template <class T> | 
|---|
|  | 595 | LinFitter<T>::LinFitter() | 
|---|
| [804] | 596 | { | 
|---|
|  | 597 | } | 
|---|
| [772] | 598 |  | 
|---|
| [939] | 599 | //! Destructor | 
|---|
|  | 600 | template <class T> | 
|---|
|  | 601 | LinFitter<T>::~LinFitter() | 
|---|
| [804] | 602 | { | 
|---|
|  | 603 | } | 
|---|
|  | 604 |  | 
|---|
| [939] | 605 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1; | 
|---|
|  | 606 | //! Linear fitting | 
|---|
|  | 607 | /*! | 
|---|
|  | 608 | Linear fit of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$ | 
|---|
|  | 609 | \param x : vector of X values | 
|---|
|  | 610 | \param y : vector of datas | 
|---|
|  | 611 | \param nf: number of functions | 
|---|
|  | 612 | \param f : f(i,x(k)), i=0..nf-1 | 
|---|
|  | 613 | \return c : vector of solutions | 
|---|
|  | 614 | \return return chisquare | 
|---|
|  | 615 | */ | 
|---|
|  | 616 | template <class T> | 
|---|
|  | 617 | r_8 LinFitter<T>::LinFit(const TVector<T>& x, const TVector<T>& y, | 
|---|
|  | 618 | uint_4 nf, T (*f)(uint_4,T), TVector<T>& c) | 
|---|
| [804] | 619 | { | 
|---|
| [939] | 620 | uint_4 n = x.NElts(); | 
|---|
|  | 621 | if (n != y.NElts()) THROW(sizeMismatchErr); | 
|---|
|  | 622 |  | 
|---|
|  | 623 | TMatrix<T> fx(nf,n); | 
|---|
| [804] | 624 |  | 
|---|
| [939] | 625 | for(uint_4 i=0; i<nf; i++) | 
|---|
|  | 626 | for(uint_4 j=0; j<n; j++) fx(i,j) = f(i,x(j)); | 
|---|
|  | 627 |  | 
|---|
|  | 628 | return LinFit(fx,y,c); | 
|---|
| [804] | 629 | } | 
|---|
|  | 630 |  | 
|---|
| [939] | 631 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1, | 
|---|
|  | 632 | // la matrice fx contient les valeurs des f: fx(i,j) = f(i, x(j)). | 
|---|
|  | 633 | //! Linear fitting | 
|---|
|  | 634 | /*! | 
|---|
|  | 635 | Linear fit of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$. | 
|---|
|  | 636 | \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$. | 
|---|
|  | 637 | \param y : vector of datas | 
|---|
|  | 638 | \return c : vector of solutions | 
|---|
|  | 639 | \return return chisquare | 
|---|
|  | 640 | */ | 
|---|
|  | 641 | template <class T> | 
|---|
|  | 642 | r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c) | 
|---|
| [804] | 643 | { | 
|---|
| [939] | 644 | uint_4 n = y.NElts(); | 
|---|
|  | 645 | if (n != fx.NCol()) THROW(sizeMismatchErr); | 
|---|
| [804] | 646 |  | 
|---|
| [939] | 647 | uint_4 nf = fx.NRows(); | 
|---|
| [804] | 648 |  | 
|---|
| [939] | 649 | TMatrix<T> a(nf,nf); | 
|---|
| [804] | 650 |  | 
|---|
| [939] | 651 | for(uint_4 j=0; j<nf; j++) | 
|---|
|  | 652 | for(uint_4 k=j; k<nf; k++) | 
|---|
|  | 653 | a(j,k) = a(k,j) = TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), j) | 
|---|
|  | 654 | * TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), k); | 
|---|
| [804] | 655 |  | 
|---|
| [939] | 656 | c = fx * y; | 
|---|
| [804] | 657 |  | 
|---|
| [939] | 658 | if(SimpleMatrixOperation<T>::GausPiv(a,c)==(T) 0) THROW(singMatxErr); | 
|---|
| [804] | 659 |  | 
|---|
| [939] | 660 | r_8 xi2 = 0., ax; | 
|---|
|  | 661 | T x; | 
|---|
|  | 662 | for(uint_4 k=0; k<n; k++) { | 
|---|
|  | 663 | x = (T) 0; | 
|---|
|  | 664 | for(uint_4 i=0; i<nf; i++) x += c(i) * fx(i,k); | 
|---|
|  | 665 | x -= y(k); | 
|---|
|  | 666 | ax = TMatrixRC<T>::Abs_Value(x); | 
|---|
|  | 667 | xi2 += ax*ax; | 
|---|
| [804] | 668 | } | 
|---|
| [939] | 669 | return xi2; | 
|---|
|  | 670 | } | 
|---|
| [804] | 671 |  | 
|---|
| [939] | 672 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1, | 
|---|
|  | 673 | // errY2 contient les carres des erreurs sur les Y. | 
|---|
|  | 674 | // au retour, errC contient les erreurs sur les coefs. | 
|---|
|  | 675 | //! Linear fitting with errors | 
|---|
|  | 676 | /*! | 
|---|
|  | 677 | Linear fit with errors of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$. | 
|---|
|  | 678 | \param x : vector of X values | 
|---|
|  | 679 | \param y : vector of datas | 
|---|
|  | 680 | \param errY2 : vector of errors square on Y | 
|---|
|  | 681 | \param nf: number of functions | 
|---|
|  | 682 | \param f : f(i,x(k)), i=0..nf-1 | 
|---|
|  | 683 | \return c : vector of solutions | 
|---|
|  | 684 | \return errC : vector of errors on solutions C | 
|---|
|  | 685 | \return return chisquare | 
|---|
|  | 686 | */ | 
|---|
|  | 687 | template <class T> | 
|---|
|  | 688 | r_8 LinFitter<T>::LinFit(const TVector<T>& x, const TVector<T>& y, | 
|---|
|  | 689 | const TVector<T>& errY2, uint_4 nf, T (*f)(uint_4,T), | 
|---|
|  | 690 | TVector<T>& c, TVector<T>& errC) | 
|---|
|  | 691 | { | 
|---|
|  | 692 | uint_4 n = x.NElts(); | 
|---|
|  | 693 | if (n != y.NElts()) THROW(sizeMismatchErr); | 
|---|
| [804] | 694 |  | 
|---|
| [939] | 695 | TMatrix<T> fx(nf, n); | 
|---|
|  | 696 | for(uint_4 i=0; i<nf; i++) | 
|---|
|  | 697 | for(uint_4 j=0; j<n; j++) | 
|---|
|  | 698 | fx(i,j) = f(i,x(j)); | 
|---|
| [804] | 699 |  | 
|---|
| [939] | 700 | return LinFit(fx,y,errY2,c,errC); | 
|---|
| [804] | 701 | } | 
|---|
|  | 702 |  | 
|---|
| [939] | 703 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1, | 
|---|
|  | 704 | // la matrice fx contient les valeurs des f: | 
|---|
|  | 705 | // fx(i,j) = f(i, x(j)). | 
|---|
|  | 706 | // errY2 contient les carres des erreurs sur les Y. | 
|---|
|  | 707 | // au retour, errC contient les erreurs sur les coefs. | 
|---|
|  | 708 | //! Linear fitting with errors | 
|---|
|  | 709 | /*! | 
|---|
|  | 710 | \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$. | 
|---|
|  | 711 | \param y : vector of datas | 
|---|
|  | 712 | \param errY2 : vector of errors square on Y | 
|---|
|  | 713 | \return c : vector of solutions | 
|---|
|  | 714 | \return errC : vector of errors on solutions on C | 
|---|
|  | 715 | \return return chisquare | 
|---|
|  | 716 | */ | 
|---|
|  | 717 | template <class T> | 
|---|
|  | 718 | r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y, | 
|---|
|  | 719 | const TVector<T>& errY2,TVector<T>& c, TVector<T>& errC) | 
|---|
| [804] | 720 | { | 
|---|
| [939] | 721 | uint_4 n = y.NElts(); | 
|---|
|  | 722 | if( n != errY2.NElts()) THROW(sizeMismatchErr); | 
|---|
|  | 723 | if( n != fx.NCol()) THROW(sizeMismatchErr); | 
|---|
| [804] | 724 |  | 
|---|
| [939] | 725 | uint_4 nf = fx.NRows(); | 
|---|
| [804] | 726 |  | 
|---|
| [939] | 727 | TMatrix<T> a(nf,nf); | 
|---|
| [804] | 728 |  | 
|---|
| [939] | 729 | c.Realloc(nf); | 
|---|
|  | 730 | errC.Realloc(nf); | 
|---|
| [804] | 731 |  | 
|---|
| [939] | 732 | for(uint_4 j=0; j<nf; j++) | 
|---|
|  | 733 | for(uint_4 k=j; k<nf; k++) { | 
|---|
|  | 734 | T x=0; | 
|---|
|  | 735 | // Matrice a inverser | 
|---|
|  | 736 | for(uint_4 l=0; l<n; l++) x += fx(j,l)*fx(k,l)/errY2(l); | 
|---|
|  | 737 | a(j,k) = a(k,j) = x; | 
|---|
|  | 738 | } | 
|---|
| [804] | 739 |  | 
|---|
| [939] | 740 | TMatrix<T> d(nf,nf+1); | 
|---|
|  | 741 | for(uint_4 k=0; k<nf; k++) { | 
|---|
|  | 742 | T x = (T) 0; | 
|---|
|  | 743 | // Second membre 1ere colonne | 
|---|
|  | 744 | for(uint_4 l=0; l<n; l++) x += fx(k,l)*y(l)/errY2(l); | 
|---|
|  | 745 | d(k,0) = x; | 
|---|
|  | 746 | // Reste second membre = Id. | 
|---|
|  | 747 | for(uint_4 m=1; m<=nf; m++) d(k,m) = (k==m)?1:0; | 
|---|
|  | 748 | } | 
|---|
| [804] | 749 |  | 
|---|
| [939] | 750 | if(SimpleMatrixOperation<T>::GausPiv(a,d)==(T) 0) THROW(singMatxErr); | 
|---|
| [804] | 751 |  | 
|---|
| [939] | 752 | for(uint_4 l=0; l<nf; l++) { | 
|---|
|  | 753 | c(l) = d(l,0);        // Parametres = 1ere colonne | 
|---|
|  | 754 | errC(l) = d(l,l+1);   // Erreurs = diag inverse. | 
|---|
|  | 755 | } | 
|---|
| [804] | 756 |  | 
|---|
| [939] | 757 | r_8 xi2 = 0., ax; | 
|---|
|  | 758 | T x; | 
|---|
|  | 759 | for(uint_4 jj=0; jj<n; jj++) { | 
|---|
|  | 760 | x = (T) 0; | 
|---|
|  | 761 | for(uint_4 ii=0; ii<nf; ii++) x += c(ii) * fx(ii,jj); | 
|---|
| [804] | 762 | x -= y(jj); | 
|---|
| [939] | 763 | ax = TMatrixRC<T>::Abs_Value(x); | 
|---|
|  | 764 | xi2 += ax*ax/TMatrixRC<T>::Abs_Value(errY2(jj)); | 
|---|
| [804] | 765 | } | 
|---|
|  | 766 | return xi2; | 
|---|
|  | 767 | } | 
|---|
|  | 768 |  | 
|---|
| [939] | 769 | /////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 770 | /////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 771 | /////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 772 | /////////////////////////////////////////////////////////////////////////// | 
|---|
| [813] | 773 | void _ZZ_TestTMatrixRC_YY_(TMatrix<r_8> & m) | 
|---|
|  | 774 | { | 
|---|
|  | 775 | cout << " ::::: _ZZ_TestTMatrixRC_YY_ :::: M= \n" << m << endl; | 
|---|
|  | 776 | TMatrixRC<r_8> l0 = TMatrixRC<r_8>::Row(m,0); | 
|---|
|  | 777 | cout << "TMatrixRC<r_8>::Row(m,0) = \n" ; | 
|---|
|  | 778 | l0.Print(cout); | 
|---|
|  | 779 | TMatrixRC<r_8> l1 = TMatrixRC<r_8>::Row(m,1); | 
|---|
|  | 780 | cout << "TMatrixRC<r_8>::Row(m,1) = \n" ; | 
|---|
|  | 781 | l1.Print(cout); | 
|---|
|  | 782 | l0.SetRow(2); | 
|---|
|  | 783 | cout << "TMatrixRC<r_8>::l0.SetRow(2 = \n" ; | 
|---|
|  | 784 | l0.Print(cout); | 
|---|
| [804] | 785 |  | 
|---|
| [813] | 786 | TMatrixRC<r_8> c0 = TMatrixRC<r_8>::Col(m,0); | 
|---|
|  | 787 | cout << "TMatrixRC<r_8>::Col(m,0) = \n" ; | 
|---|
|  | 788 | c0.Print(cout); | 
|---|
|  | 789 | TMatrixRC<r_8> c1 = TMatrixRC<r_8>::Col(m,1); | 
|---|
|  | 790 | cout << "TMatrixRC<r_8>::Col(m,1) = \n" ; | 
|---|
|  | 791 | c1.Print(cout); | 
|---|
|  | 792 | c0.SetCol(2); | 
|---|
|  | 793 | cout << "TMatrixRC<r_8>::c0.SetCol(2) = \n" ; | 
|---|
|  | 794 | c0.Print(cout); | 
|---|
|  | 795 | TMatrixRC<r_8> c00 = TMatrixRC<r_8>::Col(m,0); | 
|---|
|  | 796 | TMatrixRC<r_8>::Swap(c0, c00); | 
|---|
|  | 797 | cout << " :::::  M Apres Swap = \n" << m << endl; | 
|---|
|  | 798 |  | 
|---|
|  | 799 | } | 
|---|
| [804] | 800 |  | 
|---|
| [772] | 801 | /////////////////////////////////////////////////////////////// | 
|---|
|  | 802 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
|  | 803 | // Instances gestion lignes/colonnes | 
|---|
|  | 804 | #pragma define_template TMatrixRC<int_4> | 
|---|
|  | 805 | #pragma define_template TMatrixRC<r_4> | 
|---|
|  | 806 | #pragma define_template TMatrixRC<r_8> | 
|---|
| [926] | 807 | #pragma define_template TMatrixRC< complex<r_4> > | 
|---|
|  | 808 | #pragma define_template TMatrixRC< complex<r_8> > | 
|---|
| [804] | 809 | #pragma define_template SimpleMatrixOperation<int_4> | 
|---|
|  | 810 | #pragma define_template SimpleMatrixOperation<r_4> | 
|---|
| [772] | 811 | #pragma define_template SimpleMatrixOperation<r_8> | 
|---|
| [926] | 812 | #pragma define_template SimpleMatrixOperation< complex<r_4> > | 
|---|
|  | 813 | #pragma define_template SimpleMatrixOperation< complex<r_8> > | 
|---|
| [939] | 814 | #pragma define_template LinFitter<r_4> | 
|---|
|  | 815 | #pragma define_template LinFitter<r_8> | 
|---|
|  | 816 | #pragma define_template LinFitter< complex<r_4> > | 
|---|
|  | 817 | #pragma define_template LinFitter< complex<r_8> > | 
|---|
| [772] | 818 | #endif | 
|---|
|  | 819 |  | 
|---|
|  | 820 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
|  | 821 | // Instances gestion lignes/colonnes | 
|---|
|  | 822 | template class TMatrixRC<int_4>; | 
|---|
|  | 823 | template class TMatrixRC<r_4>; | 
|---|
|  | 824 | template class TMatrixRC<r_8>; | 
|---|
| [926] | 825 | template class TMatrixRC< complex<r_4> >; | 
|---|
|  | 826 | template class TMatrixRC< complex<r_8> >; | 
|---|
| [804] | 827 | template class SimpleMatrixOperation<int_4>; | 
|---|
| [772] | 828 | template class SimpleMatrixOperation<r_4>; | 
|---|
|  | 829 | template class SimpleMatrixOperation<r_8>; | 
|---|
| [926] | 830 | template class SimpleMatrixOperation< complex<r_4> >; | 
|---|
|  | 831 | template class SimpleMatrixOperation< complex<r_8> >; | 
|---|
| [939] | 832 | template class LinFitter<r_4>; | 
|---|
|  | 833 | template class LinFitter<r_8>; | 
|---|
|  | 834 | template class LinFitter< complex<r_4> >; | 
|---|
|  | 835 | template class LinFitter< complex<r_8> >; | 
|---|
| [772] | 836 | #endif | 
|---|
| [935] | 837 |  | 
|---|