| 1 | //                         C.Magneville, R.Ansari        03/2000
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include "sopnamsp.h"
 | 
|---|
| 4 | #include "machdefs.h"
 | 
|---|
| 5 | #include <stdio.h>
 | 
|---|
| 6 | #include <iostream>
 | 
|---|
| 7 | #include <complex>
 | 
|---|
| 8 | #include <math.h>
 | 
|---|
| 9 | #include "sopemtx.h"
 | 
|---|
| 10 | #include "smathconst.h"
 | 
|---|
| 11 | 
 | 
|---|
| 12 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 13 | // ---------------------------------------------------------- //
 | 
|---|
| 14 | //  La classe de gestion des lignes et colonnes d'une matrice //
 | 
|---|
| 15 | // ---------------------------------------------------------- //
 | 
|---|
| 16 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /*! \cond
 | 
|---|
| 19 |   \class SOPHYA::TMatrixRC
 | 
|---|
| 20 |   \ingroup TArray
 | 
|---|
| 21 |   Class for representing rows, columns and diagonal of a matrix.
 | 
|---|
| 22 |   \sa TMatrix TArray
 | 
|---|
| 23 | */
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /*! \endcond */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace SOPHYA {
 | 
|---|
| 28 | template <class T>
 | 
|---|
| 29 | class TMatrixRC {
 | 
|---|
| 30 | public:
 | 
|---|
| 31 |   //! Define type of TMatrixRC
 | 
|---|
| 32 |   enum TRCKind {
 | 
|---|
| 33 |     TmatrixRow=0,  //!< TMatrixRC ligne
 | 
|---|
| 34 |     TmatrixCol=1,  //!< TMatrixRC column
 | 
|---|
| 35 |     TmatrixDiag=2  //!< TMatrixRC diagonal
 | 
|---|
| 36 |   };
 | 
|---|
| 37 |   TMatrixRC();
 | 
|---|
| 38 |   TMatrixRC(TMatrix<T>& m, TRCKind kind, uint_4 index=0);
 | 
|---|
| 39 |   virtual ~TMatrixRC() {}
 | 
|---|
| 40 | 
 | 
|---|
| 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 | 
 | 
|---|
| 56 |   //! Return the kind of TMatrix (line,column,diagonal)
 | 
|---|
| 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);
 | 
|---|
| 81 |   void Print(ostream & os) const ;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   static void Swap(TMatrixRC<T>& rc1, TMatrixRC<T>& rc2);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   //! Define Absolute value for uint_1
 | 
|---|
| 86 |   inline static double Abs_Value(uint_1 v) {return (double) v;}
 | 
|---|
| 87 |   //! Define Absolute value for uint_2
 | 
|---|
| 88 |   inline static double Abs_Value(uint_2 v) {return (double) v;}
 | 
|---|
| 89 |   //! Define Absolute value for int_2
 | 
|---|
| 90 |   inline static double Abs_Value(int_2 v)  {return (v>0)? (double) v: (double) -v;}
 | 
|---|
| 91 |   //! Define Absolute value for int_4
 | 
|---|
| 92 |   inline static double Abs_Value(int_4 v)  {return (v>0)? (double) v: (double) -v;}
 | 
|---|
| 93 |   //! Define Absolute value for int_8
 | 
|---|
| 94 |   inline static double Abs_Value(int_8 v)  {return (v>0)? (double) v: (double) -v;}
 | 
|---|
| 95 |   //! Define Absolute value for uint_4
 | 
|---|
| 96 |   inline static double Abs_Value(uint_4 v) {return (double) v;}
 | 
|---|
| 97 |   //! Define Absolute value for uint_8
 | 
|---|
| 98 |   inline static double Abs_Value(uint_8 v) {return (double) v;}
 | 
|---|
| 99 |   //! Define Absolute value for r_4
 | 
|---|
| 100 |   inline static double Abs_Value(r_4 v)    {return (double) fabs((double)v);}
 | 
|---|
| 101 |   //! Define Absolute value for r_8
 | 
|---|
| 102 |   inline static double Abs_Value(r_8 v)    {return fabs(v);}
 | 
|---|
| 103 |   //! Define Absolute value for complex r_4
 | 
|---|
| 104 |   inline static double Abs_Value(complex<r_4> v)
 | 
|---|
| 105 |                 {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
 | 
|---|
| 106 |   //! Define Absolute value for complex r_8
 | 
|---|
| 107 |   inline static double Abs_Value(complex<r_8> v)
 | 
|---|
| 108 |                 {return sqrt(v.real()*v.real()+v.imag()*v.imag());}
 | 
|---|
| 109 | 
 | 
|---|
| 110 | protected:
 | 
|---|
| 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
 | 
|---|
| 116 | };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | //! Scalar product of two TMatrixRC
 | 
|---|
| 119 | /*!
 | 
|---|
| 120 |   \return sum[ a(i) * b(i) ]
 | 
|---|
| 121 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 139 | template <class T>
 | 
|---|
| 140 | inline uint_4 TMatrixRC<T>::Step(const TMatrix<T>& m, TRCKind rckind)
 | 
|---|
| 141 |   { switch (rckind) { case TmatrixRow  : return m.Step(m.ColsKA());
 | 
|---|
| 142 |                       case TmatrixCol  : return m.Step(m.RowsKA());
 | 
|---|
| 143 |                       case TmatrixDiag : return (m.Step(m.RowsKA())+m.Step(m.ColsKA())); }
 | 
|---|
| 144 |     return 0; }
 | 
|---|
| 145 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 161 | //! return number of elements for a TMatrixRC
 | 
|---|
| 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 | 
 | 
|---|
| 169 | //! access of element \b i
 | 
|---|
| 170 | template <class T>
 | 
|---|
| 171 | inline T& TMatrixRC<T>::operator()(uint_4 i) {return data[i*step];}
 | 
|---|
| 172 | //! access of element \b i
 | 
|---|
| 173 | template <class T>
 | 
|---|
| 174 | inline T  TMatrixRC<T>::operator()(uint_4 i) const {return data[i*step];}
 | 
|---|
| 175 | 
 | 
|---|
| 176 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 177 | //! Typedef to simplify TMatrixRC<r_8> writing
 | 
|---|
| 178 | typedef TMatrixRC<r_8> MatrixRC;
 | 
|---|
| 179 | 
 | 
|---|
| 180 | 
 | 
|---|
| 181 | //! Default constructor
 | 
|---|
| 182 | template <class T> TMatrixRC<T>::TMatrixRC()
 | 
|---|
| 183 | : matrix(NULL), data(NULL), index(0), step(0)
 | 
|---|
| 184 | {}
 | 
|---|
| 185 | 
 | 
|---|
| 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 | */
 | 
|---|
| 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 | 
 | 
|---|
| 203 | //! Return TMatrixRC for line \b r of matrix \b m
 | 
|---|
| 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 | 
 | 
|---|
| 211 | //! Return TMatrixRC for column \b r of matrix \b m
 | 
|---|
| 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 | 
 | 
|---|
| 219 | //! Return TMatrixRC for diagonal of matrix \b m
 | 
|---|
| 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 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 242 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | //! Set column \b c for this TMatrixRC
 | 
|---|
| 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 | 
 | 
|---|
| 265 | //! Set line \b r for this TMatrixRC
 | 
|---|
| 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 | 
 | 
|---|
| 277 | //! Set line diaginal for this TMatrixRC
 | 
|---|
| 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 | 
 | 
|---|
| 290 | //! Operator =
 | 
|---|
| 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 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 308 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 318 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 328 | 
 | 
|---|
| 329 | //! Operator to multiply by constant \b x
 | 
|---|
| 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 | 
 | 
|---|
| 336 | //! Operator to divide by constant \b x
 | 
|---|
| 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 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 350 | 
 | 
|---|
| 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 | // }
 | 
|---|
| 356 | 
 | 
|---|
| 357 | //! Linear combination
 | 
|---|
| 358 | /*!
 | 
|---|
| 359 |   Do : \f$ MRC(i) = MRC(i)*a + rc(i)*b \f$
 | 
|---|
| 360 |   \return *this
 | 
|---|
| 361 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 373 | //! Linear combination
 | 
|---|
| 374 | /*!
 | 
|---|
| 375 |   Do : \f$ MRC(i) = MRC(i) + rc(i)*b \f$
 | 
|---|
| 376 |  */
 | 
|---|
| 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 | 
 | 
|---|
| 388 | //! Find maximum absolute value in TMatrixRC, search begin at \b first
 | 
|---|
| 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 | 
 | 
|---|
| 402 | //! Print on stream \b os
 | 
|---|
| 403 | template <class T>
 | 
|---|
| 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 | 
 | 
|---|
| 416 | //! Swap two TMatrixRC of the same kind
 | 
|---|
| 417 | template <class T>
 | 
|---|
| 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 | 
 | 
|---|
| 430 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 431 | // ---------------------------------------------------------- //
 | 
|---|
| 432 | //      La classe de calcul simple sur les TMatrix            //
 | 
|---|
| 433 | // ---------------------------------------------------------- //
 | 
|---|
| 434 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 435 | 
 | 
|---|
| 436 | //**** Pour inversion
 | 
|---|
| 437 | #ifndef M_LN2
 | 
|---|
| 438 | #define M_LN2 0.69314718055994530942
 | 
|---|
| 439 | #endif
 | 
|---|
| 440 | //// CMV BUG BUG : sur OSF 5.0 DMINEXP est deconnant (~1.e+19 !!!)
 | 
|---|
| 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 | 
 | 
|---|
| 448 | template <class T>
 | 
|---|
| 449 | int SimpleMatrixOperation<T>::GausPivScaling = PIV_GLOB_SCALE;
 | 
|---|
| 450 | 
 | 
|---|
| 451 | //! Gaussian pivoting
 | 
|---|
| 452 | /*!
 | 
|---|
| 453 |   Do Gauss pivoting of \b a, doing the same operations on matrix \b b
 | 
|---|
| 454 |   \param computedet = true : return determimant of \b a (beware of over/underfloat)
 | 
|---|
| 455 |   \param computedet = false : return 1 if OK, 0 if not.
 | 
|---|
| 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.
 | 
|---|
| 462 |  */  
 | 
|---|
| 463 | template <class T>
 | 
|---|
| 464 | T SimpleMatrixOperation<T>::GausPiv(TMatrix<T>& a, TMatrix<T>& b,bool computedet)
 | 
|---|
| 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 | 
 | 
|---|
| 474 | T det = 1;
 | 
|---|
| 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;
 | 
|---|
| 497 |   }
 | 
