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