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