|---|
| 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 |   }
 | 
|---|
| 522 | }
 | 
|---|
| 523 | 
 | 
|---|
| 524 | ////////////////////////////////////////
 | 
|---|
| 525 | // Gaussian elimination with pivoting //
 | 
|---|
| 526 | ////////////////////////////////////////
 | 
|---|
| 527 | 
 | 
|---|
| 528 | TMatrixRC<T> pivRowa(a,TMatrixRC<T>::TmatrixRow);
 | 
|---|
| 529 | TMatrixRC<T> pivRowb(b,TMatrixRC<T>::TmatrixRow);
 | 
|---|
| 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 |   }
 | 
|---|
| 541 |   T pivot = a(k,k);
 | 
|---|
| 542 |   if( TMatrixRC<T>::Abs_Value(pivot) < 1.e-50 ) return (T) 0;
 | 
|---|
| 543 |   if(computedet) det *= pivot;
 | 
|---|
| 544 |   pivRowa.SetRow(k); // to avoid constructors
 | 
|---|
| 545 |   pivRowb.SetRow(k);
 | 
|---|
| 546 |   for (uint_4 i=k+1; i<n; i++) {
 | 
|---|
| 547 |     T r = -a(i,k)/pivot;
 | 
|---|
| 548 |     TMatrixRC<T>::Row(a, i).LinComb(r, pivRowa); // + rapide que -= r * pivRowa
 | 
|---|
| 549 |     TMatrixRC<T>::Row(b, i).LinComb(r, pivRowb);
 | 
|---|
| 550 |   }
 | 
|---|
| 551 | }
 | 
|---|
| 552 | if(computedet) det *= a(n-1, n-1);
 | 
|---|
| 553 | 
 | 
|---|
| 554 | // on remonte
 | 
|---|
| 555 | for(uint_4 kk=n-1; kk>0; kk--) {
 | 
|---|
| 556 |   T pivot = a(kk,kk);
 | 
|---|
| 557 |   if( TMatrixRC<T>::Abs_Value(pivot) <= 1.e-50 ) return (T) 0;
 | 
|---|
| 558 |   pivRowa.SetRow(kk); // to avoid constructors
 | 
|---|
| 559 |   pivRowb.SetRow(kk);
 | 
|---|
| 560 |   for(uint_4 jj=0; jj<kk; jj++) {
 | 
|---|
| 561 |     T r = -a(jj,kk)/pivot;
 | 
|---|
| 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++) {
 | 
|---|
| 568 |   if( TMatrixRC<T>::Abs_Value(a(l,l)) <= 1.e-50 ) return (T) 0;
 | 
|---|
| 569 |   TMatrixRC<T>::Row(b, l) /= a(l,l);
 | 
|---|
| 570 | }
 | 
|---|
| 571 | 
 | 
|---|
| 572 | return det;
 | 
|---|
| 573 | }
 | 
|---|
| 574 | 
 | 
|---|
| 575 | //! Return the inverse matrix of \b A
 | 
|---|
| 576 | template <class T>
 | 
|---|
| 577 | TMatrix<T> SimpleMatrixOperation<T>::Inverse(TMatrix<T> const & A) 
 | 
|---|
| 578 | {
 | 
|---|
| 579 | TMatrix<T> a(A,false);
 | 
|---|
| 580 | TMatrix<T> b(a.NCols(),a.NRows());  b = IdentityMatrix(1.);
 | 
|---|
| 581 | if(GausPiv(a,b)==(T) 0)
 | 
|---|
| 582 |   throw(MathExc("TMatrix Inverse() Singular Matrix"));
 | 
|---|
| 583 | return b;
 | 
|---|
| 584 | }
 | 
|---|
| 585 | 
 | 
|---|
| 586 | 
 | 
|---|
| 587 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 588 | // ---------------------------------------------------------- //
 | 
|---|
| 589 | //               La classe fit lineaire                       //
 | 
|---|
| 590 | // ---------------------------------------------------------- //
 | 
|---|
| 591 | ////////////////////////////////////////////////////////////////
 | 
|---|
| 592 | 
 | 
|---|
| 593 | //! Creator
 | 
|---|
| 594 | template <class T>
 | 
|---|
| 595 | LinFitter<T>::LinFitter()
 | 
|---|
| 596 | {
 | 
|---|
| 597 | }
 | 
|---|
| 598 | 
 | 
|---|
| 599 | //! Destructor
 | 
|---|
| 600 | template <class T>
 | 
|---|
| 601 | LinFitter<T>::~LinFitter()
 | 
|---|
| 602 | {
 | 
|---|
| 603 | }
 | 
|---|
| 604 | 
 | 
|---|
| 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)
 | 
|---|
| 619 | {
 | 
|---|
| 620 | uint_4 n = x.NElts();
 | 
|---|
| 621 | if (n != y.NElts()) 
 | 
|---|
| 622 |   throw SzMismatchError("LinFitter<T>::LinFit(x,y,nf,f,c)/Error x.NElts() <> y.Nelts() ");
 | 
|---|
| 623 |  
 | 
|---|
| 624 | TMatrix<T> fx(nf,n);
 | 
|---|
| 625 | 
 | 
|---|
| 626 | for(uint_4 i=0; i<nf; i++)
 | 
|---|
| 627 |   for(uint_4 j=0; j<n; j++) fx(i,j) = f(i,x(j));
 | 
|---|
| 628 | 
 | 
|---|
| 629 | return LinFit(fx,y,c);
 | 
|---|
| 630 | }
 | 
|---|
| 631 | 
 | 
|---|
| 632 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
 | 
|---|
| 633 | // la matrice fx contient les valeurs des f: fx(i,j) = f(i, x(j)).
 | 
|---|
| 634 | //! Linear fitting
 | 
|---|
| 635 | /*!
 | 
|---|
| 636 |   Linear fit of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$.
 | 
|---|
| 637 |   \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$.
 | 
|---|
| 638 |   \param y : vector of datas
 | 
|---|
| 639 |   \return c : vector of solutions
 | 
|---|
| 640 |   \return return chisquare
 | 
|---|
| 641 |  */
 | 
|---|
| 642 | template <class T>
 | 
|---|
| 643 | r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y, TVector<T>& c)
 | 
|---|
| 644 | {
 | 
|---|
| 645 | uint_4 n = y.NElts();
 | 
|---|
| 646 | if (n != fx.NCol()) 
 | 
|---|
| 647 |   throw SzMismatchError("LinFitter<T>::LinFit(fx, y, c)/Error y.NElts() <> fx.Nelts() ");
 | 
|---|
| 648 | 
 | 
|---|
| 649 | uint_4 nf = fx.NRows();
 | 
|---|
| 650 | 
 | 
|---|
| 651 | TMatrix<T> a(nf,nf);
 | 
|---|
| 652 | 
 | 
|---|
| 653 | for(uint_4 j=0; j<nf; j++)
 | 
|---|
| 654 |   for(uint_4 k=j; k<nf; k++)
 | 
|---|
| 655 |      a(j,k) = a(k,j) = TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), j) 
 | 
|---|
| 656 |                      * TMatrixRC<T>::Row(const_cast<TMatrix<T> &>(fx), k);
 | 
|---|
| 657 | 
 | 
|---|
| 658 | c = fx * y;
 | 
|---|
| 659 | 
 | 
|---|
| 660 | if(SimpleMatrixOperation<T>::GausPiv(a,c)==(T) 0) 
 | 
|---|
| 661 |   throw SingMatrixExc("LinFitter<T>::LinFit(fx, y, c) - Non invertible matrix (by GausPiv())");
 | 
|---|
| 662 | 
 | 
|---|
| 663 | r_8 xi2 = 0., ax;
 | 
|---|
| 664 | T x;
 | 
|---|
| 665 | for(uint_4 k=0; k<n; k++) {
 | 
|---|
| 666 |   x = (T) 0;
 | 
|---|
| 667 |   for(uint_4 i=0; i<nf; i++) x += c(i) * fx(i,k);
 | 
|---|
| 668 |   x -= y(k);
 | 
|---|
| 669 |   ax = TMatrixRC<T>::Abs_Value(x);
 | 
|---|
| 670 |   xi2 += ax*ax;
 | 
|---|
| 671 | }
 | 
|---|
| 672 | return xi2;
 | 
|---|
| 673 | }
 | 
|---|
| 674 | 
 | 
|---|
| 675 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
 | 
|---|
| 676 | // errY2 contient les carres des erreurs sur les Y.
 | 
|---|
| 677 | // au retour, errC contient les erreurs sur les coefs.
 | 
|---|
| 678 | //! Linear fitting with errors
 | 
|---|
| 679 | /*!
 | 
|---|
| 680 |   Linear fit with errors of y(k) as the sum of \f$ c(i)f(i,x(k)), i=0..nf-1 \f$.
 | 
|---|
| 681 |   \param x : vector of X values
 | 
|---|
| 682 |   \param y : vector of datas
 | 
|---|
| 683 |   \param errY2 : vector of errors square on Y
 | 
|---|
| 684 |   \param nf: number of functions
 | 
|---|
| 685 |   \param f : f(i,x(k)), i=0..nf-1
 | 
|---|
| 686 |   \return c : vector of solutions
 | 
|---|
| 687 |   \return errC : vector of errors on solutions C
 | 
|---|
| 688 |   \return return chisquare
 | 
|---|
| 689 |  */
 | 
|---|
| 690 | template <class T>
 | 
|---|
| 691 | r_8 LinFitter<T>::LinFit(const TVector<T>& x, const TVector<T>& y,
 | 
|---|
| 692 |              const TVector<T>& errY2, uint_4 nf, T (*f)(uint_4,T),
 | 
|---|
| 693 |                                   TVector<T>& c, TVector<T>& errC)
 | 
|---|
| 694 | {
 | 
|---|
| 695 | uint_4 n = x.NElts();
 | 
|---|
| 696 | if (n != y.NElts()) 
 | 
|---|
| 697 |   throw SzMismatchError("LinFitter<T>::LinFit(x,y,errY2,nf,f,c,errC)/Error x.NElts() <> y.Nelts() ");
 | 
|---|
| 698 | 
 | 
|---|
| 699 | TMatrix<T> fx(nf, n);
 | 
|---|
| 700 | for(uint_4 i=0; i<nf; i++)
 | 
|---|
| 701 |   for(uint_4 j=0; j<n; j++)
 | 
|---|
| 702 |     fx(i,j) = f(i,x(j));
 | 
|---|
| 703 | 
 | 
|---|
| 704 | return LinFit(fx,y,errY2,c,errC);
 | 
|---|
| 705 | }
 | 
|---|
| 706 | 
 | 
|---|
| 707 | // fit lineaire des y(k) en tant que somme de c(i)f(i,x(k)), i=0..nf-1,
 | 
|---|
| 708 | // la matrice fx contient les valeurs des f:
 | 
|---|
| 709 | // fx(i,j) = f(i, x(j)).
 | 
|---|
| 710 | // errY2 contient les carres des erreurs sur les Y.
 | 
|---|
| 711 | // au retour, errC contient les erreurs sur les coefs.
 | 
|---|
| 712 | //! Linear fitting with errors
 | 
|---|
| 713 | /*!
 | 
|---|
| 714 |   \param fx : matrix which contains \f$ fx(i,j) = f(i, x(j)) \f$.
 | 
|---|
| 715 |   \param y : vector of datas
 | 
|---|
| 716 |   \param errY2 : vector of errors square on Y
 | 
|---|
| 717 |   \return c : vector of solutions
 | 
|---|
| 718 |   \return errC : vector of errors on solutions on C
 | 
|---|
| 719 |   \return return chisquare
 | 
|---|
| 720 |  */
 | 
|---|
| 721 | template <class T>
 | 
|---|
| 722 | r_8 LinFitter<T>::LinFit(const TMatrix<T>& fx, const TVector<T>& y,
 | 
|---|
| 723 |            const TVector<T>& errY2,TVector<T>& c, TVector<T>& errC)
 | 
|---|
| 724 | {
 | 
|---|
| 725 | uint_4 n = y.NElts();
 | 
|---|
| 726 | if( n != errY2.NElts()) 
 | 
|---|
| 727 |   throw SzMismatchError("LinFitter<T>::LinFit(fx,y,errY2,c,errC)/Error y.NElts() <> errY2.Nelts() ");
 | 
|---|
| 728 | if( n != fx.NCol()) 
 | 
|---|
| 729 |   throw SzMismatchError("LinFitter<T>::LinFit(fx,y,errY2,c,errC)/Error y.NElts() <> fx.NCols() ");
 | 
|---|
| 730 | uint_4 nf = fx.NRows();
 | 
|---|
| 731 | 
 | 
|---|
| 732 | TMatrix<T> a(nf,nf);
 | 
|---|
| 733 | 
 | 
|---|
| 734 | c.Realloc(nf);
 | 
|---|
| 735 | errC.Realloc(nf);
 | 
|---|
| 736 | 
 | 
|---|
| 737 | for(uint_4 j=0; j<nf; j++)
 | 
|---|
| 738 |   for(uint_4 k=j; k<nf; k++) {
 | 
|---|
| 739 |     T x=0;
 | 
|---|
| 740 |     // Matrice a inverser
 | 
|---|
| 741 |     for(uint_4 l=0; l<n; l++) x += fx(j,l)*fx(k,l)/errY2(l);
 | 
|---|
| 742 |     a(j,k) = a(k,j) = x;
 | 
|---|
| 743 |   }
 | 
|---|
| 744 |  
 | 
|---|
| 745 | TMatrix<T> d(nf,nf+1);
 | 
|---|
| 746 | for(uint_4 k=0; k<nf; k++) {
 | 
|---|
| 747 |   T x = (T) 0;
 | 
|---|
| 748 |   // Second membre 1ere colonne
 | 
|---|
| 749 |   for(uint_4 l=0; l<n; l++) x += fx(k,l)*y(l)/errY2(l);             
 | 
|---|
| 750 |   d(k,0) = x;
 | 
|---|
| 751 |   // Reste second membre = Id.
 | 
|---|
| 752 |   for(uint_4 m=1; m<=nf; m++) d(k,m) = (k==m)?1:0;
 | 
|---|
| 753 | }
 | 
|---|
| 754 | 
 | 
|---|
| 755 | if(SimpleMatrixOperation<T>::GausPiv(a,d)==(T) 0) 
 | 
|---|
| 756 |   throw SingMatrixExc("LinFitter<T>::LinFit(...ErrY2...) - Non invertible matrix (by GausPiv())");
 | 
|---|
| 757 | 
 | 
|---|
| 758 | 
 | 
|---|
| 759 | for(uint_4 l=0; l<nf; l++) {
 | 
|---|
| 760 |   c(l) = d(l,0);        // Parametres = 1ere colonne
 | 
|---|
| 761 |   errC(l) = d(l,l+1);   // Erreurs = diag inverse.
 | 
|---|
| 762 | }
 | 
|---|
| 763 | 
 | 
|---|
| 764 | r_8 xi2 = 0., ax;
 | 
|---|
| 765 | T x;
 | 
|---|
| 766 | for(uint_4 jj=0; jj<n; jj++) {
 | 
|---|
| 767 |   x = (T) 0;
 | 
|---|
| 768 |   for(uint_4 ii=0; ii<nf; ii++) x += c(ii) * fx(ii,jj);
 | 
|---|
| 769 |     x -= y(jj);
 | 
|---|
| 770 |     ax = TMatrixRC<T>::Abs_Value(x);
 | 
|---|
| 771 |     xi2 += ax*ax/TMatrixRC<T>::Abs_Value(errY2(jj));
 | 
|---|
| 772 |   }
 | 
|---|
| 773 |   return xi2;
 | 
|---|
| 774 | }
 | 
|---|
| 775 | 
 | 
|---|
| 776 | } // Fin namespace SOPHYA 
 | 
|---|
| 777 | ///////////////////////////////////////////////////////////////////////////
 | 
|---|
| 778 | ///////////////////////////////////////////////////////////////////////////
 | 
|---|
| 779 | ///////////////////////////////////////////////////////////////////////////
 | 
|---|
| 780 | ///////////////////////////////////////////////////////////////////////////
 | 
|---|
| 781 | 
 | 
|---|
| 782 | void _ZZ_TestTMatrixRC_YY_(TMatrix<r_8> & m)
 | 
|---|
| 783 | {
 | 
|---|
| 784 |   cout << " ::::: _ZZ_TestTMatrixRC_YY_ :::: M= \n" << m << endl;
 | 
|---|
| 785 |   TMatrixRC<r_8> l0 = TMatrixRC<r_8>::Row(m,0);
 | 
|---|
| 786 |   cout << "TMatrixRC<r_8>::Row(m,0) = \n" ;
 | 
|---|
| 787 |   l0.Print(cout);
 | 
|---|
| 788 |   TMatrixRC<r_8> l1 = TMatrixRC<r_8>::Row(m,1);
 | 
|---|
| 789 |   cout << "TMatrixRC<r_8>::Row(m,1) = \n" ;
 | 
|---|
| 790 |   l1.Print(cout);
 | 
|---|
| 791 |   l0.SetRow(2);
 | 
|---|
| 792 |   cout << "TMatrixRC<r_8>::l0.SetRow(2 = \n" ;
 | 
|---|
| 793 |   l0.Print(cout);
 | 
|---|
| 794 | 
 | 
|---|
| 795 |   TMatrixRC<r_8> c0 = TMatrixRC<r_8>::Col(m,0);
 | 
|---|
| 796 |   cout << "TMatrixRC<r_8>::Col(m,0) = \n" ;
 | 
|---|
| 797 |   c0.Print(cout);
 | 
|---|
| 798 |   TMatrixRC<r_8> c1 = TMatrixRC<r_8>::Col(m,1);
 | 
|---|
| 799 |   cout << "TMatrixRC<r_8>::Col(m,1) = \n" ;
 | 
|---|
| 800 |   c1.Print(cout);  
 | 
|---|
| 801 |   c0.SetCol(2);
 | 
|---|
| 802 |   cout << "TMatrixRC<r_8>::c0.SetCol(2) = \n" ;
 | 
|---|
| 803 |   c0.Print(cout);
 | 
|---|
| 804 |   TMatrixRC<r_8> c00 = TMatrixRC<r_8>::Col(m,0);
 | 
|---|
| 805 |   TMatrixRC<r_8>::Swap(c0, c00);
 | 
|---|
| 806 |   cout << " :::::  M Apres Swap = \n" << m << endl;
 | 
|---|
| 807 |   
 | 
|---|
| 808 | }
 | 
|---|
| 809 | 
 | 
|---|
| 810 | ///////////////////////////////////////////////////////////////
 | 
|---|
| 811 | #ifdef __CXX_PRAGMA_TEMPLATES__
 | 
|---|
| 812 | // Instances gestion lignes/colonnes
 | 
|---|
| 813 | #pragma define_template TMatrixRC<int_4>
 | 
|---|
| 814 | #pragma define_template TMatrixRC<r_4>
 | 
|---|
| 815 | #pragma define_template TMatrixRC<r_8>
 | 
|---|
| 816 | #pragma define_template TMatrixRC< complex<r_4> >
 | 
|---|
| 817 | #pragma define_template TMatrixRC< complex<r_8> >
 | 
|---|
| 818 | #pragma define_template SimpleMatrixOperation<int_4>
 | 
|---|
| 819 | #pragma define_template SimpleMatrixOperation<r_4>
 | 
|---|
| 820 | #pragma define_template SimpleMatrixOperation<r_8>
 | 
|---|
| 821 | #pragma define_template SimpleMatrixOperation< complex<r_4> >
 | 
|---|
| 822 | #pragma define_template SimpleMatrixOperation< complex<r_8> >
 | 
|---|
| 823 | #pragma define_template LinFitter<r_4>
 | 
|---|
| 824 | #pragma define_template LinFitter<r_8>
 | 
|---|
| 825 | #pragma define_template LinFitter< complex<r_4> >
 | 
|---|
| 826 | #pragma define_template LinFitter< complex<r_8> >
 | 
|---|
| 827 | #endif
 | 
|---|
| 828 | 
 | 
|---|
| 829 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
 | 
|---|
| 830 | namespace SOPHYA {
 | 
|---|
| 831 | // Instances gestion lignes/colonnes
 | 
|---|
| 832 | template class TMatrixRC<int_4>;
 | 
|---|
| 833 | template class TMatrixRC<r_4>;
 | 
|---|
| 834 | template class TMatrixRC<r_8>;
 | 
|---|
| 835 | template class TMatrixRC< complex<r_4> >;
 | 
|---|
| 836 | template class TMatrixRC< complex<r_8> >;
 | 
|---|
| 837 | template class SimpleMatrixOperation<int_4>;
 | 
|---|
| 838 | template class SimpleMatrixOperation<r_4>;
 | 
|---|
| 839 | template class SimpleMatrixOperation<r_8>;
 | 
|---|
| 840 | template class SimpleMatrixOperation< complex<r_4> >;
 | 
|---|
| 841 | template class SimpleMatrixOperation< complex<r_8> >;
 | 
|---|
| 842 | template class LinFitter<r_4>;
 | 
|---|
| 843 | template class LinFitter<r_8>;
 | 
|---|
| 844 | template class LinFitter< complex<r_4> >;
 | 
|---|
| 845 | template class LinFitter< complex<r_8> >;
 | 
|---|
| 846 | }
 | 
|---|
| 847 | #endif
 | 
|---|
| 848 | 
 | 
|---|